vagrant-exec 0.1.0 → 0.2.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.
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
- vagrant-exec
1
+ vagrant-exec [![Gem Version](https://badge.fury.io/rb/vagrant-exec.png)](http://badge.fury.io/rb/vagrant-exec)
2
2
  ===============
3
3
 
4
4
  Vagrant plugin to execute commands within the context of VM synced directory.
5
5
 
6
- [![Gem Version](https://badge.fury.io/rb/vagrant-exec.png)](http://badge.fury.io/rb/vagrant-exec)
7
-
8
6
  Description
9
7
  -----------
10
8
 
@@ -37,6 +35,12 @@ Vagrant.configure('2') do |config|
37
35
  end
38
36
  ```
39
37
 
38
+ ```shell
39
+ ➜ vagrant exec pwd
40
+ # is the same as
41
+ ➜ vagrant ssh -c "cd /custom && bundle exec pwd'
42
+ ```
43
+
40
44
  You can also enable bundler to prepend each command with `bundle exec` (note, that it won't be done for commands like `bundle install`).
41
45
 
42
46
  ```ruby
@@ -46,6 +50,32 @@ Vagrant.configure('2') do |config|
46
50
  end
47
51
  ```
48
52
 
53
+ ```shell
54
+ ➜ vagrant exec pwd
55
+ # is the same as
56
+ ➜ vagrant ssh -c "cd /vagrant && bundle exec pwd'
57
+
58
+ ➜ vagrant exec bundle install
59
+ # is the same as
60
+ ➜ vagrant ssh -c "cd /vagrant && bundle install'
61
+ ```
62
+
63
+ You can also add environment variables to be exported before.
64
+
65
+ ```ruby
66
+ Vagrant.configure('2') do |config|
67
+ config.vm.box = 'precise32'
68
+ config.exec.env['RAILS_ENV'] = 'test'
69
+ config.exec.env['RAILS_ROOT'] = '/vagrant'
70
+ end
71
+ ```
72
+
73
+ ```shell
74
+ ➜ vagrant exec pwd
75
+ # is the same as
76
+ ➜ vagrant ssh -c "cd /vagrant && export RAILS_ENV=test && export RAILS_ROOT=/vagrant && pwd'
77
+ ```
78
+
49
79
  Acceptance tests
50
80
  ----------------
51
81
 
@@ -56,6 +86,12 @@ Before running features, you'll need to bootstrap box.
56
86
  ➜ bundle exec rake features:run
57
87
  ```
58
88
 
89
+ After you're done, remove Vagrant box.
90
+
91
+ ```shell
92
+ ➜ bundle exec rake features:cleanup
93
+ ```
94
+
59
95
  Note on Patches/Pull Requests
60
96
  -----------------------------
61
97
 
data/Rakefile CHANGED
@@ -3,12 +3,18 @@ require 'cucumber/rake/task'
3
3
  Bundler::GemHelper.install_tasks
4
4
 
5
5
  namespace :features do
6
+ desc 'Downloads and adds vagrant box for testing.'
6
7
  task(:bootstrap) do
7
- # download and add box for acceptance tests
8
- system("bundle exec vagrant box add precise32 http://files.vagrantup.com/precise32.box")
8
+ system('bundle exec vagrant box add vagrant_exec http://files.vagrantup.com/precise32.box')
9
9
  end
10
10
 
11
11
  Cucumber::Rake::Task.new(:run) do |t|
12
12
  t.cucumber_opts = %w(--format pretty)
13
13
  end
14
+
15
+ desc 'Removes testing vagrant box .'
16
+ task(:cleanup) do
17
+ system('bundle exec vagrant destroy -f')
18
+ system('bundle exec vagrant box remove vagrant_exec virtualbox')
19
+ end
14
20
  end
@@ -3,12 +3,13 @@ Given(/^I have default Vagrantfile$/) do
3
3
  Vagrant.require_plugin 'vagrant-exec'
4
4
 
5
5
  Vagrant.configure('2') do |config|
6
- config.vm.box = 'precise32'
6
+ config.vm.box = 'vagrant_exec'
7
7
  end
8
8
  RUBY
9
9
  step 'a file named "Vagrantfile" with:', vagrantfile
10
10
  end
11
11
 
12
+
12
13
  Given(/^I set vagrant-exec folder to (.+)$/) do |folder|
13
14
  config = <<-RUBY
14
15
 
@@ -19,6 +20,7 @@ end
19
20
  step 'I append to "Vagrantfile" with:', config
20
21
  end
21
22
 
23
+
22
24
  Given(/^I set vagrant-exec bundler to (.+)$/) do |bundler|
23
25
  config = <<-RUBY
24
26
 
@@ -28,3 +30,20 @@ end
28
30
  RUBY
29
31
  step 'I append to "Vagrantfile" with:', config
30
32
  end
33
+
34
+
35
+ Given(/^I set vagrant-exec env with the following values:$/) do |table|
36
+ data = table.hashes
37
+ config = data.map do |hash|
38
+ key, value = "#{hash['key']}", "#{hash['value']}"
39
+ %(config.exec.env['#{key}'] = '#{value}')
40
+ end
41
+
42
+ config = <<-RUBY
43
+
44
+ Vagrant.configure('2') do |config|
45
+ #{config.join("\n\s\s")}
46
+ end
47
+ RUBY
48
+ step 'I append to "Vagrantfile" with:', config
49
+ end
@@ -1,4 +1,5 @@
1
1
  require 'aruba/cucumber'
2
+ ENV['VAGRANT_LOG'] = 'info'
2
3
 
3
4
  Before do
4
5
  # VM start takes a long time
@@ -6,7 +6,8 @@ Feature: vagrant-exec
6
6
  I want to use "vagrant exec" command
7
7
  And be able to customize folder
8
8
  And prepend commands with "bundle exec"
9
- Using Vagrantfile configuraiotn
9
+ And set exported environmental variables
10
+ Using Vagrantfile configuration
10
11
 
11
12
  Background:
12
13
  Given I have default Vagrantfile
@@ -15,14 +16,14 @@ Feature: vagrant-exec
15
16
  Given I run `bundle exec vagrant up`
16
17
  When I run `bundle exec vagrant exec pwd`
17
18
  Then the exit status should be 0
18
- And the output should contain "/vagrant"
19
+ And the output should contain "Executing single command on remote machine: cd /vagrant && pwd"
19
20
 
20
21
  Scenario: can use custom folder
21
22
  Given I set vagrant-exec folder to "/tmp"
22
23
  And I run `bundle exec vagrant up`
23
24
  When I run `bundle exec vagrant exec pwd`
24
25
  Then the exit status should be 0
25
- And the output should contain "/tmp"
26
+ And the output should contain "Executing single command on remote machine: cd /tmp && pwd"
26
27
 
27
28
  Scenario: raises error if folder is improperly set
28
29
  Given I set vagrant-exec folder to true
@@ -41,13 +42,13 @@ Feature: vagrant-exec
41
42
  And I run `bundle exec vagrant up`
42
43
  When I run `bundle exec vagrant exec pwd`
43
44
  Then the exit status should not be 0
44
- And the output should contain "bundle: command not found"
45
+ And the output should contain "Executing single command on remote machine: cd /vagrant && bundle exec pwd"
45
46
 
46
47
  Scenario: does not use bundler for bundle commands
47
48
  Given I set vagrant-exec bundler to true
48
49
  And I run `bundle exec vagrant up`
49
50
  When I run `bundle exec vagrant exec bundle install`
50
- Then the output should not contain "bundle exec bundle install"
51
+ Then the output should contain "Executing single command on remote machine: cd /vagrant && bundle install"
51
52
 
52
53
  Scenario: raises error if bundler is improperly set
53
54
  Given I set vagrant-exec bundler to "true"
@@ -55,7 +56,36 @@ Feature: vagrant-exec
55
56
  Then the exit status should not be 0
56
57
  And the output should contain "bundler should be boolean"
57
58
 
58
- Scenario: can use custom VM
59
+ Scenario: can export environment variables
60
+ Given I set vagrant-exec env with the following values:
61
+ | key | value |
62
+ | TEST1 | true |
63
+ | TEST2 | false |
64
+ And I run `bundle exec vagrant up`
65
+ When I run `bundle exec vagrant exec pwd`
66
+ Then the exit status should be 0
67
+ And the output should contain "Executing single command on remote machine: cd /vagrant && export TEST1=true && export TEST2=false && pwd"
68
+
69
+ Scenario Outline: shows help correctly
70
+ Given I run `bundle exec vagrant up`
71
+ When I run `bundle exec vagrant exec <args>`
72
+ Then the output should contain "Usage: vagrant exec [options] <command>"
73
+ Examples:
74
+ | args |
75
+ | |
76
+ | -h |
77
+ | --help |
78
+ | -h pwd |
79
+ | --help pwd -h |
80
+
81
+ Scenario Outline: passes command arguments correctly
59
82
  Given I run `bundle exec vagrant up`
60
- When I run `bundle exec vagrant exec --machine vm pwd`
61
- And the output should contain "machine with the name 'vm' was not found"
83
+ When I run `bundle exec vagrant exec <cmd>`
84
+ Then the output should contain "Executing single command on remote machine: cd /vagrant && <cmd>"
85
+ Examples:
86
+ | cmd |
87
+ | cwd . |
88
+ | cwd ~ |
89
+ | cwd -h |
90
+ | cwd --blah |
91
+ | cwd -h blah -v blah |
@@ -3,32 +3,45 @@ module VagrantPlugins
3
3
  class Command < Vagrant.plugin(2, :command)
4
4
 
5
5
  def execute
6
- options = {}
7
-
8
- opts = OptionParser.new do | o |
6
+ opts = OptionParser.new do |o|
9
7
  o.banner = 'Usage: vagrant exec [options] <command>'
10
8
  o.separator ''
11
9
 
12
- o.on('-m', '--machine VM', 'VM name to use.') do | vm |
13
- options[:machine] = vm
10
+ o.on('-h', '--help', 'Print this help') do
11
+ safe_puts(opts.help)
14
12
  end
15
13
  end
16
14
 
17
- # Parse the options
18
- argv = parse_options(opts)
19
- return unless argv
15
+ argv = split_main_and_subcommand(@argv.dup)
16
+ exec_args, cmd, cmd_args = argv[0], argv[1], argv[2]
17
+
18
+ # show help
19
+ if !cmd || exec_args.any? { |a| a == '-h' || a == '--help' }
20
+ safe_puts(opts.help)
21
+ return nil
22
+ end
20
23
 
21
24
  # Execute the actual SSH
22
- with_target_vms(options[:machine], single_target: true) do | vm |
25
+ with_target_vms(nil, single_target: true) do |vm|
23
26
  vm.config.exec.finalize! # TODO: do we have to call it explicitly?
24
27
 
25
- plain = argv.join(' ')
26
- command = "cd #{vm.config.exec.folder}; "
28
+ plain = "#{cmd} " << cmd_args.join(' ')
29
+ command = "cd #{vm.config.exec.folder} && "
30
+
31
+ env = vm.config.exec.env
32
+ if env.any?
33
+ env.each do |key, value|
34
+ command << "export #{key}=#{value} && "
35
+ end
36
+ end
37
+
27
38
  if vm.config.exec.bundler && !(plain =~ /^bundle /)
28
39
  command << 'bundle exec '
29
40
  end
41
+
30
42
  command << plain
31
- @logger.debug("Executing single command on remote machine: #{command}")
43
+
44
+ @logger.info("Executing single command on remote machine: #{command}")
32
45
  env = vm.action(:ssh_run, ssh_run_command: command)
33
46
 
34
47
  status = env[:ssh_run_exit_status] || 0
@@ -2,17 +2,19 @@ module VagrantPlugins
2
2
  module Exec
3
3
  class Config < Vagrant.plugin(2, :config)
4
4
 
5
- attr_accessor :folder
5
+ attr_reader :env
6
6
  attr_accessor :bundler
7
+ attr_accessor :folder
7
8
 
8
9
  def initialize
9
- @folder = UNSET_VALUE
10
+ @env = {}
10
11
  @bundler = UNSET_VALUE
12
+ @folder = UNSET_VALUE
11
13
  end
12
14
 
13
15
  def validate(_)
14
- return { 'exec' => ['folder should be a string'] } unless @folder.is_a?(String)
15
16
  return { 'exec' => ['bundler should be boolean'] } unless [true, false].include?(@bundler)
17
+ return { 'exec' => ['folder should be a string'] } unless @folder.is_a?(String)
16
18
 
17
19
  {}
18
20
  end
@@ -1,7 +1,7 @@
1
1
  module VagrantPlugins
2
2
  module Exec
3
3
 
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
 
6
6
  end # Exec
7
7
  end # VagrantPlugins
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-exec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-29 00:00:00.000000000 Z
12
+ date: 2013-06-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aruba
@@ -76,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
76
  version: '0'
77
77
  segments:
78
78
  - 0
79
- hash: 612892604228850569
79
+ hash: 1395843341620425617
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  version: '0'
86
86
  segments:
87
87
  - 0
88
- hash: 612892604228850569
88
+ hash: 1395843341620425617
89
89
  requirements: []
90
90
  rubyforge_project:
91
91
  rubygems_version: 1.8.23