Use Vagrantfile to build virtual site
[icn.git] / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
3
4 # IMPORTANT To bring up the machines, use the "--no-parallel" option
5 # to vagrant up.  This is to workaround dependencies between the jump
6 # machine and the machine pool machines.  Specifically, the pool
7 # machines will fail to come up until the baremetal network (created
8 # by vagrant from the jump machine definition) is up.
9
10 vars = {
11   :site => 'vm',
12   :baremetal_cidr => '192.168.151.0/24',
13   :num_machines => 2
14 }
15
16 $post_up_message = <<MSG
17 ------------------------------------------------------
18
19 To get started with ICN:
20
21   $ vagrant ssh jump
22   vagrant@jump:~$ sudo su
23   root@jump:/home/vagrant# cd /icn
24   root@jump:/home/vagrant# make install
25
26 ------------------------------------------------------
27 MSG
28
29 #
30 # Networks
31 #
32 # The ICN baremetal network will be the vagrant management network.
33 # It is created automatically by vagrant.  The provisioning network
34 # will be a vagrant private network, and is required to be created by
35 # this script.  The IPMI network is created with virtualbmc.
36
37 #
38 # Machines
39 #
40 Vagrant.configure("2") do |config|
41   # The jump machine
42   config.vm.define 'jump' do |m|
43     # Note the apparent typo in the name below, it is correct as-is
44     m.vm.box = 'intergratedcloudnative/ubuntu1804'
45     m.vm.hostname = 'jump'
46     m.vm.synced_folder '.', '/icn'
47     m.vm.provider :libvirt do |libvirt|
48       libvirt.graphics_ip = '0.0.0.0'
49       libvirt.default_prefix = "#{vars[:site]}-"
50       libvirt.cpu_mode = 'host-passthrough'
51       libvirt.cpus = 8
52       libvirt.memory = 16384
53       libvirt.nested = true
54
55       # The ICN baremetal network is the vagrant management network,
56       # and is created by vagrant for us
57       libvirt.management_network_name = "#{vars[:site]}-baremetal"
58       libvirt.management_network_address = vars[:baremetal_cidr]
59       libvirt.management_network_autostart = true
60     end
61
62     # The ICN provisioning network will be a vagrant private network
63     # created upon bringing up the jump machine
64     m.trigger.before [:up] do |trigger|
65       trigger.name = 'Creating provisioning network'
66       trigger.run = {inline: "./tools/vagrant/create_provisioning_network.sh #{vars[:site]}"}
67     end
68     m.trigger.after [:destroy] do |trigger|
69       trigger.name = 'Destroying provisioning network'
70       trigger.run = {inline: "./tools/vagrant/destroy_provisioning_network.sh #{vars[:site]}"}
71     end
72     m.vm.network :private_network,
73                  :libvirt__network_name => "#{vars[:site]}-provisioning",
74                  :type => 'dhcp'
75
76     # IPMI control of machines is provided by vbmc on the host
77     m.trigger.after [:up] do |trigger|
78       trigger.name = 'Starting virtualbmc for IPMI network'
79       trigger.run = {inline: "./tools/vagrant/start_vbmc.sh"}
80     end
81     m.trigger.after [:destroy] do |trigger|
82       trigger.name = 'Stopping virtualbmc for IPMI network'
83       trigger.run = {inline: "./tools/vagrant/stop_vbmc.sh"}
84     end
85
86     m.trigger.after [:up] do |trigger|
87       trigger.name = 'Creating ICN user_config.sh'
88       trigger.run = {inline: "./tools/vagrant/create_user_config.sh"}
89     end
90     m.vm.provision 'Configuring ICN prerequisites', type: 'shell', privileged: true, inline: <<-SHELL
91       ssh-keygen -f "${HOME}/.ssh/id_rsa" -P "" <<<y
92       DEBIAN_FRONTEND=noninteractive apt-get install -y make
93     SHELL
94     m.vm.post_up_message = $post_up_message
95   end
96
97   # The machine pool used by cluster creation
98   (1..vars[:num_machines]).each do |i|
99     config.vm.define "machine-#{i}" do |m|
100       m.vm.hostname = "machine-#{i}"
101       m.vm.provider :libvirt do |libvirt|
102         libvirt.graphics_ip = '0.0.0.0'
103         libvirt.default_prefix = "#{vars[:site]}-"
104         libvirt.cpu_mode = 'host-passthrough'
105         libvirt.cpus = 8
106         libvirt.memory = 16384
107         libvirt.nested = true
108         # The image will be provisioned by ICN so just create an empty
109         # disk for the machine
110         libvirt.storage :file, :size => 50, :type => 'raw', :cache => 'none'
111         # Management attach is false so that vagrant will not interfere
112         # with these machines: the jump server will manage them
113         # completely
114         libvirt.mgmt_attach = false
115       end
116       # The provisioning network must be listed first for PXE boot to
117       # the metal3/ironic provided image
118       m.vm.network :private_network,
119                    :libvirt__network_name => "#{vars[:site]}-provisioning",
120                    :type => 'dhcp'
121       m.vm.network :private_network,
122                    :libvirt__network_name => "#{vars[:site]}-baremetal",
123                    :type => 'dhcp'
124
125       # IPMI control
126       m.trigger.after [:up] do |trigger|
127         trigger.name = 'Adding machine to IPMI network'
128         trigger.run = {inline: "./tools/vagrant/add_machine_to_vbmc.sh #{i} #{vars[:site]} machine-#{i}"}
129       end
130       m.trigger.after [:destroy] do |trigger|
131         trigger.name = 'Removing machine from IPMI network'
132         trigger.run = {inline: "./tools/vagrant/remove_machine_from_vbmc.sh #{i} #{vars[:site]} machine-#{i}"}
133       end
134
135       # Create configuration for ICN provisioning
136       m.trigger.after [:up] do |trigger|
137         if i == vars[:num_machines] then
138           trigger.info = 'Creating nodes.json.sample describing the machines'
139           trigger.run = {inline: "./tools/vagrant/create_nodes_json_sample.sh #{vars[:num_machines]} #{vars[:site]} machine-"}
140         end
141       end
142       m.trigger.after [:up] do |trigger|
143         if i == vars[:num_machines] then
144           trigger.info = 'Creating Provisioning resource describing the cluster'
145           trigger.run = {inline: "./tools/vagrant/create_provisioning_cr.sh #{vars[:num_machines]} #{vars[:site]} machine-"}
146         end
147       end
148     end
149   end
150 end