summaryrefslogtreecommitdiffstats
path: root/vagrant/Vagrantfile
blob: 8bf508b696ae7dc26537b89a98015e876a19b693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- mode: ruby -*-
# vi: ft=ruby:et:ts=2:sts=2:sw=2

VAGRANTFILE_API_VERSION = "2"

require 'yaml'
require 'ipaddr'
require 'resolv'
require 'securerandom'

f = ENV['AUTOCLUSTER_STATE'] + '/config.yml'
if File.exists?(f)
  settings = YAML::load_file f

  puts "Loaded config from #{f}."
end

def resolvURL(url)
  u = URI.parse(url)
  u.host = Resolv.getaddress(u.host)
  u.to_s
end

shared_disk_ids = []
for i in 1..settings['shared_disks']['count']
  shared_disk_ids[i] = sprintf('AUTO-%02d-', i) + SecureRandom.uuid[0..7]
end

#
# The vagrant machine definitions
#

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  if ENV['http_proxy'] or ENV['https_proxy']
    if Vagrant.has_plugin?("vagrant-proxyconf")
      if ENV['http_proxy']
        config.proxy.http = resolvURL(ENV['http_proxy'])
      end
      if ENV['http_proxys']
        config.proxy.https = resolvURL(ENV['http_proxys'])
      end
      config.proxy.no_proxy = "localhost,127.0.0.1"
    end
  end

  if Vagrant.has_plugin?("vagrant-libvirt")
    config.vm.provider :libvirt do |libvirt|
      libvirt.storage_pool_name = "autocluster"
    end
  end

  settings['nodes'].each do |hostname, node|
    config.vm.define hostname do |v|
      v.vm.box = settings['vagrant_box']
      v.vm.hostname = hostname

      node['ips'].each do |ip|
        v.vm.network "private_network",
                     ip: ip,
                     nm_controlled: "no"
      end

      if settings['virthost']
        virthost = settings['virthost']
        v.vm.provision "shell",
                       run: "always",
                       inline: "route add default gw " + virthost + "|| :"
      end

      # No shared folders - they might require extra software on the
      # nodes and installation can time out  :-(
      v.vm.synced_folder ".", "/vagrant", disabled: true

      v.vm.provision "file",
                     source: "~/.ssh/id_autocluster",
                     destination: "~/.ssh/id_autocluster"
      v.vm.provision "file",
                     source: "~/.ssh/id_autocluster.pub",
                     destination: "~/.ssh/id_autocluster.pub"
      v.vm.provision :shell,
                     privileged: true,
                     path: "autocluster_ssh_node_setup.sh"

      v.vm.provider :libvirt do |libvirt|

        libvirt.default_prefix = 'autocluster'
        libvirt.cpus = settings['cpus']
        #FIXME: causes an error ### libvirt.memory = settings['memory']

        if node['has_shared_storage']
          for i in 1..settings['shared_disks']['count']
            libvirt.storage :file,
                            :serial => shared_disk_ids[i],
                            :path => sprintf('autocluster_%s_shared%02d.img',
                                             settings['cluster'], i),
                            :size => settings['shared_disks'].size,
                            :allow_existing => true,
                            :shareable => true,
                            :type => 'raw'
          end
        end
      end

      # The libvirt provider sometimes configures a private network
      # but doesn't bring it up.  Check that all desired IPs are
      # assigned, failing if any are missing.
      v.vm.provision "shell",
                     run: "always",
                     path: "autocluster_check_ips.sh",
                     args: node['ips']
    end
  end
end