vagrant-rake 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in vagrant-rake.gemspec
4
+ gem "vagrant-rake", :path => "."
5
+
6
+ gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git"
7
+
8
+ group :test do
9
+ gem "protest", "~> 0.4.0"
10
+ gem "mocha", "~> 0.9.8"
11
+ end
@@ -0,0 +1,52 @@
1
+ GIT
2
+ remote: git://github.com/mitchellh/vagrant.git
3
+ revision: daa6caffe98d6285a0dad0467f002dd57e822985
4
+ specs:
5
+ vagrant (0.6.0.dev)
6
+ archive-tar-minitar (= 0.5.2)
7
+ erubis (~> 2.6.6)
8
+ i18n (~> 0.4.1)
9
+ json (~> 1.4.6)
10
+ mario (~> 0.0.6)
11
+ net-scp (~> 1.0.3)
12
+ net-ssh (~> 2.0.23)
13
+ thor (~> 0.14.1)
14
+ virtualbox (~> 0.7.3)
15
+
16
+ PATH
17
+ remote: .
18
+ specs:
19
+ vagrant-rake (0.1.0)
20
+ vagrant (~> 0.6.0)
21
+
22
+ GEM
23
+ remote: http://rubygems.org/
24
+ specs:
25
+ abstract (1.0.0)
26
+ archive-tar-minitar (0.5.2)
27
+ erubis (2.6.6)
28
+ abstract (>= 1.0.0)
29
+ ffi (0.6.3)
30
+ rake (>= 0.8.7)
31
+ i18n (0.4.1)
32
+ json (1.4.6)
33
+ mario (0.0.6)
34
+ mocha (0.9.8)
35
+ rake
36
+ net-scp (1.0.4)
37
+ net-ssh (>= 1.99.1)
38
+ net-ssh (2.0.23)
39
+ protest (0.4.1)
40
+ rake (0.8.7)
41
+ thor (0.14.2)
42
+ virtualbox (0.7.5)
43
+ ffi (>= 0.6.3)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ mocha (~> 0.9.8)
50
+ protest (~> 0.4.0)
51
+ vagrant!
52
+ vagrant-rake!
@@ -0,0 +1,49 @@
1
+ # vagrant-rake
2
+
3
+ `vagrant-rake` is a plugin for [Vagrant](http://vagrantup.com) which allows
4
+ a developer to execute rake tasks on the host using the `vagrant rake` command
5
+ and have them be invoked on the guest. This alleviates the need to SSH into
6
+ the VM to simply run a rake task.
7
+
8
+ This gem is also a good example of how to create a proper Vagrant plugin.
9
+
10
+ **NOTE:** This plugin requires Vagrant 0.6 or later.
11
+
12
+ ## Installing / Getting Started
13
+
14
+ To use this plugin, first install Vagrant 0.6 or later. Next, install this gem:
15
+
16
+ gem install vagrant-rake
17
+
18
+ The `vagrant rake` command should now be available. To use it, simply
19
+ pass use it as if you were running rake locally. The various ways to
20
+ call the command is shown below:
21
+
22
+ vagrant rake db:migrate
23
+ vagrant rake build
24
+ vagrant rake test --cwd /some/other/directory
25
+
26
+ The plugin will auotomatically run your rake task on the VM, forwarding
27
+ the output to you on the host.
28
+
29
+ You can also specify the default working directory for executed
30
+ rake tasks in your Vagrantfile:
31
+
32
+ Vagrant::Config.run do |config|
33
+ config.rake.directory = "/my/custom/directory"
34
+ end
35
+
36
+ ## Working with the Plugin Source
37
+
38
+ If you'd like to work with the plugin source (for learning purposes or
39
+ to make a contribution), then clone out this repository, then run the
40
+ test suite to verify everything is working:
41
+
42
+ git clone git://github.com/mitchellh/vagrant-rake.git
43
+ cd vagrant-rake
44
+
45
+ Then bundle the environment to get the required gems, and run rake to
46
+ verify that the test suite passes:
47
+
48
+ bundle install
49
+ rake
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'bundler/setup'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :default => :test
7
+
8
+ desc "Run the test suite."
9
+ task :test do
10
+ $:.unshift File.expand_path("../test", __FILE__)
11
+
12
+ Dir["test/**/*_test.rb"].each do |f|
13
+ load f
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'vagrant'
2
+ require 'vagrant-rake/config'
3
+ require 'vagrant-rake/command'
4
+ require 'vagrant-rake/middleware'
5
+
6
+ # Create a new middleware stack "rake" which is executed for
7
+ # rake commands. See the VagrantRake::Middleware docs for more
8
+ # information.
9
+ rake = Vagrant::Action::Builder.new do
10
+ use VagrantRake::Middleware
11
+ end
12
+
13
+ Vagrant::Action.register(:rake, rake)
14
+
15
+ # Add our custom translations to the load path
16
+ I18n.load_path << File.expand_path("../../locales/en.yml", __FILE__)
@@ -0,0 +1,23 @@
1
+ module VagrantRake
2
+ class Command < Vagrant::Command::Base
3
+ register "rake", "Run a rake task inside the VM environment"
4
+ argument :rake_command, :type => :array, :required => false, :desc => "The command to run on the VM via Rake"
5
+ class_option :cwd, :type => :string, :default => nil
6
+
7
+ # Executes the given rake command on the VMs that are represented
8
+ # by this environment.
9
+ def execute
10
+ command = (rake_command || []).join(" ")
11
+ target_vms.each { |vm| execute_on_vm(vm, command) }
12
+ end
13
+
14
+ protected
15
+
16
+ # Executes a command on a specific VM.
17
+ def execute_on_vm(vm, command)
18
+ vm.env.actions.run(:rake,
19
+ "rake.command" => command,
20
+ "rake.cwd" => options[:cwd])
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module VagrantRake
2
+ # A configuration class to configure defaults which are used for
3
+ # the `vagrant-rake` plugin.
4
+ class Config < Vagrant::Config::Base
5
+ configures :rake
6
+ attr_accessor :directory
7
+
8
+ def validate(errors)
9
+ errors.add(I18n.t("vagrant.plugins.rake.config_directory")) if directory && !directory.is_a?(String)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module VagrantRake
2
+ # A Vagrant middleware which executes a rake task on a given
3
+ # VM. This task will run "rake" on the VM that this action sequence
4
+ # was run on. If an env variable "rake.command" is populated, then
5
+ # this command will be executed by rake.
6
+ class Middleware
7
+ def initialize(app, env)
8
+ @app = app
9
+ @env = env
10
+ end
11
+
12
+ def call(env)
13
+ if env["vm"].created? && env["vm"].vm.running?
14
+ command = "rake #{env["rake.command"]}".strip
15
+
16
+ env["vm"].ssh.execute do |ssh|
17
+ env.ui.info I18n.t("vagrant.plugins.rake.executing", :command => command)
18
+
19
+ ssh.exec!("cd #{working_directory}; #{command}") do |channel, type, data|
20
+ # Print the data directly to STDOUT, not doing any newlines
21
+ # or any extra formatting of our own
22
+ $stdout.print(data) if type != :exit_status
23
+ end
24
+
25
+ # Puts out an ending newline just to make sure we end on a new
26
+ # line.
27
+ $stdout.puts
28
+ end
29
+ end
30
+
31
+ @app.call(env)
32
+ end
33
+
34
+ protected
35
+
36
+ def working_directory
37
+ @env["rake.cwd"] || @env["config"].rake.directory || @env["config"].vm.shared_folders["v-root"][:guestpath]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module VagrantRake
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # This file is automatically loaded by Vagrant to load any
2
+ # plugins. This file kicks off this plugin.
3
+
4
+ require 'vagrant-rake'
@@ -0,0 +1,6 @@
1
+ en:
2
+ vagrant:
3
+ plugins:
4
+ rake:
5
+ executing: "Executing: %{command}"
6
+ config_directory: "Directory must be a string."
@@ -0,0 +1,35 @@
1
+ require "test/unit/assertions"
2
+ require "protest"
3
+ require "mocha"
4
+ require "vagrant-rake"
5
+
6
+ class Protest::TestCase
7
+ include Test::Unit::Assertions
8
+ include Mocha::API
9
+ include Vagrant::TestHelpers
10
+
11
+ # Get Mocha integrated properly into the tests
12
+ alias :original_run :run
13
+ def run(report)
14
+ original_run(report)
15
+ mocha_verify
16
+ ensure
17
+ mocha_teardown
18
+ end
19
+
20
+ # Helper to silence streams, courtesy of Ruby Facets library.
21
+ def silence_stream(*streams) #:yeild:
22
+ on_hold = streams.collect{ |stream| stream.dup }
23
+ streams.each do |stream|
24
+ stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
25
+ stream.sync = true
26
+ end
27
+ yield
28
+ ensure
29
+ streams.each_with_index do |stream, i|
30
+ stream.reopen(on_hold[i])
31
+ end
32
+ end
33
+ end
34
+
35
+ Protest.report_with(:documentation)
@@ -0,0 +1,51 @@
1
+ require "test_helper"
2
+
3
+ Protest.describe("`vagrant rake` command") do
4
+ setup do
5
+ clean_paths
6
+
7
+ @klass = VagrantRake::Command
8
+ @env = vagrant_env
9
+ @vm = @env.vms.values.first
10
+ end
11
+
12
+ should "execute the command on every VM" do
13
+ @vm.env.actions.expects(:run).with() do |action, options|
14
+ assert_equal :rake, action
15
+ assert_equal "db:migrate", options["rake.command"]
16
+ true
17
+ end
18
+
19
+ @env.cli("rake", "db:migrate")
20
+ end
21
+
22
+ should "execute fine with no command given" do
23
+ @vm.env.actions.expects(:run).with() do |action, options|
24
+ assert_equal :rake, action
25
+ assert_equal "", options["rake.command"]
26
+ true
27
+ end
28
+
29
+ @env.cli("rake")
30
+ end
31
+
32
+ should "execute within the nil cwd by default" do
33
+ @vm.env.actions.expects(:run).with() do |action, options|
34
+ assert_equal :rake, action
35
+ assert_equal nil, options["rake.cwd"]
36
+ true
37
+ end
38
+
39
+ @env.cli("rake")
40
+ end
41
+
42
+ should "execute within the given cwd" do
43
+ @vm.env.actions.expects(:run).with() do |action, options|
44
+ assert_equal :rake, action
45
+ assert_equal "/foo/bar", options["rake.cwd"]
46
+ true
47
+ end
48
+
49
+ @env.cli("rake", "--cwd", "/foo/bar")
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+
3
+ Protest.describe("Configuration class") do
4
+ setup do
5
+ @klass = VagrantRake::Config
6
+ @instance = @klass.new
7
+ @errors = Vagrant::Config::ErrorRecorder.new
8
+ end
9
+
10
+ should "be valid by default" do
11
+ @instance.validate(@errors)
12
+ assert @errors.errors.empty?
13
+ end
14
+
15
+ should "be invalid if directory is not a string" do
16
+ @instance.directory = 24
17
+ @instance.validate(@errors)
18
+
19
+ assert !@errors.errors.empty?
20
+ end
21
+ end
@@ -0,0 +1,71 @@
1
+ require "test_helper"
2
+
3
+ Protest.describe("rake middleware") do
4
+ setup do
5
+ @klass = VagrantRake::Middleware
6
+ @app, @env = action_env(vagrant_env.vms.values.first.env)
7
+
8
+ @instance = @klass.new(@app, @env)
9
+
10
+ @env["vm"].stubs(:created?).returns(true)
11
+ @env["vm"].vm.stubs(:running?).returns(true)
12
+ end
13
+
14
+ should "do nothing if the VM is not created" do
15
+ @env["vm"].stubs(:created?).returns(false)
16
+ @env["vm"].expects(:ssh).never
17
+ @instance.call(@env)
18
+ end
19
+
20
+ should "do nothing if the VM is not running" do
21
+ @env["vm"].vm.stubs(:running?).returns(false)
22
+ @env["vm"].expects(:ssh).never
23
+ @instance.call(@env)
24
+ end
25
+
26
+ should "run rake via SSH on the VM" do
27
+ ssh = mock("ssh")
28
+ @env["vm"].ssh.expects(:execute).yields(ssh)
29
+
30
+ ssh.expects(:exec!).with("cd /vagrant; rake")
31
+
32
+ silence_stream($stdout) { @instance.call(@env) }
33
+ end
34
+
35
+ should "run the specified rake command via SSH on the VM" do
36
+ ssh = mock("ssh")
37
+ @env["vm"].ssh.expects(:execute).yields(ssh)
38
+
39
+ ssh.expects(:exec!).with("cd /vagrant; rake db:migrate")
40
+
41
+ @env["rake.command"] = "db:migrate"
42
+ silence_stream($stdout) { @instance.call(@env) }
43
+ end
44
+
45
+ context "cwd" do
46
+ setup do
47
+ @ssh = mock("ssh")
48
+ @env["vm"].ssh.expects(:execute).yields(@ssh)
49
+ @env["rake.cwd"] = nil
50
+ end
51
+
52
+ should "run the rake command in the specified cwd" do
53
+ @ssh.expects(:exec!).with("cd /foo/bar; rake")
54
+
55
+ @env["rake.cwd"] = "/foo/bar"
56
+ silence_stream($stdout) { @instance.call(@env) }
57
+ end
58
+
59
+ should "run the rake command with the configured directory if middleware variable is not set" do
60
+ @env.env.config.rake.directory = "/configured"
61
+
62
+ @ssh.expects(:exec!).with("cd /configured; rake")
63
+ silence_stream($stdout) { @instance.call(@env) }
64
+ end
65
+
66
+ should "run the rake command with the root shared directory if nothing configured" do
67
+ @ssh.expects(:exec!).with("cd /vagrant; rake")
68
+ silence_stream($stdout) { @instance.call(@env) }
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/vagrant-rake/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "vagrant-rake"
6
+ s.version = VagrantRake::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Mitchell Hashimoto"]
9
+ s.email = ["mitchell.hashimoto@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/vagrant-rake"
11
+ s.summary = "A Vagrant plugin to execute `rake` commands from the host in the VM"
12
+ s.description = "A Vagrant plugin to execute `rake` commands from the host in the VM"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "vagrant-rake"
16
+
17
+ s.add_dependency "vagrant", "~> 0.6.0"
18
+ s.add_development_dependency "protest", "~> 0.4.0"
19
+ s.add_development_dependency "mocha", "~> 0.9.8"
20
+ s.add_development_dependency "bundler", ">= 1.0.0"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
24
+ s.require_path = 'lib'
25
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-rake
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Mitchell Hashimoto
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-27 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: vagrant
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 6
30
+ - 0
31
+ version: 0.6.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: protest
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 4
45
+ - 0
46
+ version: 0.4.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: mocha
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ - 9
60
+ - 8
61
+ version: 0.9.8
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: bundler
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
74
+ - 0
75
+ - 0
76
+ version: 1.0.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ description: A Vagrant plugin to execute `rake` commands from the host in the VM
81
+ email:
82
+ - mitchell.hashimoto@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files: []
88
+
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - README.md
94
+ - Rakefile
95
+ - lib/vagrant-rake.rb
96
+ - lib/vagrant-rake/command.rb
97
+ - lib/vagrant-rake/config.rb
98
+ - lib/vagrant-rake/middleware.rb
99
+ - lib/vagrant-rake/version.rb
100
+ - lib/vagrant_init.rb
101
+ - locales/en.yml
102
+ - test/test_helper.rb
103
+ - test/vagrant-rake/command_test.rb
104
+ - test/vagrant-rake/config_test.rb
105
+ - test/vagrant-rake/middleware_test.rb
106
+ - vagrant-rake.gemspec
107
+ has_rdoc: true
108
+ homepage: http://rubygems.org/gems/vagrant-rake
109
+ licenses: []
110
+
111
+ post_install_message:
112
+ rdoc_options: []
113
+
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: -913781413029022123
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ segments:
131
+ - 1
132
+ - 3
133
+ - 6
134
+ version: 1.3.6
135
+ requirements: []
136
+
137
+ rubyforge_project: vagrant-rake
138
+ rubygems_version: 1.3.7
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: A Vagrant plugin to execute `rake` commands from the host in the VM
142
+ test_files: []
143
+