-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
221 lines (174 loc) · 5.82 KB
/
Vagrantfile
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- mode: ruby -*-
# vi: set ft=ruby :
require "getoptlong"
opts = GetoptLong.new(
["--local", GetoptLong::NO_ARGUMENT],
["--headless", GetoptLong::NO_ARGUMENT],
["--playbook", GetoptLong::OPTIONAL_ARGUMENT],
["--architecture", GetoptLong::OPTIONAL_ARGUMENT],
["--os", GetoptLong::OPTIONAL_ARGUMENT],
["-f", GetoptLong::OPTIONAL_ARGUMENT],
# Pass arguments to actual vagrant, but they're not used here
["--provider", GetoptLong::OPTIONAL_ARGUMENT],
["--help", GetoptLong::OPTIONAL_ARGUMENT],
["--debug", GetoptLong::OPTIONAL_ARGUMENT]
)
local = false
gui = true
os = "ubuntu/focal64"
playbook = "common-desktop.yml"
architecture = "amd64"
opts.each do |opt, arg|
case opt
when "--local"
local = true
when "--os"
os = arg
when "--headless"
gui = false
when "--playbook"
playbook = arg
when "--architecture"
architecture = arg
end
end
# Example:
# * vagrant --os=ubuntu/jammy64 --playbook=user.yml --local --headless up
# * vagrant --os=bento/ubuntu-24.04 --playbook=common-desktop.yml --local up
# * vagrant --os=cloud-image/ubuntu-24.04 --local --headless --architecture=arm64 up --provider libvirt
#
# Known problems:
# * Sometimes disk fails to set up (systemd waits on disk uuid), `vagrant destroy` and try again in this case
Vagrant.configure("2") do |config|
if architecture == "arm64"
config.vagrant.plugins = "vagrant-libvirt"
config.vm.box_architecture = architecture
end
config.vm.synced_folder ".", "/vagrant"
# https://developer.hashicorp.com/vagrant/docs/vagrantfile/machine_settings
config.vm.box = os
config.vm.boot_timeout = 600
# Need to run `vagrant provision` explicitly for that to work
config.trigger.after [:provision] do |trigger|
trigger.name = "Reboot after provisioning"
trigger.run = { :inline => "vagrant reload" }
end
# https://vagrant-libvirt.github.io/vagrant-libvirt/configuration.html
# Currently libvirt is only used for arm64. However, to destroy arm machines
# even if the architecture argument is not passed, we need to set all the
# libvirt options anyway, in particular to make this fix work
# https://github.com/vagrant-libvirt/vagrant-libvirt/pull/1329/files
config.vm.provider "libvirt" do |libvirt|
# Give more resources, OOMs by default
libvirt.memory = 8000
# Max 8 cores allowed for arm cpu
libvirt.cpus = [`nproc`.to_i, 8].min
libvirt.machine_type = "virt"
# https://libvirt.org/formatdomain.html
# In this mode, the cpu element describes the CPU that should be presented
# to the guest. This is the default when no mode attribute is specified.
# This mode makes it so that a persistent guest will see the same hardware
# no matter what host the guest is booted on.
libvirt.cpu_mode = "custom"
libvirt.cpu_model = "cortex-a57"
libvirt.driver = "qemu"
libvirt.machine_arch = "aarch64"
# Enable UEFI, refuses to work otherwise
libvirt.nvram = true
libvirt.loader = "/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd"
# Errors otherwise
libvirt.inputs = []
end
config.vm.provider "virtualbox" do |vb|
# Give more resources, OOMs by default
vb.memory = 8000
vb.cpus = `nproc`.to_i
# Make UI fast
vb.gui = gui
if gui
vb.customize ["modifyvm", :id, "--vram", "128"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
end
# Disable annoying warnings
vb.check_guest_additions = false
end
if local
config.vm.provision "shell", env: {}, inline: <<-SHELL
set -uex
systemctl disable systemd-networkd.service
rm -f /etc/resolv.conf
echo "nameserver 8.8.8.8" > /etc/resolv.conf
TMPDIR=$(mktemp -d)
cd "${TMPDIR}"
rm -rf ansible-playbooks
cp -R /vagrant ansible-playbooks
cd ansible-playbooks
rm -rf manual
mkdir manual
sed 's/# //g' roles/user/defaults/main.yml > manual/common.yml
chown -R vagrant .
set +e
for i in {1..3}; do
# Run twice to make sure users are added to correct groups
sudo -u vagrant ./bootstrap.sh #{playbook} LOCAL && \
sudo -u vagrant ./bootstrap.sh #{playbook} LOCAL && \
break
done
if [[ $? -ne 0 ]]; then
echo "Failed to provision"
exit 1
fi
set -e
apt-get update
apt-get upgrade -y
apt-get update
apt-get dist-upgrade -y
apt-get update
apt-get upgrade -y
sudo apt-get autoremove -y
sudo apt purge -y '~c'
id -u user && cd /tmp/ && sudo -u user /home/user/.bin/init-user-env.sh || true
reboot
SHELL
else
config.vm.provision "shell", env: {}, inline: <<-SHELL
set -uex
systemctl disable systemd-networkd.service
rm -f /etc/resolv.conf
echo "nameserver 8.8.8.8" > /etc/resolv.conf
TMPDIR=$(mktemp -d)
cd "${TMPDIR}"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y git
rm -rf ansible-playbooks
git clone https://github.com/fspv/ansible-playbooks.git
cd ansible-playbooks
mkdir manual
sed 's/# //g' roles/user/defaults/main.yml > manual/common.yml
chown -R vagrant .
set +e
if [[ $? -ne 0 ]]; then
# Run twice to make sure users are added to correct groups
sudo -u vagrant ./bootstrap.sh #{playbook} LOCAL && \
sudo -u vagrant ./bootstrap.sh #{playbook} LOCAL && \
break
done
if $? -ne 0; then
echo "Failed to provision"
exit 1
fi
set -e
apt-get update
apt-get upgrade -y
apt-get update
apt-get dist-upgrade -y
apt-get update
apt-get upgrade -y
sudo apt-get autoremove -y
sudo apt purge -y '~c'
id -u user && cd /tmp/ && sudo -u user /home/user/.bin/init-user-env.sh || true
reboot
SHELL
end
end