Class: Vagrant::Hivemind::Command::List

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Hivemind::Constants, Util
Defined in:
lib/vagrant/hivemind/commands/list.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



14
15
16
# File 'lib/vagrant/hivemind/commands/list.rb', line 14

def self.synopsis
  "Lists all the Drones in the Hive"
end

Instance Method Details

#executeObject



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

def execute
  options = {}

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

    o.on("-n", "--hostname", "List the Drones ordered by the hostname") do |n|
      options[:hostname] = n
    end

    o.on("-a", "--ip-address", "List the Drones ordered by the IP address") do |a|
      options[:ip_address] = a
    end

    o.on("-c", "--control", "List the Drones, Control Machines first") do |c|
      options[:control] = c
    end

    o.on("-s", "--size", "List the Drones ordered by the Box Size") do |s|
      options[:size] = s
    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

  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

  sorted_hosts = sort_hosts hosts, options

  Vagrant::Hivemind::Util::Vagrantfile.generate_hivemind_vagrantfile @env, sorted_hosts, root_path

  @env.ui.info "Hostname             IP Address     C Size         Box Type   G D Status"
  sorted_hosts.values.each do |host|
    status = ""
    with_target_vms(host.hostname) do |machine|
      status = machine.state.short_description
    end
    hostname       = host.hostname
    ip_address     = host.ip_address
    is_control_y_n = host.is_control ? 'Y' : 'N'
    box_size       = BOX_SIZES[host.box_size.to_sym][:name]
    box_type       = BOX_TYPES[host.box_type.to_sym][:name]
    is_gui_y_n     = BOX_TYPES[host.box_type.to_sym][:is_gui] ? 'Y' : 'N'
    is_data_detached_y_n = host.is_data_detached ? 'Y' : 'N'
    @env.ui.info "#{'%-20.20s' % hostname} #{'%-14.14s' % ip_address} #{is_control_y_n} #{'%-12.12s' % box_size} #{'%-10.10s' % box_type} #{is_gui_y_n} #{is_data_detached_y_n} #{status}"
  end
  @env.ui.info ""

  0
end