vagrant-list 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
19
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vagrant-list.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Josh McArthur
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.
@@ -0,0 +1,60 @@
1
+ vagrant-list
2
+ =====
3
+
4
+ ### What does it do?
5
+
6
+ `vagrant-list` adds a command to the Vagrant CLI to list both all VMs known to VirtualBox, as well as any that are running. It's handy for seeing what all your VMs are up to.
7
+
8
+
9
+ ### How do I install it?
10
+
11
+ #### Using bundler?
12
+ Just add `gem 'vagrant-list'` to your `Gemfile`
13
+
14
+ #### Not using bundler?
15
+ Untested, but just running `gem install vagrant-list` should have the same effect.
16
+
17
+ ### How do I use it?
18
+
19
+ #### From the CLI
20
+ `vagrant-list` adds just one command to vagrant - `list`.
21
+ For example, this following command will list the VMs:
22
+
23
+ `bundle exec vagrant list` will output:
24
+
25
+ ``` sh
26
+ [vagrant] ALL:
27
+ [vagrant] 35279736-412a-47b0-aa09-b1aef373a9c7: IE7 (Windows Vista)
28
+ [vagrant] ebb807dd-64d6-4a17-87f8-1d6d80a3f467: IE8 (Windows 7)
29
+ [vagrant] 4db11a4d-0a97-48c4-8b23-9cb2ca4fcb93: IE9 (Windows 7)
30
+ [vagrant] 508bf5cd-dcce-48f6-b19d-287fba4306d6: Windows XP (Windows XP)
31
+ [vagrant] 729322cf-e11d-438c-b9dc-6b1d31e9a56d: Python Development (Windows XP)
32
+ [vagrant] RUNNING:
33
+ [vagrant] ebb807dd-64d6-4a17-87f8-1d6d80a3f467: IE8 (Windows 7)
34
+ ```
35
+
36
+ #### From your own code
37
+
38
+ `vagrant-list` handily extends the driver for VirtualBox 4.1 to query both running and all VMs (previous to this you could only query all). If you'd like to use this feature in your code, you should just be able to add the gem to your `Gemfile`, and then run:
39
+
40
+
41
+ `Vagrant::Driver::VirtualBox.new(nil).read_vms(:vms)` to show **all** VMs (default), or `Vagrant::Driver::VirtualBox.new(nil).read_vms(:runningvms)` to show **running** VMs.
42
+
43
+ ### Running Tests
44
+
45
+ The code has exactly three specs that check:
46
+
47
+ * That the overridden driver method lists a known VM
48
+ * That the overridden driver method does not list an unknown VM
49
+ * That the overridden driver method lists a running VM
50
+
51
+ Unfortunately, because we are almost directly interacting with the VirtualBox command line tools, there is little opportunity for mocking or stubbing virtual machines.
52
+
53
+ **Therefore, in order for the specs to pass, it is required that you have a VM called 'Test' running when the specs are run. It doesn't matter what this VM _is_ (XP, Ubuntu etc.), just as long as it's there**
54
+
55
+ ### License
56
+
57
+ This open source software is licensed under the MIT License.
58
+
59
+ ---
60
+ ** If this project has been useful to you, I ask that you consider a donation to an open source project in need - check out my donations page at http://joshmcarthur.com/donations for projects that I've donated to for inspiration.**
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ require "vagrant-list/version"
2
+ require "vagrant/vm_info"
3
+ require "vagrant"
4
+ require "pp"
5
+
6
+ module Vagrant
7
+ module List
8
+ class All < ::Vagrant::Command::Base
9
+ def execute
10
+ all = Driver::VirtualBox.new(nil).read_vms(:vms)
11
+ @env.ui.info "ALL:"
12
+ all.each do |uuid|
13
+ @env.ui.info Vagrant::VMInfo.new(uuid).inspect
14
+ end
15
+
16
+ running = Driver::VirtualBox.new(nil).read_vms(:runningvms)
17
+ @env.ui.info "RUNNING:"
18
+ running.each do |uuid|
19
+ @env.ui.info Vagrant::VMInfo.new(uuid).inspect
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Vagrant.commands.register(:list) { Vagrant::List::All }
@@ -0,0 +1,5 @@
1
+ module Vagrant
2
+ module List
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ module Vagrant
2
+ module Ext
3
+ module Driver
4
+ module DriverOverrides
5
+ def self.included(base)
6
+ base.class_eval do
7
+
8
+ # Public - Support passing in a category
9
+ # of vms to list.
10
+ #
11
+ # Currently supported options:
12
+ # vms - All vms, regardless of status
13
+ # runningvms - Only running vms
14
+ #
15
+ # This method can be used to query the state of all vms.
16
+ # In the case of https://github.com/joshmcarthur/urchin,
17
+ # it is used to query for a list of VMs to display
18
+ def read_vms(type = :vms)
19
+ results = []
20
+ execute("list", type.to_s, :retryable => true).split("\n").each do |line|
21
+ if vm = line[/^".+?" \{(.+?)\}$/, 1]
22
+ results << vm
23
+ end
24
+ end
25
+
26
+ results
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ module Vagrant
2
+ class VMInfo
3
+ include Vagrant::Util
4
+
5
+ attr_accessor :raw
6
+ attr_accessor :uuid
7
+ attr_accessor :name
8
+ attr_accessor :guest_os
9
+
10
+ def initialize(uuid)
11
+ driver = Driver::VirtualBox.new
12
+ self.raw = driver.execute("showvminfo", uuid)
13
+ process!
14
+ end
15
+
16
+
17
+ # Public - Override inspect to display
18
+ # vm attributes
19
+ def inspect
20
+ "#{uuid}: #{name} (#{guest_os})"
21
+ end
22
+
23
+ private
24
+
25
+ # Private - Accept raw output from VBoxManage showvminfo command
26
+ # and manipulate string into a useful form.
27
+ #
28
+ # Requires raw be defined on the instance
29
+ #
30
+ # Returns the processed object
31
+ def process!
32
+ lines = self.raw.split("\n")
33
+ lines.each do |line|
34
+ raw_key, value = line.split(/\:\s+/)
35
+
36
+ if raw_key
37
+ key = raw_key.downcase.gsub(/\s+/, '_')
38
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
39
+ end
40
+ end
41
+
42
+ self
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,4 @@
1
+ require 'vagrant-list'
2
+ require 'vagrant/ext/driver/driver_overrides.rb'
3
+
4
+ Vagrant::Driver::VirtualBox_4_1.send(:include, Vagrant::Ext::Driver::DriverOverrides)
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'vagrant'
5
+ require 'vagrant-list'
6
+ require 'vagrant_init'
7
+
8
+
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Vagrant List' do
4
+
5
+ before :all do
6
+ puts <<-WARNING
7
+
8
+
9
+ WARNING: Because there is currently no support for mocking or stubbing
10
+ out calls to VirtualBox, it is required that there be at least
11
+ one running VM called 'Test' loaded into VirtualBox in order
12
+ for these tests to pass.
13
+
14
+ WARNING
15
+ end
16
+
17
+ let(:driver) { Vagrant::Driver::VirtualBox_4_1.new(nil) }
18
+
19
+ it "should list a VM" do
20
+ driver.read_vms.map { |vm| Vagrant::VMInfo.new(vm) }.select { |info|
21
+ info.name == "Test"
22
+ }.should_not be_empty
23
+ end
24
+
25
+ it "should not list a non-existent VM" do
26
+ driver.read_vms.map { |vm| Vagrant::VMInfo.new(vm) }.select { |info|
27
+ info.name == "Non-existent"
28
+ }.should be_empty
29
+ end
30
+
31
+ it "should list a VM when one is running" do
32
+ driver.read_vms(:runningvms).map { |vm| Vagrant::VMInfo.new(vm) }.select { |info|
33
+ info.name == "Test"
34
+ }.should_not be_empty
35
+ end
36
+ end
37
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/vagrant-list/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Josh McArthur"]
6
+ gem.email = ["joshua.mcarthur@gmail.com"]
7
+ gem.description = %q{Extends Vagrant with a 'list' command to list known VirtualBox virtual machines}
8
+ gem.summary = %q{Extends Vagrant with a 'list' command to list known VirtualBox virtual machines}
9
+ gem.homepage = "https://github.com/joshmcarthur/vagrant-list"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "vagrant-list"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Vagrant::List::VERSION
17
+
18
+ gem.add_runtime_dependency "vagrant"
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec"
21
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh McArthur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: vagrant
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Extends Vagrant with a 'list' command to list known VirtualBox virtual
63
+ machines
64
+ email:
65
+ - joshua.mcarthur@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - LICENSE
74
+ - README.md
75
+ - Rakefile
76
+ - lib/vagrant-list.rb
77
+ - lib/vagrant-list/version.rb
78
+ - lib/vagrant/ext/driver/driver_overrides.rb
79
+ - lib/vagrant/vm_info.rb
80
+ - lib/vagrant_init.rb
81
+ - spec/spec_helper.rb
82
+ - spec/vagrant_list_spec.rb
83
+ - vagrant-list.gemspec
84
+ homepage: https://github.com/joshmcarthur/vagrant-list
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 738696425748107712
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 738696425748107712
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.24
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Extends Vagrant with a 'list' command to list known VirtualBox virtual machines
114
+ test_files:
115
+ - spec/spec_helper.rb
116
+ - spec/vagrant_list_spec.rb