Class: DaptivChefCI::Shell
- Inherits:
-
Object
- Object
- DaptivChefCI::Shell
- Defined in:
- lib/daptiv-chef-ci/shell.rb
Instance Method Summary collapse
-
#exec_cmd(command, timeout = nil, environment = {}) ⇒ Array
Executes the specified shell command and returns the stdout.
-
#initialize ⇒ Shell
constructor
A new instance of Shell.
-
#path_without_gem_dir ⇒ String
Returns the PATH environment variable as it was before Bundler prepended the system gem directory to it.
Constructor Details
#initialize ⇒ Shell
Returns a new instance of Shell.
8 9 10 |
# File 'lib/daptiv-chef-ci/shell.rb', line 8 def initialize() @logger = Log4r::Logger.new("daptiv_chef_ci::shell") end |
Instance Method Details
#exec_cmd(command, timeout = nil, environment = {}) ⇒ Array
Executes the specified shell command and returns the stdout.
This method ensure that any invoked command use the same PATH environment that the user has outside Ruby/Bundler.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/daptiv-chef-ci/shell.rb', line 21 def exec_cmd(command, timeout = nil, environment = {}) timeout ||= 600 environment = Hash[ environment.map{ |k, v| [k.to_s, v.to_s] } ] environment['LC_ALL'] = ENV['LC_ALL'] if !environment.has_key?('LC_ALL') path_at_start = ENV['PATH'] begin ENV['PATH'] = path_without_gem_dir() @logger.debug("Temporarily setting PATH: #{ENV['PATH']}") @logger.info("Executing: '#{command}'\n\ttimeout: #{timeout}\n\tenvironment: #{environment}") shell_out = Mixlib::ShellOut.new(command, :timeout => timeout, :environment => environment) shell_out.run_command() shell_out.invalid! if shell_out.exitstatus != 0 @logger.info(shell_out.stdout) shell_out.stdout.split("\n") ensure @logger.debug("Resetting PATH: #{path_at_start}") ENV['PATH'] = path_at_start end end |
#path_without_gem_dir ⇒ String
Returns the PATH environment variable as it was before Bundler prepended the system gem directory to it.
This can happen if the user has invoked “require ‘bundler/setup’” somewhere, like in this gems Rakefile.
This is needed because sometimes a user will have the Vagrant gem installed on their system and we don’t want to use it, we should use the one that’s in their PATH as if they invoked vagrant themselves (i.e. the installed version)
54 55 56 57 58 59 60 |
# File 'lib/daptiv-chef-ci/shell.rb', line 54 def path_without_gem_dir paths = ENV['PATH'].split(':') system_gem_dir = "#{Bundler.bundle_path}/bin" @logger.debug("System gem dir: #{system_gem_dir}") paths.delete_if { |p| p.downcase() == system_gem_dir.downcase() } paths.join(':') end |