Class: PEBuild::Command::Facts

Inherits:
Object
  • Object
show all
Defined in:
lib/pe_build/command/facts.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



5
6
7
# File 'lib/pe_build/command/facts.rb', line 5

def self.synopsis
  'Load facts from running VMs'
end

Instance Method Details

#executeObject



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
# File 'lib/pe_build/command/facts.rb', line 9

def execute
  argv = parse_options(parser)
  argv.shift # Remove 'facts' subcommand.

  running = @env.active_machines.map do |name, provider|
    @env.machine(name, provider)
  end.select do |vm|
    begin
      vm.communicate.ready?
    rescue Vagrant::Errors::VagrantError
      # WinRM will raise an error if the VM isn't running instead of
      # returning false (GH-6356).
      false
    end
  end

  # Filter the list of VMs for inspection down to just those passed on the
  # command line. Warn if the user passed a VM that was not running.
  unless argv.empty?
    running_vms = running.map {|vm| vm.name.to_s}
    argv.each do |name|
      @env.ui.warn I18n.t('pebuild.command.facts.vm_not_running', :vm_name => name) unless running_vms.include? name
    end

    running.select! {|vm| argv.include? vm.name.to_s}
  end

  running.each do |vm|
    facts = vm.guest.capability(:pebuild_facts)

    @env.ui.machine('guest-facts', facts, {:target => vm.name.to_s})
    @env.ui.info(JSON.pretty_generate(facts))
  end

  return 0
end