-
Notifications
You must be signed in to change notification settings - Fork 12
/
lima
executable file
·164 lines (129 loc) · 4.75 KB
/
lima
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env bash
set -e
# proxy most commands straight through to limactl
if [ "$1" != "create-dev" ] && [ "$1" != "shell" ]; then
echo "${@:1}"
limactl "${@:1}"
exit $?
fi
if [ "$1" == "shell" ]; then
echo "${@:1}"
limactl shell --workdir=$(limactl list --json | jq -r .config.user.home) "${@:2}"
exit $?
fi
if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then
echo "Error: Bash version 4 required. If on macos, install bash from homebrew: brew install bash"
exit 1
fi
if ! [ -x "$(command -v limactl)" ]; then
echo "no limactl command found. Install lima from https://lima-vm.io/docs/installation/"
exit 1
fi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
if [ ! -d venv ]; then
./update.sh
fi
source ./venv/bin/activate
project=$2
if [ -z "$project" ]; then
PS3="Select a project: "
select project in $(./.project.py --format json --env=lima | jq -r '.|join(" ")')
do
project=${project:-$REPLY}
break;
done
fi
if ! (./.project.py --format json --env=lima $project 2>&1 >/dev/null); then
echo ""
echo "project $project doesn't exist in builder project config"
exit 1
fi
project_config="$(./.project.py --format json --env=lima $project)"
dependant_formulas=$(echo "$project_config" | yq -o j '(.["formula-dependencies"] // [])|map({"url": ., "name": .|split("/")[-1]})' | jq -c)
project_formula=$(echo "$project_config" | yq -o j '.["formula-repo"]|{"url": ., "name": .|split("/")[-1]}' | jq -c)
all_formulas=$(echo "[$project_formula]$dependant_formulas" | jq -s 'add' | jq -c)
first_dependant_formula=$(echo "$dependant_formulas" | jq -c '.[0]')
all_the_rest_dependant_formula=$(echo "$dependant_formulas" | jq -c '.[1:]')
readarray -t all_formulas_array < <(echo "$all_formulas" | jq -c '.[]')
for formula in "${all_formulas_array[@]}"; do
formula_name=$(echo $formula | jq -r .name)
formula_url=$(echo $formula | jq -r .url)
formula_path="$DIR/cloned-projects/$formula_name"
if [ ! -d "$formula_path/.git" ]; then
echo "cloning $formula_name"
git clone "$formula_url" "$formula_path" 2>&1 > /dev/null
else
echo "updating $formula_name"
git -C "$formula_path" pull 2>&1 > /dev/null || echo "issue updating checkout"
fi
done
# Make directories for override salt formulas
mkdir -p $DIR/dev-env/$project/salt
mkdir -p $DIR/dev-env/$project/pillar
# Make the project formula example.top into a top.sls
(project_formula_name=$(echo $project_formula | jq -r .name); cd $DIR/cloned-projects/$project_formula_name/salt; ln -sf example.top top.sls)
minion_config=$(cat << EOF
---
file_client: local
log_level: info
fileserver_backend:
- roots
# Expected order:
# The dev-env override salt formulas
# The first dependant salt formula (usually builder-base-formula)
# The project salt formulas
# The rest of the dependant salt formulas
$(echo "[$first_dependant_formula] [$project_formula] $all_the_rest_dependant_formula" | jq -s "add" | yq -P -o yaml '{"file_roots":{"base":["/builder/dev-env/'$project'/salt/"] + (.|map("/builder/cloned-projects/" + .name + "/salt/"))}}')
# Expected order:
# The dev-env override formula pillar data
# The project formula pillar data
# The dependant formula pillar data
$(echo "$all_formulas" | yq -P -o yaml '{"pillar_roots":{"base":["/builder/dev-env/'$project'/pillar/"] + (.|map("/builder/cloned-projects/" + .name + "/salt/pillar"))}}')
EOF
)
minion_config_base64=$(echo "$minion_config" | yq '.|@yaml|@base64')
instance_name="$project--dev"
salt_version="$(echo $project_config | jq -r .salt)"
lima_config=$(cat << EOF
minimumLimaVersion: "1.0.0"
# turn off containerd setup
containerd:
system: false
user: false
ssh:
forwardAgent: true
# Mount script directory in the VM
mounts:
- location: "$DIR"
mountPoint: /builder
writable: false
## Provision
# Update system before anything else
upgradePackages: true
provision:
# Do bootstrap
- mode: system
script: |
#!/bin/bash
bash /builder/scripts/bootstrap.sh "$salt_version" "$instance_name" "false"
systemctl stop salt-minion 2> /dev/null
systemctl disable salt-minion 2> /dev/null
hostname $instance_name
# Setup new dev env
- mode: system
script: |
#!/bin/bash
echo $minion_config_base64 | base64 -d > /etc/salt/minion
# Embed entire project lima config here
$(echo $project_config | jq .lima | yq -P --output-format yaml '.')
# End project config
EOF
)
vm_name="$project"
echo "$lima_config" | limactl create --tty=false --name="$vm_name" -
limactl start $vm_name
if [[ $* != *--skip-highstate* ]]; then
limactl shell --workdir=$(limactl list --json | jq -r .config.user.home) $vm_name sudo salt-call state.apply
limactl shell --workdir=$(limactl list --json | jq -r .config.user.home) $vm_name sudo loginctl kill-user $(limactl list --json | jq -r .config.user.name)
fi