blob: 78668dfc3dc814df6aba827b07cf8e54c8ef66d3 [file] [log] [blame]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +01001# -*- mode: ruby -*-
2# -*- coding: utf-8 -*-
3
4host_ip = "192.168.121.1"
5operator_key = "${HOME}/.ssh/onap-key"
6vagrant_user = "vagrant"
7vagrant_password = "vagrant"
8synced_folder_main = "/vagrant"
9synced_folder_config = "#{synced_folder_main}/config"
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +010010synced_folder_tools_config = "#{synced_folder_main}/tools/config"
Pawel Wieczorekdf0edea2019-11-29 16:18:36 +010011os_config = "#{synced_folder_config}/local.conf"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +010012cluster_yml = "cluster.yml"
13apt_prefs_dir = "/etc/apt/apt.conf.d"
14apt_prefs = "95silent-approval"
15
Pawel Wieczorek3cb78212019-12-09 16:00:47 +010016vm_memory = 1 * 1024
17vm_memory_os = 6 * 1024
18vm_memory_onap = 12 * 1024
19vm_cpu = 1
20vm_cpus = 2
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +010021vm_box = "generic/ubuntu1804"
22
Pawel Wieczorek3cb78212019-12-09 16:00:47 +010023operation = { name: 'operator', hostname: 'operator', ip: '172.17.4.254', cpus: vm_cpu, memory: vm_memory }
24devstack = { name: 'devstack', hostname: 'devstack', ip: '172.17.4.200', cpus: vm_cpus, memory: vm_memory_os }
25control = { name: 'control', hostname: 'control', ip: '172.17.4.100', cpus: vm_cpu, memory: vm_memory }
26worker = { name: 'worker', hostname: 'worker', ip: '172.17.4.101', cpus: vm_cpus, memory: vm_memory_onap }
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +010027
Pawel Wieczorek3cb78212019-12-09 16:00:47 +010028cluster = [] << control << worker
Pawel Wieczorekdf0edea2019-11-29 16:18:36 +010029all = cluster.dup << operation << devstack
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +010030
31operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=rke_up,setup_kubectl\" to complete cluster creation"
32
33$replace_dns = <<-SCRIPT
34 HOST_IP="$1"
35 rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved
36 echo nameserver "$HOST_IP" | tee /etc/resolv.conf
37SCRIPT
38
Pawel Wieczorekdf0edea2019-11-29 16:18:36 +010039$enable_ipv6 = <<-SCRIPT
40 sed -i'' 's/net.ipv6.conf.all.disable_ipv6.*$/net.ipv6.conf.all.disable_ipv6 = 0/' /etc/sysctl.conf
41 sysctl -p
42SCRIPT
43
44$setup_devstack = <<-SCRIPT
45 CONFIG="$1"
46 git clone https://opendev.org/openstack/devstack
47 cd devstack
48 cp "$CONFIG" .
49 ./stack.sh
50SCRIPT
51
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +010052$add_to_docker_group = <<-SCRIPT
53 USER="$1"
54 echo "Adding ${USER} to 'docker' group"
55 usermod -aG docker "$USER"
56SCRIPT
57
58$setup_debconf = <<-SCRIPT
59 echo "Setting debconf frontend to noninteractive"
60 sed -i'.orig' '/^Config:/a Frontend: noninteractive' /etc/debconf.conf
61SCRIPT
62
63$install_sshpass = <<-SCRIPT
64 apt-get update
65 echo "Installing 'sshpass'"
66 apt-get install sshpass
67SCRIPT
68
69$generate_key = <<-SCRIPT
70 KEY_FILE="$1"
71 echo "Generating SSH key (${KEY_FILE})"
72 ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N ""
73SCRIPT
74
75$deploy_key = <<-SCRIPT
76 KEY="$1"
77 USER="$2"
78 PASS="$PASSWORD"
79 IPS="$3"
80 echo "Deploying ${KEY} for ${USER}"
81 for ip in $IPS; do
82 echo "on ${ip}"
83 sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}"
84 done
85SCRIPT
86
87$link_dotfiles = <<-SCRIPT
88 SYNC_DIR="$1"
89 for rc in ${SYNC_DIR}/dot_*; do
90 src="$rc"
91 dst="${HOME}/.${rc##*dot_}"
92 echo "Symlinking ${src} to ${dst}"
93 ln -sf "$src" "$dst"
94 done
95SCRIPT
96
97$link_file = <<-SCRIPT
98 SYNC_DIR="$1"
99 FILE="$2"
100 src="${SYNC_DIR}/${FILE}"
101 dst="$3"
102 echo "Symlinking ${src} to ${dst}"
103 ln -sf "$src" "$dst"
104SCRIPT
105
106$rke_up = "rke up"
107$rke_down = "rke remove --force"
108
Pawel Wieczorekf1176da2019-12-05 13:45:45 +0100109$get_oom = <<-SCRIPT
110 BRANCH="${1:-5.0.1-ONAP}"
111 git clone -b "$BRANCH" https://git.onap.org/oom --recurse-submodules
112SCRIPT
113
114$get_helm_plugins = "cp -R ${HOME}/oom/kubernetes/helm/plugins/ ${HOME}/.helm"
115
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100116Vagrant.configure('2') do |config|
117 all.each do |machine|
118 config.vm.define machine[:name] do |config|
119 config.vm.box = vm_box
120 config.vm.hostname = machine[:hostname]
121
122 config.vm.provider :virtualbox do |v|
123 v.name = machine[:name]
Pawel Wieczorek3cb78212019-12-09 16:00:47 +0100124 v.memory = machine[:memory]
125 v.cpus = machine[:cpus]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100126 end
127
128 config.vm.provider :libvirt do |v|
Pawel Wieczorek3cb78212019-12-09 16:00:47 +0100129 v.memory = machine[:memory]
130 v.cpus = machine[:cpus]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100131 end
132
133 config.vm.network :private_network, ip: machine[:ip]
134 config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip
135
Pawel Wieczorekdf0edea2019-11-29 16:18:36 +0100136 if machine[:name] == 'devstack'
137 config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
138
139 config.vm.provision "enable_ipv6", type: :shell, run: "always", inline: $enable_ipv6
140 config.vm.provision "setup_devstack", type: :shell, privileged: false, inline: $setup_devstack, args: os_config
141 end
142
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100143 if machine[:name] == 'control'
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100144 config.vm.provision "customize_control", type: :shell, path: "tools/imported/openstack-k8s-controlnode.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100145 config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user
146 end
147
148 if machine[:name] == 'worker'
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100149 config.vm.provision "customize_worker", type: :shell, path: "tools/imported/openstack-k8s-workernode.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100150 config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user
151 end
152
153 if machine[:name] == 'operator'
154 config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100155
156 config.vm.provision "setup_debconf", type: :shell, inline: $setup_debconf
157 config.vm.provision "link_apt_prefs", type: :shell, run: "always" do |s|
158 s.inline = $link_file
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100159 s.args = [synced_folder_tools_config, apt_prefs, apt_prefs_dir]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100160 end
161 config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s|
162 s.inline = $link_dotfiles
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100163 s.args = synced_folder_tools_config
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100164 end
165 config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s|
166 s.privileged = false
167 s.inline = $link_dotfiles
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100168 s.args = synced_folder_tools_config
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100169 end
170
171 config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass
172 config.vm.provision "generate_key", type: :shell, privileged: false, inline: $generate_key, args: operator_key
173
174 ips = ""
175 cluster.each { |node| ips << node[:ip] << " " }
176 config.vm.provision "deploy_key", type: :shell do |s|
177 s.privileged = false
178 s.inline = $deploy_key
179 s.args = [operator_key, vagrant_user, ips]
180 s.env = {'PASSWORD': vagrant_password}
181 end
182
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100183 config.vm.provision "get_rke", type: :shell, path: "tools/get_rke.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100184 config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s|
185 s.privileged = false
186 s.inline = $link_file
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100187 s.args = [synced_folder_config, cluster_yml, "$HOME"]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100188 end
189
190 config.vm.post_up_message = operation_post_msg
191 config.vm.provision "rke_up", type: :shell, run: "never", privileged: false, inline: $rke_up
192 config.trigger.before :destroy do |trigger|
193 trigger.warn = "Removing cluster"
194 trigger.run_remote = {privileged: false, inline: $rke_down}
195 end
196
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100197 config.vm.provision "get_kubectl", type: :shell, path: "tools/get_kubectl.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100198 config.vm.provision "setup_kubectl", type: :shell, run: "never" do |s|
199 s.privileged = false
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100200 s.path = "tools/setup_kubectl.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100201 end
Pawel Wieczorekf1176da2019-12-05 13:45:45 +0100202 config.vm.provision "get_helm", type: :shell, path: "tools/get_helm.sh"
203 config.vm.provision "get_oom", type: :shell, privileged: false, inline: $get_oom
204 config.vm.provision "get_helm_plugins", type: :shell, privileged: false, inline: $get_helm_plugins
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100205 end
206 end
207 end
208end