Class: VagrantVminfo::InfoCommand

Inherits:
Object
  • Object
show all
Includes:
VagrantPlugins::CommandUp::StartMixins
Defined in:
lib/vagrant-vm-info/plugin.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/vagrant-vm-info/plugin.rb', line 131

def execute
    # Not sure if this is the only way to do this?  How else would I get argv?
    argv = parse_options(OptionParser.new)

    info = {}

    with_target_vms(argv) do |vm|
        info["name"] = vm.name.id2name
        info["status"] = vm.state.id.id2name

        # Check if vm is running - try to get some provider specific details
        if vm.state.id == :running
            info["id"] = vm.id
            info["provider"] = vm.provider_name.id2name
            info.merge!(get_vm_info(vm))
        end
    end
    puts info.to_yaml
end

#get_vm_info(vm) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/vagrant-vm-info/plugin.rb', line 32

def get_vm_info(vm)
    # Get some provider specific details for vm
    provider = vm.provider_name.id2name
    if provider == 'virtualbox'
        return get_vm_info_virtualbox(vm)
    elsif provider == 'vmware_workstation' or provider == 'vmware_fusion'
        return get_vm_info_vmware(vm)
    end
end

#get_vm_info_virtualbox(vm) ⇒ Object



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
# File 'lib/vagrant-vm-info/plugin.rb', line 42

def get_vm_info_virtualbox(vm)
    # Get VM info by virtualbox provider
    # All we need is network interfaces details

    networks = []
    begin
        nic_count = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/Count")[/Value: (\d)/, 1]
    rescue Exception => e
        @logger.warn(e.message)
        nic_count = 0
    else
        nic_count = Integer(nic_count)
    end
    # Check nic_count is not defined in case of VBox Guest Additions not install, or some other issue
    vminfo = vm.provider.driver.execute("showvminfo", vm.id, "--machinereadable")
    if nic_count > 0
        (0..(nic_count-1)).each do |i|
            nic_ip = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/V4/IP")[/Value: (.*)/, 1]
            nic_mac = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/MAC")[/Value: (.*)/, 1]
            nic_broadcast = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/V4/Broadcast")[/Value: (.*)/, 1]
            nic_netmask = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/V4/Netmask")[/Value: (.*)/, 1]
            # Get the number of this network device based on the mac address
            # TODO: probably just should just get a hash of all of this info somehow, my ruby foo is weak
            nic_number = vminfo[/\w*(\d)=\"#{nic_mac}\"/, 1]
            nic_type = vminfo[/nic#{nic_number}=\"(.*)\"/, 1]
            networks << {'ip' => nic_ip,
                         'mac' => nic_mac,
                         'netmask' => nic_netmask,
                         'broadcast' => nic_broadcast,
                         'type' => nic_type}
        end
    end
    # If VM is running - VMStateChangeTime is actually it's launch time 
    state_change_time = vminfo[/VMStateChangeTime=\"(.*)\"/, 1]
    return {"launch_time" => Time.parse(state_change_time).strftime("%Y-%m-%d %H:%M:%S"),
            "networks" => networks}

end

#get_vm_info_vmware(vm) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vagrant-vm-info/plugin.rb', line 81

def get_vm_info_vmware(vm)
    # Get VM info by virtualbox provider
    # We can get some network interfaces details and current IP address

    # Collect details for all the network interfaces
    nics = {}
    vm.provider.driver.send(:read_vmx_data).each do |key, val|
        key1, key2 = key.split('.')
        m = /^ethernet\d+$/.match(key1)
        if m
            if !nics.include?(m[0])
                nics[m[0]] = {}
            end
            nics[m[0]][key2] = val
        end
    end

    # Check and normalize collected network information
    networks = []
    nics.values.each do |nic|
        nic_type = nic['connectiontype']
        nic_addresstype = nic['addresstype']
        nic_mac = nic_addresstype == 'generated' ? nic['generatedaddress'] : nic['address']
        #TODO: For now I don't know simple way to retrieve these details
        #nic_ip = nil
        #nic_netmask = nil
        #nic_broadcast = nil

        networks << {#'ip' => nic_ip,
                     'mac' => nic_mac,
                     #'netmask' => nic_netmask,
                     #'broadcast' => nic_broadcast,
                     'type' => nic_type}
    end

    # We can try to get IP address for VM but can not assign it to any network interface
    ip = nil
    begin
        resp =  vm.provider.driver.send(:vmrun, *['getGuestIPAddress', vm.id])
    rescue Exception => e
        @logger.warn(e.message)
    else
        m = /(?<ip>\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3})/.match(resp.stdout)
        ip = (resp.exit_code == 0 and m) ? m['ip'] : nil
    end

    return {'ip' => ip,
            'networks' => networks }
end