Skip to content

Commit 7225aae

Browse files
committed
Allow providing scripts as separate files
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 771592f commit 7225aae

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

examples/default.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ containerd:
194194
# Provisioning scripts need to be idempotent because they might be called
195195
# multiple times, e.g. when the host VM is being restarted.
196196
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
197+
# They can be provided inline (as field `script`), or in external files (as field `path`).
197198
# 🟢 Builtin default: null
198199
# provision:
199200
# # `system` is executed with root privileges
@@ -236,6 +237,7 @@ containerd:
236237

237238
# Probe scripts to check readiness.
238239
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
240+
# They can be provided inline (as field `script`), or in external files (as field `path`).
239241
# 🟢 Builtin default: null
240242
# probes:
241243
# # Only `readiness` probes are supported right now.

pkg/cidata/cidata.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,19 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
331331
}
332332

333333
for i, f := range y.Provision {
334+
script := f.Script
335+
if f.Path != "" {
336+
b, err := os.ReadFile(f.Path)
337+
if err != nil {
338+
return err
339+
}
340+
script = string(b)
341+
}
334342
switch f.Mode {
335343
case limayaml.ProvisionModeSystem, limayaml.ProvisionModeUser, limayaml.ProvisionModeDependency:
336344
layout = append(layout, iso9660util.Entry{
337345
Path: fmt.Sprintf("provision.%s/%08d", f.Mode, i),
338-
Reader: strings.NewReader(f.Script),
346+
Reader: strings.NewReader(script),
339347
})
340348
case limayaml.ProvisionModeBoot:
341349
continue
@@ -414,8 +422,16 @@ func getBootCmds(p []limayaml.Provision) ([]BootCmds, error) {
414422
if f.Mode != limayaml.ProvisionModeBoot {
415423
continue
416424
}
425+
script := f.Script
426+
if f.Path != "" {
427+
b, err := os.ReadFile(f.Path)
428+
if err != nil {
429+
return nil, err
430+
}
431+
script = string(b)
432+
}
417433
lines := []string{}
418-
for _, line := range strings.Split(f.Script, "\n") {
434+
for _, line := range strings.Split(script, "\n") {
419435
if line == "" {
420436
continue
421437
}

pkg/hostagent/requirements.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hostagent
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"time"
78

89
"github.com/lima-vm/lima/pkg/limayaml"
@@ -158,10 +159,19 @@ Also see "/var/log/cloud-init-output.log" in the guest.
158159
})
159160
}
160161
for _, probe := range a.y.Probes {
162+
script := probe.Script
163+
if probe.Path != "" {
164+
b, err := os.ReadFile(probe.Path)
165+
if err != nil {
166+
logrus.WithError(err).Errorf("failed to read script %q", probe.Path)
167+
continue
168+
}
169+
script = string(b)
170+
}
161171
if probe.Mode == limayaml.ProbeModeReadiness {
162172
req = append(req, requirement{
163173
description: probe.Description,
164-
script: probe.Script,
174+
script: script,
165175
debugHint: probe.Hint,
166176
})
167177
}

pkg/limayaml/limayaml.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ type Provision struct {
188188
Mode ProvisionMode `yaml:"mode" json:"mode"` // default: "system"
189189
SkipDefaultDependencyResolution *bool `yaml:"skipDefaultDependencyResolution,omitempty" json:"skipDefaultDependencyResolution,omitempty"`
190190
Script string `yaml:"script" json:"script"`
191+
Path string `yaml:"path,omitempty" json:"path,omitempty"`
191192
Playbook string `yaml:"playbook,omitempty" json:"playbook,omitempty"`
192193
}
193194

@@ -207,6 +208,7 @@ type Probe struct {
207208
Mode ProbeMode // default: "readiness"
208209
Description string
209210
Script string
211+
Path string `yaml:",omitempty" json:",omitempty"`
210212
Hint string
211213
}
212214

pkg/limayaml/validate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ func Validate(y *LimaYAML, warn bool) error {
189189
return fmt.Errorf("field `provision[%d].mode` must one of %q, %q, %q, %q, or %q",
190190
i, ProvisionModeSystem, ProvisionModeUser, ProvisionModeBoot, ProvisionModeDependency, ProvisionModeAnsible)
191191
}
192+
if p.Path != "" {
193+
if p.Script != "" {
194+
return fmt.Errorf("field `provision[%d].script must be empty if path is set", i)
195+
}
196+
if _, err := os.Stat(p.Path); err != nil {
197+
return fmt.Errorf("field `provision[%d].path` refers to an inaccessible path: %q: %w", i, p.Path, err)
198+
}
199+
}
192200
if strings.Contains(p.Script, "LIMA_CIDATA") {
193201
logrus.Warn("provisioning scripts should not reference the LIMA_CIDATA variables")
194202
}
@@ -204,6 +212,14 @@ func Validate(y *LimaYAML, warn bool) error {
204212
return fmt.Errorf("field `probe[%d].mode` can only be %q",
205213
i, ProbeModeReadiness)
206214
}
215+
if p.Path != "" {
216+
if p.Script != "" {
217+
return fmt.Errorf("field `probe[%d].script must be empty if path is set", i)
218+
}
219+
if _, err := os.Stat(p.Path); err != nil {
220+
return fmt.Errorf("field `probe[%d].path` refers to an inaccessible path: %q: %w", i, p.Path, err)
221+
}
222+
}
207223
}
208224
for i, rule := range y.PortForwards {
209225
field := fmt.Sprintf("portForwards[%d]", i)

0 commit comments

Comments
 (0)