vagrant-list 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in vagrant-list.gemspec
4
4
  gemspec
5
+
6
+ # Required for running rake spec
7
+ group :development, :test do
8
+ gem 'vagrant', :git => 'https://github.com/mitchellh/vagrant.git'
9
+ end
data/README.md CHANGED
@@ -8,11 +8,33 @@ vagrant-list
8
8
 
9
9
  ### How do I install it?
10
10
 
11
- #### Using bundler?
12
- Just add `gem 'vagrant-list'` to your `Gemfile`
11
+ If you are using the packaged version of `vagrant` from [the Vagrant homepage]()http://downloads.vagrantup.com/) then you simply install this gem as a plugin by entering the following command in your terminal:
13
12
 
14
- #### Not using bundler?
15
- Untested, but just running `gem install vagrant-list` should have the same effect.
13
+ ``` bash
14
+ vagrant plugin install vagrant-list
15
+ ```
16
+
17
+ If you have vagrant installed from Rubygems, then you need to do a little more work - first, you can install the gem from [Rubygems](https://rubygems.org):
18
+
19
+ ``` bash
20
+ gem install vagrant-list
21
+ ```
22
+
23
+ Next, you need to tell Vagrant about this plugin, by adding to your project's `Vagrantfile`:
24
+
25
+ ``` ruby
26
+ Vagrant.require_plugin "vagrant-list"
27
+ ```
28
+
29
+ If you are still using a gem version though, it's worth considering the package upgrade, as that seems to be the preferred way for end-users to install Vagrant nowadays and will probably be better supported than a gem install.
30
+
31
+ ##### Vagrant < 1.1.x
32
+
33
+ If you are using a version of Vagrant older than 1.1.x, then you need to install an older version of this gem from Rubygems - only 0.0.5 and below will work with Vagrant 1.0.x:
34
+
35
+ `gem install vagrant-list --version "0.0.5"`
36
+
37
+ Please note that versions older than 0.0.5 **will not** work with Vagrant 1.1.x and above.
16
38
 
17
39
  ### How do I use it?
18
40
 
@@ -20,7 +42,7 @@ Untested, but just running `gem install vagrant-list` should have the same effec
20
42
  `vagrant-list` adds just one command to vagrant - `list`.
21
43
  For example, this following command will list the VMs:
22
44
 
23
- `bundle exec vagrant list` will output:
45
+ `vagrant list` will output:
24
46
 
25
47
  ``` sh
26
48
  [vagrant] ALL:
@@ -35,26 +57,48 @@ For example, this following command will list the VMs:
35
57
 
36
58
  #### From your own code
37
59
 
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:
60
+ If you need to acquire the same sort of list that the `vagrant list` command does, but programmatically, feel free to leverage a gem module that wraps around a couple of `VBoxManage` shell commands, like this:
61
+
62
+ ``` ruby
63
+ require 'vagrant-list'
64
+
65
+ Vagrant::List::VirtualBox::List.all # Returns all VMs
66
+ Vagrant::List::VirtualBox::List.running # Returns only running VMs
67
+ ```
39
68
 
69
+ The two methods above return an array of UUIDs. If you want to gather further information about a VM, you may also use another class - `Vagrant::List::VMInfo`, like so:
40
70
 
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.
71
+ ``` ruby
72
+ require 'vagrant-list`
73
+
74
+ Vagrant::List::VirtualBox::List.all.map do |uuid|
75
+ puts Vagrant::List::VMInfo.new(uuid).inspect
76
+ end
77
+ ```
42
78
 
43
79
  ### Running Tests
44
80
 
45
- The code has exactly three specs that check:
81
+ Tests for this type of gem are slightly problematic, since much of the core functionality involves shelling out, which is inherently difficult to test, however there are tests to ensure that methods are called as designed, and that helper classes function the way they should.
46
82
 
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
83
+ To run the tests, follow the steps below:
50
84
 
51
- Unfortunately, because we are almost directly interacting with the VirtualBox command line tools, there is little opportunity for mocking or stubbing virtual machines.
85
+ 1. Clone the project: `git clone https://github.com/joshmcarthur/vagrant-list.git`
86
+ 2. Install dependencies: `bundle install`
87
+ 3. Create a virtual machine named `Test` and boot it up
88
+ 4. Run the tests: `rake spec`
52
89
 
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
90
 
55
- ### License
91
+ ** Remember, 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**
92
+
93
+ ### Contributing
56
94
 
57
- This open source software is licensed under the MIT License.
95
+ 1. Fork and clone the project
96
+ 2. Run specs and make sure everything is passing
97
+ 3. Install the plugin into your vagrant install from the local source and run `vagrant list` to test: `rake build && vagrant plugin install pkg/vagrant-list-#{version}.gem && vagrant list`.
98
+ 4. Make your changes, rinse and repeat steps 2 & 3 often
99
+ 5. Push your changes in a `feature` or `bugfix` branch to Github.
100
+ 6. Send me a pull request!
101
+
102
+ ### License
58
103
 
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.**
104
+ This open source software is licensed under the MIT License. For more details, see `./LICENSE.txt`.
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -1,29 +1,7 @@
1
+ require 'i18n'
1
2
  require "vagrant-list/version"
2
- require "vagrant/ext/driver/driver_overrides"
3
- require "vagrant/vm_info"
4
- require "vagrant"
5
- require "pp"
3
+ require "vagrant-list/plugin"
4
+ require "vagrant-list/vm_info"
5
+ require "vagrant-list/command_error"
6
+ require "vagrant-list/virtual_box/list"
6
7
 
7
- module Vagrant
8
- module List
9
- class All < ::Vagrant::Command::Base
10
- def execute
11
- all = Driver::VirtualBox.new(nil).read_vms(:vms)
12
- @env.ui.info "ALL:"
13
- all.each do |uuid|
14
- @env.ui.info Vagrant::VMInfo.new(uuid).inspect
15
- end
16
-
17
- running = Driver::VirtualBox.new(nil).read_vms(:runningvms)
18
- @env.ui.info "RUNNING:"
19
- running.each do |uuid|
20
- @env.ui.info Vagrant::VMInfo.new(uuid).inspect
21
- end
22
- end
23
- end
24
- end
25
- end
26
-
27
- Vagrant::Driver::VirtualBox_4_1.send(:include, Vagrant::Ext::Driver::DriverOverrides)
28
- Vagrant::Driver::VirtualBox_4_2.send(:include, Vagrant::Ext::Driver::DriverOverrides)
29
- Vagrant.commands.register(:list) { Vagrant::List::All }
@@ -0,0 +1,19 @@
1
+
2
+
3
+ module Vagrant
4
+ module List
5
+ class Command < Vagrant.plugin("2", :command)
6
+ def execute
7
+ @env.ui.info "ALL:"
8
+ Vagrant::List::VirtualBox::List.all.each do |uuid|
9
+ @env.ui.info Vagrant::List::VMInfo.new(uuid).inspect
10
+ end
11
+
12
+ @env.ui.info "RUNNING:"
13
+ Vagrant::List::VirtualBox::List.running.each do |uuid|
14
+ @env.ui.info Vagrant::List::VMInfo.new(uuid).inspect
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require 'vagrant/errors'
2
+
3
+
4
+ module Vagrant
5
+ module List
6
+ class CommandError < ::Vagrant::Errors::VagrantError
7
+ error_key "command_failed"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'vagrant'
3
+ rescue LoadError
4
+ raise 'vagrant-list should be used from within vagrant.'
5
+ end
6
+
7
+ # Ensure that the gem is not trying to be used from within a vagrant release
8
+ # that will not have the ability to load the plugin
9
+ if Vagrant::VERSION < "1.1.0"
10
+ raise <<-WARN
11
+ The vagrant-list plugin is only compatible with Vagrant 1.1+.
12
+ See https://github.com/joshmcarthur/vagrant-list/tree/1.0.x for a 1.0.x-comaptible version
13
+ WARN
14
+ end
15
+
16
+ module Vagrant
17
+ module List
18
+ class Plugin < Vagrant.plugin("2")
19
+ name "List"
20
+ description <<-DESC
21
+ This plugin makes a `list` command available to you to list all or running vms.
22
+ The information is returned in a clear but easy to parse format which is handy for automated
23
+ control of vagrant boxes.
24
+ DESC
25
+
26
+ command "list" do
27
+ require_relative 'command'
28
+ Command
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module List
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -0,0 +1,37 @@
1
+ module Vagrant
2
+ module List
3
+ module VirtualBox
4
+ module List
5
+
6
+ def self.all
7
+ raw = `VBoxManage list vms`.split("\n")
8
+ raise Vagrant::List::CommandError, "VBoxManage list returned non-zero status" if errored?
9
+
10
+ process(raw)
11
+ end
12
+
13
+ def self.running
14
+ raw = `VBoxManage list runningvms`.split("\n")
15
+ raise Vagrant::List::CommandError, "VBoxManage list returned non-zero status" if errored?
16
+
17
+ process(raw)
18
+ end
19
+
20
+ def self.errored?
21
+ $? != 0
22
+ end
23
+
24
+ private
25
+
26
+ def self.process(raw)
27
+ raw.map! do |line|
28
+ next nil if line =~ /\A"<inaccessible>"/
29
+ if vm = line[/^".+?" \{(.+?)\}$/, 1]
30
+ next vm
31
+ end
32
+ end.compact
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,48 @@
1
+ require 'vagrant/util'
2
+
3
+ module Vagrant
4
+ module List
5
+ class VMInfo
6
+ include Vagrant::Util
7
+
8
+ attr_accessor :raw
9
+ attr_accessor :uuid
10
+ attr_accessor :name
11
+ attr_accessor :guest_os
12
+
13
+ def initialize(uuid)
14
+ self.raw = `VBoxManage showvminfo #{uuid}`
15
+ process!
16
+ end
17
+
18
+
19
+ # Public - Override inspect to display
20
+ # vm attributes
21
+ def inspect
22
+ "#{uuid}: #{name} (#{guest_os})"
23
+ end
24
+
25
+ private
26
+
27
+ # Private - Accept raw output from VBoxManage showvminfo command
28
+ # and manipulate string into a useful form.
29
+ #
30
+ # Requires raw be defined on the instance
31
+ #
32
+ # Returns the processed object
33
+ def process!
34
+ lines = self.raw.split("\n")
35
+ lines.each do |line|
36
+ raw_key, value = line.split(/\:\s+/)
37
+
38
+ if raw_key
39
+ key = raw_key.downcase.gsub(/\s+/, '_')
40
+ self.send("#{key}=", value) if self.respond_to?("#{key}=") && !self.send(key)
41
+ end
42
+ end
43
+
44
+ self
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,8 +1,14 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
-
4
- require 'vagrant'
5
3
  require 'vagrant-list'
6
- require 'vagrant_init'
4
+
5
+
6
+ puts <<-WARNING
7
+ WARNING: Because there is currently no support for mocking or stubbing
8
+ out calls to VirtualBox, it is required that there be at least
9
+ one running VM called 'Test' loaded into VirtualBox in order
10
+ for these tests to pass.
11
+ WARNING
12
+
7
13
 
8
14
 
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vagrant::List::VirtualBox::List do
4
+ describe ".all" do
5
+ it "should list VMs" do
6
+ subject.all.should have_at_least(1).string
7
+ end
8
+
9
+ it "should gracefully handle command failure" do
10
+ subject.stub(:errored?).and_return(true)
11
+ expect {
12
+ subject.all
13
+ }.to raise_error Vagrant::List::CommandError
14
+ end
15
+ end
16
+
17
+ describe ".running" do
18
+ it "should list the 'Test' VM" do
19
+ subject.running.should have(1).string
20
+ end
21
+
22
+ it "should gracefully handle command failure" do
23
+ subject.stub(:errored?).and_return(true)
24
+ expect {
25
+ subject.running
26
+ }.to raise_error Vagrant::List::CommandError
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vagrant::List::VMInfo do
4
+ let(:uuid) { Vagrant::List::VirtualBox::List.all.first }
5
+ let(:attributes) { %w( raw uuid name guest_os ) }
6
+
7
+ subject do
8
+ Vagrant::List::VMInfo.new(uuid)
9
+ end
10
+
11
+ describe "attributes" do
12
+ %w( raw uuid name guest_os ).each do |attribute|
13
+ it { subject.methods.should include attribute.to_sym }
14
+ it { subject.methods.should include "#{attribute}=".to_sym }
15
+ end
16
+ end
17
+
18
+ describe "initialization" do
19
+ it "should delegate to process! for deriving VM information" do
20
+ subject.class.any_instance.should_receive(:process!).and_call_original
21
+ subject.class.new(uuid)
22
+ end
23
+
24
+ %w( raw uuid name guest_os ).each do |attribute|
25
+ it "should set #{attribute}" do
26
+ subject.send(attribute).should be_a(String)
27
+ subject.send(attribute).should_not eq ""
28
+ end
29
+ end
30
+ end
31
+
32
+ describe ".inspect" do
33
+ let(:vm_info) { Vagrant::List::VMInfo.new(uuid) }
34
+ subject do
35
+ vm_info.inspect
36
+ end
37
+
38
+ it "should include the UUID" do
39
+ subject.should include vm_info.uuid
40
+ end
41
+
42
+ it "should include the name" do
43
+ subject.should include vm_info.name
44
+ end
45
+
46
+ it "should include the guest OS" do
47
+ subject.should include vm_info.guest_os
48
+ end
49
+ end
50
+ end
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Vagrant::List::VERSION
17
17
 
18
- gem.add_runtime_dependency "vagrant"
19
18
  gem.add_development_dependency "rake"
20
19
  gem.add_development_dependency "rspec"
20
+ gem.add_dependency "i18n"
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,17 +9,17 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-16 00:00:00.000000000 Z
12
+ date: 2013-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: vagrant
15
+ name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
- type: :runtime
22
+ type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: rake
31
+ name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -44,14 +44,14 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ name: i18n
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- type: :development
54
+ type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
@@ -74,12 +74,15 @@ files:
74
74
  - README.md
75
75
  - Rakefile
76
76
  - lib/vagrant-list.rb
77
+ - lib/vagrant-list/command.rb
78
+ - lib/vagrant-list/command_error.rb
79
+ - lib/vagrant-list/plugin.rb
77
80
  - 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
+ - lib/vagrant-list/virtual_box/list.rb
82
+ - lib/vagrant-list/vm_info.rb
81
83
  - spec/spec_helper.rb
82
- - spec/vagrant_list_spec.rb
84
+ - spec/vagrant-list/virtual_box/list_spec.rb
85
+ - spec/vagrant-list/vm_info_spec.rb
83
86
  - vagrant-list.gemspec
84
87
  homepage: https://github.com/joshmcarthur/vagrant-list
85
88
  licenses: []
@@ -95,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
98
  version: '0'
96
99
  segments:
97
100
  - 0
98
- hash: -1256749499276378399
101
+ hash: 3465248863129003724
99
102
  required_rubygems_version: !ruby/object:Gem::Requirement
100
103
  none: false
101
104
  requirements:
@@ -104,13 +107,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
107
  version: '0'
105
108
  segments:
106
109
  - 0
107
- hash: -1256749499276378399
110
+ hash: 3465248863129003724
108
111
  requirements: []
109
112
  rubyforge_project:
110
- rubygems_version: 1.8.24
113
+ rubygems_version: 1.8.23
111
114
  signing_key:
112
115
  specification_version: 3
113
116
  summary: Extends Vagrant with a 'list' command to list known VirtualBox virtual machines
114
117
  test_files:
115
118
  - spec/spec_helper.rb
116
- - spec/vagrant_list_spec.rb
119
+ - spec/vagrant-list/virtual_box/list_spec.rb
120
+ - spec/vagrant-list/vm_info_spec.rb
@@ -1,38 +0,0 @@
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
- begin
21
- execute("list", type.to_s, :retryable => true).split("\n").each do |line|
22
- next if line =~ /\A"<inaccessible>"/
23
- if vm = line[/^".+?" \{(.+?)\}$/, 1]
24
- results << vm
25
- end
26
- end
27
- rescue
28
- puts "Could not list #{type.to_s}"
29
- end
30
-
31
- results
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,45 +0,0 @@
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}=") && !self.send(key)
39
- end
40
- end
41
-
42
- self
43
- end
44
- end
45
- end
@@ -1,2 +0,0 @@
1
- require 'vagrant-list'
2
-
@@ -1,37 +0,0 @@
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
-