blob: 8870580c3997c52243624a7289b8379988f4328a [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 Wieczorek6b1f78b2019-07-26 14:04:39 +020024$replace_dns = <<-SCRIPT
25 HOST_IP="$1"
26 rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved
27 echo nameserver "$HOST_IP" | tee /etc/resolv.conf
28SCRIPT
29
Pawel Wieczorek5ee92062019-07-26 13:58:02 +020030$add_to_docker_group = <<-SCRIPT
31 USER="$1"
Pawel Wieczorek435200f2019-07-26 14:14:19 +020032 echo "Adding ${USER} to 'docker' group"
Pawel Wieczorek5ee92062019-07-26 13:58:02 +020033 usermod -aG docker "$USER"
34SCRIPT
35
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020036$install_sshpass = <<-SCRIPT
37 apt-get update
Pawel Wieczorek435200f2019-07-26 14:14:19 +020038 echo "Installing 'sshpass'"
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020039 apt-get install sshpass
40SCRIPT
41
42$generate_key = <<-SCRIPT
43 KEY_FILE="$1"
Pawel Wieczorek435200f2019-07-26 14:14:19 +020044 echo "Generating SSH key (${KEY_FILE})"
Pawel Wieczorek6b1f78b2019-07-26 14:04:39 +020045 ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N ""
46SCRIPT
47
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +020048$deploy_key = <<-SCRIPT
49 KEY="$1"
50 USER="$2"
51 PASS="$PASSWORD"
52 IPS="$3"
Pawel Wieczorek435200f2019-07-26 14:14:19 +020053 echo "Deploying ${KEY} for ${USER}"
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +020054 for ip in $IPS; do
Pawel Wieczorek435200f2019-07-26 14:14:19 +020055 echo "on ${ip}"
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +020056 sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}"
57 done
58SCRIPT
59
Pawel Wieczorek34e59322019-07-26 13:29:50 +020060$link_dotfiles = <<-SCRIPT
Pawel Wieczorekeb166a52019-07-26 14:38:54 +020061 SYNC_DIR="$1"
62 for rc in ${SYNC_DIR}/dot_*; do
Pawel Wieczorek435200f2019-07-26 14:14:19 +020063 src="$rc"
64 dst="${HOME}/.${rc##*dot_}"
65 echo "Symlinking ${src} to ${dst}"
66 ln -sf "$src" "$dst"
Pawel Wieczorek34e59322019-07-26 13:29:50 +020067 done
68SCRIPT
69
Pawel Wieczorek3664e042019-07-26 15:53:26 +020070$link_cluster_yml = <<-SCRIPT
71 SYNC_DIR="$1"
72 CLUSTER_YML="$2"
73 src="${SYNC_DIR}/${CLUSTER_YML}"
74 dst="$HOME"
75 echo "Symlinking ${src} to ${dst}"
76 ln -sf "$src" "$dst"
77SCRIPT
78
79$rke_up = "rke up"
80
Pawel Wieczorekb7f08112019-07-19 19:10:07 +020081Vagrant.configure('2') do |config|
82 all.each do |machine|
83 config.vm.define machine[:name] do |config|
84 config.vm.box = vm_box
85 config.vm.hostname = machine[:hostname]
86
87 config.vm.provider :virtualbox do |v|
88 v.name = machine[:name]
89 v.memory = vm_memory
90 v.cpus = vm_cpus
91 end
92
93 config.vm.provider :libvirt do |v|
94 v.memory = vm_memory
95 v.cpus = vm_cpus
96 end
97
98 config.vm.network :private_network, ip: machine[:ip]
Pawel Wieczorek63640ac2019-07-26 14:22:03 +020099 config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200100
101 if machine[:name] == 'control'
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200102 config.vm.provision "customize_control", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-controlnode.sh"
103 config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200104 end
105
106 if machine[:name] == 'worker'
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200107 config.vm.provision "customize_worker", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-workernode.sh"
108 config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200109 end
110
111 if machine[:name] == 'operator'
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200112 config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
113 config.vm.synced_folder "../../tools/config", synced_folder_config, type: "rsync"
Pawel Wieczorek34e59322019-07-26 13:29:50 +0200114
Pawel Wieczorekeb166a52019-07-26 14:38:54 +0200115 config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s|
116 s.inline = $link_dotfiles
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200117 s.args = synced_folder_config
Pawel Wieczorekeb166a52019-07-26 14:38:54 +0200118 end
119 config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s|
120 s.privileged = false
121 s.inline = $link_dotfiles
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200122 s.args = synced_folder_config
Pawel Wieczorekeb166a52019-07-26 14:38:54 +0200123 end
Pawel Wieczorek34e59322019-07-26 13:29:50 +0200124
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200125 config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass
126 config.vm.provision "generate_key", type: :shell, privileged: false, inline: $generate_key, args: operator_key
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200127
128 ips = ""
129 cluster.each { |node| ips << node[:ip] << " " }
Pawel Wieczorek63640ac2019-07-26 14:22:03 +0200130 config.vm.provision "deploy_key", type: :shell do |s|
Pawel Wieczorek9cf6f592019-07-26 13:40:23 +0200131 s.privileged = false
132 s.inline = $deploy_key
133 s.args = [operator_key, vagrant_user, ips]
134 s.env = {'PASSWORD': vagrant_password}
135 end
Pawel Wieczorek3664e042019-07-26 15:53:26 +0200136
137 config.vm.provision "get_rke", type: :shell, path: "../../tools/dublin/get_rke.sh"
138 config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s|
139 s.privileged = false
140 s.inline = $link_cluster_yml
141 s.args = [synced_folder_main, cluster_yml]
142 end
143 config.vm.provision "rke_up", type: :shell, run: "never", privileged: false, inline: $rke_up
Pawel Wieczorekb7f08112019-07-19 19:10:07 +0200144 end
145 end
146 end
147end