aboutsummaryrefslogtreecommitdiffstats
path: root/test/security/k8s/vagrant/dublin/Vagrantfile
blob: 224cd9a1188cce7149b5c850e62e5ad6145d080c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- mode: ruby -*-
# -*- coding: utf-8 -*-

host_ip = "192.168.121.1"
operator_key = "${HOME}/.ssh/onap-key"
vagrant_user = "vagrant"
vagrant_password = "vagrant"

vm_memory = 2 * 1024
vm_cpus = 1
vm_box = "generic/ubuntu1804"

operation = { name: 'operator', hostname: 'operator', ip: '172.17.0.254' }
cluster = [
  { name: 'control', hostname: 'control', ip: '172.17.0.100' },
  { name: 'worker', hostname: 'worker', ip: '172.17.0.101' }
]

all = cluster.dup << operation

$replace_dns = <<-SCRIPT
  HOST_IP="$1"
  rm -f /etc/resolv.conf # drop its dynamic management by systemd-resolved
  echo nameserver "$HOST_IP" | tee /etc/resolv.conf
SCRIPT

$add_to_docker_group = <<-SCRIPT
  USER="$1"
  echo "Adding ${USER} to 'docker' group"
  usermod -aG docker "$USER"
SCRIPT

$install_sshpass = <<-SCRIPT
  apt-get update
  echo "Installing 'sshpass'"
  apt-get install sshpass
SCRIPT

$generate_key = <<-SCRIPT
  KEY_FILE="$1"
  echo "Generating SSH key (${KEY_FILE})"
  ssh-keygen -q -b 4096 -t rsa -f "$KEY_FILE" -N ""
SCRIPT

$deploy_key = <<-SCRIPT
  KEY="$1"
  USER="$2"
  PASS="$PASSWORD"
  IPS="$3"
  echo "Deploying ${KEY} for ${USER}"
  for ip in $IPS; do
    echo "on ${ip}"
    sshpass -p "$PASS" ssh-copy-id -o StrictHostKeyChecking=no -i "$KEY" "${USER}@${ip}"
  done
SCRIPT

$link_dotfiles = <<-SCRIPT
  for rc in /vagrant/dot_*; do
    src="$rc"
    dst="${HOME}/.${rc##*dot_}"
    echo "Symlinking ${src} to ${dst}"
    ln -sf "$src" "$dst"
  done
SCRIPT

Vagrant.configure('2') do |config|
  all.each do |machine|
    config.vm.define machine[:name] do |config|
      config.vm.box = vm_box
      config.vm.hostname = machine[:hostname]

      config.vm.provider :virtualbox do |v|
        v.name = machine[:name]
        v.memory = vm_memory
        v.cpus = vm_cpus
      end

      config.vm.provider :libvirt do |v|
        v.memory = vm_memory
        v.cpus = vm_cpus
      end

      config.vm.network :private_network, ip: machine[:ip]
      config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip

      if machine[:name] == 'control'
        config.vm.provision "customize_control", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-controlnode.sh"
        config.vm.provision "fix_groups_control", type: :shell, inline: $add_to_docker_group, args: vagrant_user
      end

      if machine[:name] == 'worker'
        config.vm.provision "customize_worker", type: :shell, path: "../../tools/dublin/imported/openstack-k8s-workernode.sh"
        config.vm.provision "fix_group_worker", type: :shell, inline: $add_to_docker_group, args: vagrant_user
      end

      if machine[:name] == 'operator'
        config.vm.synced_folder "../../tools/config", "/vagrant", type: "rsync"

        config.vm.provision "link_dotfiles_root", type: :shell, run: "always", inline: $link_dotfiles
        config.vm.provision "link_dotfiles_user", type: :shell, run: "always", privileged: false, inline: $link_dotfiles

        config.vm.provision "get_rke", type: :shell, path: "../../tools/dublin/get_rke.sh"

        config.vm.provision "install_sshpass", type: :shell, inline: $install_sshpass
        config.vm.provision "generate_key", type: :shell, privileged: false, inline: $generate_key, args: operator_key

        ips = ""
        cluster.each { |node| ips << node[:ip] << " " }
        config.vm.provision "deploy_key", type: :shell do |s|
          s.privileged = false
          s.inline = $deploy_key
          s.args = [operator_key, vagrant_user, ips]
          s.env = {'PASSWORD': vagrant_password}
        end
      end
    end
  end
end