Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 1 | # -*- mode: ruby -*- |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | host_ip = "192.168.121.1" |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 5 | operator_key = "${HOME}/.ssh/onap-key" |
| 6 | vagrant_user = "vagrant" |
| 7 | vagrant_password = "vagrant" |
Pawel Wieczorek | 3664e04 | 2019-07-26 15:53:26 +0200 | [diff] [blame] | 8 | synced_folder_main = "/vagrant" |
| 9 | synced_folder_config = "#{synced_folder_main}/config" |
| 10 | cluster_yml = "cluster.yml" |
Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 11 | |
| 12 | vm_memory = 2 * 1024 |
| 13 | vm_cpus = 1 |
| 14 | vm_box = "generic/ubuntu1804" |
| 15 | |
| 16 | operation = { name: 'operator', hostname: 'operator', ip: '172.17.0.254' } |
| 17 | cluster = [ |
| 18 | { name: 'control', hostname: 'control', ip: '172.17.0.100' }, |
| 19 | { name: 'worker', hostname: 'worker', ip: '172.17.0.101' } |
| 20 | ] |
| 21 | |
| 22 | all = cluster.dup << operation |
| 23 | |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 24 | $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 |
| 28 | SCRIPT |
| 29 | |
Pawel Wieczorek | 5ee9206 | 2019-07-26 13:58:02 +0200 | [diff] [blame] | 30 | $add_to_docker_group = <<-SCRIPT |
| 31 | USER="$1" |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 32 | echo "Adding ${USER} to 'docker' group" |
Pawel Wieczorek | 5ee9206 | 2019-07-26 13:58:02 +0200 | [diff] [blame] | 33 | usermod -aG docker "$USER" |
| 34 | SCRIPT |
| 35 | |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 36 | $install_sshpass = <<-SCRIPT |
| 37 | apt-get update |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 38 | echo "Installing 'sshpass'" |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 39 | apt-get install sshpass |
| 40 | SCRIPT |
| 41 | |
| 42 | $generate_key = <<-SCRIPT |
| 43 | KEY_FILE="$1" |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 44 | echo "Generating SSH key (${KEY_FILE})" |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 45 | ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N "" |
| 46 | SCRIPT |
| 47 | |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 48 | $deploy_key = <<-SCRIPT |
| 49 | KEY="$1" |
| 50 | USER="$2" |
| 51 | PASS="$PASSWORD" |
| 52 | IPS="$3" |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 53 | echo "Deploying ${KEY} for ${USER}" |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 54 | for ip in $IPS; do |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 55 | echo "on ${ip}" |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 56 | sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}" |
| 57 | done |
| 58 | SCRIPT |
| 59 | |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 60 | $link_dotfiles = <<-SCRIPT |
Pawel Wieczorek | eb166a5 | 2019-07-26 14:38:54 +0200 | [diff] [blame] | 61 | SYNC_DIR="$1" |
| 62 | for rc in ${SYNC_DIR}/dot_*; do |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 63 | src="$rc" |
| 64 | dst="${HOME}/.${rc##*dot_}" |
| 65 | echo "Symlinking ${src} to ${dst}" |
| 66 | ln -sf "$src" "$dst" |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 67 | done |
| 68 | SCRIPT |
| 69 | |
Pawel Wieczorek | 3664e04 | 2019-07-26 15:53:26 +0200 | [diff] [blame] | 70 | $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" |
| 77 | SCRIPT |
| 78 | |
| 79 | $rke_up = "rke up" |
| 80 | |
Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 81 | Vagrant.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 Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame] | 99 | config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip |
Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 100 | |
| 101 | if machine[:name] == 'control' |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame] | 102 | 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 Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 104 | end |
| 105 | |
| 106 | if machine[:name] == 'worker' |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame] | 107 | 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 Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 109 | end |
| 110 | |
| 111 | if machine[:name] == 'operator' |
Pawel Wieczorek | 3664e04 | 2019-07-26 15:53:26 +0200 | [diff] [blame] | 112 | 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 Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 114 | |
Pawel Wieczorek | eb166a5 | 2019-07-26 14:38:54 +0200 | [diff] [blame] | 115 | config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s| |
| 116 | s.inline = $link_dotfiles |
Pawel Wieczorek | 3664e04 | 2019-07-26 15:53:26 +0200 | [diff] [blame] | 117 | s.args = synced_folder_config |
Pawel Wieczorek | eb166a5 | 2019-07-26 14:38:54 +0200 | [diff] [blame] | 118 | end |
| 119 | config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s| |
| 120 | s.privileged = false |
| 121 | s.inline = $link_dotfiles |
Pawel Wieczorek | 3664e04 | 2019-07-26 15:53:26 +0200 | [diff] [blame] | 122 | s.args = synced_folder_config |
Pawel Wieczorek | eb166a5 | 2019-07-26 14:38:54 +0200 | [diff] [blame] | 123 | end |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 124 | |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame] | 125 | 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 Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 127 | |
| 128 | ips = "" |
| 129 | cluster.each { |node| ips << node[:ip] << " " } |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame] | 130 | config.vm.provision "deploy_key", type: :shell do |s| |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 131 | 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 Wieczorek | 3664e04 | 2019-07-26 15:53:26 +0200 | [diff] [blame] | 136 | |
| 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 Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 144 | end |
| 145 | end |
| 146 | end |
| 147 | end |