| 1 | |
| 2 | |
| 3 | |
| 4 | package limaembed |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/sirupsen/logrus" |
| 14 | "github.com/spf13/cobra" |
| 15 | |
| 16 | "github.com/docker/go-units" |
| 17 | "github.com/lima-vm/lima/v2/pkg/debugutil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 19 | "github.com/lima-vm/lima/v2/pkg/driver/external/server" |
| 20 | "github.com/lima-vm/lima/v2/pkg/envutil" |
| 21 | "github.com/lima-vm/lima/v2/pkg/hostagent" |
| 22 | "github.com/lima-vm/lima/v2/pkg/hostagent/dns" |
| 23 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 24 | "github.com/lima-vm/lima/v2/pkg/networks/usernet" |
| 25 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 26 | "github.com/lima-vm/sshocker/pkg/reversesshfs" |
| 27 | |
| 28 | "github.com/sylvinus/agent-vm/internal/cage" |
| 29 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 30 | "github.com/sylvinus/agent-vm/internal/guard" |
| 31 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 32 | "github.com/sylvinus/agent-vm/internal/netguard" |
| 33 | "github.com/sylvinus/agent-vm/internal/state" |
| 34 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 35 | ) |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | func Run(args []string) bool { |
| 41 | var stopGateControl func() |
| 42 | var stopJobWatch func() |
| 43 | i := 0 |
| 44 | for i < len(args) && args[i] == "--debug" { |
| 45 | i++ |
| 46 | } |
| 47 | if i == len(args) || args[i] != "hostagent" { |
| 48 | return false |
| 49 | } |
| 50 | var diagnosticStream *diagnostics.Stream |
| 51 | if path := os.Getenv(cage.ConfigEnv); path != "" { |
| 52 | hostagent.ManageResources = func(ctx context.Context, dir string, cfg *limatype.LimaYAML, balloon driver.MemoryBalloon, read func(context.Context, string) ([]byte, error)) func() { |
| 53 | maximum, err := units.RAMInBytes(*cfg.Memory) |
| 54 | if err != nil || *cfg.OS != limatype.LINUX { |
| 55 | return func() {} |
| 56 | } |
| 57 | return cage.StartResourceManager(ctx, dir, uint64(maximum), *cfg.CPUs, read, balloon.SetMemoryTarget) |
| 58 | } |
| 59 | diagnosticStream = diagnostics.New(filepath.Dir(path)).Stream("vm-hostagent", "warn") |
| 60 | diagnostics.SetDefault(diagnostics.New(filepath.Dir(path))) |
| 61 | logrus.SetOutput(diagnosticStream) |
| 62 | defer diagnosticStream.Close() |
| 63 | } |
| 64 | if runtime.GOOS == "windows" { |
| 65 | windowsPath() |
| 66 | } |
| 67 | |
| 68 | |
| 69 | os.Unsetenv("LIMA_SSH_PORT_FORWARDER") |
| 70 | |
| 71 | |
| 72 | |
| 73 | st, err := state.Default() |
| 74 | if err != nil { |
| 75 | logrus.Fatal(err) |
| 76 | } |
| 77 | policy, err := netguard.Load(string(st)) |
| 78 | if err != nil { |
| 79 | logrus.Fatal(err) |
| 80 | } |
| 81 | usernet.Outbound = policy.Allows |
| 82 | if len(policy.Domains) > 0 { |
| 83 | |
| 84 | |
| 85 | usernet.Names, usernet.Answered = policy.Name, policy.Answered |
| 86 | dns.Names, dns.Answered = policy.Name, policy.Answered |
| 87 | } |
| 88 | if path := os.Getenv(cage.ConfigEnv); path != "" { |
| 89 | stopJobWatch, err = cage.WatchJobStop(filepath.Dir(path), os.Getenv(cage.JobEnv), notifyHostagentStop) |
| 90 | if err != nil { |
| 91 | logrus.Fatal(err) |
| 92 | } |
| 93 | cfg, err := cage.Load(path) |
| 94 | if err != nil { |
| 95 | logrus.Fatal(err) |
| 96 | } |
| 97 | if err = cfg.RequireManagedProxy(filepath.Dir(path)); err != nil { |
| 98 | logrus.Fatal(err) |
| 99 | } |
| 100 | usernet.Names, usernet.Answered = nil, nil |
| 101 | dns.Names, dns.Answered = nil, nil |
| 102 | name := args[len(args)-1] |
| 103 | if name == vmname.Template+"-building" { |
| 104 | |
| 105 | |
| 106 | egress, err := cage.NewEgress(cfg) |
| 107 | if err != nil { |
| 108 | logrus.Fatal(err) |
| 109 | } |
| 110 | usernet.Outbound, usernet.GuestDial = egress.Allows, egress.DialContext |
| 111 | usernet.DNSExchange, dns.Exchange = egress.ExchangeDNS, egress.ExchangeDNS |
| 112 | } else { |
| 113 | gate, err := cage.NewEgressGate(cfg) |
| 114 | if err != nil { |
| 115 | logrus.Fatal(err) |
| 116 | } |
| 117 | if stopGateControl, err = cage.StartGateControl(path, filepath.Join(string(st), "lima", name), gate); err != nil { |
| 118 | logrus.Fatal(err) |
| 119 | } |
| 120 | if name == "mac" { |
| 121 | |
| 122 | |
| 123 | if binding, err := guestos.LoadMacOSProfile(filepath.Dir(path)); err != nil { |
| 124 | logrus.Fatal(err) |
| 125 | } else if binding != nil && binding.Instance == name { |
| 126 | usernet.DroppedIPv6 = gate.ObserveDroppedIPv6 |
| 127 | } |
| 128 | } |
| 129 | usernet.Outbound, usernet.GuestDial = gate.Allows, gate.DialContext |
| 130 | usernet.DNSExchange, dns.Exchange = gate.ExchangeDNS, gate.ExchangeDNS |
| 131 | } |
| 132 | if err := cfg.RecordRunning(string(st), args[len(args)-1]); err != nil { |
| 133 | logrus.Fatal(err) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | if os.Getenv(cage.ConfigEnv) == "" { |
| 139 | g, err := guard.Load(string(st), args[len(args)-1]) |
| 140 | if err != nil { |
| 141 | logrus.Fatal(err) |
| 142 | } |
| 143 | reversesshfs.Guard = g.Allow |
| 144 | } else { |
| 145 | |
| 146 | reversesshfs.Guard = nil |
| 147 | } |
| 148 | root := &cobra.Command{ |
| 149 | Use: "agent-vm", |
| 150 | SilenceUsage: true, |
| 151 | SilenceErrors: true, |
| 152 | PersistentPreRun: func(cmd *cobra.Command, _ []string) { |
| 153 | if debug, _ := cmd.Flags().GetBool("debug"); debug { |
| 154 | logrus.SetLevel(logrus.DebugLevel) |
| 155 | debugutil.Debug = true |
| 156 | } |
| 157 | }, |
| 158 | } |
| 159 | root.PersistentFlags().Bool("debug", false, "debug mode") |
| 160 | root.AddCommand(newHostagentCommand()) |
| 161 | root.SetArgs(args) |
| 162 | |
| 163 | |
| 164 | root.SetOut(newEventWatch(os.Stdout, args[len(args)-1])) |
| 165 | if diagnosticStream != nil { |
| 166 | root.SetErr(diagnosticStream) |
| 167 | } |
| 168 | |
| 169 | err = root.Execute() |
| 170 | if stopJobWatch != nil { |
| 171 | stopJobWatch() |
| 172 | } |
| 173 | if stopGateControl != nil { |
| 174 | stopGateControl() |
| 175 | } |
| 176 | server.Disconnect() |
| 177 | osutil.HandleExitError(err) |
| 178 | if err != nil { |
| 179 | logrus.Fatal(err) |
| 180 | } |
| 181 | os.Exit(0) |
| 182 | return true |
| 183 | } |
| 184 | |
| 185 | |
| 186 | func windowsPath() { |
| 187 | if extras, ok := os.LookupEnv("_LIMA_WINDOWS_EXTRA_PATH"); ok && strings.TrimSpace(extras) != "" { |
| 188 | _ = os.Setenv("PATH", strings.TrimSpace(extras)+string(filepath.ListSeparator)+os.Getenv("PATH")) |
| 189 | } |
| 190 | p := os.Getenv("PATH") |
| 191 | if np := envutil.AppendDirsToPath(p, envutil.PlatformCommonToolDirs); np != p { |
| 192 | _ = os.Setenv("PATH", np) |
| 193 | } |
| 194 | } |