Class: Vbinfo::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



12
13
14
# File 'lib/command.rb', line 12

def self.synopsis
  "outputs information for each Virtualbox VM"
end

Instance Method Details

#enumerate_guest_info(id) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/command.rb', line 73

def enumerate_guest_info(id)
  if vboxmanage?
    command = Mixlib::ShellOut.new("vboxmanage guestproperty enumerate #{id}")
    command.run_command
    return command.stdout
  end
end

#executeObject

Print detailed information for each Virtualbox VM associated with the project in the current directory



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

def execute
  total_hash = Hash.new
  ids.each do |id|
    mini_hash = Hash.new
    name = id['name']
    i = id['id']
    guest_str = enumerate_guest_info(i).to_s
    guest_hash = get_guest_hash(guest_str)
    mini_hash['guest_info'] = guest_hash
    vm_str = show_vm_info(i).to_s
    vm_hash = get_vm_hash(vm_str)
    mini_hash['vm_info'] = vm_hash
    total_hash[name] = mini_hash 
  end
  if total_hash.empty? 
    @env.ui.info("No Virtualbox data was found")
  else
    @env.ui.info(JSON.pretty_generate(total_hash))
  end
  exit 0
end

#get_guest_hash(str) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/command.rb', line 81

def get_guest_hash(str)
  h = Hash.new
  lines = str.split("\n")
  lines.each do |line|
    name_match = Regexp.new("(?<=Name: )[^,]*")
    value_match = Regexp.new("(?<=value: )[^,]*")
    name = line[name_match]
    value = line[value_match]
    h[name] = value
  end
  return h
end

#get_vm_hash(str) ⇒ Object

Get hash



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/command.rb', line 60

def get_vm_hash(str)
  h = Hash.new
  lines = str.split("\n")
  lines.each do |line|
    key, val = line.split("=")
    [key, val].each do |item|
      item.tr!('"', '')
    end       
    h[key.to_s] = val.to_s
  end
  return h
end

#idsObject

return an array of hashes, each hash containing the name and id. Single VM configurations typically are named default, while multiple VM configurations are named individually.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/command.rb', line 20

def ids
  a = Array.new
  Find.find("#{ENV['PWD']}/.vagrant") do |item|
    if File.file?(item) and File.basename(item) == 'id'
      h = Hash.new
      # Id will be found in the id file
      file = File.open(item)
      h["id"] = file.read
      # the name will be in /<name>/virtualbox/id of path
      regex_obj = Regexp.new('[^\/]*(?=/virtualbox)')
      h["name"] = item[regex_obj]
      a << h
      file.close
    end
  end
  return a
end

#show_vm_info(id) ⇒ Object

Print detailed info for the given VM ID



50
51
52
53
54
55
56
57
# File 'lib/command.rb', line 50

def show_vm_info(id)
  if vboxmanage?
    # Return the output
    command = Mixlib::ShellOut.new("vboxmanage showvminfo --machinereadable #{id}")
    command.run_command
    return command.stdout
  end 
end

#vboxmanage?Boolean

Check for vboxmanage and exit 1 if not available

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'lib/command.rb', line 39

def vboxmanage?
  # Fail if vboxmanage does not exist in the path
  if not Vagrant::Util::Which.which('vboxmanage')
    raise Vagrant::Errors::CommandUnavailable, file: 'vboxmanage'
    exit 1
  else
    true
  end
end