Module: StartCloud

Defined in:
lib/vagrant-startcloud.rb

Overview

Top-level module for the plugin

Class Method Summary collapse

Class Method Details

.configure(config, settings_file = 'Hosts.yml') ⇒ Object


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
# File 'lib/vagrant-startcloud.rb', line 10

def self.configure(config, settings_file = 'Hosts.yml')
  unless File.exist?(settings_file)
    raise Vagrant::Errors::VagrantError.new(
      error_message: "StartCloud configuration file not found: #{settings_file}"
    )
  end

  yaml_config = YAML.load_file(settings_file)
  return unless yaml_config && yaml_config['hosts']

  # Configure each VM defined in hosts
  yaml_config['hosts'].each do |host|
    settings = host['settings']
    hostname = "#{settings['hostname']}.#{settings['domain']}"
    vm_name = "#{settings['server_id']}--#{hostname}"

    config.vm.define vm_name do |machine|
      # Configure the StartCloud plugin first
      machine.startcloud.config_path = settings_file
      machine.startcloud.settings = settings
      machine.startcloud.networks = host['networks'] || []
      machine.startcloud.disks = host['disks'] || {}
      machine.startcloud.folders = host['folders'] || []
      machine.startcloud.roles = host['roles'] || []
      machine.startcloud.vars = host['vars'] || {}
      machine.startcloud.plugins = host['plugins'] || {}
      machine.startcloud.zones = host['zones'] || {}

      # Set basic VM settings
      machine.vm.box = settings['box']
      machine.vm.hostname = hostname
      machine.vm.boot_timeout = settings['setup_wait'] if settings['setup_wait']

      # Configure SSH settings
      if settings['vagrant_user']
        machine.ssh.username = settings['vagrant_user']
        machine.ssh.private_key_path = settings['vagrant_user_private_key_path'] if settings['vagrant_user_private_key_path']
      end
      machine.ssh.insert_key = false
      machine.ssh.forward_agent = true

      # Set provider
      machine.vm.provider settings['provider_type'].to_sym do |provider|
        provider.name = vm_name if provider.respond_to?(:name=)
      end
    end
  end
end