-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathVagrantfile
220 lines (165 loc) · 6.35 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.require_version ">= 2.0.0"
# just a single node is required
NODES = ENV['NODES'] || 1
# Memory & CPUs
MEM = ENV['MEM'] || 4096
CPUS = ENV['CPUS'] || 2
# User Data Mount
#SRCDIR = ENV['SRCDIR'] || "/home/"+ENV['USER']+"/test"
SRCDIR = ENV['SRCDIR'] || "/tmp/vagrant"
DSTDIR = ENV['DSTDIR'] || "/home/vagrant/data"
# Management
GROWPART = ENV['GROWPART'] || "true"
# Minikube Variables
KUBERNETES_VERSION = ENV['KUBERNETES_VERSION'] || "1.16.3"
# Common installation script
$installer = <<SCRIPT
#!/bin/bash
# Update apt and get dependencies
sudo apt-get -y update
sudo apt-mark hold grub
sudo apt-mark hold grub-pc
sudo apt-get -y upgrade
sudo apt-get install -y zip unzip curl wget socat ebtables git vim
SCRIPT
$docker = <<SCRIPT
#!/bin/bash
#curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -
#sudo apt-add-repository "deb https://apt.dockerproject.org/repo ubuntu-xenial main"
#sudo apt-get install -y docker-engine=17.03.1~ce-0~ubuntu-xenial
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get install -y docker-ce
sudo systemctl start docker
sudo usermod -a -G docker vagrant
SCRIPT
$growpart = <<SCRIPT
#!/bin/bash
if [[ -b /dev/vda3 ]]; then
sudo growpart /dev/vda 3
sudo resize2fs /dev/vda3
elif [[ -b /dev/sda3 ]]; then
sudo growpart /dev/sda 3
sudo resize2fs /dev/sda3
fi
SCRIPT
$minikubescript = <<SCRIPT
#!/bin/bash
#Install minikube
echo "Downloading Minikube"
curl -q -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 2>/dev/null
chmod +x minikube
sudo mv minikube /usr/local/bin/
#Install kubectl
echo "Downloading Kubectl"
curl -q -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VERSION}/bin/linux/amd64/kubectl 2>/dev/null
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
# Install crictl
curl -qL https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.16.1/crictl-v1.16.1-linux-amd64.tar.gz 2>/dev/null | tar xzvf -
chmod +x crictl
sudo mv crictl /usr/local/bin/
#Install stern
# TODO: Check sha256sum
echo "Downloading Stern"
curl -q -Lo stern https://github.com/wercker/stern/releases/download/1.10.0/stern_linux_amd64 2>/dev/null
chmod +x stern
sudo mv stern /usr/local/bin/
#Install kubecfg
# TODO: Check sha256sum
echo "Downloading Kubecfg"
curl -q -Lo kubecfg https://github.com/ksonnet/kubecfg/releases/download/v0.9.0/kubecfg-linux-amd64 2>/dev/null
chmod +x kubecfg
sudo mv kubecfg /usr/local/bin/
#Setup minikube
echo "127.0.0.1 minikube minikube." | sudo tee -a /etc/hosts
mkdir -p $HOME/.minikube
mkdir -p $HOME/.kube
touch $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
# Permissions
sudo chown -R $USER:$USER $HOME/.kube
sudo chown -R $USER:$USER $HOME/.minikube
export MINIKUBE_WANTUPDATENOTIFICATION=false
export MINIKUBE_WANTREPORTERRORPROMPT=false
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true
export KUBECONFIG=$HOME/.kube/config
# Disable SWAP since is not supported on a kubernetes cluster
sudo swapoff -a
## Start minikube
sudo -E minikube start -v 4 --vm-driver none --kubernetes-version v${KUBERNETES_VERSION} --bootstrapper kubeadm
## Addons
sudo -E minikube addons enable ingress
## Configure vagrant clients dir
printf "export MINIKUBE_WANTUPDATENOTIFICATION=false\n" >> /home/vagrant/.bashrc
printf "export MINIKUBE_WANTREPORTERRORPROMPT=false\n" >> /home/vagrant/.bashrc
printf "export MINIKUBE_HOME=/home/vagrant\n" >> /home/vagrant/.bashrc
printf "export CHANGE_MINIKUBE_NONE_USER=true\n" >> /home/vagrant/.bashrc
printf "export KUBECONFIG=/home/vagrant/.kube/config\n" >> /home/vagrant/.bashrc
printf "source <(kubectl completion bash)\n" >> /home/vagrant/.bashrc
# Permissions
sudo chown -R $USER:$USER $HOME/.kube
sudo chown -R $USER:$USER $HOME/.minikube
# Enforce sysctl
sudo sysctl -w vm.max_map_count=262144
sudo echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.d/90-vm_max_map_count.conf
SCRIPT
required_plugins = %w(vagrant-sshfs vagrant-vbguest vagrant-libvirt)
required_plugins.each do |plugin|
need_restart = false
unless Vagrant.has_plugin? plugin
system "vagrant plugin install #{plugin}"
need_restart = true
end
exec "vagrant #{ARGV.join(' ')}" if need_restart
end
def configureVM(vmCfg, hostname, cpus, mem, srcdir, dstdir)
vmCfg.vm.box = "roboxes/ubuntu1804"
vmCfg.vm.hostname = hostname
vmCfg.vm.network "private_network", type: "dhcp", :model_type => "virtio", :autostart => true
vmCfg.vm.synced_folder '.', '/vagrant', disabled: true
# sync your laptop's development with this Vagrant VM
vmCfg.vm.synced_folder srcdir, dstdir, type: "rsync", rsync__exclude: ".git/", create: true
# First Provider - Libvirt
vmCfg.vm.provider "libvirt" do |provider, override|
provider.memory = mem
provider.cpus = cpus
provider.driver = "kvm"
provider.disk_bus = "scsi"
provider.machine_virtual_size = 64
provider.video_vram = 64
override.vm.synced_folder srcdir, dstdir, type: 'sshfs', ssh_opts_append: "-o Compression=yes", sshfs_opts_append: "-o cache=no", disabled: false, create: true
end
vmCfg.vm.provider "virtualbox" do |provider, override|
provider.memory = mem
provider.cpus = cpus
provider.customize ["modifyvm", :id, "--cableconnected1", "on"]
override.vm.synced_folder srcdir, dstdir, type: 'virtualbox', create: true
end
# ensure docker is installed # Use our script so we can get a proper support version
vmCfg.vm.provision "shell", inline: $docker, privileged: false
# Script to prepare the VM
vmCfg.vm.provision "shell", inline: $installer, privileged: false
vmCfg.vm.provision "shell", inline: $growpart, privileged: false if GROWPART == "true"
vmCfg.vm.provision "shell", inline: $minikubescript, privileged: false, env: {"KUBERNETES_VERSION" => KUBERNETES_VERSION}
return vmCfg
end
# Entry point of this Vagrantfile
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vbguest.auto_update = false
1.upto(NODES.to_i) do |i|
hostname = "minikube-vagrant-%02d" % [i]
cpus = CPUS
mem = MEM
srcdir = SRCDIR
dstdir = DSTDIR
config.vm.define hostname do |vmCfg|
vmCfg = configureVM(vmCfg, hostname, cpus, mem, srcdir, dstdir)
end
end
end