package main import ( "context" "errors" "fmt" "io" "sync" "time" incus "github.com/lxc/incus/v6/client" "github.com/lxc/incus/v6/shared/api" ) // maxOutput caps what one command may return to the agent. const maxOutput = 64 << 10 type instance struct { Name string Status string } type snapshot struct { Name string Created time.Time } type execResult struct { Stdout string Exit int } // incusAPI is the part of Incus the connector uses, so the checks around it // can be tested without a server. type incusAPI interface { Instances(ctx context.Context, project string) ([]instance, error) Snapshots(ctx context.Context, project, name string) ([]snapshot, error) Snapshot(ctx context.Context, project, name, snap string) error // Exec runs argv directly, with no shell. Exec(ctx context.Context, project, name string, argv []string) (execResult, error) } // remote is the official client connected over HTTPS. The server certificate // is pinned: any other certificate is refused. type remote struct { endpoint string args incus.ConnectionArgs mu sync.Mutex server incus.InstanceServer } func newRemote(endpoint, serverCert, clientCert, clientKey string) *remote { return &remote{endpoint: endpoint, args: incus.ConnectionArgs{ TLSServerCert: serverCert, TLSClientCert: clientCert, TLSClientKey: clientKey, IdenticalCertificate: true, UserAgent: "archipelago-incus", }} } func (r *remote) project(project string) (incus.InstanceServer, error) { r.mu.Lock() defer r.mu.Unlock() if r.server == nil { s, err := incus.ConnectIncus(r.endpoint, &r.args) if err != nil { return nil, fmt.Errorf("connect %s: %w", r.endpoint, err) } r.server = s } return r.server.UseProject(project), nil } func (r *remote) Instances(_ context.Context, project string) ([]instance, error) { s, err := r.project(project) if err != nil { return nil, err } list, err := s.GetInstances(api.InstanceTypeContainer) if err != nil { return nil, err } out := make([]instance, 0, len(list)) for _, i := range list { out = append(out, instance{Name: i.Name, Status: i.Status}) } return out, nil } func (r *remote) Snapshots(_ context.Context, project, name string) ([]snapshot, error) { s, err := r.project(project) if err != nil { return nil, err } list, err := s.GetInstanceSnapshots(name) if err != nil { return nil, err } out := make([]snapshot, 0, len(list)) for _, sn := range list { out = append(out, snapshot{Name: sn.Name, Created: sn.CreatedAt}) } return out, nil } func (r *remote) Snapshot(_ context.Context, project, name, snap string) error { s, err := r.project(project) if err != nil { return err } op, err := s.CreateInstanceSnapshot(name, api.InstanceSnapshotsPost{Name: snap}) if err != nil { return err } return op.Wait() } func (r *remote) Exec(_ context.Context, project, name string, argv []string) (execResult, error) { s, err := r.project(project) if err != nil { return execResult{}, err } out := &capped{limit: maxOutput} done := make(chan bool) op, err := s.ExecInstance(name, api.InstanceExecPost{Command: argv, WaitForWS: true}, &incus.InstanceExecArgs{ Stdin: eof{}, Stdout: out, Stderr: io.Discard, DataDone: done, }) if err != nil { return execResult{}, err } if err := op.Wait(); err != nil { return execResult{}, err } <-done code, ok := op.Get().Metadata["return"].(float64) if !ok { return execResult{}, errors.New("exec returned no exit code") } return execResult{Stdout: out.String(), Exit: int(code)}, nil } // eof is an empty standard input. type eof struct{} func (eof) Read([]byte) (int, error) { return 0, io.EOF } // capped keeps the first limit bytes and discards the rest. type capped struct { limit int buf []byte } func (c *capped) Write(p []byte) (int, error) { if room := c.limit - len(c.buf); room > 0 { c.buf = append(c.buf, p[:min(room, len(p))]...) } return len(p), nil } func (c *capped) String() string { return string(c.buf) }