Skip to content

Commit f62ef00

Browse files
committed
Do guest template expansion for provisioning scripts
The LIMA_CIDATA_* variables are not a supported API and can change without warning. Signed-off-by: Jan Dubois <[email protected]>
1 parent bd7442e commit f62ef00

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

examples/default.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ containerd:
184184

185185
# Provisioning scripts need to be idempotent because they might be called
186186
# multiple times, e.g. when the host VM is being restarted.
187+
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, and {{.User}}
187188
# 🟢 Builtin default: null
188189
# provision:
189190
# # `system` is executed with the root privilege

examples/docker-rootful.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# $ export DOCKER_HOST=$(limactl list docker-rootful --format 'unix://{{.Dir}}/sock/docker.sock')
77
# $ docker ...
88

9-
# This template requires Lima v0.8.0 or later
9+
# This template requires Lima v0.20.0 or later
1010
images:
1111
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
1212
- location: "https://cloud-images.ubuntu.com/releases/22.04/release-20231211/ubuntu-22.04-server-cloudimg-amd64.img"
@@ -49,7 +49,7 @@ provision:
4949
# Alternatively we could just add the user to the "docker" group, but that requires restarting the user session
5050
cat <<-EOF >/etc/systemd/system/docker.socket.d/override.conf
5151
[Socket]
52-
SocketUser=${LIMA_CIDATA_USER}
52+
SocketUser={{.User}}
5353
EOF
5454
fi
5555
export DEBIAN_FRONTEND=noninteractive

examples/experimental/vnc.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# A template to run ubuntu using display: vnc
2-
# This template requires Lima v0.15.0 or later.
2+
# This template requires Lima v0.20.0 or later.
33
images:
44
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
55
- location: "https://cloud-images.ubuntu.com/releases/23.10/release-20231011/ubuntu-23.10-server-cloudimg-amd64.img"
@@ -33,7 +33,7 @@ provision:
3333
export DEBIAN_FRONTEND=noninteractive
3434
# x-terminal-emulator x-session-manager x-window-manager
3535
apt-get install -y xorg xterm openbox hsetroot tint2 slim
36-
printf "auto_login yes\ndefault_user ${LIMA_CIDATA_USER}\n" >>/etc/slim.conf
36+
printf "auto_login yes\ndefault_user {{.User}}\n" >>/etc/slim.conf
3737
# configure some nice lima green, set up panel and apps
3838
printf "hsetroot -solid \"#32CD32\" &\ntint2 &\n" >>/etc/xdg/openbox/autostart
3939
sed -i 's/Clearlooks/Clearlooks-Olive/' /etc/xdg/openbox/rc.xml # go for green

examples/podman-rootful.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# $ export DOCKER_HOST=$(limactl list podman-rootful --format 'unix://{{.Dir}}/sock/podman.sock')
1111
# $ docker ...
1212

13-
# This template requires Lima v0.8.0 or later
13+
# This template requires Lima v0.20.0 or later
1414
images:
1515
- location: "https://download.fedoraproject.org/pub/fedora/linux/releases/39/Cloud/x86_64/images/Fedora-Cloud-Base-39-1.5.x86_64.qcow2"
1616
arch: "x86_64"
@@ -36,12 +36,12 @@ provision:
3636
mkdir -p /etc/systemd/system/podman.socket.d
3737
cat <<-EOF >/etc/systemd/system/podman.socket.d/override.conf
3838
[Socket]
39-
SocketUser=${LIMA_CIDATA_USER}
39+
SocketUser={{.User}}
4040
EOF
4141
fi
4242
if [ ! -e /etc/tmpfiles.d/podman.conf ]; then
4343
mkdir -p /etc/tmpfiles.d
44-
echo "d /run/podman 0700 ${LIMA_CIDATA_USER} -" > /etc/tmpfiles.d/podman.conf
44+
echo "d /run/podman 0700 {{.User}} -" > /etc/tmpfiles.d/podman.conf
4545
fi
4646
dnf -y install podman
4747
- mode: system

pkg/limayaml/defaults.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
427427
if provision.Mode == ProvisionModeDependency && provision.SkipDefaultDependencyResolution == nil {
428428
provision.SkipDefaultDependencyResolution = ptr.Of(false)
429429
}
430+
if out, err := executeGuestTemplate(provision.Script); err == nil {
431+
provision.Script = out.String()
432+
} else {
433+
logrus.WithError(err).Warnf("Couldn't process provisioning script %q as a template", provision.Script)
434+
}
430435
}
431436

432437
if y.GuestInstallPrefix == nil {
@@ -795,7 +800,7 @@ func executeHostTemplate(format, instDir string) (bytes.Buffer, error) {
795800
"User": user.Username,
796801

797802
"Instance": filepath.Base(instDir), // DEPRECATED, use `{{.Name}}`
798-
"LimaHome": limaHome, // DEPRECATED, (use `Dir` instead of `{{.LimaHome}}/{{.Instance}}`
803+
"LimaHome": limaHome, // DEPRECATED, use `{{.Dir}}` instead of `{{.LimaHome}}/{{.Instance}}`
799804
}
800805
var out bytes.Buffer
801806
if err := tmpl.Execute(&out, data); err == nil {

0 commit comments

Comments
 (0)