Class: Vagrant::Hivemind::Command::Up

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Hivemind::Constants, Util
Defined in:
lib/vagrant/hivemind/commands/up.rb

Constant Summary

Constants included from Vagrant::Hivemind::Constants

Vagrant::Hivemind::Constants::ANSIBLE_HOSTS_FILE, Vagrant::Hivemind::Constants::BOX_SIZES, Vagrant::Hivemind::Constants::BOX_TYPES, Vagrant::Hivemind::Constants::CONTROL_HOSTNAME, Vagrant::Hivemind::Constants::HIVE_FILE, Vagrant::Hivemind::Constants::IP_ADDRESS_REGEX, Vagrant::Hivemind::Constants::PORT_PAIR_REGEX, Vagrant::Hivemind::Constants::PRIVATE_NETWORK, Vagrant::Hivemind::Constants::PRIVATE_NETWORK_END, Vagrant::Hivemind::Constants::PRIVATE_NETWORK_IP_ADDRESS_POOL, Vagrant::Hivemind::Constants::PRIVATE_NETWORK_START, Vagrant::Hivemind::Constants::SIMPLE_HOSTNAME_MAX_LENGTH, Vagrant::Hivemind::Constants::SIMPLE_HOSTNAME_REGEX, Vagrant::Hivemind::Constants::SYSTEM_HOSTS_FILE, Vagrant::Hivemind::Constants::VAGRANT_FILE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



15
16
17
# File 'lib/vagrant/hivemind/commands/up.rb', line 15

def self.synopsis
  "Starts and provisions a Drone in the Hive"
end

Instance Method Details

#executeObject



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
# File 'lib/vagrant/hivemind/commands/up.rb', line 19

def execute
  options = {}

  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant hivemind up [options]"
    o.separator ""
    o.separator "Options:"
    o.separator ""

    o.on("-n", "--hostname HOSTNAME", "A comma-separated list of Drone hostnames (REQUIRED)") do |n|
      options[:hostname] = Args.from_csv n
    end

    o.on("-d", "--directory DIRECTORY", "Specify the directory where '#{HIVE_FILE}' is located (default: current directory)") do |d|
      options[:directory] = []
      options[:directory] << d
    end
  end

  argv = parse_options(opts)
  return if !argv

  unless options[:hostname]
    @env.ui.info opts.help
    return 0
  end

  root_path = Path.get_root_path_from_options options

  unless HiveFile.exist? root_path
    @env.ui.error "There is no Hive file in the working directory."
    return 1
  end

  hosts = HiveFile.read_from root_path

  hostnames = []
  options[:hostname].each do |hostname|
    if hosts.values.map(&:hostname).include? hostname
      hostnames << hostname
    else
      @env.ui.warn "The hostname '#{hostname}' does not exist in the Hive!"
    end
  end

  Vagrant::Hivemind::Util::Vagrantfile.generate_hivemind_vagrantfile @env, hosts, root_path
  Ansible.generate_hosts_file hosts, Path.local_data_path(root_path)
  HostsFile.generate_hosts_file hosts, Path.local_data_path(root_path)

  machines = []
  @env.batch do |batch|
    with_target_vms(hostnames) do |machine|
      @env.ui.info(I18n.t("vagrant.commands.up.upping",
        name: machine.name,
        provider: machine.provider_name))

      machines << machine

      batch.action(machine, :up, options)
    end
  end

  if machines.empty?
    @env.ui.info(I18n.t("vagrant.up_no_machines"))
    return 1
  end

  machines.each do |m|
    next if !m.config.vm.post_up_message
    next if m.config.vm.post_up_message == ""

    @env.ui.info("", prefix: false)

    m.ui.success(I18n.t(
      "vagrant.post_up_message",
      name: m.name.to_s,
      message: m.config.vm.post_up_message))
  end

  0
end