blob: 3685df25f9da7ec2949ceeb74df4b230653c5273 [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 Wieczorek216bd6a2019-11-29 15:15:51 +010011cluster_yml = "cluster.yml"
12apt_prefs_dir = "/etc/apt/apt.conf.d"
13apt_prefs = "95silent-approval"
14
15vm_memory = 2 * 1024
16vm_cpus = 1
17vm_box = "generic/ubuntu1804"
18
19operation = { name: 'operator', hostname: 'operator', ip: '172.17.4.254' }
20cluster = [
21 { name: 'control', hostname: 'control', ip: '172.17.4.100' },
22 { name: 'worker', hostname: 'worker', ip: '172.17.4.101' }
23]
24
25all = cluster.dup << operation
26
27operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=rke_up,setup_kubectl\" to complete cluster creation"
28
29$replace_dns = <<-SCRIPT
30 HOST_IP="$1"
31 rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved
32 echo nameserver "$HOST_IP" | tee /etc/resolv.conf
33SCRIPT
34
35$add_to_docker_group = <<-SCRIPT
36 USER="$1"
37 echo "Adding ${USER} to 'docker' group"
38 usermod -aG docker "$USER"
39SCRIPT
40
41$setup_debconf = <<-SCRIPT
42 echo "Setting debconf frontend to noninteractive"
43 sed -i'.orig' '/^Config:/a Frontend: noninteractive' /etc/debconf.conf
44SCRIPT
45
46$install_sshpass = <<-SCRIPT
47 apt-get update
48 echo "Installing 'sshpass'"
49 apt-get install sshpass
50SCRIPT
51
52$generate_key = <<-SCRIPT
53 KEY_FILE="$1"
54 echo "Generating SSH key (${KEY_FILE})"
55 ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N ""
56SCRIPT
57
58$deploy_key = <<-SCRIPT
59 KEY="$1"
60 USER="$2"
61 PASS="$PASSWORD"
62 IPS="$3"
63 echo "Deploying ${KEY} for ${USER}"
64 for ip in $IPS; do
65 echo "on ${ip}"
66 sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}"
67 done
68SCRIPT
69
70$link_dotfiles = <<-SCRIPT
71 SYNC_DIR="$1"
72 for rc in ${SYNC_DIR}/dot_*; do
73 src="$rc"
74 dst="${HOME}/.${rc##*dot_}"
75 echo "Symlinking ${src} to ${dst}"
76 ln -sf "$src" "$dst"
77 done
78SCRIPT
79
80$link_file = <<-SCRIPT
81 SYNC_DIR="$1"
82 FILE="$2"
83 src="${SYNC_DIR}/${FILE}"
84 dst="$3"
85 echo "Symlinking ${src} to ${dst}"
86 ln -sf "$src" "$dst"
87SCRIPT
88
89$rke_up = "rke up"
90$rke_down = "rke remove --force"
91
92Vagrant.configure('2') do |config|
93 all.each do |machine|
94 config.vm.define machine[:name] do |config|
95 config.vm.box = vm_box
96 config.vm.hostname = machine[:hostname]
97
98 config.vm.provider :virtualbox do |v|
99 v.name = machine[:name]
100 v.memory = vm_memory
101 v.cpus = vm_cpus
102 end
103
104 config.vm.provider :libvirt do |v|
105 v.memory = vm_memory
106 v.cpus = vm_cpus
107 end
108
109 config.vm.network :private_network, ip: machine[:ip]
110 config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip
111
112 if machine[:name] == 'control'
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100113 config.vm.provision "customize_control", type: :shell, path: "tools/imported/openstack-k8s-controlnode.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100114 config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user
115 end
116
117 if machine[:name] == 'worker'
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100118 config.vm.provision "customize_worker", type: :shell, path: "tools/imported/openstack-k8s-workernode.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100119 config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user
120 end
121
122 if machine[:name] == 'operator'
123 config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100124
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
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100128 s.args = [synced_folder_tools_config, apt_prefs, apt_prefs_dir]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100129 end
130 config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s|
131 s.inline = $link_dotfiles
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100132 s.args = synced_folder_tools_config
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100133 end
134 config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s|
135 s.privileged = false
136 s.inline = $link_dotfiles
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100137 s.args = synced_folder_tools_config
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100138 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
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100152 config.vm.provision "get_rke", type: :shell, path: "tools/get_rke.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100153 config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s|
154 s.privileged = false
155 s.inline = $link_file
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100156 s.args = [synced_folder_config, cluster_yml, "$HOME"]
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100157 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
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100166 config.vm.provision "get_kubectl", type: :shell, path: "tools/get_kubectl.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100167 config.vm.provision "setup_kubectl", type: :shell, run: "never" do |s|
168 s.privileged = false
Pawel Wieczorek55c0c9d2019-11-29 15:23:43 +0100169 s.path = "tools/setup_kubectl.sh"
Pawel Wieczorek216bd6a2019-11-29 15:15:51 +0100170 end
171 end
172 end
173 end
174end