Skip to content

Commit

Permalink
Use temporary file names for installing
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Aug 31, 2024
1 parent 0b14792 commit 923573b
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions cmd/limactl/guest_install.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -35,6 +36,16 @@ func runCmd(name string, flags []string, args ...string) error {
return cmd.Run()
}

func shell(name string, flags []string, args ...string) (string, error) {
cmd := exec.Command(name, append(flags, args...)...)
out, err := cmd.Output()
if err != nil {
return "", err
}
out = bytes.TrimSuffix(out, []byte{'\n'})
return string(out), nil
}

func guestInstallAction(cmd *cobra.Command, args []string) error {
instName := DefaultInstanceName
if len(args) > 0 {
Expand Down Expand Up @@ -69,18 +80,23 @@ func guestInstallAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
tmp := "/tmp/lima-guestagent"
tmpname := "lima-guestagent"
tmp, err := shell(sshExe, sshFlags, hostname, "mktemp", "-t", "lima-guestagent.XXXXXX")
if err != nil {
return err
}
bin := prefix + "/bin/lima-guestagent"
logrus.Infof("Copying %q to %s", guestAgentBinary, hostname)
logrus.Infof("Copying %q to %s:%s", guestAgentBinary, inst.Name, tmpname)
scpArgs := []string{guestAgentBinary, hostname + ":" + tmp}
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
return nil
}
logrus.Infof("Installing %s to %s", tmp, bin)
logrus.Infof("Installing %s to %s", tmpname, bin)
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin}
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
return nil
}
_, _ = shell(sshExe, sshFlags, hostname, "rm", tmp)

// nerdctl-full.tgz
nerdctlFilename := cacheutil.NerdctlArchive(y)
Expand All @@ -89,17 +105,22 @@ func guestInstallAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
tmp := "/tmp/nerdctl-full.tgz"
logrus.Infof("Copying %q to %s", nerdctlFilename, hostname)
tmpname := "nerdctl-full.tgz"
tmp, err := shell(sshExe, sshFlags, hostname, "mktemp", "-t", "nerdctl-full.XXXXXX.tgz")
if err != nil {
return err
}
logrus.Infof("Copying %q to %s:%s", nerdctlFilename, inst.Name, tmpname)
scpArgs := []string{nerdctlArchive, hostname + ":" + tmp}
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
return nil
}
logrus.Infof("Installing %s in %s", tmp, prefix)
logrus.Infof("Installing %s in %s", tmpname, prefix)
sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp}
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
return nil
}
_, _ = shell(sshExe, sshFlags, hostname, "rm", tmp)
}

return nil
Expand Down

0 comments on commit 923573b

Please sign in to comment.