44package main
55
66import (
7+ "bytes"
78 "fmt"
89 "context"
910 "os"
@@ -40,6 +41,16 @@ func runCmd(ctx context.Context, name string, flags []string, args ...string) er
4041 return cmd .Run ()
4142}
4243
44+ func shell (ctx context.Context , name string , flags []string , args ... string ) (string , error ) {
45+ cmd := exec .CommandContext (ctx , name , append (flags , args ... )... )
46+ out , err := cmd .Output ()
47+ if err != nil {
48+ return "" , err
49+ }
50+ out = bytes .TrimSuffix (out , []byte {'\n' })
51+ return string (out ), nil
52+ }
53+
4354func guestInstallAction (cmd * cobra.Command , args []string ) error {
4455 instName := DefaultInstanceName
4556 if len (args ) > 0 {
@@ -71,18 +82,23 @@ func guestInstallAction(cmd *cobra.Command, args []string) error {
7182 if err != nil {
7283 return err
7384 }
74- tmp := "/tmp/lima-guestagent"
85+ tmpname := "lima-guestagent"
86+ tmp , err := shell (ctx , sshExe , sshFlags , hostname , "mktemp" , "-t" , "lima-guestagent.XXXXXX" )
87+ if err != nil {
88+ return err
89+ }
7590 bin := prefix + "/bin/lima-guestagent"
76- logrus .Infof ("Copying %q to %s" , guestAgentBinary , hostname )
91+ logrus .Infof ("Copying %q to %s:%s " , guestAgentBinary , inst . Name , tmpname )
7792 scpArgs := []string {guestAgentBinary , hostname + ":" + tmp }
7893 if err := runCmd (ctx , scpExe , scpFlags , scpArgs ... ); err != nil {
7994 return nil
8095 }
81- logrus .Infof ("Installing %s to %s" , tmp , bin )
96+ logrus .Infof ("Installing %s to %s" , tmpname , bin )
8297 sshArgs := []string {hostname , "sudo" , "install" , "-m" , "755" , tmp , bin }
8398 if err := runCmd (ctx , sshExe , sshFlags , sshArgs ... ); err != nil {
8499 return nil
85100 }
101+ _ , _ = shell (ctx , sshExe , sshFlags , hostname , "rm" , tmp )
86102
87103 // nerdctl-full.tgz
88104 nerdctlFilename := cacheutil .NerdctlArchive (inst .Config )
@@ -91,17 +107,22 @@ func guestInstallAction(cmd *cobra.Command, args []string) error {
91107 if err != nil {
92108 return err
93109 }
94- tmp := "/tmp/nerdctl-full.tgz"
95- logrus .Infof ("Copying %q to %s" , nerdctlFilename , hostname )
110+ tmpname := "nerdctl-full.tgz"
111+ tmp , err := shell (ctx , sshExe , sshFlags , hostname , "mktemp" , "-t" , "nerdctl-full.XXXXXX.tgz" )
112+ if err != nil {
113+ return err
114+ }
115+ logrus .Infof ("Copying %q to %s:%s" , nerdctlFilename , inst .Name , tmpname )
96116 scpArgs := []string {nerdctlArchive , hostname + ":" + tmp }
97117 if err := runCmd (ctx , scpExe , scpFlags , scpArgs ... ); err != nil {
98118 return nil
99119 }
100- logrus .Infof ("Installing %s in %s" , tmp , prefix )
120+ logrus .Infof ("Installing %s in %s" , tmpname , prefix )
101121 sshArgs := []string {hostname , "sudo" , "tar" , "Cxzf" , prefix , tmp }
102122 if err := runCmd (ctx , sshExe , sshFlags , sshArgs ... ); err != nil {
103123 return nil
104124 }
125+ _ , _ = shell (ctx , sshExe , sshFlags , hostname , "rm" , tmp )
105126 }
106127
107128 return nil
0 commit comments