Update jump server to Ubuntu 20.04
[icn.git] / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
3 require 'ipaddr'
4 require 'uri'
5 require 'yaml'
6
7 # IMPORTANT To bring up the machines, use the "--no-parallel" option
8 # to vagrant up.  This is to workaround dependencies between the jump
9 # machine and the machine pool machines.  Specifically, the pool
10 # machines will fail to come up until the baremetal network (created
11 # by vagrant from the jump machine definition) is up.
12
13 site = ENV['ICN_SITE'] || 'vm'
14
15 # Calculate the baremetal network address from the bmcAddress (aka
16 # IPMI address) specified in the machine pool values.  IPMI in the
17 # virtual environment is emulated by virtualbmc listening on the host.
18 baremetal_cidr = nil
19 Dir.glob("deploy/site/#{site}/*.yaml") do |file|
20   YAML.load_stream(File.read(file)) do |document|
21     values = document.fetch('spec', {}).fetch('values', {})
22     next if values['machineName'].nil? || values['bootMACAddress'].nil?
23     bmc_host = URI.parse(values['bmcAddress']).host
24     baremetal_cidr = "#{IPAddr.new(bmc_host).mask(24)}/24"
25   end
26 end
27 if baremetal_cidr.nil?
28   puts "Missing bmcAddress value in site definition, can't determine baremetal network address"
29   exit 1
30 end
31
32 $post_up_message = <<MSG
33 ------------------------------------------------------
34
35 To get started with ICN:
36
37   $ vagrant ssh jump
38   vagrant@jump:~$ sudo su
39   root@jump:/home/vagrant# cd /icn
40   root@jump:/icn# make jump_server
41   root@jump:/icn# make vm_cluster
42
43 ------------------------------------------------------
44 MSG
45
46 #
47 # Networks
48 #
49 # The ICN baremetal network will be the vagrant management network.
50 # It is created automatically by vagrant.  The provisioning network
51 # will be a vagrant private network, and is required to be created by
52 # this script.  The IPMI network is created with virtualbmc.
53
54 #
55 # Machines
56 #
57 Vagrant.configure("2") do |config|
58   # The jump machine
59   config.vm.define 'jump' do |m|
60     # Note the apparent typo in the name below, it is correct as-is
61     m.vm.box = 'intergratedcloudnative/ubuntu2004'
62     m.vm.hostname = 'jump'
63     m.vm.synced_folder '.', '/icn'
64     m.vm.provider :libvirt do |libvirt|
65       libvirt.graphics_ip = '0.0.0.0'
66       libvirt.default_prefix = "#{site}-"
67       libvirt.cpu_mode = 'host-passthrough'
68       libvirt.cpus = 8
69       libvirt.memory = 24576
70       libvirt.nested = true
71
72       # The ICN baremetal network is the vagrant management network,
73       # and is created by vagrant for us
74       libvirt.management_network_name = "#{site}-baremetal"
75       libvirt.management_network_address = baremetal_cidr
76       libvirt.management_network_autostart = true
77     end
78
79     # The ICN provisioning network will be a vagrant private network
80     # created upon bringing up the jump machine
81     m.trigger.before [:up] do |trigger|
82       trigger.name = 'Creating provisioning network'
83       trigger.run = {inline: "./tools/vagrant/create_provisioning_network.sh #{site}"}
84     end
85     m.trigger.after [:destroy] do |trigger|
86       trigger.name = 'Destroying provisioning network'
87       trigger.run = {inline: "./tools/vagrant/destroy_provisioning_network.sh #{site}"}
88     end
89     m.vm.network :private_network,
90                  :libvirt__network_name => "#{site}-provisioning",
91                  :type => 'dhcp'
92
93     # IPMI control of machines is provided by vbmc on the host
94     m.trigger.after [:up] do |trigger|
95       trigger.name = 'Starting virtualbmc for IPMI network'
96       trigger.run = {inline: "./tools/vagrant/start_vbmc.sh"}
97     end
98     m.trigger.after [:destroy] do |trigger|
99       trigger.name = 'Stopping virtualbmc for IPMI network'
100       trigger.run = {inline: "./tools/vagrant/stop_vbmc.sh"}
101     end
102
103     m.trigger.after [:up] do |trigger|
104       trigger.name = 'Creating ICN user_config.sh'
105       trigger.run = {inline: "./tools/vagrant/create_user_config.sh"}
106     end
107     m.vm.provision 'Configuring ICN prerequisites', type: 'shell', privileged: true, inline: <<-SHELL
108       ssh-keygen -f "${HOME}/.ssh/id_rsa" -P "" <<<y
109       DEBIAN_FRONTEND=noninteractive apt-get install -y make
110     SHELL
111     m.vm.post_up_message = $post_up_message
112   end
113
114   # Look for any HelmReleases in the site directory with machineName in
115   # the values dictionary.  This will provide the values needed to
116   # create the machine pool.
117   legacy_machine_args = ""
118   Dir.glob("deploy/site/#{site}/*.yaml") do |file|
119     YAML.load_stream(File.read(file)) do |document|
120       values = document.fetch('spec', {}).fetch('values', {})
121       next if values['machineName'].nil? || values['bootMACAddress'].nil?
122       machine_name = values['machineName']
123       boot_mac_address = values['bootMACAddress']
124       bmc_port = URI.parse(values['bmcAddress']).port
125       config.vm.define machine_name do |m|
126         m.vm.hostname = machine_name
127         m.vm.provider :libvirt do |libvirt|
128           libvirt.graphics_ip = '0.0.0.0'
129           libvirt.default_prefix = "#{site}-"
130           libvirt.cpu_mode = 'host-passthrough'
131           libvirt.cpus = 8
132           libvirt.memory = 16384
133           libvirt.nested = true
134           # The image will be provisioned by ICN so just create an empty
135           # disk for the machine
136           libvirt.storage :file, :size => 50, :type => 'raw', :cache => 'none'
137           # Management attach is false so that vagrant will not interfere
138           # with these machines: the jump server will manage them
139           # completely
140           libvirt.mgmt_attach = false
141         end
142         # The provisioning network must be listed first for PXE boot to
143         # the metal3/ironic provided image
144         m.vm.network :private_network,
145                      :libvirt__network_name => "#{site}-provisioning",
146                      :mac => boot_mac_address,
147                      :type => 'dhcp'
148         m.vm.network :private_network,
149                      :libvirt__network_name => "#{site}-baremetal",
150                      :type => 'dhcp'
151
152         # IPMI control
153         m.trigger.after [:up] do |trigger|
154           trigger.name = 'Adding machine to IPMI network'
155           trigger.run = {inline: "./tools/vagrant/add_machine_to_vbmc.sh #{site} #{machine_name} #{bmc_port}"}
156         end
157         m.trigger.after [:destroy] do |trigger|
158           trigger.name = 'Removing machine from IPMI network'
159           trigger.run = {inline: "./tools/vagrant/remove_machine_from_vbmc.sh #{site} #{machine_name} #{bmc_port}"}
160         end
161
162         # Create configuration for ICN provisioning
163         legacy_machine_args = "#{legacy_machine_args} #{machine_name} #{bmc_port}"
164         m.trigger.after [:up] do |trigger|
165           trigger.info = 'Creating nodes.json.sample describing the machines'
166           trigger.run = {inline: "./tools/vagrant/create_nodes_json_sample.sh #{site} #{legacy_machine_args}"}
167         end
168         m.trigger.after [:up] do |trigger|
169           trigger.info = 'Creating Provisioning resource describing the cluster'
170           trigger.run = {inline: "./tools/vagrant/create_provisioning_cr.sh #{site} #{legacy_machine_args}"}
171         end
172       end
173     end
174   end
175 end