Merge "Update README.md for CAPI install"
[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/ubuntu1804'
62     m.vm.box_version = '1.0.0'
63     m.vm.hostname = 'jump'
64     m.vm.synced_folder '.', '/icn'
65     m.vm.provider :libvirt do |libvirt|
66       libvirt.graphics_ip = '0.0.0.0'
67       libvirt.default_prefix = "#{site}-"
68       libvirt.cpu_mode = 'host-passthrough'
69       libvirt.cpus = 8
70       libvirt.memory = 24576
71       libvirt.nested = true
72
73       # The ICN baremetal network is the vagrant management network,
74       # and is created by vagrant for us
75       libvirt.management_network_name = "#{site}-baremetal"
76       libvirt.management_network_address = baremetal_cidr
77       libvirt.management_network_autostart = true
78     end
79
80     # The ICN provisioning network will be a vagrant private network
81     # created upon bringing up the jump machine
82     m.trigger.before [:up] do |trigger|
83       trigger.name = 'Creating provisioning network'
84       trigger.run = {inline: "./tools/vagrant/create_provisioning_network.sh #{site}"}
85     end
86     m.trigger.after [:destroy] do |trigger|
87       trigger.name = 'Destroying provisioning network'
88       trigger.run = {inline: "./tools/vagrant/destroy_provisioning_network.sh #{site}"}
89     end
90     m.vm.network :private_network,
91                  :libvirt__network_name => "#{site}-provisioning",
92                  :type => 'dhcp'
93
94     # IPMI control of machines is provided by vbmc on the host
95     m.trigger.after [:up] do |trigger|
96       trigger.name = 'Starting virtualbmc for IPMI network'
97       trigger.run = {inline: "./tools/vagrant/start_vbmc.sh"}
98     end
99     m.trigger.after [:destroy] do |trigger|
100       trigger.name = 'Stopping virtualbmc for IPMI network'
101       trigger.run = {inline: "./tools/vagrant/stop_vbmc.sh"}
102     end
103
104     m.trigger.after [:up] do |trigger|
105       trigger.name = 'Creating ICN user_config.sh'
106       trigger.run = {inline: "./tools/vagrant/create_user_config.sh"}
107     end
108     m.vm.provision 'Configuring ICN prerequisites', type: 'shell', privileged: true, inline: <<-SHELL
109       ssh-keygen -f "${HOME}/.ssh/id_rsa" -P "" <<<y
110       DEBIAN_FRONTEND=noninteractive apt-get install -y make
111     SHELL
112     m.vm.post_up_message = $post_up_message
113   end
114
115   # Look for any HelmReleases in the site directory with machineName in
116   # the values dictionary.  This will provide the values needed to
117   # create the machine pool.
118   legacy_machine_args = ""
119   Dir.glob("deploy/site/#{site}/*.yaml") do |file|
120     YAML.load_stream(File.read(file)) do |document|
121       values = document.fetch('spec', {}).fetch('values', {})
122       next if values['machineName'].nil? || values['bootMACAddress'].nil?
123       machine_name = values['machineName']
124       boot_mac_address = values['bootMACAddress']
125       bmc_port = URI.parse(values['bmcAddress']).port
126       config.vm.define machine_name do |m|
127         m.vm.hostname = machine_name
128         m.vm.provider :libvirt do |libvirt|
129           libvirt.graphics_ip = '0.0.0.0'
130           libvirt.default_prefix = "#{site}-"
131           libvirt.cpu_mode = 'host-passthrough'
132           libvirt.cpus = 8
133           libvirt.memory = 16384
134           libvirt.nested = true
135           # The image will be provisioned by ICN so just create an empty
136           # disk for the machine
137           libvirt.storage :file, :size => 50, :type => 'raw', :cache => 'none'
138           # Management attach is false so that vagrant will not interfere
139           # with these machines: the jump server will manage them
140           # completely
141           libvirt.mgmt_attach = false
142         end
143         # The provisioning network must be listed first for PXE boot to
144         # the metal3/ironic provided image
145         m.vm.network :private_network,
146                      :libvirt__network_name => "#{site}-provisioning",
147                      :mac => boot_mac_address,
148                      :type => 'dhcp'
149         m.vm.network :private_network,
150                      :libvirt__network_name => "#{site}-baremetal",
151                      :type => 'dhcp'
152
153         # IPMI control
154         m.trigger.after [:up] do |trigger|
155           trigger.name = 'Adding machine to IPMI network'
156           trigger.run = {inline: "./tools/vagrant/add_machine_to_vbmc.sh #{site} #{machine_name} #{bmc_port}"}
157         end
158         m.trigger.after [:destroy] do |trigger|
159           trigger.name = 'Removing machine from IPMI network'
160           trigger.run = {inline: "./tools/vagrant/remove_machine_from_vbmc.sh #{site} #{machine_name} #{bmc_port}"}
161         end
162
163         # Create configuration for ICN provisioning
164         legacy_machine_args = "#{legacy_machine_args} #{machine_name} #{bmc_port}"
165         m.trigger.after [:up] do |trigger|
166           trigger.info = 'Creating nodes.json.sample describing the machines'
167           trigger.run = {inline: "./tools/vagrant/create_nodes_json_sample.sh #{site} #{legacy_machine_args}"}
168         end
169         m.trigger.after [:up] do |trigger|
170           trigger.info = 'Creating Provisioning resource describing the cluster'
171           trigger.run = {inline: "./tools/vagrant/create_provisioning_cr.sh #{site} #{legacy_machine_args}"}
172         end
173       end
174     end
175   end
176 end