• Stars
    star
    140
  • Rank 252,898 (Top 6 %)
  • Language Gherkin
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Execute commands in Vagrant synced folder

vagrant-exec Gem Version

Vagrant plugin to execute commands within the context of VM synced directory.

Description

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).

Installation

➜ vagrant plugin install vagrant-exec

Example

➜ vagrant exec pwd
/vagrant

Configuration

vagrant-exec has only one configuration option for Vagrantfile, which allows you to alter the behavior of all or specific commands.

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'
  config.exec.commands '*', directory: '/tmp'
end

Commands can either be:

  • "*" (wildcard) - apply options to all the commands
  • "command" (string) - apply options for specific commands
  • %w(command1 command2) (array) - apply options to all commands in array

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).

Directory

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'

  # Make /tmp working directory for all the commands:
  #   ➜ vagrant exec pwd
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /tmp && pwd"
  config.exec.commands '*', directory: '/tmp'

  # Make /etc working directory for env command:
  #   ➜ vagrant exec env
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /etc && env"
  config.exec.commands 'env', directory: '/etc'
end

Prepend

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'

  # Automatically prepend apt-get command with sudo:
  #   ➜ vagrant exec apt-get install htop
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && sudo apt-get install htop"
  config.exec.commands 'apt-get', prepend: 'sudo'

  # Automatically prepend rails and rspec commands with bundle exec:
  #   ➜ vagrant exec rails c
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && bundle exec rails c"
  #
  #   ➜ vagrant exec rspec spec/
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && bundle exec rspec spec/"
  config.exec.commands %w[rails rspec], prepend: 'bundle exec'
end

Environment variables

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'

  # Automatically export environment variables for ruby command:
  #   ➜ vagrant exec ruby -e 'puts 1'
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && export RUBY_GC_MALLOC_LIMIT=100000000 && ruby -e 'puts 1'"
  config.exec.commands 'ruby', env: {'RUBY_GC_MALLOC_LIMIT' => 100000000}
end

Binstubs

It is possible to generate binstubs for all your configured commands. You might want to do this to avoid typing vagrant exec every time before command, or if you want integrate your flow in editor (e.g. running tests from editor).

Assuming you have the following configuration:

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'
  config.exec.commands 'bundle'
  config.exec.commands %w[rails rake], prepend: 'bundle exec'
  config.exec.commands %w[rspec cucumber], prepend: 'spring'
end

You can generate binstubs for each command:

➜ vagrant exec --binstubs
Generated binstub for bundle in bin/bundle.
Generated binstub for cucumber in bin/cucumber.
Generated binstub for rails in bin/rails.
Generated binstub for rake in bin/rake.
Generated binstub for rspec in bin/rspec.

Now you can use bin/cucumber instead of vagrant exec cucumber to execute commands in VM. All your configuration (directory, environment variables, prepend) will still be used.

Since binstubs use plain SSH to connect to VM, it creates connection much faster because Vagrant is not invoked:

➜ time bin/echo 1
        0.28 real
➜ time vagrant exec echo 1
        2.84 real

To make plain SSH work, it saves current SSH configuration of Vagrant to temporary file when you run vagrant exec --binstubs. This means that if your VM network configuration has changed (IP address, port), you will have to regenerate binstubs again. So far I had no problems with this, but it's not very convenient for sure.

Moving forward, you can use projects like direnv to add bin/ to PATH and completely forget that you have VM running.

It is also possible to configure binstubs directory (e.g. default bin/ collides with Rails binstubs):

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'
  config.exec.binstubs_path = 'vbin'
end

Testing

Before running features, you'll need to bootstrap box.

➜ bundle exec rake features:bootstrap

To run features, execute the following rake task.

➜ bundle exec rake features:run

After you're done, remove Vagrant box.

➜ bundle exec rake features:cleanup

To show stdout, add @announce-stdout tag to scenario/feature.

Known issues

vagrant-exec cannot properly handle -v in command args (it's caught somewhere before plugin), so executing vagrant exec ruby -v will return Vagrant version rather than Ruby. As a workaround, wrap it in quotes: vagrant exec "ruby -v".

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2013-2015 Alex Rodionov. See LICENSE.md for details.

More Repositories

1

Maccy

Lightweight clipboard manager for macOS
Swift
8,710
star
2

yard-doctest

Doctests from YARD examples
Gherkin
100
star
3

vagrant-vultr

Vultr provider for Vagrant
Ruby
46
star
4

watirsome

Awesome page objects with Watir
Ruby
44
star
5

watir-scroll

(MERGED INTO WATIR 6.16). Scrolling API for Watir
Ruby
33
star
6

content-security-policy

Full-featured Content Security Policy as Rack middleware
Ruby
20
star
7

action_mailer_cache_delivery

Cache delivery method for ActionMailer
Ruby
17
star
8

watir-dom-wait

Watir extension providing with DOM-based waiting
Ruby
17
star
9

vagrant-mosh

Vagrant plugin to use Mosh to connect to box
Ruby
12
star
10

vim-ruby-interpolation

Simple plugin to add {} after hitting #
Vim Script
8
star
11

dotfiles

dotfiles
Lua
8
star
12

vim-dispatch-vimshell

Vimshell strategy for dispatch.vim
Vim Script
7
star
13

hubot-temploy

Hubot script to temporarily deploy pull requests
CoffeeScript
5
star
14

rules_ruby

Ruby ruleset for Bazel
Starlark
4
star
15

webdriver-highlighter

Highlight used elements in Selenium-WebDriver
Ruby
4
star
16

watizzle

for watir my sizzle
Ruby
3
star
17

seckit

Security module from Drupal CMS
PHP
3
star
18

apache-access-log-parser

Apache access_log parser
Python
2
star
19

android-jenkins-ci

Continuous integration script for Android applications on Jenkins
Python
2
star
20

se-boxes

Simple Vagrant + Puppet setup to easily develop and test Selenium for different platforms and browser versions.
Ruby
2
star
21

setup-bazel

GitHub Action to configure Bazel
JavaScript
2
star
22

watir-timecop

(NOT MAINTAINED) Use Watir with Timecop
Ruby
1
star
23

rubymine-github-colors

GitHub-like color scheme for JetBrains RubyMine
1
star
24

puppet-firefox

Simple Puppet module installing Firefox from Apt repository
Puppet
1
star