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