如何使用流浪者从主机操作系统外壳在来宾操作系统外壳中执行“bundle exec命令”



我得到了流浪箱。当我使用vagrant ssh登录时,我在shell中执行:

cd app
bundle exec cucumber

并进行黄瓜测试。

现在我需要从我的主机操作系统外壳执行bundle exec whatever

我试过了:

~/vbox > vagrant ssh -c 'cd app; bundle exec cucumber'
https://github.com/rsim/oracle-enhanced.git (at rails4) is not yet checked out. Run `bundle install` first.
Connection to 127.0.0.1 closed.
~/vbox > vagrant ssh -c 'cd app; pwd; ls; bundle list'
/home/vagrant/app
account  bin      certs   config.ru  db        features  Gemfile.lock  log        Rakefile   script       spec  vbox
app      Capfile  config  data       Gemfile   lib       public        README.md  tmp        vendor
Gems included by the bundle:
https://github.com/rsim/oracle-enhanced.git (at rails4) is not yet checked out. Run `bundle install` first.
Connection to 127.0.0.1 closed.
~/vbox > vagrant ssh -c 'ruby -v; rvm list'
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
rvm rubies
=* ruby-2.0.0-p353 [ x86_64 ]
# => - current
# =* - current && default
#  * - default
Connection to 127.0.0.1 closed.

如何在与我交互执行命令完全相同的环境中执行命令?

更新:

> vagrant ssh -c 'cd app; bundle env'
Bundler 1.6.5
Ruby 2.0.0 (2013-11-22 patchlevel 353) [x86_64-linux]
Rubygems 2.2.2
rvm 1.25.28 (version)
GEM_HOME /home/vagrant/.rvm/gems/ruby-2.0.0-p353@ds-cabinet
GEM_PATH /home/vagrant/.rvm/gems/ruby-2.0.0-p353@ds-cabinet:/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global
rubygems-bundler (1.4.4)

Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'
# Use postgres as the database for Active Record
gem 'pg'
# Use Oracle adapter for reading from Siebel
gem 'ruby-oci8'
gem 'activerecord-oracle_enhanced-adapter', git: 'https://github.com/rsim/oracle-enhanced.git', branch: 'rails4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# Use slim for markup
gem 'slim-rails'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'jquery-fileupload-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
gem 'quiet_assets'
# Use unicorn as the app server
gem 'unicorn'
group :development do
  gem 'thin'
end
# Use Capistrano for deployment
group :development do
  gem 'capistrano'
  gem 'capistrano-rvm'
  gem 'capistrano-rails'
  gem 'capistrano3-unicorn'
end
# Use RSpec for testing
group :development, :test do
  gem 'rspec-rails'
  gem 'rspec-mocks'
  gem 'factory_girl_rails'
  gem 'timecop'
  gem 'pry-byebug'
  gem 'pry-stack_explorer'
  gem 'awesome_print'
end
group :test do
  gem 'webmock'
  gem 'vcr'
  gem 'capybara-webkit'
  gem 'cucumber-rails', require: false
  gem 'database_cleaner'
  gem 'email_spec'
end
# Use debugger
# gem 'debugger', group: [:development, :test]
# Use DS gems
gem 'ds-sns', git: 'git@github.com:BusinessEnvironment/ds-sns-gem.git', branch: 'sns2'
gem 'ds-siebel', git: 'git@github.com:BusinessEnvironment/ds-siebel-gem.git'
gem 'ds-spark', git: 'git@github.com:BusinessEnvironment/DS-Spark-gem.git'
# Helpers
gem 'simple_form'
gem 'cancan'
gem 'kaminari'
gem 'acts-as-taggable-on'
gem 'carrierwave'
gem 'mini_magick'
gem 'rails-observers'
gem 'exception_notification'
gem 'letter_opener', group: :development
gem 'keepass-password-generator'
# Localization
gem 'russian'
# Use curb for working with external services
gem 'curb'
# Use workflow as state machine
gem 'workflow'
# Use whenever for cron tasks
gem 'whenever', require: false

Gemfile.lock
GIT
  remote: git@github.com:BusinessEnvironment/DS-Spark-gem.git
  revision: 1ec2a4342f23c41c4c67030d3488563a26ea27dc
  specs:
    ds-spark (0.0.1)
      savon (= 2.1.0)
GIT
  remote: git@github.com:BusinessEnvironment/ds-siebel-gem.git
  revision: 55b78fd651c972f667c6e019ba5ce9bcf22518ee
  specs:
    ds-siebel (0.0.1)
      savon (= 2.1.0)
GIT
  remote: git@github.com:BusinessEnvironment/ds-sns-gem.git
  revision: 6430105e8efccd30de031017f91935c02bd03853
  branch: sns2
  specs:
    ds-sns (0.0.1)
      andand
      curb
      guid
      memoist
      rails (>= 4.0.0)
GIT
  remote: https://github.com/rsim/oracle-enhanced.git
  revision: f15de843c572f1d73a52907cb47f8a4b884c5ec5
  branch: rails4
  specs:
    activerecord-oracle_enhanced-adapter (1.4.3)
GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.0.3)
      actionpack (= 4.0.3)
      mail (~> 2.5.4)
    actionpack (4.0.3)
      activesupport (= 4.0.3)
      builder (~> 3.1.0)
      erubis (~> 2.7.0)
      rack (~> 1.5.2)
      rack-test (~> 0.6.2)
    activemodel (4.0.3)
      activesupport (= 4.0.3)
      builder (~> 3.1.0)
    activerecord (4.0.3)
      activemodel (= 4.0.3)
      activerecord-deprecated_finders (~> 1.0.2)
      activesupport (= 4.0.3)
      arel (~> 4.0.0)
    activerecord-deprecated_finders (1.0.3)
    activesupport (4.0.3)
      i18n (~> 0.6, >= 0.6.4)
      minitest (~> 4.2)
      multi_json (~> 1.3)
      thread_safe (~> 0.1)
      tzinfo (~> 0.3.37)
    acts-as-taggable-on (3.0.1)
      rails (>= 3, < 5)
    addressable (2.3.6)
    akami (1.2.2)
      gyoku (>= 0.4.0)
      nokogiri
    andand (1.3.3)
    arel (4.0.2)
    atomic (1.1.14)
    awesome_print (1.2.0)
    binding_of_caller (0.7.2)
      debug_inspector (>= 0.0.1)
    builder (3.1.4)
    byebug (2.7.0)
      columnize (~> 0.3)
      debugger-linecache (~> 1.2)
    cancan (1.6.10)
    capistrano (3.1.0)
      i18n
      rake (>= 10.0.0)
      sshkit (~> 1.3)
    capistrano-bundler (1.1.2)
      capistrano (~> 3.0)
      sshkit (~> 1.2)
    capistrano-rails (1.1.1)
      capistrano (~> 3.1)
      capistrano-bundler (~> 1.1)
    capistrano-rvm (0.1.1)
      capistrano (~> 3.0)
      sshkit (~> 1.2)
    capistrano3-unicorn (0.1.1)
      capistrano (>= 3.1.0)
    capybara (2.3.0)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (~> 2.0)
    capybara-webkit (1.2.0)
      capybara (>= 2.0.2, < 2.4.0)
      json
    carrierwave (0.10.0)
      activemodel (>= 3.2.0)
      activesupport (>= 3.2.0)
      json (>= 1.7)
      mime-types (>= 1.16)
    chronic (0.10.2)
    coderay (1.1.0)
    coffee-rails (4.0.1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.7.0)
    columnize (0.8.9)
    crack (0.4.2)
      safe_yaml (~> 1.0.0)
    cucumber (1.3.16)
      builder (>= 2.1.2)
      diff-lcs (>= 1.1.3)
      gherkin (~> 2.12)
      multi_json (>= 1.7.5, < 2.0)
      multi_test (>= 0.1.1)
    cucumber-rails (1.4.1)
      capybara (>= 1.1.2, < 3)
      cucumber (>= 1.3.8, < 2)
      mime-types (~> 1.16)
      nokogiri (~> 1.5)
      rails (>= 3, < 5)
    curb (0.8.5)
    daemons (1.1.9)
    database_cleaner (1.3.0)
    debug_inspector (0.0.2)
    debugger-linecache (1.2.0)
    diff-lcs (1.2.5)
    email_spec (1.6.0)
      launchy (~> 2.1)
      mail (~> 2.2)
    erubis (2.7.0)
    eventmachine (1.0.3)
    exception_notification (4.0.1)
      actionmailer (>= 3.0.4)
      activesupport (>= 3.0.4)
    execjs (2.0.2)
    factory_girl (4.4.0)
      activesupport (>= 3.0.0)
    factory_girl_rails (4.4.1)
      factory_girl (~> 4.4.0)
      railties (>= 3.0.0)
    gherkin (2.12.2)
      multi_json (~> 1.3)
    guid (0.1.1)
    gyoku (1.0.0)
      builder (>= 2.1.2)
    hike (1.2.3)
    httpi (2.0.2)
      rack
    i18n (0.6.9)
    jbuilder (1.5.3)
      activesupport (>= 3.0.0)
      multi_json (>= 1.2.0)
    jquery-fileupload-rails (0.4.1)
      actionpack (>= 3.1)
      railties (>= 3.1)
    jquery-rails (3.1.0)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    jquery-ui-rails (4.2.0)
      railties (>= 3.2.16)
    json (1.8.1)
    kaminari (0.15.1)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    keepass-password-generator (0.1.1)
    kgio (2.9.2)
    launchy (2.4.2)
      addressable (~> 2.3)
    letter_opener (1.2.0)
      launchy (~> 2.2)
    libv8 (3.16.14.3)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    memoist (0.10.0)
    method_source (0.8.2)
    mime-types (1.25.1)
    mini_magick (3.7.0)
      subexec (~> 0.2.1)
    mini_portile (0.5.2)
    minitest (4.7.5)
    multi_json (1.8.4)
    multi_test (0.1.1)
    net-scp (1.1.2)
      net-ssh (>= 2.6.5)
    net-ssh (2.8.0)
    nokogiri (1.6.1)
      mini_portile (~> 0.5.0)
    nori (2.0.4)
    pg (0.17.1)
    polyglot (0.3.4)
    pry (0.10.0)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
    pry-byebug (1.3.3)
      byebug (~> 2.7)
      pry (~> 0.10)
    pry-stack_explorer (0.4.9.1)
      binding_of_caller (>= 0.7)
      pry (>= 0.9.11)
    quiet_assets (1.0.2)
      railties (>= 3.1, < 5.0)
    rack (1.5.2)
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (4.0.3)
      actionmailer (= 4.0.3)
      actionpack (= 4.0.3)
      activerecord (= 4.0.3)
      activesupport (= 4.0.3)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.0.3)
      sprockets-rails (~> 2.0.0)
    rails-observers (0.1.2)
      activemodel (~> 4.0)
    railties (4.0.3)
      actionpack (= 4.0.3)
      activesupport (= 4.0.3)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    raindrops (0.13.0)
    rake (10.1.1)
    rdoc (4.1.1)
      json (~> 1.4)
    ref (1.0.5)
    rspec-core (2.14.7)
    rspec-expectations (2.14.5)
      diff-lcs (>= 1.1.3, < 2.0)
    rspec-mocks (2.14.6)
    rspec-rails (2.14.1)
      actionpack (>= 3.0)
      activemodel (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 2.14.0)
      rspec-expectations (~> 2.14.0)
      rspec-mocks (~> 2.14.0)
    ruby-oci8 (2.1.7)
    russian (0.6.0)
      i18n (>= 0.5.0)
    safe_yaml (1.0.3)
    sass (3.2.14)
    sass-rails (4.0.1)
      railties (>= 4.0.0, < 5.0)
      sass (>= 3.1.10)
      sprockets-rails (~> 2.0.0)
    savon (2.1.0)
      akami (~> 1.2.0)
      builder (>= 2.1.2)
      gyoku (~> 1.0.0)
      httpi (~> 2.0.2)
      nokogiri (>= 1.4.0)
      nori (~> 2.0.3)
      wasabi (~> 3.0.0)
    sdoc (0.4.0)
      json (~> 1.8)
      rdoc (~> 4.0, < 5.0)
    simple_form (3.0.1)
      actionpack (>= 4.0.0, < 4.1)
      activemodel (>= 4.0.0, < 4.1)
    slim (2.0.2)
      temple (~> 0.6.6)
      tilt (>= 1.3.3, < 2.1)
    slim-rails (2.1.0)
      actionpack (>= 3.0, < 4.1)
      activesupport (>= 3.0, < 4.1)
      railties (>= 3.0, < 4.1)
      slim (~> 2.0)
    slop (3.6.0)
    sprockets (2.10.1)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.0.1)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (~> 2.8)
    sshkit (1.3.0)
      net-scp (>= 1.1.2)
      net-ssh
      term-ansicolor
    subexec (0.2.3)
    temple (0.6.7)
    term-ansicolor (1.3.0)
      tins (~> 1.0)
    therubyracer (0.12.1)
      libv8 (~> 3.16.14.0)
      ref
    thin (1.6.1)
      daemons (>= 1.0.9)
      eventmachine (>= 1.0.0)
      rack (>= 1.0.0)
    thor (0.18.1)
    thread_safe (0.1.3)
      atomic
    tilt (1.4.1)
    timecop (0.7.1)
    tins (1.0.0)
    treetop (1.4.15)
      polyglot
      polyglot (>= 0.3.1)
    tzinfo (0.3.38)
    uglifier (2.4.0)
      execjs (>= 0.3.0)
      json (>= 1.8.0)
    unicorn (4.8.2)
      kgio (~> 2.6)
      rack
      raindrops (~> 0.7)
    vcr (2.9.2)
    wasabi (3.0.0)
      httpi (~> 2.0)
      nokogiri (>= 1.4.0)
    webmock (1.18.0)
      addressable (>= 2.3.6)
      crack (>= 0.3.2)
    whenever (0.9.2)
      activesupport (>= 2.3.4)
      chronic (>= 0.6.3)
    workflow (1.1.0)
    xpath (2.0.0)
      nokogiri (~> 1.3)
