-
Notifications
You must be signed in to change notification settings - Fork 424
/
Vagrantfile
285 lines (239 loc) · 9.5 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'etc'
host_os = case
when Vagrant::Util::Platform.linux?
"Linux"
when Vagrant::Util::Platform.darwin?
"Darwin"
else
puts "ERROR: Host OS is not supported."
abort
end
arch = case `uname -m`.strip
when "x86_64", "amd64"
"amd64"
when "aarch64", "arm64"
"arm64"
else
puts "ERROR: Architecture is not supported."
abort
end
# VM_TYPE is used to determine the environment (dev or test) to provision the VM for.
# By default, the VM is provisioned for the "dev" environment.
# To provision the VM for the "test" environment, set the VM_TYPE environment variable to "test".
# Example: VM_TYPE=test vagrant up
vm_type = ENV["VM_TYPE"]
if vm_type.nil? || vm_type.empty?
vm_type = "dev"
end
if vm_type != "dev" && vm_type != 'test'
puts "ERROR: Invalid VM_TYPE value. Only 'dev' or 'test' values are allowed."
abort
end
vm_name = "tracee-#{vm_type}-vm"
vm_proc = ENV["VM_PROC"]
if vm_proc.nil? || vm_proc.empty?
vm_proc = Etc.nprocessors / 2
end
# in GB
vm_mem = ENV["VM_MEM"]
if vm_mem.nil? || vm_mem.empty?
vm_mem = "8"
end
vm_user = "vagrant"
vm_synced_folder = "/#{vm_user}"
provider_settings = lambda do |prov, name|
if name == "virtualbox" || name == "parallels"
prov.name = vm_name
prov.cpus = vm_proc
prov.memory = vm_mem.to_i * 1024
end
if name == "virtualbox"
prov.gui = false
end
if name == "parallels"
prov.update_guest_tools = true
end
end
Vagrant.configure("2") do |config|
config.ssh.extra_args = ["-t", "cd #{vm_synced_folder}; bash --login"]
# set the synced folder type based on the host OS and architecture
if host_os == "Linux" || (host_os == "Darwin" && arch == "amd64")
config.vm.synced_folder ".", "#{vm_synced_folder}", type: "virtualbox", auto_mount: false
elsif host_os == "Darwin" && arch == "arm64"
config.vm.synced_folder ".", "#{vm_synced_folder}", type: "parallels"
end
# define the machine with the dynamically set vm_name
config.vm.define vm_name do |vm_config|
# virtualbox, parallels, vmware_desktop, qemu, libvirt
vm_config.vm.box = "bento/ubuntu-22.04"
case host_os
when "Linux"
vm_config.vm.provider "virtualbox" do |vb|
provider_settings.call(vb, "virtualbox")
end
when "Darwin"
case arch
when "amd64"
vm_config.vm.provider "virtualbox" do |vb|
provider_settings.call(vb, "virtualbox")
end
end
when "arm64"
vm_config.vm.provider "parallels" do |prl|
provider_settings.call(prl, "parallels")
end
end
# network settings
vm_config.vm.hostname = vm_name
vm_config.vm.network "forwarded_port", guest: 9090, host: 9090, auto_correct: true
vm_config.vm.network "forwarded_port", guest: 3366, host: 3366, auto_correct: true
vm_config.vm.network "forwarded_port", guest: 3000, host: 3000, auto_correct: true
# forward MkDocs dev server to preview documentation on the host at http://localhost:8000/tracee
vm_config.vm.network :forwarded_port, guest: 8000, host: 8000, auto_correct: true
if vm_type == "dev"
# Forward MicroK8s dashboard to access it on the host at https://localhost:10443
#
# To access the Kubernetes dashboard from the host run the following command:
# kubectl port-forward --address 0.0.0.0 -n kube-system service/kubernetes-dashboard 10443:443
#
# To sign in use the token retrieved with
# token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
# kubectl -n kube-system describe secret $token
#
# TIP For Google Chrome you may allow insecure TLS connections at chrome://flags/#allow-insecure-localhost
vm_config.vm.network :forwarded_port, guest: 10443, host: 10443, auto_correct: true
end
vm_config.vm.provision "shell", privileged: true, inline: <<-SHELL
set -e
ARCH="#{arch}"
USER="#{vm_user}"
HOME="/home/#{vm_user}"
LLVM_VERSION="14"
GO_VERSION="1.22.3"
KUBECTL_VERSION="v1.29"
VM_TYPE="#{vm_type}"
# silence 'dpkg-preconfigure: unable to re-open stdin: No such file or directory'
export DEBIAN_FRONTEND=noninteractive
echo ">>> Updating system packages"
apt-get update
apt-get --yes upgrade
#
# build environment for tracee
#
echo ">>> Installing build environment for tracee"
apt-get install --yes bsdutils
apt-get install --yes build-essential
apt-get install --yes pkgconf
apt-get install --yes llvm-${LLVM_VERSION} clang-${LLVM_VERSION}
for tool in "clang" "llc" "llvm-strip"
do
path=$(which ${tool}-${LLVM_VERSION})
ln -s -f "$path" "${path%-*}"
done
rm -f /usr/bin/clang-format-12
curl -L -o /tmp/clang-format-12 https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-f4f85437/clang-format-12.0.1_linux-${ARCH}
sudo mv -f /tmp/clang-format-12 /usr/bin/clang-format-12
sudo chmod 755 /usr/bin/clang-format-12
apt-get install --yes zlib1g-dev libelf-dev libzstd-dev
apt-get install --yes protobuf-compiler
# golang
cd /tmp
wget --quiet https://golang.org/dl/go${GO_VERSION}.linux-${ARCH}.tar.gz
tar -C /usr/local -xzf go${GO_VERSION}.linux-${ARCH}.tar.gz
GOBIN_PATH=/usr/local/go/bin
echo "export PATH=${PATH}:${GOBIN_PATH}" >> ${HOME}/.profile
# integration tests run as root, so go needs to be in root's path as well
echo "export PATH=${PATH}:${GOBIN_PATH}" >> $HOME/.bashrc
# sudo needs to be able to find go as well
echo "Defaults secure_path=\"${PATH}:${GOBIN_PATH}\"" >> /etc/sudoers.d/${USER}
rm -f go${GO_VERSION}.linux-${ARCH}.tar.gz
cd -
# other tools
apt-get install --yes python3 pip jq
pip install docker boto3 psutil jmespath
# install MicroK8s and related tools if VM_TYPE is "dev"
if [ "${VM_TYPE}" = "dev" ]; then
echo ">>> Installing MicroK8s and related tools"
# microk8s
echo ">>> Installing microk8s"
snap install microk8s --classic
microk8s status --wait-ready
usermod -a -G microk8s ${USER}
microk8s enable hostpath-storage dns dashboard
mkdir -p ${HOME}/.kube/
microk8s kubectl config view --raw > ${HOME}/.kube/config
chmod 600 ${HOME}/.kube/config
chown ${USER}:${USER} ${HOME}/.kube/config
# kubectl
echo ">>> Installing kubectl"
apt-get install -y apt-transport-https ca-certificates curl
curl -fsSL https://pkgs.k8s.io/core:/stable:/${KUBECTL_VERSION}/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/${KUBECTL_VERSION}/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list
chmod 644 /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install --yes kubectl
echo 'source <(kubectl completion bash)' >> ${HOME}/.profile
# helm
echo ">>> Installing helm"
snap install helm --classic
echo 'source <(helm completion bash)' >> ${HOME}/.profile
fi
#
# docker
#
echo ">>> Installing docker"
apt-get install --yes docker.io
usermod -aG docker ${USER}
SHELL
vm_config.vm.provision "shell", privileged: true, reboot: true, inline: <<-SHELL
set -e
KERNEL_VERSION="6.2.0-1018-aws"
USER="#{vm_user}"
# silence 'dpkg-preconfigure: unable to re-open stdin: No such file or directory'
export DEBIAN_FRONTEND=noninteractive
echo ">>> Installing kernel ${KERNEL_VERSION}"
apt-get install --yes \
dkms \
linux-image-${KERNEL_VERSION} \
linux-headers-${KERNEL_VERSION} \
linux-modules-${KERNEL_VERSION} \
linux-tools-${KERNEL_VERSION}
SHELL
share_provisioning = lambda do |vm_config, name|
if name == "virtualbox"
vbox_version = `VBoxManage --version`.strip.match(/^(\d+\.\d+\.\d+)/)[1]
# this provision stage must always run to ensure the shared folder is mounted
# when host changes the VirtualBox Guest Additions version.
vm_config.vm.provision "shell", run: "always", privileged: true, inline: <<-SHELL
set +e
VBOX_VERSION="#{vbox_version}"
SYNCED_FOLDER="#{vm_synced_folder}"
USER="#{vm_user}"
echo ">>> Installing VirtualBox Guest Additions ${VBOX_VERSION}"
cd /tmp
wget --quiet https://download.virtualbox.org/virtualbox/${VBOX_VERSION}/VBoxGuestAdditions_${VBOX_VERSION}.iso
mkdir -p /mnt/media
mount -o loop,ro VBoxGuestAdditions_${VBOX_VERSION}.iso /mnt/media
/mnt/media/VBoxLinuxAdditions.run --nox11
umount /mnt/media
rm -f VBoxGuestAdditions_${VBOX_VERSION}.iso
cd -
/sbin/rcvboxadd quicksetup all
echo ">>> Mounting shared folder ${SYNCED_FOLDER}"
mkdir -p ${SYNCED_FOLDER}
mount -t vboxsf -o uid=1000,gid=1000,_netdev ${USER} ${SYNCED_FOLDER}
SHELL
elsif name == "parallels"
# nothing to do here, shared folders are mounted automatically
end
end
if host_os == "Linux" || (host_os == "Darwin" && arch == "amd64")
share_provisioning.call(vm_config, "virtualbox")
elsif host_os == "Darwin" && arch == "arm64"
share_provisioning.call(vm_config, "parallels")
end
end
end