Class: VagrantPlugins::Dotvm::Group::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dotvm/group/command.rb

Constant Summary collapse

COMMANDS =
%w(up halt destroy provision reload hosts suspend resume)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



8
9
10
# File 'lib/vagrant-dotvm/group/command.rb', line 8

def self.synopsis
  'runs vagrant command on specific group of VMs'
end

Instance Method Details

#all_groupsObject

do_action



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vagrant-dotvm/group/command.rb', line 87

def all_groups
  groups = Set.new

  with_target_vms do |machine|
    machine.config.dotvm_group.groups.to_h.each do |group_name, _hosts|
      groups << group_name
    end
  end

  groups.to_a
end

#do_action(action, options, group) ⇒ Object

print_hosts



77
78
79
80
81
82
83
84
85
# File 'lib/vagrant-dotvm/group/command.rb', line 77

def do_action(action, options, group)
  with_target_vms do |machine|
    if machine.config.dotvm_group.groups.key?(group)
      if machine.config.dotvm_group.groups[group].include? machine.name.to_s
        machine.action(action, **options)
      end
    end
  end
end

#executeObject

self.synopsis



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
# File 'lib/vagrant-dotvm/group/command.rb', line 12

def execute
  options = {
    provision_ignore_sentinel: false, # otherwise reload command does provision
  }
  opts = OptionParser.new do |o|
    o.banner = sprintf('Usage: vagrant group <%s> <group-name>', COMMANDS.join('|'))
    o.separator ''

    o.on('-h', '--help', 'Print this help') do
      safe_puts(opts.help)
      return nil
    end

    o.on('-f', '--force', 'Do action (destroy, halt) without confirmation.') do
      options[:force_confirm_destroy] = true
      options[:force_halt]            = true
    end

    o.on(nil, '--provision', 'Enable provisioning (up, reload).') do
      options[:provision_ignore_sentinel] = true
    end
  end

  argv = parse_options(opts)

  action = argv[0]
  patterns = argv[1]

  if !patterns || !action || !COMMANDS.include?(action)
    safe_puts(opts.help)
    return nil
  end

  groups = find_groups(patterns.split(","))
  if groups.length == 0
    @env.ui.error('No groups matched the pattern given.')
    return nil
  end

  if action == 'hosts'
    groups.each do |group|
      print_hosts(group)
    end
  else
    groups.each do |group|
      do_action(action, options, group)
    end
  end
end

#find_groups(patterns) ⇒ Object

all_groups



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-dotvm/group/command.rb', line 99

def find_groups(patterns)
  groups = []

  patterns.each do |pattern|
    if pattern[0] == '/' && pattern[-1] == '/'
      reg = Regexp.new(pattern[1..-2])
      all_groups.each do |item|
        groups << item if item.match(reg)
      end
    else
      all_groups.each do |item|
        groups << item if item == pattern
      end
    end
  end

  groups
end

execute



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vagrant-dotvm/group/command.rb', line 62

def print_hosts(group)
  @env.ui.info("Hosts in #{group} group:")

  with_target_vms do |machine|
    if machine.config.dotvm_group.groups.key?(group)
      if machine.config.dotvm_group.groups[group].to_a.include? machine.name.to_s
        @env.ui.info(" - #{machine.name}")
      end
    else
      @env.ui.warn('No hosts associated.')
      break
    end
  end
end