Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 1 | # -*- mode: ruby -*- |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | host_ip = "192.168.121.1" |
| 5 | operator_key = "${HOME}/.ssh/onap-key" |
| 6 | vagrant_user = "vagrant" |
| 7 | vagrant_password = "vagrant" |
| 8 | synced_folder_main = "/vagrant" |
| 9 | synced_folder_config = "#{synced_folder_main}/config" |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 10 | synced_folder_tools_config = "#{synced_folder_main}/tools/config" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 11 | cluster_yml = "cluster.yml" |
| 12 | apt_prefs_dir = "/etc/apt/apt.conf.d" |
| 13 | apt_prefs = "95silent-approval" |
| 14 | |
| 15 | vm_memory = 2 * 1024 |
| 16 | vm_cpus = 1 |
| 17 | vm_box = "generic/ubuntu1804" |
| 18 | |
| 19 | operation = { name: 'operator', hostname: 'operator', ip: '172.17.4.254' } |
| 20 | cluster = [ |
| 21 | { name: 'control', hostname: 'control', ip: '172.17.4.100' }, |
| 22 | { name: 'worker', hostname: 'worker', ip: '172.17.4.101' } |
| 23 | ] |
| 24 | |
| 25 | all = cluster.dup << operation |
| 26 | |
| 27 | operation_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 |
| 33 | SCRIPT |
| 34 | |
| 35 | $add_to_docker_group = <<-SCRIPT |
| 36 | USER="$1" |
| 37 | echo "Adding ${USER} to 'docker' group" |
| 38 | usermod -aG docker "$USER" |
| 39 | SCRIPT |
| 40 | |
| 41 | $setup_debconf = <<-SCRIPT |
| 42 | echo "Setting debconf frontend to noninteractive" |
| 43 | sed -i'.orig' '/^Config:/a Frontend: noninteractive' /etc/debconf.conf |
| 44 | SCRIPT |
| 45 | |
| 46 | $install_sshpass = <<-SCRIPT |
| 47 | apt-get update |
| 48 | echo "Installing 'sshpass'" |
| 49 | apt-get install sshpass |
| 50 | SCRIPT |
| 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 "" |
| 56 | SCRIPT |
| 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 |
| 68 | SCRIPT |
| 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 |
| 78 | SCRIPT |
| 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" |
| 87 | SCRIPT |
| 88 | |
| 89 | $rke_up = "rke up" |
| 90 | $rke_down = "rke remove --force" |
| 91 | |
| 92 | Vagrant.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 Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 113 | config.vm.provision "customize_control", type: :shell, path: "tools/imported/openstack-k8s-controlnode.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 114 | 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 Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 118 | config.vm.provision "customize_worker", type: :shell, path: "tools/imported/openstack-k8s-workernode.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 119 | 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 Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 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 |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 128 | s.args = [synced_folder_tools_config, apt_prefs, apt_prefs_dir] |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 129 | end |
| 130 | config.vm.provision "link_dotfiles_root", type: :shell, run: "always" do |s| |
| 131 | s.inline = $link_dotfiles |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 132 | s.args = synced_folder_tools_config |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 133 | end |
| 134 | config.vm.provision "link_dotfiles_user", type: :shell, run: "always" do |s| |
| 135 | s.privileged = false |
| 136 | s.inline = $link_dotfiles |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 137 | s.args = synced_folder_tools_config |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 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 | |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 152 | config.vm.provision "get_rke", type: :shell, path: "tools/get_rke.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 153 | config.vm.provision "link_cluster_yml", type: :shell, run: "always" do |s| |
| 154 | s.privileged = false |
| 155 | s.inline = $link_file |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 156 | s.args = [synced_folder_config, cluster_yml, "$HOME"] |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 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 | |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 166 | config.vm.provision "get_kubectl", type: :shell, path: "tools/get_kubectl.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 167 | config.vm.provision "setup_kubectl", type: :shell, run: "never" do |s| |
| 168 | s.privileged = false |
Pawel Wieczorek | 55c0c9d | 2019-11-29 15:23:43 +0100 | [diff] [blame^] | 169 | s.path = "tools/setup_kubectl.sh" |
Pawel Wieczorek | 216bd6a | 2019-11-29 15:15:51 +0100 | [diff] [blame] | 170 | end |
| 171 | end |
| 172 | end |
| 173 | end |
| 174 | end |