aboutsummaryrefslogtreecommitdiffstats
path: root/deployment/noheat/infra-openstack/vagrant/Vagrantfile
blob: ed1a3d076d339749a59e49588487a1c88c3e4639 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- 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_template = "#{synced_folder_config}/clouds.yaml"
os_clouds_dir = "${HOME}/.config/openstack"
os_clouds_config = "#{os_clouds_dir}/clouds.yaml"
os_admin = "admin"
os_user = "demo"
image_url = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img"
image_name = "Ubuntu_20.04"

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

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

all = [] << operation << devstack

operation_post_msg = "Run: \"vagrant provision #{operation[:name]} --provision-with=add_os_image,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
  apt-get update
  apt-get install -yq python3-distutils

  curl -fsSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  python3 get-pip.py
SCRIPT

$setup_openstackclient = <<-SCRIPT
  pip install --ignore-installed python-openstackclient
  mkdir -p #{os_clouds_dir}
SCRIPT

$setup_openstacksdk = <<-SCRIPT
  pip install ansible openstacksdk
  mkdir -p #{os_clouds_dir}
SCRIPT

$create_os_clouds = <<-SCRIPT
  user="$1"
  template="$2"
  config="$3"
  OS_USERNAME="$user" envsubst < "$template" > "$config"
SCRIPT

$add_os_image = <<-SCRIPT
  url="$1"
  name="$2"
  image="/root/${name}.img"
  wget --quiet --continue --output-document="$image" "$url"
  export OS_CLOUD=openstack
  openstack image create "$name" --public --disk-format qcow2 --container-format bare --file "$image"
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]
      config.vm.network :private_network, ip: machine[:ip_os]

      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_py", type: :shell, inline: $setup_py
        config.vm.provision "setup_openstackclient", type: :shell, inline: $setup_openstackclient
        config.vm.provision "create_os_clouds_admin", type: :shell, run: "always" do |s|
          s.inline = $create_os_clouds
          s.args = [os_admin, os_clouds_template, os_clouds_config]
        end
        config.vm.provision "setup_openstacksdk", type: :shell, privileged: false, inline: $setup_openstacksdk
        config.vm.provision "create_os_clouds", type: :shell, run: "always" do |s|
          s.privileged = false
          s.inline = $create_os_clouds
          s.args = [os_user, os_clouds_template, os_clouds_config]
        end

        config.vm.post_up_message = operation_post_msg
        config.vm.provision "add_os_image", type: :shell, run: "never" do |s|
          s.inline = $add_os_image
          s.args = [image_url, image_name]
        end
        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