Class: DaptivChefCI::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/daptiv-chef-ci/shell.rb

Overview

Command shell wrapper

Instance Method Summary collapse

Constructor Details

#initializeShell

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.

defaults to 600 command’s environment.

Parameters:

  • The (String)

    command line to execute

  • The (Int)

    number of seconds to wait for the command to finish,

  • Key (Hash)

    value pairs of environment variables to pass to the

Returns:

  • (Array)

    Each entry represents a line from the stdout



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/daptiv-chef-ci/shell.rb', line 52

def exec_cmd(command, timeout = nil, environment = {})
  path_at_start = ENV['PATH']
  begin
    ENV['PATH'] = path_without_gem_dir
    @logger.debug("Setting PATH: #{ENV['PATH']}")
    exec_cmd_in_context(command, timeout, environment)
  ensure
    @logger.debug("Resetting PATH: #{path_at_start}")
    ENV['PATH'] = path_at_start
  end
end

#exec_cmd_in_context(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.

defaults to 600 command’s environment.

Parameters:

  • The (String)

    command line to execute

  • The (Int)

    number of seconds to wait for the command to finish,

  • Key (Hash)

    value pairs of environment variables to pass to the

Returns:

  • (Array)

    Each entry represents a line from the stdout



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/daptiv-chef-ci/shell.rb', line 23

def exec_cmd_in_context(command, timeout = nil, environment = {})
  timeout ||= 600
  environment = Hash[ environment.map{ |k, v| [k.to_s, v.to_s] } ]
  environment['LC_ALL'] = ENV['LC_ALL'] unless environment.key?('LC_ALL')

  @logger.info("Executing: '#{command}'")
  @logger.debug("\n\ttimeout: #{timeout}\n\tenvironment: #{environment}")

  shell_out = Mixlib::ShellOut.new(
    command, timeout: timeout, environment: environment)
  shell_out.live_stream = STDOUT

  shell_out.run_command
  shell_out.invalid! if shell_out.exitstatus != 0
  @logger.info(shell_out.stdout)
  shell_out.stdout.split("\n")
end

#path_without_gem_dirString

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)

prepended

Returns:

  • (String)

    The ENV without the Bundler system gem dir



77
78
79
80
81
82
83
# File 'lib/daptiv-chef-ci/shell.rb', line 77

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