-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to install the guest components
This will install the lima-guestagent in the instance. It will install nerdctl-full.tgz, if it has been enabled. Signed-off-by: Anders F Björklund <[email protected]>
- Loading branch information
1 parent
ca07442
commit f356704
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/lima-vm/lima/pkg/cacheutil" | ||
"github.com/lima-vm/lima/pkg/store" | ||
"github.com/lima-vm/lima/pkg/store/filenames" | ||
"github.com/lima-vm/lima/pkg/usrlocalsharelima" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newGuestInstallCommand() *cobra.Command { | ||
guestInstallCommand := &cobra.Command{ | ||
Use: "guest-install INSTANCE", | ||
Short: "Install guest components", | ||
Args: WrapArgsError(cobra.MaximumNArgs(1)), | ||
RunE: guestInstallAction, | ||
ValidArgsFunction: cobra.NoFileCompletions, | ||
Hidden: true, | ||
} | ||
return guestInstallCommand | ||
} | ||
|
||
func guestInstallAction(_ *cobra.Command, args []string) error { | ||
instName := DefaultInstanceName | ||
if len(args) > 0 { | ||
instName = args[0] | ||
} | ||
|
||
inst, err := store.Inspect(instName) | ||
if err != nil { | ||
return err | ||
} | ||
if inst.Status == store.StatusStopped { | ||
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName) | ||
} | ||
|
||
sshConfig := filepath.Join(inst.Dir, filenames.SSHConfig) | ||
sshFlags := []string{"-F", sshConfig} | ||
|
||
y, err := inst.LoadYAML() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hostname := fmt.Sprintf("lima-%s", inst.Name) | ||
prefix := *y.GuestInstallPrefix | ||
|
||
// lima-guestagent | ||
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*y.OS, *y.Arch) | ||
if err != nil { | ||
return err | ||
} | ||
tmp := "/tmp/lima-guestagent" | ||
bin := prefix + "/bin/lima-guestagent" | ||
logrus.Infof("Copying %q to %s", guestAgentBinary, hostname) | ||
scpArgs := []string{guestAgentBinary, hostname + ":" + tmp} | ||
scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...) | ||
scpCmd.Stdout = os.Stdout | ||
scpCmd.Stderr = os.Stderr | ||
logrus.Debugf("executing %v", scpCmd.Args) | ||
if err := scpCmd.Run(); err != nil { | ||
return nil | ||
} | ||
logrus.Infof("Installing %s to %s", tmp, bin) | ||
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin} | ||
sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...) | ||
sshCmd.Stdout = os.Stdout | ||
sshCmd.Stderr = os.Stderr | ||
logrus.Debugf("executing %v", sshCmd.Args) | ||
if err := sshCmd.Run(); err != nil { | ||
return nil | ||
} | ||
|
||
// nerdctl-full.tgz | ||
nerdctlFilename := cacheutil.NerdctlArchive(y) | ||
if nerdctlFilename != "" { | ||
nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(y, false) | ||
if err != nil { | ||
return err | ||
} | ||
tmp := "/tmp/nerdctl-full.tgz" | ||
logrus.Infof("Copying %q to %s", nerdctlFilename, hostname) | ||
scpArgs := []string{nerdctlArchive, hostname + ":" + tmp} | ||
scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...) | ||
scpCmd.Stdout = os.Stdout | ||
scpCmd.Stderr = os.Stderr | ||
logrus.Debugf("executing %v", scpCmd.Args) | ||
if err := scpCmd.Run(); err != nil { | ||
return nil | ||
} | ||
logrus.Infof("Installing %s in %s", tmp, prefix) | ||
sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp} | ||
sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...) | ||
sshCmd.Stdout = os.Stdout | ||
sshCmd.Stderr = os.Stderr | ||
logrus.Debugf("executing %v", sshCmd.Args) | ||
if err := sshCmd.Run(); err != nil { | ||
return nil | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters