Skip to content

Commit 1c0d827

Browse files
author
Kevin Paulisse
committed
Change puppet version determinator to use scriptrunner
1 parent dfeb2a2 commit 1c0d827

5 files changed

Lines changed: 36 additions & 18 deletions

File tree

lib/octocatalog-diff/catalog-util/git.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ def self.check_out_git_archive(options = {})
4343
script = OctocatalogDiff::Util::ScriptRunner.new(sr_opts)
4444

4545
sr_run_opts = {
46-
:working_dir => dir,
46+
:working_dir => dir,
47+
:pass_env_vars => options[:pass_env_vars],
4748
'OCD_GIT_EXTRACT_BRANCH' => branch,
4849
'OCD_GIT_EXTRACT_TARGET' => path
4950
}
51+
5052
begin
5153
script.run(sr_run_opts)
5254
logger.debug("Success git archive #{dir}:#{branch}")

lib/octocatalog-diff/util/puppetversion.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
# Helper to determine the version of Puppet
44

5-
require 'fileutils'
6-
require 'open3'
7-
require 'shellwords'
5+
require 'logger'
6+
require 'stringio'
7+
require_relative 'scriptrunner'
88

99
module OctocatalogDiff
1010
module Util
@@ -17,19 +17,24 @@ class PuppetVersion
1717
def self.puppet_version(puppet, options = {})
1818
raise ArgumentError, 'Puppet binary was not supplied' if puppet.nil?
1919
raise Errno::ENOENT, "Puppet binary #{puppet} doesn't exist" unless File.file?(puppet)
20-
cmdline = [Shellwords.escape(puppet), '--version'].join(' ')
2120

22-
# This is the environment provided to the puppet command.
23-
env = {
24-
'HOME' => ENV['HOME'],
25-
'PATH' => ENV['PATH'],
26-
'PWD' => File.dirname(puppet)
21+
sr_opts = {
22+
logger: Logger.new(StringIO.new),
23+
default_script: 'puppet-version/puppet-version.sh',
24+
override_script_path: options[:override_script_path]
2725
}
28-
pass_env_vars = options.fetch(:pass_env_vars, [])
29-
pass_env_vars.each { |var| env[var] ||= ENV[var] }
30-
out, err, _status = Open3.capture3(env, cmdline, unsetenv_others: true, chdir: env['PWD'])
31-
return Regexp.last_match(1) if out =~ /^([\d\.]+)\s*$/
32-
raise "Unable to determine Puppet version: #{out} #{err}"
26+
27+
script = OctocatalogDiff::Util::ScriptRunner.new(sr_opts)
28+
29+
sr_run_opts = {
30+
:working_dir => File.dirname(puppet),
31+
:pass_env_vars => options[:pass_env_vars],
32+
'OCD_PUPPET_BINARY' => puppet
33+
}
34+
35+
output = script.run(sr_run_opts)
36+
return Regexp.last_match(1) if output =~ /^([\d\.]+)\s*$/
37+
raise "Unable to determine Puppet version: #{script.output}"
3338
end
3439
end
3540
end

lib/octocatalog-diff/util/scriptrunner.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@ def initialize(opts = {})
3838
# @param opts [Hash] Options hash
3939
# opts[:working_dir] (Required) Directory where script is to be executed
4040
# opts[:argv] (Optional Array) Command line arguments
41+
# opts[:pass_env_vars] (Optional Array) Environment variables to pass (default: HOME, PATH)
4142
# opts[<STRING>] (Optional) Environment variable
4243
def run(opts = {})
4344
working_dir = opts.fetch(:working_dir)
4445
assert_directory_exists(working_dir)
4546

4647
argv = opts.fetch(:argv, [])
4748

49+
pass_env_vars = opts[:pass_env_vars] || %w(HOME PATH)
4850
env = opts.select { |k, _v| k.is_a?(String) }
49-
env['HOME'] ||= ENV['HOME']
51+
pass_env_vars.each { |var| env[var] ||= ENV[var] }
5052
env['PWD'] = working_dir
51-
env['PATH'] ||= ENV['PATH']
5253

5354
cmdline = [script, argv].flatten.compact.map { |x| Shellwords.escape(x) }.join(' ')
5455
@logger.debug "Execute: #{cmdline}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Script to determine the Puppet version.
4+
5+
if [ -z "$OCD_PUPPET_BINARY" ]; then
6+
echo "Error: PUPPET_BINARY must be set"
7+
exit 255
8+
fi
9+
10+
"$OCD_PUPPET_BINARY" --version

spec/octocatalog-diff/tests/catalog/computed_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
branch: '.'
295295
}
296296
expect { OctocatalogDiff::Catalog::Computed.new(opts).build }
297-
.to raise_error(RuntimeError, /Unable to determine Puppet version/)
297+
.to raise_error(OctocatalogDiff::Util::ScriptRunner::ScriptException, /something failed horribly/)
298298
end
299299
end
300300
end

0 commit comments

Comments
 (0)