|
| 1 | +package hostagent |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/lima-vm/lima/pkg/limayaml" |
| 8 | + "github.com/lima-vm/sshocker/pkg/ssh" |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | +) |
| 11 | + |
| 12 | +func (a *HostAgent) runProvisionScripts() error { |
| 13 | + var errs []error |
| 14 | + |
| 15 | + for i, f := range a.y.Provision { |
| 16 | + switch f.Mode { |
| 17 | + case limayaml.ProvisionModeSystem, limayaml.ProvisionModeUser: |
| 18 | + logrus.Infof("Running %s provision %d of %d", f.Mode, i+1, len(a.y.Provision)) |
| 19 | + err := a.waitForProvision( |
| 20 | + provision{ |
| 21 | + description: fmt.Sprintf("provision.%s/%08d", f.Mode, i), |
| 22 | + sudo: f.Mode == limayaml.ProvisionModeSystem, |
| 23 | + script: f.Script, |
| 24 | + }) |
| 25 | + if err != nil { |
| 26 | + errs = append(errs, err) |
| 27 | + } |
| 28 | + case limayaml.ProvisionModeDependency, limayaml.ProvisionModeBoot: |
| 29 | + logrus.Infof("Skipping %s provision %d of %d", f.Mode, i+1, len(a.y.Provision)) |
| 30 | + continue |
| 31 | + default: |
| 32 | + return fmt.Errorf("unknown provision mode %q", f.Mode) |
| 33 | + } |
| 34 | + } |
| 35 | + return errors.Join(errs...) |
| 36 | +} |
| 37 | + |
| 38 | +func (a *HostAgent) waitForProvision(p provision) error { |
| 39 | + if p.sudo { |
| 40 | + return a.waitForSystemProvision(p) |
| 41 | + } |
| 42 | + return a.waitForUserProvision(p) |
| 43 | +} |
| 44 | + |
| 45 | +func (a *HostAgent) waitForSystemProvision(p provision) error { |
| 46 | + logrus.Debugf("executing script %q", p.description) |
| 47 | + stdout, stderr, err := sudoExecuteScript(a.instSSHAddress, a.sshLocalPort, a.sshConfig, p.script, p.description) |
| 48 | + logrus.Debugf("stdout=%q, stderr=%q, err=%v", stdout, stderr, err) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err) |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func (a *HostAgent) waitForUserProvision(p provision) error { |
| 56 | + logrus.Debugf("executing script %q", p.description) |
| 57 | + stdout, stderr, err := ssh.ExecuteScript(a.instSSHAddress, a.sshLocalPort, a.sshConfig, p.script, p.description) |
| 58 | + logrus.Debugf("stdout=%q, stderr=%q, err=%v", stdout, stderr, err) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err) |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +type provision struct { |
| 66 | + description string |
| 67 | + script string |
| 68 | + sudo bool |
| 69 | +} |
0 commit comments