PLATFORMS
  ruby
DEPENDENCIES
  activerecord-oracle_enhanced-adapter!
  acts-as-taggable-on
  awesome_print
  cancan
  capistrano
  capistrano-rails
  capistrano-rvm
  capistrano3-unicorn
  capybara-webkit
  carrierwave
  coffee-rails (~> 4.0.0)
  cucumber-rails
  curb
  database_cleaner
  ds-siebel!
  ds-sns!
  ds-spark!
  email_spec
  exception_notification
  factory_girl_rails
  jbuilder (~> 1.2)
  jquery-fileupload-rails
  jquery-rails
  jquery-ui-rails
  kaminari
  keepass-password-generator
  letter_opener
  mini_magick
  pg
  pry-byebug
  pry-stack_explorer
  quiet_assets
  rails (= 4.0.3)
  rails-observers
  rspec-mocks
  rspec-rails
  ruby-oci8
  russian
  sass-rails (~> 4.0.0)
  sdoc
  simple_form
  slim-rails
  therubyracer
  thin
  timecop
  uglifier (>= 1.3.0)
  unicorn
  vcr
  webmock
  whenever
  workflow

@TimMoore

非常感谢蒂姆!受到你评论的启发,我可以从不同的角度看待这个问题。我比较了从主机操作系统执行的命令vagrant ssh -с cd app; env > 4的输出和在客户操作系统外壳中执行的env > 3的输出

