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