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 | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 8 | |
| 9 | vm_memory = 2 * 1024 |
| 10 | vm_cpus = 1 |
| 11 | vm_box = "generic/ubuntu1804" |
| 12 | |
| 13 | operation = { name: 'operator', hostname: 'operator', ip: '172.17.0.254' } |
| 14 | cluster = [ |
| 15 | { name: 'control', hostname: 'control', ip: '172.17.0.100' }, |
| 16 | { name: 'worker', hostname: 'worker', ip: '172.17.0.101' } |
| 17 | ] |
| 18 | |
| 19 | all = cluster.dup << operation |
| 20 | |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 21 | $replace_dns = <<-SCRIPT |
| 22 | HOST_IP="$1" |
| 23 | rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved |
| 24 | echo nameserver "$HOST_IP" | tee /etc/resolv.conf |
| 25 | SCRIPT |
| 26 | |
Pawel Wieczorek | 5ee9206 | 2019-07-26 13:58:02 +0200 | [diff] [blame] | 27 | $add_to_docker_group = <<-SCRIPT |
| 28 | USER="$1" |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 29 | echo "Adding ${USER} to 'docker' group" |
Pawel Wieczorek | 5ee9206 | 2019-07-26 13:58:02 +0200 | [diff] [blame] | 30 | usermod -aG docker "$USER" |
| 31 | SCRIPT |
| 32 | |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 33 | $install_sshpass = <<-SCRIPT |
| 34 | apt-get update |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 35 | echo "Installing 'sshpass'" |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 36 | apt-get install sshpass |
| 37 | SCRIPT |
| 38 | |
| 39 | $generate_key = <<-SCRIPT |
| 40 | KEY_FILE="$1" |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 41 | echo "Generating SSH key (${KEY_FILE})" |
Pawel Wieczorek | 6b1f78b | 2019-07-26 14:04:39 +0200 | [diff] [blame] | 42 | ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N "" |
| 43 | SCRIPT |
| 44 | |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 45 | $deploy_key = <<-SCRIPT |
| 46 | KEY="$1" |
| 47 | USER="$2" |
| 48 | PASS="$PASSWORD" |
| 49 | IPS="$3" |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 50 | echo "Deploying ${KEY} for ${USER}" |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 51 | for ip in $IPS; do |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 52 | echo "on ${ip}" |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 53 | sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}" |
| 54 | done |
| 55 | SCRIPT |
| 56 | |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 57 | $link_dotfiles = <<-SCRIPT |
| 58 | for rc in /vagrant/dot_*; do |
Pawel Wieczorek | 435200f | 2019-07-26 14:14:19 +0200 | [diff] [blame] | 59 | src="$rc" |
| 60 | dst="${HOME}/.${rc##*dot_}" |
| 61 | echo "Symlinking ${src} to ${dst}" |
| 62 | ln -sf "$src" "$dst" |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 63 | done |
| 64 | SCRIPT |
| 65 | |
Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 66 | Vagrant.configure('2') do |config| |
| 67 | all.each do |machine| |
| 68 | config.vm.define machine[:name] do |config| |
| 69 | config.vm.box = vm_box |
| 70 | config.vm.hostname = machine[:hostname] |
| 71 | |
| 72 | config.vm.provider :virtualbox do |v| |
| 73 | v.name = machine[:name] |
| 74 | v.memory = vm_memory |
| 75 | v.cpus = vm_cpus |
| 76 | end |
| 77 | |
| 78 | config.vm.provider :libvirt do |v| |
| 79 | v.memory = vm_memory |
| 80 | v.cpus = vm_cpus |
| 81 | end |
| 82 | |
| 83 | config.vm.network :private_network, ip: machine[:ip] |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 84 | 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] | 85 | |
| 86 | if machine[:name] == 'control' |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 87 | config.vm.provision "customize_control", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-controlnode.sh" |
| 88 | 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] | 89 | end |
| 90 | |
| 91 | if machine[:name] == 'worker' |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 92 | config.vm.provision "customize_worker", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-workernode.sh" |
| 93 | 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] | 94 | end |
| 95 | |
| 96 | if machine[:name] == 'operator' |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 97 | config.vm.synced_folder "../../tools/config", "/vagrant", type: "rsync" |
| 98 | |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 99 | config.vm.provision "link_dotfiles_root", type: :shell, run: "always", inline: $link_dotfiles |
| 100 | config.vm.provision "link_dotfiles_user", type: :shell, run: "always", privileged: false, inline: $link_dotfiles |
Pawel Wieczorek | 34e5932 | 2019-07-26 13:29:50 +0200 | [diff] [blame] | 101 | |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 102 | config.vm.provision "get_rke", type: :shell, path: "../../tools/dublin/get_rke.sh" |
Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 103 | |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 104 | config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass |
| 105 | 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] | 106 | |
| 107 | ips = "" |
| 108 | cluster.each { |node| ips << node[:ip] << " " } |
Pawel Wieczorek | 63640ac | 2019-07-26 14:22:03 +0200 | [diff] [blame^] | 109 | config.vm.provision "deploy_key", type: :shell do |s| |
Pawel Wieczorek | 9cf6f59 | 2019-07-26 13:40:23 +0200 | [diff] [blame] | 110 | s.privileged = false |
| 111 | s.inline = $deploy_key |
| 112 | s.args = [operator_key, vagrant_user, ips] |
| 113 | s.env = {'PASSWORD': vagrant_password} |
| 114 | end |
Pawel Wieczorek | b7f0811 | 2019-07-19 19:10:07 +0200 | [diff] [blame] | 115 | end |
| 116 | end |
| 117 | end |
| 118 | end |