aboutsummaryrefslogtreecommitdiffstats
path: root/deployment/noheat/infra-openstack/vagrant/Vagrantfile
blob: c3b5d98190d36710c0cfbe933bea6a831c97cceb (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- mode: ruby -*-
# -*- coding: utf-8 -*-

host_folder_ansible = "../ansible"
synced_folder_ansible = "/ansible"
synced_folder_main = "/vagrant"
synced_folder_config = "#{synced_folder_main}/config"
os_config = "#{synced_folder_config}/local.conf"
os_clouds = "#{synced_folder_config}/clouds.yaml"
os_clouds_dir = "${HOME}/.config/openstack"

vm_cpu = 1
vm_cpus = 4
vm_memory = 1 * 1024
vm_memory_os = 8 * 1024
vm_disk = 32
vm_box = "generic/ubuntu1804"

operation = {
  name: 'operator',
  hostname: 'operator',
  ip: '172.17.5.254',
  cpus: vm_cpu,
  memory: vm_memory,
  disk: vm_disk
}
devstack = {
  name: 'devstack',
  hostname: 'devstack',
  ip: '172.17.5.200',
  cpus: vm_cpus,
  memory: vm_memory_os,
  disk: vm_disk
}

all = [] << operation << devstack

operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=run_playbook_create\" to complete infrastructure deployment"

$enable_ipv6 = <<-SCRIPT
  sed -i'' 's/net.ipv6.conf.all.disable_ipv6.*$/net.ipv6.conf.all.disable_ipv6 = 0/' /etc/sysctl.conf
  sysctl -p
SCRIPT

$setup_devstack = <<-SCRIPT
  CONFIG="$1"
  git clone https://opendev.org/openstack/devstack
  cd devstack
  cp "$CONFIG" .
  ./stack.sh
SCRIPT

$setup_py = <<-SCRIPT
  export DEBIAN_FRONTEND=noninteractive
  sudo -E apt-get update
  sudo -E apt-get install -yq python3-distutils

  curl -fsSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  sudo -H python3 get-pip.py
  pip install ansible python-openstackclient
  mkdir -p #{os_clouds_dir}
SCRIPT

$link_file = <<-SCRIPT
  src="$1"
  dst="$2"
  echo "Symlinking ${src} to ${dst}"
  ln -sf "$src" "$dst"
SCRIPT

$run_playbook = <<-SCRIPT
  PLAYBOOK="$1"
  export OS_CLOUD=openstack
  cd #{synced_folder_ansible}
  ansible-playbook "$PLAYBOOK"
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 = machine[:memory]
        v.cpus = machine[:cpus]
      end

      config.vm.provider :libvirt do |v|
        v.memory = machine[:memory]
        v.cpus = machine[:cpus]
        v.machine_virtual_size = machine[:disk] # set at VM creation
      end

      config.vm.network :private_network, ip: machine[:ip]

      if machine[:name] == 'devstack'
        config.vm.network "forwarded_port", guest: 80, host: 8080

        config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"

        config.vm.provision "enable_ipv6", type: :shell, run: "always", inline: $enable_ipv6
        config.vm.provision "setup_devstack", type: :shell, privileged: false, inline: $setup_devstack, args: os_config
      end

      if machine[:name] == 'operator'
        config.vm.synced_folder ".", synced_folder_main, type: "rsync", rsync__exclude: "Vagrantfile"
        config.vm.synced_folder host_folder_ansible, synced_folder_ansible, type: "rsync"

        config.vm.provision "setup_openstacksdk", type: :shell, privileged: false, inline: $setup_py
        config.vm.provision "link_os_clouds", type: :shell, run: "always" do |s|
          s.privileged = false
          s.inline = $link_file
          s.args = [os_clouds, os_clouds_dir]
        end

        config.vm.post_up_message = operation_post_msg
        config.vm.provision "run_playbook_create", type: :shell, run: "never" do |s|
          s.privileged = false
          s.inline = $run_playbook
          s.args = "create.yml"
        end
        config.vm.provision "run_playbook_destroy", type: :shell, run: "never" do |s|
          s.privileged = false
          s.inline = $run_playbook
          s.args = "destroy.yml"
        end
      end
    end
  end
end