|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/lima-vm/lima/pkg/cacheutil" |
| 10 | + "github.com/lima-vm/lima/pkg/store" |
| 11 | + "github.com/lima-vm/lima/pkg/store/filenames" |
| 12 | + "github.com/lima-vm/lima/pkg/usrlocalsharelima" |
| 13 | + |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +func newGuestInstallCommand() *cobra.Command { |
| 19 | + guestInstallCommand := &cobra.Command{ |
| 20 | + Use: "guest-install INSTANCE", |
| 21 | + Short: "Install guest components", |
| 22 | + Args: WrapArgsError(cobra.MaximumNArgs(1)), |
| 23 | + RunE: guestInstallAction, |
| 24 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 25 | + Hidden: true, |
| 26 | + } |
| 27 | + return guestInstallCommand |
| 28 | +} |
| 29 | + |
| 30 | +func runCmd(name string, flags []string, args ...string) error { |
| 31 | + cmd := exec.Command(name, append(flags, args...)...) |
| 32 | + cmd.Stdout = os.Stdout |
| 33 | + cmd.Stderr = os.Stderr |
| 34 | + logrus.Debugf("executing %v", cmd.Args) |
| 35 | + return cmd.Run() |
| 36 | +} |
| 37 | + |
| 38 | +func guestInstallAction(cmd *cobra.Command, args []string) error { |
| 39 | + instName := DefaultInstanceName |
| 40 | + if len(args) > 0 { |
| 41 | + instName = args[0] |
| 42 | + } |
| 43 | + |
| 44 | + inst, err := store.Inspect(instName) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + if inst.Status == store.StatusStopped { |
| 49 | + return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName) |
| 50 | + } |
| 51 | + |
| 52 | + sshExe := "ssh" |
| 53 | + sshConfig := filepath.Join(inst.Dir, filenames.SSHConfig) |
| 54 | + sshFlags := []string{"-F", sshConfig} |
| 55 | + |
| 56 | + scpExe := "scp" |
| 57 | + scpFlags := sshFlags |
| 58 | + |
| 59 | + y, err := inst.LoadYAML() |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + hostname := fmt.Sprintf("lima-%s", inst.Name) |
| 65 | + prefix := *y.GuestInstallPrefix |
| 66 | + |
| 67 | + // lima-guestagent |
| 68 | + guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*y.OS, *y.Arch) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + tmp := "/tmp/lima-guestagent" |
| 73 | + bin := prefix + "/bin/lima-guestagent" |
| 74 | + logrus.Infof("Copying %q to %s", guestAgentBinary, hostname) |
| 75 | + scpArgs := []string{guestAgentBinary, hostname + ":" + tmp} |
| 76 | + if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil { |
| 77 | + return nil |
| 78 | + } |
| 79 | + logrus.Infof("Installing %s to %s", tmp, bin) |
| 80 | + sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin} |
| 81 | + if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil { |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + // nerdctl-full.tgz |
| 86 | + nerdctlFilename := cacheutil.NerdctlArchive(y) |
| 87 | + if nerdctlFilename != "" { |
| 88 | + nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(cmd.Context(), y, false) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + tmp := "/tmp/nerdctl-full.tgz" |
| 93 | + logrus.Infof("Copying %q to %s", nerdctlFilename, hostname) |
| 94 | + scpArgs := []string{nerdctlArchive, hostname + ":" + tmp} |
| 95 | + if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil { |
| 96 | + return nil |
| 97 | + } |
| 98 | + logrus.Infof("Installing %s in %s", tmp, prefix) |
| 99 | + sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp} |
| 100 | + if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil { |
| 101 | + return nil |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
0 commit comments