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