Class: OctocatalogDiff::Bootstrap
- Inherits:
-
Object
- Object
- OctocatalogDiff::Bootstrap
- Defined in:
- lib/octocatalog-diff/bootstrap.rb
Overview
Contains bootstrap function to bootstrap a checked-out Puppet environment.
Class Method Summary collapse
-
.bootstrap(options = {}) ⇒ Hash
Bootstrap a checked-out Puppet environment.
Class Method Details
.bootstrap(options = {}) ⇒ Hash
Bootstrap a checked-out Puppet environment
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/octocatalog-diff/bootstrap.rb', line 14 def self.bootstrap( = {}) # Options validation unless [:path].is_a?(String) raise ArgumentError, 'Directory to bootstrap (:path) undefined or wrong data type' end unless File.directory?([:path]) raise Errno::ENOENT, "Non-existent directory '#{[:path]}' in bootstrap" end unless [:bootstrap_script].is_a?(String) raise ArgumentError, 'Bootstrap script (:bootstrap_script) undefined or wrong data type' end bootstrap_script = File.join([:path], [:bootstrap_script]) unless File.file?(bootstrap_script) raise Errno::ENOENT, "Non-existent bootstrap script '#{[:bootstrap_script]}'" end # 'env' sets up the environment variables that will be passed to the script. # This is a clean environment. env = { 'PWD' => [:path], 'HOME' => ENV['HOME'], 'PATH' => ENV['PATH'], 'BASEDIR' => [:basedir] } env.merge!([:bootstrap_environment]) if [:bootstrap_environment].is_a?(Hash) # 'opts' are options passed to the Open3.capture2e command which are described # here: http://ruby-doc.org/stdlib-2.1.0/libdoc/open3/rdoc/Open3.html#method-c-capture2e # Setting { :chdir => dir } means the shelled-out script will execute in the specified directory # This natively avoids the need to shell out to 'cd $dir && script/bootstrap' opts = { chdir: [:path], unsetenv_others: true } # Actually execute the command and capture the output (combined stdout and stderr). cmd = [bootstrap_script, [:bootstrap_args]].compact.map { |x| Shellwords.escape(x) }.join(' ') output, status = Open3.capture2e(env, cmd, opts) { status_code: status.exitstatus, output: output } end |