forked from openebs-archive/maya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
212 lines (163 loc) · 5.9 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.require_version ">= 1.6.0"
# Maya Nodes
M_NODES = ENV['M_NODES'] || 1
# Maya Memory & CPUs
M_MEM = ENV['M_MEM'] || 4096
M_CPUS = ENV['M_CPUS'] || 2
# Common installation script
$installer = <<SCRIPT
#!/bin/bash
echo Will run the common installer script ...
# Update apt and get dependencies
sudo apt-get update
sudo apt-get install -y zip unzip curl wget
SCRIPT
$mayascript = <<SCRIPT
#!/bin/bash
cd /opt/gopath/src/github.com/openebs/maya
# Install dependencies required for Development
bash buildscripts/install_go.sh
# CD into the maya working directory when we login to the VM
grep "cd /opt/gopath/src/github.com/openebs/maya" /home/vagrant/.profile || \
echo "cd /opt/gopath/src/github.com/openebs/maya" >> /home/vagrant/.profile
echo ""
echo "================================================"
echo "Congrats!! Maya has been setup for development"
echo "================================================"
echo ""
SCRIPT
$minikubescript = <<SCRIPT
#!/bin/bash
#Install minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/
#Install kubectl
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
#Setup minikube
mkdir -p $HOME/.minikube
mkdir -p $HOME/.kube
touch $HOME/.kube/config
# Push these ENV k:v to /home/vagrant/.profile to
# start/restart minikube. This vagrant VM might have been created
# earlier with minikube stopped for some reason.
grep "MINIKUBE_WANTUPDATENOTIFICATION=false" /home/vagrant/.profile || \
echo "MINIKUBE_WANTUPDATENOTIFICATION=false" >> /home/vagrant/.profile
grep "MINIKUBE_WANTREPORTERRORPROMPT=false" /home/vagrant/.profile || \
echo "MINIKUBE_WANTREPORTERRORPROMPT=false" >> /home/vagrant/.profile
grep "MINIKUBE_HOME=$HOME" /home/vagrant/.profile || \
echo "MINIKUBE_HOME=$HOME" >> /home/vagrant/.profile
grep "CHANGE_MINIKUBE_NONE_USER=true" /home/vagrant/.profile || \
echo "CHANGE_MINIKUBE_NONE_USER=true" >> /home/vagrant/.profile
grep "KUBECONFIG=$HOME/.kube/config" /home/vagrant/.profile || \
echo "KUBECONFIG=$HOME/.kube/config" >> /home/vagrant/.profile
# Export above as well for `minikube start` to work
# in the same session of `vagrant up`
export MINIKUBE_WANTUPDATENOTIFICATION=false
export MINIKUBE_WANTREPORTERRORPROMPT=false
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true
export KUBECONFIG=$HOME/.kube/config
# Permissions
sudo chown -R $USER $HOME/.kube
sudo chgrp -R $USER $HOME/.kube
sudo chown -R $USER $HOME/.minikube
sudo chgrp -R $USER $HOME/.minikube
# Start minikube on this host itself
sudo -E minikube start --vm-driver=none
# This loop waits until kubectl can access the api server
# that Minikube has created
for i in {1..20}; do # timeout for 20x3=60 seconds/1 minutes
kubectl get po &> /dev/null
if [ $? -ne 1 ]; then
echo ""
echo "============================================"
echo "Congrats!! minikube's apiserver is running"
echo "============================================"
echo ""
exit 0
fi
sleep 3
done
# Re-try minikube start
now=$(date +%Y%m%d-%H%M%S)
kubectl get po > /tmp/mk-$now 2>&1
grep "The connection to the server 127.0.0.1:8443 was refused - did you specify the right host or port?" /tmp/mk-$now && sudo -E minikube start --vm-driver=none
# loop for the final time
for i in {1..10}; do # timeout for 10x5=50 seconds/ < 1 minute
kubectl get po &> /dev/null
if [ $? -ne 1 ]; then
echo ""
echo "============================================"
echo "Congrats!! minikube's apiserver is running"
echo "============================================"
echo ""
exit 0
fi
sleep 5
done
# If still not running
kubectl get po &> /dev/null
if [ $? -ne 0 ]; then
echo ""
echo "================================================="
echo "Check Status :: minikube status"
echo "Start minikube if it's in stopped state"
echo "Start Command :: sudo -E minikube start --vm-driver=none"
echo "================================================="
echo ""
fi
SCRIPT
required_plugins = %w(vagrant-cachier)
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)
vmCfg.vm.box = "bento/ubuntu-16.04"
vmCfg.vm.hostname = hostname
vmCfg.vm.network "private_network", type: "dhcp"
#Adding Vagrant-cachier
if Vagrant.has_plugin?("vagrant-cachier")
vmCfg.cache.scope = :machine
vmCfg.cache.enable :apt
vmCfg.cache.enable :gem
end
# Set resources w.r.t Virtualbox provider
vmCfg.vm.provider "virtualbox" do |vb|
vb.memory = mem
vb.cpus = cpus
vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
end
# ensure docker is installed
vmCfg.vm.provision "docker"
# sync your laptop's development with this Vagrant VM
vmCfg.vm.synced_folder '.', '/opt/gopath/src/github.com/openebs/maya'
# Script to prepare the VM
vmCfg.vm.provision "shell", inline: $installer, privileged: false
vmCfg.vm.provision "shell", inline: $mayascript, privileged: false
vmCfg.vm.provision "shell", inline: $minikubescript, privileged: false
return vmCfg
end
# Entry point of this Vagrantfile
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# maya master related only !!
1.upto(M_NODES.to_i) do |i|
hostname = "maya-%02d" % [i]
cpus = M_CPUS
mem = M_MEM
config.vm.define hostname do |vmCfg|
vmCfg = configureVM(vmCfg, hostname, cpus, mem)
end
end
end