blob: 1ccc3ef9f41f21db868057b9d3005e9fc16419f5 [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"
10cluster_yml = "cluster.yml"
11apt_prefs_dir = "/etc/apt/apt.conf.d"
12apt_prefs = "95silent-approval"
13
14vm_memory = 2 * 1024
15vm_cpus = 1
16vm_box = "generic/ubuntu1804"
17
18operation = { name: 'operator', hostname: 'operator', ip: '172.17.4.254' }
19cluster = [
20 { name: 'control', hostname: 'control', ip: '172.17.4.100' },
21 { name: 'worker', hostname: 'worker', ip: '172.17.4.101' }
22]
23
24all = cluster.dup << operation
25
26operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=rke_up,setup_kubectl\" to complete cluster creation"
27
28$replace_dns = <<-SCRIPT
29 HOST_IP="$1"
30 rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved
31 echo nameserver "$HOST_IP" | tee /etc/resolv.conf
32SCRIPT
33
34$add_to_docker_group = <<-SCRIPT
35 USER="$1"
36 echo "Adding ${USER} to 'docker' group"
37 usermod -aG docker "$USER"
38SCRIPT
39
40$setup_debconf = <<-SCRIPT
41 echo "Setting debconf frontend to noninteractive"
42 sed -i'.orig' '/^Config:/a Frontend: noninteractive' /etc/debconf.conf
43SCRIPT
44
45$install_sshpass = <<-SCRIPT
46 apt-get update
47 echo "Installing 'sshpass'"
48 apt-get install sshpass
49SCRIPT
50
51$generate_key = <<-SCRIPT
52 KEY_FILE="$1"
53 echo "Generating SSH key (${KEY_FILE})"
54 ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N ""
55SCRIPT
56
57$deploy_key = <<-SCRIPT
58 KEY="$1"
59 USER="$2"
60 PASS="$PASSWORD"
61 IPS="$3"
62 echo "Deploying ${KEY} for ${USER}"
63 for ip in $IPS; do
64 echo "on ${ip}"
65 sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}"
66 done
67SCRIPT
68
69$link_dotfiles = <<-SCRIPT
70 SYNC_DIR="$1"
71 for rc in ${SYNC_DIR}/dot_*; do
72 src="$rc"
73 dst="${HOME}/.${rc##*dot_}"
74 echo "Symlinking ${src} to ${dst}"
75 ln -sf "$src" "$dst"
76 done
77SCRIPT
78
79$link_file = <<-SCRIPT
80 SYNC_DIR="$1"
81 FILE="$2"
82 src="${SYNC_DIR}/${FILE}"
83 dst="$3"
84 echo "Symlinking ${src} to ${dst}"
85 ln -sf "$src" "$dst"
86SCRIPT
87
88$rke_up = "rke up"
89$rke_down = "rke remove --force"
90
91Vagrant.configure('2') do |config|
92 all.each do |machine|
93 config.vm.define machine[:name] do |config|
94 config.vm.box = vm_box
95 config.vm.hostname = machine[:hostname]
96
97 config.vm.provider :virtualbox do |v|
98 v.name = machine[:name]
99 v.memory = vm_memory
100 v.cpus = vm_cpus
101 end
102
103 config.vm.provider :libvirt do |v|
104 v.memory = vm_memory
105 v.cpus = vm_cpus
106 end
107
108 config.vm.network :private_network, ip: machine[:ip]
109 config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip
110
111 if machine[:name] == 'control'
112 config.vm.provision "customize_control", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-controlnode.sh"
113 config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user
114 end
115
116 if machine[:name] == 'worker'
117 config.vm.provision "customize_worker", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-workernode.sh"
118 config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user
119 end
120
121 if machine[:name] == 'operator'
122 config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
123 config.vm.synced_folder "../../tools/config", synced_folder_config, type: "rsync"
124
125 config.vm.provision "setup_debconf", type: :shell, inline: $setup_debconf
126 config.vm.provision "link_apt_prefs", type: :shell, run: "always" do |s|
127 s.inline = $link_file
128 s.args = [synced_folder_config, apt_prefs, apt_prefs_dir]
129 end
130 config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s|
131 s.inline = $link_dotfiles
132 s.args = synced_folder_config
133 end
134 config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s|
135 s.privileged = false
136 s.inline = $link_dotfiles
137 s.args = synced_folder_config
138 end
139
140 config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass
141 config.vm.provision "generate_key", type: :shell, privileged: false, inline: $generate_key, args: operator_key
142
143 ips = ""
144 cluster.each { |node| ips << node[:ip] << " " }
145 config.vm.provision "deploy_key", type: :shell do |s|
146 s.privileged = false
147 s.inline = $deploy_key
148 s.args = [operator_key, vagrant_user, ips]
149 s.env = {'PASSWORD': vagrant_password}
150 end
151
152 config.vm.provision "get_rke", type: :shell, path: "../../tools/dublin/get_rke.sh"
153 config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s|
154 s.privileged = false
155 s.inline = $link_file
156 s.args = [synced_folder_main, cluster_yml, "$HOME"]
157 end
158
159 config.vm.post_up_message = operation_post_msg
160 config.vm.provision "rke_up", type: :shell, run: "never", privileged: false, inline: $rke_up
161 config.trigger.before :destroy do |trigger|
162 trigger.warn = "Removing cluster"
163 trigger.run_remote = {privileged: false, inline: $rke_down}
164 end
165
166 config.vm.provision "get_kubectl", type: :shell, path: "../../tools/dublin/get_kubectl.sh"
167 config.vm.provision "setup_kubectl", type: :shell, run: "never" do |s|
168 s.privileged = false
169 s.path = "../../tools/dublin/setup_kubectl.sh"
170 end
171 end
172 end
173 end
174end