vagrant-rake-fork 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -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.2.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-fork"
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.8.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,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-rake-fork
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Mitchell Hashimoto
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: vagrant
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 63
29
+ segments:
30
+ - 0
31
+ - 8
32
+ - 0
33
+ version: 0.8.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: protest
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 15
45
+ segments:
46
+ - 0
47
+ - 4
48
+ - 0
49
+ version: 0.4.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: mocha
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 43
61
+ segments:
62
+ - 0
63
+ - 9
64
+ - 8
65
+ version: 0.9.8
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: bundler
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 23
77
+ segments:
78
+ - 1
79
+ - 0
80
+ - 0
81
+ version: 1.0.0
82
+ type: :development
83
+ version_requirements: *id004
84
+ description: A Vagrant plugin to execute `rake` commands from the host in the VM
85
+ email:
86
+ - mitchell.hashimoto@gmail.com
87
+ executables: []
88
+
89
+ extensions: []
90
+
91
+ extra_rdoc_files: []
92
+
93
+ files:
94
+ - .gitignore
95
+ - Gemfile
96
+ - README.md
97
+ - Rakefile
98
+ - lib/vagrant-rake.rb
99
+ - lib/vagrant-rake/command.rb
100
+ - lib/vagrant-rake/config.rb
101
+ - lib/vagrant-rake/middleware.rb
102
+ - lib/vagrant-rake/version.rb
103
+ - lib/vagrant_init.rb
104
+ - locales/en.yml
105
+ - test/test_helper.rb
106
+ - test/vagrant-rake/command_test.rb
107
+ - test/vagrant-rake/config_test.rb
108
+ - test/vagrant-rake/middleware_test.rb
109
+ - vagrant-rake.gemspec
110
+ homepage: http://rubygems.org/gems/vagrant-rake
111
+ licenses: []
112
+
113
+ post_install_message:
114
+ rdoc_options: []
115
+
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 23
133
+ segments:
134
+ - 1
135
+ - 3
136
+ - 6
137
+ version: 1.3.6
138
+ requirements: []
139
+
140
+ rubyforge_project: vagrant-rake
141
+ rubygems_version: 1.8.10
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: A Vagrant plugin to execute `rake` commands from the host in the VM
145
+ test_files: []
146
+