| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | package limaembed |
| 11 | |
| 12 | import ( |
| 13 | "errors" |
| 14 | "fmt" |
| 15 | "io" |
| 16 | "net" |
| 17 | "net/http" |
| 18 | "os" |
| 19 | "os/signal" |
| 20 | "runtime" |
| 21 | "sync" |
| 22 | "syscall" |
| 23 | "time" |
| 24 | |
| 25 | "github.com/sirupsen/logrus" |
| 26 | "github.com/spf13/cobra" |
| 27 | |
| 28 | "github.com/lima-vm/lima/v2/pkg/hostagent" |
| 29 | "github.com/lima-vm/lima/v2/pkg/hostagent/api/server" |
| 30 | "github.com/lima-vm/lima/v2/pkg/store" |
| 31 | ) |
| 32 | |
| 33 | func newHostagentCommand() *cobra.Command { |
| 34 | hostagentCommand := &cobra.Command{ |
| 35 | Use: "hostagent INSTANCE", |
| 36 | Short: "Run hostagent", |
| 37 | Args: cobra.ExactArgs(1), |
| 38 | RunE: hostagentAction, |
| 39 | Hidden: true, |
| 40 | } |
| 41 | hostagentCommand.Flags().StringP("pidfile", "p", "", "Write PID to file") |
| 42 | hostagentCommand.Flags().String("socket", "", "Path of hostagent socket") |
| 43 | hostagentCommand.Flags().Bool("run-gui", false, "Run GUI synchronously within hostagent") |
| 44 | hostagentCommand.Flags().String("guestagent", "", "Local file path (not URL) of lima-guestagent.OS-ARCH[.gz]") |
| 45 | hostagentCommand.Flags().String("nerdctl-archive", "", "Local file path (not URL) of nerdctl-full-VERSION-GOOS-GOARCH.tar.gz") |
| 46 | hostagentCommand.Flags().Bool("progress", false, "Show provision script progress by monitoring cloud-init logs") |
| 47 | return hostagentCommand |
| 48 | } |
| 49 | |
| 50 | func hostagentAction(cmd *cobra.Command, args []string) error { |
| 51 | ctx := cmd.Context() |
| 52 | pidfile, err := cmd.Flags().GetString("pidfile") |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | if pidfile != "" { |
| 57 | if existingPID, err := store.ReadPIDFile(pidfile); existingPID != 0 { |
| 58 | return fmt.Errorf("another hostagent may already be running with pid %d (pidfile %#q)", existingPID, pidfile) |
| 59 | } else if err != nil { |
| 60 | return fmt.Errorf("failed to determine if another hostagent is running: %w", err) |
| 61 | } |
| 62 | if err := store.WritePIDFile(pidfile, os.Getpid()); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | defer os.RemoveAll(pidfile) |
| 66 | } |
| 67 | socket, err := cmd.Flags().GetString("socket") |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if socket == "" { |
| 72 | return errors.New("socket must be specified (limactl version mismatch?)") |
| 73 | } |
| 74 | |
| 75 | instName := args[0] |
| 76 | |
| 77 | runGUI, err := cmd.Flags().GetBool("run-gui") |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if runGUI { |
| 82 | |
| 83 | runtime.LockOSThread() |
| 84 | defer runtime.UnlockOSThread() |
| 85 | } |
| 86 | |
| 87 | signalCh := hostagentSignals |
| 88 | signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) |
| 89 | stopNotification, err := watchHostagentStop(socket) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | defer stopNotification() |
| 94 | |
| 95 | stdout := &syncWriter{w: cmd.OutOrStdout()} |
| 96 | stderr := &syncWriter{w: cmd.ErrOrStderr()} |
| 97 | defer stdout.flush() |
| 98 | defer stderr.flush() |
| 99 | |
| 100 | initLogrus(stderr) |
| 101 | var opts []hostagent.Opt |
| 102 | guestagentBinary, err := cmd.Flags().GetString("guestagent") |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | if guestagentBinary != "" { |
| 107 | opts = append(opts, hostagent.WithGuestAgentBinary(guestagentBinary)) |
| 108 | } |
| 109 | nerdctlArchive, err := cmd.Flags().GetString("nerdctl-archive") |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | if nerdctlArchive != "" { |
| 114 | opts = append(opts, hostagent.WithNerdctlArchive(nerdctlArchive)) |
| 115 | } |
| 116 | showProgress, err := cmd.Flags().GetBool("progress") |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | if showProgress { |
| 121 | opts = append(opts, hostagent.WithCloudInitProgress(showProgress)) |
| 122 | } |
| 123 | ha, err := hostagent.New(ctx, instName, stdout, signalCh, opts...) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | backend := &server.Backend{ |
| 129 | Agent: ha, |
| 130 | } |
| 131 | r := http.NewServeMux() |
| 132 | server.AddRoutes(r, backend) |
| 133 | srv := &http.Server{Handler: r} |
| 134 | err = os.RemoveAll(socket) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | var lc net.ListenConfig |
| 139 | l, err := lc.Listen(ctx, "unix", socket) |
| 140 | logrus.Infof("hostagent socket created at %s", socket) |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | go func() { |
| 145 | if serveErr := srv.Serve(l); serveErr != http.ErrServerClosed { |
| 146 | logrus.WithError(serveErr).Warn("hostagent API server exited with an error") |
| 147 | } |
| 148 | }() |
| 149 | defer srv.Close() |
| 150 | return ha.Run(ctx) |
| 151 | } |
| 152 | |
| 153 | |
| 154 | type syncer interface { |
| 155 | Sync() error |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | const syncInterval = 100 * time.Millisecond |
| 168 | |
| 169 | type syncWriter struct { |
| 170 | w io.Writer |
| 171 | |
| 172 | mu sync.Mutex |
| 173 | dirty bool |
| 174 | timer *time.Timer |
| 175 | } |
| 176 | |
| 177 | func (w *syncWriter) Write(p []byte) (int, error) { |
| 178 | w.mu.Lock() |
| 179 | defer w.mu.Unlock() |
| 180 | |
| 181 | written, err := w.w.Write(p) |
| 182 | if err == nil { |
| 183 | w.dirty = true |
| 184 | if w.timer == nil { |
| 185 | w.timer = time.AfterFunc(syncInterval, w.flush) |
| 186 | } |
| 187 | } |
| 188 | return written, err |
| 189 | } |
| 190 | |
| 191 | |
| 192 | func (w *syncWriter) flush() { |
| 193 | w.mu.Lock() |
| 194 | defer w.mu.Unlock() |
| 195 | |
| 196 | if w.timer != nil { |
| 197 | w.timer.Stop() |
| 198 | w.timer = nil |
| 199 | } |
| 200 | if !w.dirty { |
| 201 | return |
| 202 | } |
| 203 | if s, ok := w.w.(syncer); ok { |
| 204 | _ = s.Sync() |
| 205 | } |
| 206 | w.dirty = false |
| 207 | } |
| 208 | |
| 209 | func initLogrus(stderr io.Writer) { |
| 210 | logrus.SetOutput(stderr) |
| 211 | |
| 212 | logrus.SetFormatter(new(logrus.JSONFormatter)) |
| 213 | |
| 214 | if logrus.GetLevel() == logrus.DebugLevel { |
| 215 | logrus.SetLevel(logrus.TraceLevel) |
| 216 | } else { |
| 217 | logrus.SetLevel(logrus.DebugLevel) |
| 218 | } |
| 219 | } |