Skip to content

Commit

Permalink
Allow providing scripts as separate files
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Aug 30, 2024
1 parent 771592f commit 7225aae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ containerd:
# Provisioning scripts need to be idempotent because they might be called
# multiple times, e.g. when the host VM is being restarted.
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
# They can be provided inline (as field `script`), or in external files (as field `path`).
# 🟢 Builtin default: null
# provision:
# # `system` is executed with root privileges
Expand Down Expand Up @@ -236,6 +237,7 @@ containerd:

# Probe scripts to check readiness.
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
# They can be provided inline (as field `script`), or in external files (as field `path`).
# 🟢 Builtin default: null
# probes:
# # Only `readiness` probes are supported right now.
Expand Down
20 changes: 18 additions & 2 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,19 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
}

for i, f := range y.Provision {
script := f.Script
if f.Path != "" {
b, err := os.ReadFile(f.Path)
if err != nil {
return err
}
script = string(b)
}
switch f.Mode {
case limayaml.ProvisionModeSystem, limayaml.ProvisionModeUser, limayaml.ProvisionModeDependency:
layout = append(layout, iso9660util.Entry{
Path: fmt.Sprintf("provision.%s/%08d", f.Mode, i),
Reader: strings.NewReader(f.Script),
Reader: strings.NewReader(script),
})
case limayaml.ProvisionModeBoot:
continue
Expand Down Expand Up @@ -414,8 +422,16 @@ func getBootCmds(p []limayaml.Provision) ([]BootCmds, error) {
if f.Mode != limayaml.ProvisionModeBoot {
continue
}
script := f.Script
if f.Path != "" {
b, err := os.ReadFile(f.Path)
if err != nil {
return nil, err
}
script = string(b)
}
lines := []string{}
for _, line := range strings.Split(f.Script, "\n") {
for _, line := range strings.Split(script, "\n") {
if line == "" {
continue
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/hostagent/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hostagent
import (
"errors"
"fmt"
"os"
"time"

"github.com/lima-vm/lima/pkg/limayaml"
Expand Down Expand Up @@ -158,10 +159,19 @@ Also see "/var/log/cloud-init-output.log" in the guest.
})
}
for _, probe := range a.y.Probes {
script := probe.Script
if probe.Path != "" {
b, err := os.ReadFile(probe.Path)
if err != nil {
logrus.WithError(err).Errorf("failed to read script %q", probe.Path)
continue
}
script = string(b)
}
if probe.Mode == limayaml.ProbeModeReadiness {
req = append(req, requirement{
description: probe.Description,
script: probe.Script,
script: script,
debugHint: probe.Hint,
})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type Provision struct {
Mode ProvisionMode `yaml:"mode" json:"mode"` // default: "system"
SkipDefaultDependencyResolution *bool `yaml:"skipDefaultDependencyResolution,omitempty" json:"skipDefaultDependencyResolution,omitempty"`
Script string `yaml:"script" json:"script"`
Path string `yaml:"path,omitempty" json:"path,omitempty"`
Playbook string `yaml:"playbook,omitempty" json:"playbook,omitempty"`
}

Expand All @@ -207,6 +208,7 @@ type Probe struct {
Mode ProbeMode // default: "readiness"
Description string
Script string
Path string `yaml:",omitempty" json:",omitempty"`
Hint string
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ func Validate(y *LimaYAML, warn bool) error {
return fmt.Errorf("field `provision[%d].mode` must one of %q, %q, %q, %q, or %q",
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot, ProvisionModeDependency, ProvisionModeAnsible)
}
if p.Path != "" {
if p.Script != "" {
return fmt.Errorf("field `provision[%d].script must be empty if path is set", i)
}
if _, err := os.Stat(p.Path); err != nil {
return fmt.Errorf("field `provision[%d].path` refers to an inaccessible path: %q: %w", i, p.Path, err)
}
}
if strings.Contains(p.Script, "LIMA_CIDATA") {
logrus.Warn("provisioning scripts should not reference the LIMA_CIDATA variables")
}
Expand All @@ -204,6 +212,14 @@ func Validate(y *LimaYAML, warn bool) error {
return fmt.Errorf("field `probe[%d].mode` can only be %q",
i, ProbeModeReadiness)
}
if p.Path != "" {
if p.Script != "" {
return fmt.Errorf("field `probe[%d].script must be empty if path is set", i)
}
if _, err := os.Stat(p.Path); err != nil {
return fmt.Errorf("field `probe[%d].path` refers to an inaccessible path: %q: %w", i, p.Path, err)
}
}
}
for i, rule := range y.PortForwards {
field := fmt.Sprintf("portForwards[%d]", i)
Expand Down

0 comments on commit 7225aae

Please sign in to comment.