blob: ef4d42b14a1b6f757fde4cd38330f5eee577cf6b [file] [log] [blame]
Pawel Wieczorekb7f08112019-07-19 19:10:07 +02001# -*- mode: ruby -*-
2# -*- coding: utf-8 -*-
3
4host_ip = "192.168.121.1"
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +02005operator_key = "${HOME}/.ssh/onap-key"
6vagrant_user = "vagrant"
7vagrant_password = "vagrant"
Pawel Wieczorek3664e042019-07-26 15:53:26 +02008synced_folder_main = "/vagrant"
9synced_folder_config = "#{synced_folder_main}/config"
10cluster_yml = "cluster.yml"
Pawel Wieczorekb7f08112019-07-19 19:10:07 +020011
12vm_memory = 2 * 1024
13vm_cpus = 1
14vm_box = "generic/ubuntu1804"
15
16operation = { name: 'operator', hostname: 'operator', ip: '172.17.0.254' }
17cluster = [
18 { name: 'control', hostname: 'control', ip: '172.17.0.100' },
19 { name: 'worker', hostname: 'worker', ip: '172.17.0.101' }
20]
21
22all = cluster.dup << operation
23
Pawel Wieczorek64092cb2019-07-30 16:27:45 +020024operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=rke_up\" to complete cluster creation"
25
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020026$replace_dns = <<-SCRIPT
27 HOST_IP="$1"
28 rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved
29 echo nameserver "$HOST_IP" | tee /etc/resolv.conf
30SCRIPT
31
Pawel Wieczorek5ee92062019-07-26 13:58:02 +020032$add_to_docker_group = <<-SCRIPT
33 USER="$1"
Pawel Wieczorek435200f2019-07-26 14:14:19 +020034 echo "Adding ${USER} to 'docker' group"
Pawel Wieczorek5ee92062019-07-26 13:58:02 +020035 usermod -aG docker "$USER"
36SCRIPT
37
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020038$install_sshpass = <<-SCRIPT
39 apt-get update
Pawel Wieczorek435200f2019-07-26 14:14:19 +020040 echo "Installing 'sshpass'"
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020041 apt-get install sshpass
42SCRIPT
43
44$generate_key = <<-SCRIPT
45 KEY_FILE="$1"
Pawel Wieczorek435200f2019-07-26 14:14:19 +020046 echo "Generating SSH key (${KEY_FILE})"
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020047 ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N ""
48SCRIPT
49
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +020050$deploy_key = <<-SCRIPT
51 KEY="$1"
52 USER="$2"
53 PASS="$PASSWORD"
54 IPS="$3"
Pawel Wieczorek435200f2019-07-26 14:14:19 +020055 echo "Deploying ${KEY} for ${USER}"
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +020056 for ip in $IPS; do
Pawel Wieczorek435200f2019-07-26 14:14:19 +020057 echo "on ${ip}"
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +020058 sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}"
59 done
60SCRIPT
61
Pawel Wieczorek34e59322019-07-26 13:29:50 +020062$link_dotfiles = <<-SCRIPT
Pawel Wieczorekeb166a52019-07-26 14:38:54 +020063 SYNC_DIR="$1"
64 for rc in ${SYNC_DIR}/dot_*; do
Pawel Wieczorek435200f2019-07-26 14:14:19 +020065 src="$rc"
66 dst="${HOME}/.${rc##*dot_}"
67 echo "Symlinking ${src} to ${dst}"
68 ln -sf "$src" "$dst"
Pawel Wieczorek34e59322019-07-26 13:29:50 +020069 done
70SCRIPT
71
Pawel Wieczorek3664e042019-07-26 15:53:26 +020072$link_cluster_yml = <<-SCRIPT
73 SYNC_DIR="$1"
74 CLUSTER_YML="$2"
75 src="${SYNC_DIR}/${CLUSTER_YML}"
76 dst="$HOME"
77 echo "Symlinking ${src} to ${dst}"
78 ln -sf "$src" "$dst"
79SCRIPT
80
81$rke_up = "rke up"
82
Pawel Wieczorekb7f08112019-07-19 19:10:07 +020083Vagrant.configure('2') do |config|
84 all.each do |machine|
85 config.vm.define machine[:name] do |config|
86 config.vm.box = vm_box
87 config.vm.hostname = machine[:hostname]
88
89 config.vm.provider :virtualbox do |v|
90 v.name = machine[:name]
91 v.memory = vm_memory
92 v.cpus = vm_cpus
93 end
94
95 config.vm.provider :libvirt do |v|
96 v.memory = vm_memory
97 v.cpus = vm_cpus
98 end
99
100 config.vm.network :private_network, ip: machine[:ip]
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200101 config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200102
103 if machine[:name] == 'control'
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200104 config.vm.provision "customize_control", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-controlnode.sh"
105 config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200106 end
107
108 if machine[:name] == 'worker'
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200109 config.vm.provision "customize_worker", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-workernode.sh"
110 config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200111 end
112
113 if machine[:name] == 'operator'
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200114 config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
115 config.vm.synced_folder "../../tools/config", synced_folder_config, type: "rsync"
Pawel Wieczorek34e59322019-07-26 13:29:50 +0200116
Pawel Wieczorekeb166a52019-07-26 14:38:54 +0200117 config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s|
118 s.inline = $link_dotfiles
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200119 s.args = synced_folder_config
Pawel Wieczorekeb166a52019-07-26 14:38:54 +0200120 end
121 config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s|
122 s.privileged = false
123 s.inline = $link_dotfiles
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200124 s.args = synced_folder_config
Pawel Wieczorekeb166a52019-07-26 14:38:54 +0200125 end
Pawel Wieczorek34e59322019-07-26 13:29:50 +0200126
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200127 config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass
128 config.vm.provision "generate_key", type: :shell, privileged: false, inline: $generate_key, args: operator_key
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200129
130 ips = ""
131 cluster.each { |node| ips << node[:ip] << " " }
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200132 config.vm.provision "deploy_key", type: :shell do |s|
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +0200133 s.privileged = false
134 s.inline = $deploy_key
135 s.args = [operator_key, vagrant_user, ips]
136 s.env = {'PASSWORD': vagrant_password}
137 end
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200138
139 config.vm.provision "get_rke", type: :shell, path: "../../tools/dublin/get_rke.sh"
140 config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s|
141 s.privileged = false
142 s.inline = $link_cluster_yml
143 s.args = [synced_folder_main, cluster_yml]
144 end
Pawel Wieczorek64092cb2019-07-30 16:27:45 +0200145
146 config.vm.post_up_message = operation_post_msg
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200147 config.vm.provision "rke_up", type: :shell, run: "never", privileged: false, inline: $rke_up
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200148 end
149 end
150 end
151end