8bf508b696ae7dc26537b89a98015e876a19b693
[autocluster.git] / vagrant / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: ft=ruby:et:ts=2:sts=2:sw=2
3
4 VAGRANTFILE_API_VERSION = "2"
5
6 require 'yaml'
7 require 'ipaddr'
8 require 'resolv'
9 require 'securerandom'
10
11 f = ENV['AUTOCLUSTER_STATE'] + '/config.yml'
12 if File.exists?(f)
13   settings = YAML::load_file f
14
15   puts "Loaded config from #{f}."
16 end
17
18 def resolvURL(url)
19   u = URI.parse(url)
20   u.host = Resolv.getaddress(u.host)
21   u.to_s
22 end
23
24 shared_disk_ids = []
25 for i in 1..settings['shared_disks']['count']
26   shared_disk_ids[i] = sprintf('AUTO-%02d-', i) + SecureRandom.uuid[0..7]
27 end
28
29 #
30 # The vagrant machine definitions
31 #
32
33 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
34
35   if ENV['http_proxy'] or ENV['https_proxy']
36     if Vagrant.has_plugin?("vagrant-proxyconf")
37       if ENV['http_proxy']
38         config.proxy.http = resolvURL(ENV['http_proxy'])
39       end
40       if ENV['http_proxys']
41         config.proxy.https = resolvURL(ENV['http_proxys'])
42       end
43       config.proxy.no_proxy = "localhost,127.0.0.1"
44     end
45   end
46
47   if Vagrant.has_plugin?("vagrant-libvirt")
48     config.vm.provider :libvirt do |libvirt|
49       libvirt.storage_pool_name = "autocluster"
50     end
51   end
52
53   settings['nodes'].each do |hostname, node|
54     config.vm.define hostname do |v|
55       v.vm.box = settings['vagrant_box']
56       v.vm.hostname = hostname
57
58       node['ips'].each do |ip|
59         v.vm.network "private_network",
60                      ip: ip,
61                      nm_controlled: "no"
62       end
63
64       if settings['virthost']
65         virthost = settings['virthost']
66         v.vm.provision "shell",
67                        run: "always",
68                        inline: "route add default gw " + virthost + "|| :"
69       end
70
71       # No shared folders - they might require extra software on the
72       # nodes and installation can time out  :-(
73       v.vm.synced_folder ".", "/vagrant", disabled: true
74
75       v.vm.provision "file",
76                      source: "~/.ssh/id_autocluster",
77                      destination: "~/.ssh/id_autocluster"
78       v.vm.provision "file",
79                      source: "~/.ssh/id_autocluster.pub",
80                      destination: "~/.ssh/id_autocluster.pub"
81       v.vm.provision :shell,
82                      privileged: true,
83                      path: "autocluster_ssh_node_setup.sh"
84
85       v.vm.provider :libvirt do |libvirt|
86
87         libvirt.default_prefix = 'autocluster'
88         libvirt.cpus = settings['cpus']
89         #FIXME: causes an error ### libvirt.memory = settings['memory']
90
91         if node['has_shared_storage']
92           for i in 1..settings['shared_disks']['count']
93             libvirt.storage :file,
94                             :serial => shared_disk_ids[i],
95                             :path => sprintf('autocluster_%s_shared%02d.img',
96                                              settings['cluster'], i),
97                             :size => settings['shared_disks'].size,
98                             :allow_existing => true,
99                             :shareable => true,
100                             :type => 'raw'
101           end
102         end
103       end
104
105       # The libvirt provider sometimes configures a private network
106       # but doesn't bring it up.  Check that all desired IPs are
107       # assigned, failing if any are missing.
108       v.vm.provision "shell",
109                      run: "always",
110                      path: "autocluster_check_ips.sh",
111                      args: node['ips']
112     end
113   end
114 end