vagrant-exec 0.3.1 → 0.4.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.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/LICENSE.md +1 -1
- data/README.md +53 -52
- data/Rakefile +1 -1
- data/features/vagrant-exec.feature +1 -142
- data/features/vagrant-exec/directory.feature +55 -0
- data/features/vagrant-exec/environment_variables.feature +65 -0
- data/features/vagrant-exec/prepend.feature +66 -0
- data/features/vagrant-exec/validations.feature +37 -0
- data/lib/vagrant-exec/command.rb +48 -22
- data/lib/vagrant-exec/config.rb +59 -21
- data/lib/vagrant-exec/version.rb +1 -1
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3a198b905f07055f13866e6362600566d379f33
|
4
|
+
data.tar.gz: 804b8f85d3c87f30ca0e82e53e3c2d60d91e023d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e6fe2bc7f19ecd536ab49942acbb7436ccebb674767e8e45aa02ec4f5fd9e2d1f37e35d12bd9075c4c4ef0cbe184109e34a7ccfde28ab04c9383acea8004ea4
|
7
|
+
data.tar.gz: e2dc1c0bd0f76c223fcb1b8af329b647d2db88d7828210d720c1225b6971ee500fe43fb45208cbc9b48aff7124553303c4ee6071f403653766c4a36eddcc5e45
|
data/.gitignore
ADDED
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -8,99 +8,100 @@ Description
|
|
8
8
|
|
9
9
|
You will probably use the plugin if you don't want to SSH into the box to execute commands simply because your machine environment is already configured (e.g. I use ZSH and TextMate bundles to run specs/features).
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
Installation
|
12
|
+
------------
|
13
13
|
|
14
14
|
```bash
|
15
|
-
➜ vagrant exec
|
16
|
-
/vagrant
|
15
|
+
➜ vagrant plugin install vagrant-exec
|
17
16
|
```
|
18
17
|
|
19
|
-
|
20
|
-
|
18
|
+
Example
|
19
|
+
-------
|
21
20
|
|
22
21
|
```bash
|
23
|
-
➜ vagrant
|
22
|
+
➜ vagrant exec pwd
|
23
|
+
/vagrant
|
24
24
|
```
|
25
25
|
|
26
26
|
Configuration
|
27
27
|
-------------
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
The root directory can be configured using Vagrantfile.
|
29
|
+
vagrant-exec has only one configuration option for Vagrantfile, which allows you to alter the behavior of all or specific commands.
|
32
30
|
|
33
31
|
```ruby
|
34
32
|
Vagrant.configure('2') do |config|
|
35
33
|
config.vm.box = 'precise32'
|
36
|
-
config.exec.
|
34
|
+
config.exec.commands '*', directory: '/tmp'
|
37
35
|
end
|
38
36
|
```
|
39
37
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
Commands can either be:
|
39
|
+
|
40
|
+
* `"*"` (wildcard) - apply options to all the commands
|
41
|
+
* `"command"` (string) - apply options for specific commands
|
42
|
+
* `%w(command1 command2)` (array) - apply options to all commands in array
|
45
43
|
|
46
|
-
|
44
|
+
Configuration options are merged, so if you specify single command in several places, all the option will be applied. The only exception is `:directory`, which is applied only once and in reverse order (i.e. the last set is used).
|
47
45
|
|
48
|
-
|
46
|
+
### Directory
|
49
47
|
|
50
48
|
```ruby
|
51
49
|
Vagrant.configure('2') do |config|
|
52
50
|
config.vm.box = 'precise32'
|
53
|
-
config.exec.prepend_with 'bundle exec'
|
54
|
-
end
|
55
|
-
```
|
56
51
|
|
57
|
-
|
58
|
-
➜ vagrant exec pwd
|
59
|
-
# is the same as
|
60
|
-
➜ vagrant ssh -c "cd /
|
52
|
+
# Make /tmp working directory for all the commands:
|
53
|
+
# ➜ vagrant exec pwd
|
54
|
+
# # is the same as
|
55
|
+
# ➜ vagrant ssh -c "cd /tmp && pwd"
|
56
|
+
config.exec.commands '*', directory: '/tmp'
|
57
|
+
|
58
|
+
# Make /etc working directory for env command:
|
59
|
+
# ➜ vagrant exec env
|
60
|
+
# # is the same as
|
61
|
+
# ➜ vagrant ssh -c "cd /etc && env"
|
62
|
+
config.exec.commands '*', directory: '/etc'
|
63
|
+
end
|
61
64
|
```
|
62
65
|
|
63
|
-
|
66
|
+
### Prepend
|
64
67
|
|
65
68
|
```ruby
|
66
69
|
Vagrant.configure('2') do |config|
|
67
70
|
config.vm.box = 'precise32'
|
68
|
-
config.exec.prepend_with 'bundle exec', :only => %w(rails rspec cucumber)
|
69
|
-
config.exec.prepend_with 'rvmsudo', :only => %w(gem)
|
70
|
-
end
|
71
|
-
```
|
72
|
-
|
73
|
-
```bash
|
74
|
-
➜ vagrant exec rails c
|
75
|
-
# is the same as
|
76
|
-
➜ vagrant ssh -c "cd /vagrant && bundle exec rails c"
|
77
|
-
```
|
78
71
|
|
79
|
-
|
80
|
-
➜ vagrant exec
|
81
|
-
# is the same as
|
82
|
-
➜ vagrant ssh -c "cd /vagrant &&
|
72
|
+
# Automatically prepend apt-get command with sudo:
|
73
|
+
# ➜ vagrant exec apt-get install htop
|
74
|
+
# # is the same as
|
75
|
+
# ➜ vagrant ssh -c "cd /vagrant && sudo apt-get install htop"
|
76
|
+
config.exec.commands 'apt-get', prepend: 'sudo'
|
77
|
+
|
78
|
+
# Automatically prepend rails and rspec commands with bundle exec:
|
79
|
+
# ➜ vagrant exec rails c
|
80
|
+
# # is the same as
|
81
|
+
# ➜ vagrant ssh -c "cd /vagrant && bundle exec rails c"
|
82
|
+
#
|
83
|
+
# ➜ vagrant exec rspec spec/
|
84
|
+
# # is the same as
|
85
|
+
# ➜ vagrant ssh -c "cd /vagrant && bundle exec rspec spec/"
|
86
|
+
config.exec.commands %w(rails rspec), prepend: 'bundle exec'
|
87
|
+
end
|
83
88
|
```
|
84
89
|
|
85
90
|
### Environment variables
|
86
91
|
|
87
|
-
You can add environment variables to be exported before.
|
88
|
-
|
89
92
|
```ruby
|
90
93
|
Vagrant.configure('2') do |config|
|
91
94
|
config.vm.box = 'precise32'
|
92
|
-
config.exec.env['RAILS_ENV'] = 'test'
|
93
|
-
config.exec.env['RAILS_ROOT'] = '/vagrant'
|
94
|
-
end
|
95
|
-
```
|
96
95
|
|
97
|
-
|
98
|
-
➜ vagrant exec
|
99
|
-
# is the same as
|
100
|
-
➜ vagrant ssh -c "cd /vagrant && export
|
96
|
+
# Automatically export environment variables for ruby command:
|
97
|
+
# ➜ vagrant exec ruby -e 'puts 1'
|
98
|
+
# # is the same as
|
99
|
+
# ➜ vagrant ssh -c "cd /vagrant && export RUBY_GC_MALLOC_LIMIT=100000000 && ruby -e 'puts 1'"
|
100
|
+
config.exec.commands 'ruby', env: { 'RUBY_GC_MALLOC_LIMIT' => 100000000 }
|
101
|
+
end
|
101
102
|
```
|
102
103
|
|
103
|
-
|
104
|
+
Testing
|
104
105
|
----------------
|
105
106
|
|
106
107
|
Before running features, you'll need to bootstrap box.
|
@@ -138,4 +139,4 @@ Note on Patches/Pull Requests
|
|
138
139
|
Copyright
|
139
140
|
---------
|
140
141
|
|
141
|
-
Copyright (c) 2013-
|
142
|
+
Copyright (c) 2013-2014 Alex Rodionov. See LICENSE.md for details.
|
data/Rakefile
CHANGED
@@ -1,13 +1,9 @@
|
|
1
1
|
@no-clobber
|
2
2
|
Feature: vagrant-exec
|
3
3
|
In order to execute commands in Vagrant box
|
4
|
-
Within context of
|
4
|
+
Within context of synced folder
|
5
5
|
As a developer using vagrant-exec plugin
|
6
6
|
I want to use "vagrant exec" command
|
7
|
-
And be able to customize folder
|
8
|
-
And prepend commands with "bundle exec"
|
9
|
-
And set exported environmental variables
|
10
|
-
Using Vagrantfile configuration
|
11
7
|
|
12
8
|
Background:
|
13
9
|
Given I write to "Vagrantfile" with:
|
@@ -43,140 +39,3 @@ Feature: vagrant-exec
|
|
43
39
|
| cwd -h |
|
44
40
|
| cwd --blah |
|
45
41
|
| "cwd -h blah -v blah" |
|
46
|
-
|
47
|
-
Scenario: uses /vagrant as default root
|
48
|
-
Given I run `bundle exec vagrant up`
|
49
|
-
When I run `bundle exec vagrant exec pwd`
|
50
|
-
Then the exit status should be 0
|
51
|
-
And SHH subprocess should execute command "cd /vagrant && pwd"
|
52
|
-
|
53
|
-
Scenario: can use custom root
|
54
|
-
Given I overwrite "Vagrantfile" with:
|
55
|
-
"""
|
56
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
57
|
-
require 'vagrant-exec'
|
58
|
-
|
59
|
-
Vagrant.configure('2') do |config|
|
60
|
-
config.vm.box = 'vagrant_exec'
|
61
|
-
config.exec.root = '/tmp'
|
62
|
-
end
|
63
|
-
"""
|
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 SHH subprocess should execute command "cd /tmp && pwd"
|
68
|
-
|
69
|
-
Scenario: raises error if root is improperly set
|
70
|
-
Given I overwrite "Vagrantfile" with:
|
71
|
-
"""
|
72
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
73
|
-
require 'vagrant-exec'
|
74
|
-
|
75
|
-
Vagrant.configure('2') do |config|
|
76
|
-
config.vm.box = 'vagrant_exec'
|
77
|
-
config.exec.root = true
|
78
|
-
end
|
79
|
-
"""
|
80
|
-
And I run `bundle exec vagrant up`
|
81
|
-
Then the exit status should not be 0
|
82
|
-
And the output should contain "root should be a string"
|
83
|
-
|
84
|
-
Scenario: can prepend all commands
|
85
|
-
Given I overwrite "Vagrantfile" with:
|
86
|
-
"""
|
87
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
88
|
-
require 'vagrant-exec'
|
89
|
-
|
90
|
-
Vagrant.configure('2') do |config|
|
91
|
-
config.vm.box = 'vagrant_exec'
|
92
|
-
config.exec.prepend_with 'echo vagrant-exec &&'
|
93
|
-
end
|
94
|
-
"""
|
95
|
-
And I run `bundle exec vagrant up`
|
96
|
-
When I run `bundle exec vagrant exec pwd`
|
97
|
-
Then the exit status should be 0
|
98
|
-
And SHH subprocess should execute command "cd /vagrant && echo vagrant-exec && pwd"
|
99
|
-
|
100
|
-
Scenario: can prepend only specific commands
|
101
|
-
Given I overwrite "Vagrantfile" with:
|
102
|
-
"""
|
103
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
104
|
-
require 'vagrant-exec'
|
105
|
-
|
106
|
-
Vagrant.configure('2') do |config|
|
107
|
-
config.vm.box = 'vagrant_exec'
|
108
|
-
config.exec.prepend_with 'echo vagrant-exec &&', :only => %w(pwd echo)
|
109
|
-
end
|
110
|
-
"""
|
111
|
-
And I run `bundle exec vagrant up`
|
112
|
-
When I run `bundle exec vagrant exec pwd`
|
113
|
-
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec && pwd"
|
114
|
-
When I run `bundle exec vagrant exec echo 1`
|
115
|
-
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec && echo 1"
|
116
|
-
When I run `bundle exec vagrant exec env`
|
117
|
-
Then SHH subprocess should execute command "cd /vagrant && env"
|
118
|
-
|
119
|
-
Scenario: can use prepend multiple times
|
120
|
-
Given I overwrite "Vagrantfile" with:
|
121
|
-
"""
|
122
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
123
|
-
require 'vagrant-exec'
|
124
|
-
|
125
|
-
Vagrant.configure('2') do |config|
|
126
|
-
config.vm.box = 'vagrant_exec'
|
127
|
-
config.exec.prepend_with 'echo vagrant-exec1 &&', :only => %w(pwd)
|
128
|
-
config.exec.prepend_with 'echo vagrant-exec2 &&', :only => %w(echo)
|
129
|
-
end
|
130
|
-
"""
|
131
|
-
And I run `bundle exec vagrant up`
|
132
|
-
When I run `bundle exec vagrant exec pwd`
|
133
|
-
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec1 && pwd"
|
134
|
-
When I run `bundle exec vagrant exec echo 1`
|
135
|
-
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec2 && echo 1"
|
136
|
-
|
137
|
-
Scenario: raises error if prepend command is improperly set
|
138
|
-
Given I overwrite "Vagrantfile" with:
|
139
|
-
"""
|
140
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
141
|
-
require 'vagrant-exec'
|
142
|
-
|
143
|
-
Vagrant.configure('2') do |config|
|
144
|
-
config.vm.box = 'vagrant_exec'
|
145
|
-
config.exec.prepend_with :test
|
146
|
-
end
|
147
|
-
"""
|
148
|
-
When I run `bundle exec vagrant up`
|
149
|
-
Then the exit status should not be 0
|
150
|
-
And the output should contain "prepend_with command should be a string"
|
151
|
-
|
152
|
-
Scenario: raises error if prepend only is improperly set
|
153
|
-
Given I overwrite "Vagrantfile" with:
|
154
|
-
"""
|
155
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
156
|
-
require 'vagrant-exec'
|
157
|
-
|
158
|
-
Vagrant.configure('2') do |config|
|
159
|
-
config.vm.box = 'vagrant_exec'
|
160
|
-
config.exec.prepend_with 'echo vagrant-exec1 &&', :only => 'test'
|
161
|
-
end
|
162
|
-
"""
|
163
|
-
And I run `bundle exec vagrant up`
|
164
|
-
Then the exit status should not be 0
|
165
|
-
And the output should contain "prepend_with :only should be an array"
|
166
|
-
|
167
|
-
Scenario: can export environment variables
|
168
|
-
Given I overwrite "Vagrantfile" with:
|
169
|
-
"""
|
170
|
-
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
171
|
-
require 'vagrant-exec'
|
172
|
-
|
173
|
-
Vagrant.configure('2') do |config|
|
174
|
-
config.vm.box = 'vagrant_exec'
|
175
|
-
config.exec.env['TEST1'] = true
|
176
|
-
config.exec.env['TEST2'] = false
|
177
|
-
end
|
178
|
-
"""
|
179
|
-
And I run `bundle exec vagrant up`
|
180
|
-
When I run `bundle exec vagrant exec pwd`
|
181
|
-
Then the exit status should be 0
|
182
|
-
And SHH subprocess should execute command "cd /vagrant && export TEST1=true && export TEST2=false && pwd"
|
@@ -0,0 +1,55 @@
|
|
1
|
+
@no-clobber
|
2
|
+
Feature: vagrant-exec directory
|
3
|
+
In order to change the working directory
|
4
|
+
For commands I execute using vagrant-exec
|
5
|
+
As a user
|
6
|
+
I should be able to specify it in Vagrantfile
|
7
|
+
|
8
|
+
Scenario: uses /vagrant as default directory
|
9
|
+
Given I write to "Vagrantfile" with:
|
10
|
+
"""
|
11
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
12
|
+
require 'vagrant-exec'
|
13
|
+
|
14
|
+
Vagrant.configure('2') do |config|
|
15
|
+
config.vm.box = 'vagrant_exec'
|
16
|
+
end
|
17
|
+
"""
|
18
|
+
Given I run `bundle exec vagrant up`
|
19
|
+
When I run `bundle exec vagrant exec pwd`
|
20
|
+
Then the exit status should be 0
|
21
|
+
And SHH subprocess should execute command "cd /vagrant && pwd"
|
22
|
+
|
23
|
+
Scenario: can use custom directory for all commands
|
24
|
+
Given I write to "Vagrantfile" with:
|
25
|
+
"""
|
26
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
27
|
+
require 'vagrant-exec'
|
28
|
+
|
29
|
+
Vagrant.configure('2') do |config|
|
30
|
+
config.vm.box = 'vagrant_exec'
|
31
|
+
config.exec.commands '*', directory: '/tmp'
|
32
|
+
end
|
33
|
+
"""
|
34
|
+
And I run `bundle exec vagrant up`
|
35
|
+
When I run `bundle exec vagrant exec pwd`
|
36
|
+
Then the exit status should be 0
|
37
|
+
And SHH subprocess should execute command "cd /tmp && pwd"
|
38
|
+
|
39
|
+
Scenario: can use custom directory for specific commands
|
40
|
+
Given I write to "Vagrantfile" with:
|
41
|
+
"""
|
42
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
43
|
+
require 'vagrant-exec'
|
44
|
+
|
45
|
+
Vagrant.configure('2') do |config|
|
46
|
+
config.vm.box = 'vagrant_exec'
|
47
|
+
config.exec.commands %w(pwd echo), directory: '/tmp'
|
48
|
+
end
|
49
|
+
"""
|
50
|
+
And I run `bundle exec vagrant up`
|
51
|
+
When I run `bundle exec vagrant exec echo 1`
|
52
|
+
Then SHH subprocess should execute command "cd /tmp && echo 1"
|
53
|
+
When I run `bundle exec vagrant exec env`
|
54
|
+
Then SHH subprocess should execute command "cd /vagrant && env"
|
55
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
@no-clobber
|
2
|
+
Feature: vagrant-exec environment variables
|
3
|
+
In order to automatically set environment variables
|
4
|
+
For commands I execute using vagrant-exec
|
5
|
+
As a user
|
6
|
+
I should be able to specify them in Vagrantfile
|
7
|
+
|
8
|
+
Scenario: can export environment variables for all commands
|
9
|
+
Given I write to "Vagrantfile" with:
|
10
|
+
"""
|
11
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
12
|
+
require 'vagrant-exec'
|
13
|
+
|
14
|
+
Vagrant.configure('2') do |config|
|
15
|
+
config.vm.box = 'vagrant_exec'
|
16
|
+
config.exec.commands '*', env: { 'TEST1' => true, 'TEST2' => false }
|
17
|
+
end
|
18
|
+
"""
|
19
|
+
And I run `bundle exec vagrant up`
|
20
|
+
When I run `bundle exec vagrant exec pwd`
|
21
|
+
Then the exit status should be 0
|
22
|
+
And SHH subprocess should execute command "cd /vagrant && export TEST1=true && export TEST2=false && pwd"
|
23
|
+
|
24
|
+
Scenario: can export environment variables for specific commands
|
25
|
+
Given I write to "Vagrantfile" with:
|
26
|
+
"""
|
27
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
28
|
+
require 'vagrant-exec'
|
29
|
+
|
30
|
+
Vagrant.configure('2') do |config|
|
31
|
+
config.vm.box = 'vagrant_exec'
|
32
|
+
config.exec.commands 'cmd', env: { 'TEST1' => 'yo' }
|
33
|
+
config.exec.commands %w(pwd echo), env: { 'TEST2' => true, 'TEST3' => false }
|
34
|
+
end
|
35
|
+
"""
|
36
|
+
And I run `bundle exec vagrant up`
|
37
|
+
When I run `bundle exec vagrant exec cmd`
|
38
|
+
Then SHH subprocess should execute command "cd /vagrant && export TEST1=yo && cmd"
|
39
|
+
When I run `bundle exec vagrant exec pwd`
|
40
|
+
Then SHH subprocess should execute command "cd /vagrant && export TEST2=true && export TEST3=false && pwd"
|
41
|
+
When I run `bundle exec vagrant exec echo 1`
|
42
|
+
Then SHH subprocess should execute command "cd /vagrant && export TEST2=true && export TEST3=false && echo 1"
|
43
|
+
When I run `bundle exec vagrant exec env`
|
44
|
+
Then SHH subprocess should execute command "cd /vagrant && env"
|
45
|
+
|
46
|
+
Scenario: can combine environment variables
|
47
|
+
Given I write to "Vagrantfile" with:
|
48
|
+
"""
|
49
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
50
|
+
require 'vagrant-exec'
|
51
|
+
|
52
|
+
Vagrant.configure('2') do |config|
|
53
|
+
config.vm.box = 'vagrant_exec'
|
54
|
+
config.exec.commands '*', env: { 'TEST1' => true }
|
55
|
+
config.exec.commands 'pwd', env: { 'TEST2' => false }
|
56
|
+
config.exec.commands %w(pwd echo), env: { 'TEST3' => false }
|
57
|
+
end
|
58
|
+
"""
|
59
|
+
And I run `bundle exec vagrant up`
|
60
|
+
When I run `bundle exec vagrant exec pwd`
|
61
|
+
Then SHH subprocess should execute command "cd /vagrant && export TEST1=true && export TEST2=false && export TEST3=false && pwd"
|
62
|
+
When I run `bundle exec vagrant exec echo 1`
|
63
|
+
Then SHH subprocess should execute command "cd /vagrant && export TEST1=true && export TEST3=false && echo 1"
|
64
|
+
When I run `bundle exec vagrant exec env`
|
65
|
+
Then SHH subprocess should execute command "cd /vagrant && export TEST1=true && env"
|
@@ -0,0 +1,66 @@
|
|
1
|
+
@no-clobber
|
2
|
+
Feature: vagrant-exec prepend
|
3
|
+
In order to automatically prepend with custom command
|
4
|
+
Commands I execute using vagrant-exec
|
5
|
+
As a user
|
6
|
+
I should be able to specify it in Vagrantfile
|
7
|
+
|
8
|
+
Scenario: can prepend all commands
|
9
|
+
Given I write to "Vagrantfile" with:
|
10
|
+
"""
|
11
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
12
|
+
require 'vagrant-exec'
|
13
|
+
|
14
|
+
Vagrant.configure('2') do |config|
|
15
|
+
config.vm.box = 'vagrant_exec'
|
16
|
+
config.exec.commands '*', prepend: 'echo vagrant-exec &&'
|
17
|
+
end
|
18
|
+
"""
|
19
|
+
And I run `bundle exec vagrant up`
|
20
|
+
When I run `bundle exec vagrant exec pwd`
|
21
|
+
Then the exit status should be 0
|
22
|
+
And SHH subprocess should execute command "cd /vagrant && echo vagrant-exec && pwd"
|
23
|
+
|
24
|
+
Scenario: can prepend specific commands
|
25
|
+
Given I write to "Vagrantfile" with:
|
26
|
+
"""
|
27
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
28
|
+
require 'vagrant-exec'
|
29
|
+
|
30
|
+
Vagrant.configure('2') do |config|
|
31
|
+
config.vm.box = 'vagrant_exec'
|
32
|
+
config.exec.commands 'cmd', prepend: 'echo vagrant-exec1 &&'
|
33
|
+
config.exec.commands %w(pwd echo), prepend: 'echo vagrant-exec2 &&'
|
34
|
+
end
|
35
|
+
"""
|
36
|
+
And I run `bundle exec vagrant up`
|
37
|
+
When I run `bundle exec vagrant exec cmd`
|
38
|
+
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec1 && cmd"
|
39
|
+
When I run `bundle exec vagrant exec pwd`
|
40
|
+
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec2 && pwd"
|
41
|
+
When I run `bundle exec vagrant exec echo 1`
|
42
|
+
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec2 && echo 1"
|
43
|
+
When I run `bundle exec vagrant exec env`
|
44
|
+
Then SHH subprocess should execute command "cd /vagrant && env"
|
45
|
+
|
46
|
+
Scenario: can combine prepended
|
47
|
+
Given I write to "Vagrantfile" with:
|
48
|
+
"""
|
49
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
50
|
+
require 'vagrant-exec'
|
51
|
+
|
52
|
+
Vagrant.configure('2') do |config|
|
53
|
+
config.vm.box = 'vagrant_exec'
|
54
|
+
config.exec.commands '*', prepend: 'echo vagrant-exec1 &&'
|
55
|
+
config.exec.commands 'pwd', prepend: 'echo vagrant-exec2 &&'
|
56
|
+
config.exec.commands %w(pwd echo), prepend: 'echo vagrant-exec3 &&'
|
57
|
+
end
|
58
|
+
"""
|
59
|
+
And I run `bundle exec vagrant up`
|
60
|
+
When I run `bundle exec vagrant exec pwd`
|
61
|
+
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec1 && echo vagrant-exec2 && echo vagrant-exec3 && pwd"
|
62
|
+
When I run `bundle exec vagrant exec echo 1`
|
63
|
+
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec1 && echo vagrant-exec3 && echo 1"
|
64
|
+
When I run `bundle exec vagrant exec env`
|
65
|
+
Then SHH subprocess should execute command "cd /vagrant && echo vagrant-exec1 && env"
|
66
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
@no-clobber
|
2
|
+
Feature: vagrant-exec validations
|
3
|
+
In order to avoid configuration mistakes for vagrant-exec commands
|
4
|
+
As a user
|
5
|
+
I should see proper validation errors
|
6
|
+
|
7
|
+
Background:
|
8
|
+
Given I write to "Vagrantfile" with:
|
9
|
+
"""
|
10
|
+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
11
|
+
require 'vagrant-exec'
|
12
|
+
|
13
|
+
Vagrant.configure('2') do |config|
|
14
|
+
config.vm.box = 'vagrant_exec'
|
15
|
+
config.exec.commands true, directory: nil, prepend: true, env: 0
|
16
|
+
end
|
17
|
+
"""
|
18
|
+
|
19
|
+
Scenario: raises error if command is not string or array of strings
|
20
|
+
When I run `bundle exec vagrant up`
|
21
|
+
Then the exit status should not be 0
|
22
|
+
And the output should contain "Commands should be String or Array<String>, received: true"
|
23
|
+
|
24
|
+
Scenario: raises error if directory is improperly set
|
25
|
+
When I run `bundle exec vagrant up`
|
26
|
+
Then the exit status should not be 0
|
27
|
+
And the output should contain ":directory should be String, received: nil"
|
28
|
+
|
29
|
+
Scenario: raises error if prepend is improperly set
|
30
|
+
When I run `bundle exec vagrant up`
|
31
|
+
Then the exit status should not be 0
|
32
|
+
And the output should contain ":prepend should be String, received: true"
|
33
|
+
|
34
|
+
Scenario: raises error if environment variables are improperly set
|
35
|
+
When I run `bundle exec vagrant up`
|
36
|
+
Then the exit status should not be 0
|
37
|
+
And the output should contain ":env should be Hash, received: 0"
|
data/lib/vagrant-exec/command.rb
CHANGED
@@ -3,7 +3,7 @@ module VagrantPlugins
|
|
3
3
|
class Command < Vagrant.plugin(2, :command)
|
4
4
|
|
5
5
|
def self.synopsis
|
6
|
-
'
|
6
|
+
'executes commands in virtual machine'
|
7
7
|
end
|
8
8
|
|
9
9
|
def execute
|
@@ -12,19 +12,34 @@ module VagrantPlugins
|
|
12
12
|
|
13
13
|
# Execute the actual SSH
|
14
14
|
with_target_vms(nil, single_target: true) do |vm|
|
15
|
-
vm.config.exec.
|
15
|
+
settings = vm.config.exec._parsed_commands
|
16
|
+
passed_command, constructed_command = cmd.dup, ''
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
# directory is applied only once
|
19
|
+
settings.reverse.each do |command|
|
20
|
+
cmd, opts = command[:cmd], command[:opts]
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
if command_matches?(cmd, passed_command) && !directory_added?
|
23
|
+
constructed_command << add_directory(opts[:directory])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# apply options
|
28
|
+
settings.each do |command|
|
29
|
+
cmd, opts = command[:cmd], command[:opts]
|
30
|
+
|
31
|
+
if command_matches?(cmd, passed_command)
|
32
|
+
constructed_command << add_env(opts[:env])
|
33
|
+
constructed_command << add_prepend(opts[:prepend])
|
34
|
+
end
|
35
|
+
end
|
24
36
|
|
25
|
-
|
37
|
+
constructed_command << passed_command
|
38
|
+
constructed_command << ' ' << cmd_args.join(' ') if cmd_args.any?
|
39
|
+
|
40
|
+
@logger.info("Executing single command on remote machine: #{constructed_command}")
|
26
41
|
ssh_opts = { extra_args: ['-q'] } # make it quiet
|
27
|
-
env = vm.action(:ssh_run, ssh_run_command:
|
42
|
+
env = vm.action(:ssh_run, ssh_run_command: constructed_command, ssh_opts: ssh_opts)
|
28
43
|
|
29
44
|
status = env[:ssh_run_exit_status] || 0
|
30
45
|
return status
|
@@ -59,26 +74,37 @@ module VagrantPlugins
|
|
59
74
|
return cmd, cmd_args
|
60
75
|
end
|
61
76
|
|
77
|
+
def add_directory(directory)
|
78
|
+
''.tap do |str|
|
79
|
+
if directory
|
80
|
+
str << "cd #{directory} && "
|
81
|
+
@directory_added = true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
62
86
|
def add_env(env)
|
63
|
-
''.tap do |
|
87
|
+
''.tap do |str|
|
64
88
|
env.each do |key, value|
|
65
|
-
|
66
|
-
end if env
|
89
|
+
str << "export #{key}=#{value} && "
|
90
|
+
end if env
|
67
91
|
end
|
68
92
|
end
|
69
93
|
|
70
|
-
def
|
71
|
-
|
72
|
-
|
73
|
-
prepends.each do |prep|
|
74
|
-
if !prep[:only] || prep[:only].include?(bin)
|
75
|
-
prep = prep[:command].strip # remove trailing space
|
76
|
-
cmd << "#{prep} "
|
77
|
-
end
|
78
|
-
end if prepends.any?
|
94
|
+
def add_prepend(prep)
|
95
|
+
''.tap do |str|
|
96
|
+
str << "#{prep.strip} " if prep
|
79
97
|
end
|
80
98
|
end
|
81
99
|
|
100
|
+
def command_matches?(expected, actual)
|
101
|
+
expected =='*' || expected == actual || expected.include?(actual)
|
102
|
+
end
|
103
|
+
|
104
|
+
def directory_added?
|
105
|
+
!!@directory_added
|
106
|
+
end
|
107
|
+
|
82
108
|
end # Command
|
83
109
|
end # Exec
|
84
110
|
end # VagrantPlugins
|
data/lib/vagrant-exec/config.rb
CHANGED
@@ -2,41 +2,79 @@ module VagrantPlugins
|
|
2
2
|
module Exec
|
3
3
|
class Config < Vagrant.plugin(2, :config)
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
DEFAULT_SETTINGS = {
|
6
|
+
cmd: '*',
|
7
|
+
opts: {
|
8
|
+
directory: '/vagrant'
|
9
|
+
}
|
10
|
+
}.freeze
|
7
11
|
|
8
12
|
def initialize
|
9
|
-
@
|
10
|
-
@prepend_with = UNSET_VALUE
|
11
|
-
@root = UNSET_VALUE
|
13
|
+
@commands = UNSET_VALUE
|
12
14
|
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
#
|
17
|
+
# Configures commands.
|
18
|
+
#
|
19
|
+
# @param cmd [String, Array<String>]
|
20
|
+
# @param opts [Hash]
|
21
|
+
# @option opts [String] :directory Directory to execute commands in
|
22
|
+
# @option opts [String] :prepend Command to prepend with
|
23
|
+
# @option opts [Hash] :env Environmental variables to export
|
24
|
+
#
|
25
|
+
def commands(cmd, opts = {})
|
26
|
+
@commands = [] if @commands == UNSET_VALUE
|
27
|
+
@commands << { cmd: cmd, opts: opts }
|
21
28
|
end
|
22
29
|
|
23
30
|
def validate(_)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
finalize!
|
32
|
+
errors = _detected_errors
|
33
|
+
|
34
|
+
@commands.each do |command|
|
35
|
+
cmd, opts = command[:cmd], command[:opts]
|
36
|
+
|
37
|
+
if !cmd.is_a?(String) && !array_of_strings?(cmd)
|
38
|
+
errors << "Commands should be String or Array<String>, received: #{cmd.inspect}"
|
28
39
|
end
|
29
|
-
|
30
|
-
|
40
|
+
|
41
|
+
if opts.has_key?(:directory) && !opts[:directory].is_a?(String)
|
42
|
+
errors << ":directory should be String, received: #{opts[:directory].inspect}"
|
43
|
+
end
|
44
|
+
|
45
|
+
if opts.has_key?(:prepend) && !opts[:prepend].is_a?(String)
|
46
|
+
errors << ":prepend should be String, received: #{opts[:prepend].inspect}"
|
47
|
+
end
|
48
|
+
|
49
|
+
if opts.has_key?(:env) && !opts[:env].is_a?(Hash)
|
50
|
+
errors << ":env should be Hash, received: #{opts[:env].inspect}"
|
31
51
|
end
|
32
52
|
end
|
33
53
|
|
34
|
-
{}
|
54
|
+
{ 'exec' => errors }
|
35
55
|
end
|
36
56
|
|
37
57
|
def finalize!
|
38
|
-
|
39
|
-
|
58
|
+
if @commands == UNSET_VALUE
|
59
|
+
@commands = [DEFAULT_SETTINGS.dup]
|
60
|
+
else
|
61
|
+
# add default settings and merge options for splat
|
62
|
+
splats, commands = @commands.partition { |command| command[:cmd] == '*' }
|
63
|
+
commands.unshift(DEFAULT_SETTINGS.dup)
|
64
|
+
splats.each { |splat| commands.first[:opts].merge!(splat[:opts]) }
|
65
|
+
@commands = commands
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# @api private
|
70
|
+
def _parsed_commands
|
71
|
+
@commands
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def array_of_strings?(array)
|
77
|
+
array.is_a?(Array) && array.all? { |i| i.is_a?(String) }
|
40
78
|
end
|
41
79
|
|
42
80
|
end # Config
|
data/lib/vagrant-exec/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-exec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rodionov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aruba
|
@@ -44,6 +44,7 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- ".gitignore"
|
47
48
|
- Gemfile
|
48
49
|
- LICENSE.md
|
49
50
|
- README.md
|
@@ -51,6 +52,10 @@ files:
|
|
51
52
|
- features/step_definitions/steps.rb
|
52
53
|
- features/support/env.rb
|
53
54
|
- features/vagrant-exec.feature
|
55
|
+
- features/vagrant-exec/directory.feature
|
56
|
+
- features/vagrant-exec/environment_variables.feature
|
57
|
+
- features/vagrant-exec/prepend.feature
|
58
|
+
- features/vagrant-exec/validations.feature
|
54
59
|
- lib/vagrant-exec.rb
|
55
60
|
- lib/vagrant-exec/command.rb
|
56
61
|
- lib/vagrant-exec/config.rb
|
@@ -85,3 +90,7 @@ test_files:
|
|
85
90
|
- features/step_definitions/steps.rb
|
86
91
|
- features/support/env.rb
|
87
92
|
- features/vagrant-exec.feature
|
93
|
+
- features/vagrant-exec/directory.feature
|
94
|
+
- features/vagrant-exec/environment_variables.feature
|
95
|
+
- features/vagrant-exec/prepend.feature
|
96
|
+
- features/vagrant-exec/validations.feature
|