vagrant-vm-info 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/vagrant-vm-info/plugin.rb +151 -0
- data/lib/vagrant-vm-info/version.rb +3 -0
- data/lib/vagrant-vm-info.rb +19 -0
- data/vagrant-vm-info.gemspec +26 -0
- metadata +121 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in vagrant-vm-info.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
# We depend on Vagrant for development, but we don't add it as a
|
8
|
+
# gem dependency because we expect to be installed within the
|
9
|
+
# Vagrant environment itself using `vagrant plugin`.
|
10
|
+
gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git" , :tag => 'v1.6.3'
|
11
|
+
end
|
12
|
+
|
13
|
+
group :plugins do
|
14
|
+
gem "vagrant-vm-info", path: "."
|
15
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kostiantyn Chkhaidze
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Vagrant::Vm::Info
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'vagrant-vm-info'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install vagrant-vm-info
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,151 @@
|
|
1
|
+
begin
|
2
|
+
require "vagrant"
|
3
|
+
require Vagrant.source_root.join('plugins/commands/up/start_mixins')
|
4
|
+
rescue LoadError
|
5
|
+
raise "The vagrant-vm-info vagrant plugin must be run with vagrant."
|
6
|
+
end
|
7
|
+
|
8
|
+
require "yaml"
|
9
|
+
require "time"
|
10
|
+
|
11
|
+
# This is a sanity check to make sure no one is attempting to install
|
12
|
+
# this into an early Vagrant version.
|
13
|
+
if Vagrant::VERSION < "1.1.0"
|
14
|
+
raise "The vagrant-vm-info vagrant plugin is only compatible with Vagrant 1.1+"
|
15
|
+
end
|
16
|
+
|
17
|
+
module VagrantVminfo
|
18
|
+
class Plugin < Vagrant.plugin("2")
|
19
|
+
name "vagrant-vm-info"
|
20
|
+
description <<-DESC
|
21
|
+
This plugin provides the 'info' command that will display detailed information about the VM in YAML for easy parsing
|
22
|
+
DESC
|
23
|
+
|
24
|
+
command("info") do
|
25
|
+
InfoCommand
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class InfoCommand < Vagrant.plugin(2, :command)
|
30
|
+
include VagrantPlugins::CommandUp::StartMixins
|
31
|
+
|
32
|
+
def get_vm_info(vm)
|
33
|
+
# Get some provider specific details for vm
|
34
|
+
provider = vm.provider_name.id2name
|
35
|
+
if provider == 'virtualbox'
|
36
|
+
return get_vm_info_virtualbox(vm)
|
37
|
+
elsif provider == 'vmware_workstation' or provider == 'vmware_fusion'
|
38
|
+
return get_vm_info_vmware(vm)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_vm_info_virtualbox(vm)
|
43
|
+
# Get VM info by virtualbox provider
|
44
|
+
# All we need is network interfaces details
|
45
|
+
|
46
|
+
networks = []
|
47
|
+
begin
|
48
|
+
nic_count = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/Count")[/Value: (\d)/, 1]
|
49
|
+
rescue Exception => e
|
50
|
+
@logger.warn(e.message)
|
51
|
+
nic_count = 0
|
52
|
+
else
|
53
|
+
nic_count = Integer(nic_count)
|
54
|
+
end
|
55
|
+
# Check nic_count is not defined in case of VBox Guest Additions not install, or some other issue
|
56
|
+
vminfo = vm.provider.driver.execute("showvminfo", vm.id, "--machinereadable")
|
57
|
+
if nic_count > 0
|
58
|
+
(0..(nic_count-1)).each do |i|
|
59
|
+
nic_ip = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/V4/IP")[/Value: (.*)/, 1]
|
60
|
+
nic_mac = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/MAC")[/Value: (.*)/, 1]
|
61
|
+
nic_broadcast = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/V4/Broadcast")[/Value: (.*)/, 1]
|
62
|
+
nic_netmask = vm.provider.driver.execute("guestproperty", "get", vm.id, "/VirtualBox/GuestInfo/Net/#{i}/V4/Netmask")[/Value: (.*)/, 1]
|
63
|
+
# Get the number of this network device based on the mac address
|
64
|
+
# TODO: probably just should just get a hash of all of this info somehow, my ruby foo is weak
|
65
|
+
nic_number = vminfo[/\w*(\d)=\"#{nic_mac}\"/, 1]
|
66
|
+
nic_type = vminfo[/nic#{nic_number}=\"(.*)\"/, 1]
|
67
|
+
networks << {'ip' => nic_ip,
|
68
|
+
'mac' => nic_mac,
|
69
|
+
'netmask' => nic_netmask,
|
70
|
+
'broadcast' => nic_broadcast,
|
71
|
+
'type' => nic_type}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
# If VM is running - VMStateChangeTime is actually it's launch time
|
75
|
+
state_change_time = vminfo[/VMStateChangeTime=\"(.*)\"/, 1]
|
76
|
+
return {"launch_time" => Time.parse(state_change_time).strftime("%Y-%m-%d %H:%M:%S"),
|
77
|
+
"networks" => networks}
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
def get_vm_info_vmware(vm)
|
82
|
+
# Get VM info by virtualbox provider
|
83
|
+
# We can get some network interfaces details and current IP address
|
84
|
+
|
85
|
+
# Collect details for all the network interfaces
|
86
|
+
nics = {}
|
87
|
+
vm.provider.driver.send(:read_vmx_data).each do |key, val|
|
88
|
+
key1, key2 = key.split('.')
|
89
|
+
m = /^ethernet\d+$/.match(key1)
|
90
|
+
if m
|
91
|
+
if !nics.include?(m[0])
|
92
|
+
nics[m[0]] = {}
|
93
|
+
end
|
94
|
+
nics[m[0]][key2] = val
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Check and normalize collected network information
|
99
|
+
networks = []
|
100
|
+
nics.values.each do |nic|
|
101
|
+
nic_type = nic['connectiontype']
|
102
|
+
nic_addresstype = nic['addresstype']
|
103
|
+
nic_mac = nic_addresstype == 'generated' ? nic['generatedaddress'] : nic['address']
|
104
|
+
#TODO: For now I don't know simple way to retrieve these details
|
105
|
+
#nic_ip = nil
|
106
|
+
#nic_netmask = nil
|
107
|
+
#nic_broadcast = nil
|
108
|
+
|
109
|
+
networks << {#'ip' => nic_ip,
|
110
|
+
'mac' => nic_mac,
|
111
|
+
#'netmask' => nic_netmask,
|
112
|
+
#'broadcast' => nic_broadcast,
|
113
|
+
'type' => nic_type}
|
114
|
+
end
|
115
|
+
|
116
|
+
# We can try to get IP address for VM but can not assign it to any network interface
|
117
|
+
ip = nil
|
118
|
+
begin
|
119
|
+
resp = vm.provider.driver.send(:vmrun, *['getGuestIPAddress', vm.id])
|
120
|
+
rescue Exception => e
|
121
|
+
@logger.warn(e.message)
|
122
|
+
else
|
123
|
+
m = /(?<ip>\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3})/.match(resp.stdout)
|
124
|
+
ip = (resp.exit_code == 0 and m) ? m['ip'] : nil
|
125
|
+
end
|
126
|
+
|
127
|
+
return {'ip' => ip,
|
128
|
+
'networks' => networks }
|
129
|
+
end
|
130
|
+
|
131
|
+
def execute
|
132
|
+
# Not sure if this is the only way to do this? How else would I get argv?
|
133
|
+
argv = parse_options(OptionParser.new)
|
134
|
+
|
135
|
+
info = {}
|
136
|
+
|
137
|
+
with_target_vms(argv) do |vm|
|
138
|
+
info["name"] = vm.name.id2name
|
139
|
+
info["status"] = vm.state.id.id2name
|
140
|
+
|
141
|
+
# Check if vm is running - try to get some provider specific details
|
142
|
+
if vm.state.id == :running
|
143
|
+
info["id"] = vm.id
|
144
|
+
info["provider"] = vm.provider_name.id2name
|
145
|
+
info.merge!(get_vm_info(vm))
|
146
|
+
end
|
147
|
+
end
|
148
|
+
puts info.to_yaml
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "pathname"
|
2
|
+
|
3
|
+
module VagrantVminfo
|
4
|
+
|
5
|
+
def self.vagrant_vm_info_root
|
6
|
+
@vagrant_vm_info_root ||= Pathname.new(File.expand_path("../../", __FILE__))
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.load_script(script_file_name)
|
10
|
+
File.read(expand_script_path(script_file_name))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.load_script_template(script_file_name, options)
|
14
|
+
Vagrant::Util::TemplateRenderer.render(expand_script_path(script_file_name), options)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
require "vagrant-vm-info/plugin"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vagrant-vm-info/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "vagrant-vm-info"
|
8
|
+
spec.version = VagrantVminfo::VERSION
|
9
|
+
spec.authors = ["Lin Salisbury", "Kostiantyn Chkhaidze"]
|
10
|
+
spec.email = ["lin.salisbury@gmail.com", "kchkhaidze@rivermeadow.com"]
|
11
|
+
spec.description = %q{Plugin to get detailed VM info from Vagrant}
|
12
|
+
spec.summary = %q{Gets detailed information about the VM, including network interfaes}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec-core", "~> 2.12.2"
|
24
|
+
spec.add_development_dependency "rspec-expectations", "~> 2.12.1"
|
25
|
+
spec.add_development_dependency "rspec-mocks", "~> 2.12.1"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vagrant-vm-info
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lin Salisbury
|
9
|
+
- Kostiantyn Chkhaidze
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2014-07-08 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rspec-core
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 2.12.2
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.12.2
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec-expectations
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.12.1
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.12.1
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec-mocks
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 2.12.1
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.12.1
|
79
|
+
description: Plugin to get detailed VM info from Vagrant
|
80
|
+
email:
|
81
|
+
- lin.salisbury@gmail.com
|
82
|
+
- kchkhaidze@rivermeadow.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- lib/vagrant-vm-info.rb
|
93
|
+
- lib/vagrant-vm-info/plugin.rb
|
94
|
+
- lib/vagrant-vm-info/version.rb
|
95
|
+
- vagrant-vm-info.gemspec
|
96
|
+
homepage: ''
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.23
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Gets detailed information about the VM, including network interfaes
|
121
|
+
test_files: []
|