> diff 3 4
1c1
< XDG_SESSION_ID=4
---
> XDG_SESSION_ID=52
3c3
< GEM_HOME=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global
---
> GEM_HOME=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@ds-cabinet
5,6c5
< TERM=screen
< SSH_CLIENT=10.0.2.2 50175 22
---
> TERM=xterm
8c7,8
< OLDPWD=/home/vagrant/app 
---
> SSH_CLIENT=10.0.2.2 65390 22
> OLDPWD=/home/vagrant
10c10
< SSH_TTY=/dev/pts/0
---
> SSH_TTY=/dev/pts/3
15,39c15
< SSH_AUTH_SOCK=/tmp/ssh-aFehksOsW6/agent.6453
< TERMCAP=SC|screen|VT 100/ANSI X3.64 virtual terminal:
<       :DO=E[%dB:LE=E[%dD:RI=E[%dC:UP=E[%dA:bs:bt=E[Z:
<       :cd=E[J:ce=E[K:cl=E[HE[J:cm=E[%i%d;%dH:ct=E[3g:
<       :do=^J:nd=E[C:pt:rc=E8:rs=Ec:sc=E7:st=EH:up=EM:
<       :le=^H:bl=^G:cr=^M:it#8:ho=E[H:nw=EE:ta=^I:is=E)0:
<       :li#31:co#119:am:xn:xv:LP:sr=EM:al=E[L:AL=E[%dL:
<       :cs=E[%i%d;%dr:dl=E[M:DL=E[%dM:dc=E[P:DC=E[%dP:
<       :im=E[4h:ei=E[4l:mi:IC=E[%d@:ks=E[?1hE=:
<       :ke=E[?1lE>:vi=E[?25l:ve=E[34hE[?25h:vs=E[34l:
<       :ti=E[?1049h:te=E[?1049l:us=E[4m:ue=E[24m:so=E[3m:
<       :se=E[23m:mb=E[5m:md=E[1m:mr=E[7m:me=E[m:ms:
<       :Co#8:pa#64:AF=E[3%dm:AB=E[4%dm:op=E[39;49m:AX:
<       :vb=Eg:G0:as=E(0:ae=E(B:
<       :ac=140140aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~..--++,,hhII00:
<       :po=E[5i:pf=E[4i:Km=E[M:k0=E[10~:k1=EOP:k2=EOQ:
<       :k3=EOR:k4=EOS:k5=E[15~:k6=E[17~:k7=E[18~:
<       :k8=E[19~:k9=E[20~:k;=E[21~:F1=E[23~:F2=E[24~:
<       :F3=E[1;2P:F4=E[1;2Q:F5=E[1;2R:F6=E[1;2S:
<       :F7=E[15;2~:F8=E[17;2~:F9=E[18;2~:FA=E[19;2~:kb=:
<       :K2=EOE:kB=E[Z:kF=E[1;2B:kR=E[1;2A:*4=E[3;2~:
<       :*7=E[1;2F:#2=E[1;2H:#3=E[2;2~:#4=E[1;2D:%c=E[6;2~:
<       :%e=E[5;2~:%i=E[1;2C:kh=E[1~:@1=E[1~:kH=E[4~:
<       :@7=E[4~:kN=E[6~:kP=E[5~:kI=E[2~:kD=E[3~:ku=EOA:
<       :kd=EOB:kr=EOC:kl=EOD:km:
---
> SSH_AUTH_SOCK=/tmp/ssh-iMxngt4dR0/agent.29396
42c18
< PATH=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global/bin:/home/vagrant/.rvm/rubies/ruby-2.0.0-p353/bin:/usr/local/sbin
:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/vagrant/.rvm/bin:/home/vagrant/.rvm/bin
:/home/vagrant/.rvm/bin
---
> PATH=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@ds-cabinet/bin:/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global/bin:/home/
vagrant/.rvm/rubies/ruby-2.0.0-p353/bin:/home/vagrant/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:
/bin:/usr/games:/usr/local/games
44,45c20
< STY=6524.pts-0.cabinet
< PWD=/home/vagrant
---
> PWD=/home/vagrant/app
51a27
> rvm_ruby_string=ruby-2.0.0-p353
53,55c29,30
< WINDOW=1
< SSH_CONNECTION=10.0.2.2 50175 10.0.2.15 22
< GEM_PATH=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global
---
> GEM_PATH=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@ds-cabinet:/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global
> SSH_CONNECTION=10.0.2.2 65390 10.0.2.15 22
56a32
> rvm_delete_flag=0
58d33
< LESSCLOSE=/usr/bin/lesspipe %s %s
59a35
> LESSCLOSE=/usr/bin/lesspipe %s %s

然后我注意到来自主机操作系统的命令使用gemset ds-cabinet

GEM_HOME=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@ds-cabinet

而客户操作系统外壳中的命令使用gemset global

GEM_PATH=/home/vagrant/.rvm/gems/ruby-2.0.0-p353@global

使用gemset ds-cabinet是因为存在包含ds-cabinet字符串的app/.ruby-gemset文件。

当我明确指定ruby版本和gemset时,来自主机操作系统的命令成功了。

vagrant ssh -c 'cd app; rvm use ruby-2.0.0-p353@global; xvfb-run bundle exec cucumber'

结论:

尽管有指向ds-cabinet gemset的app/.ruby-gemset,但实际上Bundler在来宾操作系统外壳中本地调用时使用了global gemset,而在从主机操作系统作为远程命令调用时使用的是ds-cabinet gemset,该gemset最终为空。

因此,我将app/.ruby-gemset设置为global,主机操作系统命令做得很好。

最新更新