Skip to content

Commit 1d55f56

Browse files
author
Kevin Paulisse
committed
Switch computed and command to scriptrunner
1 parent 732ffbd commit 1d55f56

2 files changed

Lines changed: 75 additions & 30 deletions

File tree

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,41 @@ def initialize(options = {}, logger = nil)
2121

2222
@node = options[:node]
2323
raise ArgumentError, 'Node must be specified to compile catalog' if @node.nil? || !@node.is_a?(String)
24+
25+
# To be initialized on-demand
26+
@puppet_argv = nil
27+
@puppet_binary = nil
28+
end
29+
30+
# Retrieve puppet_command, puppet_binary, puppet_argv
31+
def puppet_argv
32+
setup
33+
@puppet_argv
34+
end
35+
36+
def puppet_binary
37+
setup
38+
@puppet_binary
2439
end
2540

26-
# Build up the command line to run Puppet
2741
def puppet_command
28-
cmdline = []
42+
setup
43+
[@puppet_binary, @puppet_argv].flatten.join(' ')
44+
end
45+
46+
private
47+
48+
# Build up the command line to run Puppet
49+
def setup
50+
return if @puppet_binary && @puppet_argv
2951

3052
# Where is the puppet binary?
31-
puppet = @options[:puppet_binary]
32-
raise ArgumentError, 'Puppet binary was not supplied' if puppet.nil?
33-
raise Errno::ENOENT, "Puppet binary #{puppet} doesn't exist" unless File.file?(puppet)
34-
cmdline << puppet
53+
@puppet_binary = @options[:puppet_binary]
54+
raise ArgumentError, 'Puppet binary was not supplied' if @puppet_binary.nil?
55+
raise Errno::ENOENT, "Puppet binary #{@puppet_binary} doesn't exist" unless File.file?(@puppet_binary)
3556

3657
# Node to compile
58+
cmdline = []
3759
cmdline.concat ['master', '--compile', Shellwords.escape(@node)]
3860

3961
# storeconfigs?
@@ -99,11 +121,9 @@ def puppet_command
99121
override_and_append_commandline_with_user_supplied_arguments(cmdline)
100122

101123
# Return full command
102-
cmdline.join(' ')
124+
@puppet_argv = cmdline
103125
end
104126

105-
private
106-
107127
# Private: Mutate the command line with arguments that were passed directly from the
108128
# user. This appends new arguments and overwrites existing arguments.
109129
# @param cmdline [Array] Existing command line - mutated by this method

lib/octocatalog-diff/catalog/computed.rb

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
require 'fileutils'
44
require 'json'
5-
require 'open3'
65
require 'stringio'
76

87
require_relative '../catalog-util/bootstrap'
98
require_relative '../catalog-util/builddir'
109
require_relative '../catalog-util/command'
11-
require_relative '../util/puppetversion'
1210
require_relative '../catalog-util/facts'
11+
require_relative '../util/puppetversion'
12+
require_relative '../util/scriptrunner'
1313

1414
module OctocatalogDiff
1515
class Catalog
1616
# Represents a Puppet catalog that is computed (via `puppet master --compile ...`)
1717
# By instantiating this class, the catalog is computed.
1818
class Computed
19-
attr_reader :node, :error_message, :catalog, :catalog_json, :retries
19+
attr_reader :node, :error_message, :catalog, :catalog_json, :retries, :scriptrunner, :puppet_command_obj
2020

2121
# Constructor
2222
# @param :node [String] REQUIRED: Node name
@@ -160,37 +160,62 @@ def build_catalog(logger = nil)
160160

161161
# Get the command to compile the catalog
162162
# @return [String] Puppet command line
163-
def puppet_command(options = @opts)
164-
return @puppet_command if @puppet_command
165-
raise ArgumentError, '"puppet_binary" was not passed to OctocatalogDiff::Catalog::Computed' unless @puppet_binary
166-
command_opts = options.merge(
163+
def puppet_command
164+
puppet_command_obj.puppet_command
165+
end
166+
167+
def puppet_command_obj
168+
return @puppet_command_obj if @puppet_command_obj
169+
170+
unless @puppet_binary
171+
raise ArgumentError, '"puppet_binary" was not passed to OctocatalogDiff::Catalog::Computed'
172+
end
173+
174+
command_opts = @opts.merge(
167175
node: @node,
168176
compilation_dir: @builddir.tempdir,
169-
parser: options.fetch(:parser, :default),
177+
parser: @opts.fetch(:parser, :default),
170178
puppet_binary: @puppet_binary,
171179
fact_file: @builddir.fact_file,
172180
dir: @builddir.tempdir,
173181
enc: @builddir.enc
174182
)
175-
command = OctocatalogDiff::CatalogUtil::Command.new(command_opts)
176-
@puppet_command = command.puppet_command
183+
@puppet_command_obj = OctocatalogDiff::CatalogUtil::Command.new(command_opts)
177184
end
178185

179186
# Private method: Actually execute puppet
180187
# @return [Hash] { stdout, stderr, exitcode }
181-
def exec_puppet
188+
def exec_puppet(logger)
182189
# This is the environment provided to the puppet command.
183-
env = {
184-
'HOME' => ENV['HOME'],
185-
'PATH' => ENV['PATH'],
186-
'PWD' => @builddir.tempdir
187-
}
190+
env = {}
188191
@pass_env_vars.each { |var| env[var] ||= ENV[var] }
189-
out, err, status = Open3.capture3(env, puppet_command, unsetenv_others: true, chdir: @builddir.tempdir)
192+
193+
# This is the Puppet command itself
194+
env['OCD_PUPPET_BINARY'] = @puppet_command_obj.puppet_binary
195+
196+
# Additional passed-in options
197+
sr_run_opts = env.merge(
198+
logger: logger,
199+
working_dir: @builddir.tempdir,
200+
argv: @puppet_command_obj.puppet_argv
201+
)
202+
203+
# Set up the ScriptRunner
204+
scriptrunner = OctocatalogDiff::Util::ScriptRunner.new(
205+
default_script: 'puppet/puppet.sh',
206+
override_script_path: @opts[:override_script_path]
207+
)
208+
209+
begin
210+
scriptrunner.run(sr_run_opts)
211+
rescue OctocatalogDiff::Util::ScriptRunner::ScriptException => exc
212+
logger.warn "Puppet command failed: #{exc.message}" if logger
213+
end
214+
190215
{
191-
stdout: out,
192-
stderr: err,
193-
exitcode: status.exitstatus
216+
stdout: scriptrunner.stdout,
217+
stderr: scriptrunner.stderr,
218+
exitcode: scriptrunner.exitcode
194219
}
195220
end
196221

@@ -216,7 +241,7 @@ def run_puppet(logger)
216241
@retries = retry_num
217242
time_begin = Time.now
218243
logger.debug("(#{@tag}) Try #{1 + retry_num} executing Puppet #{puppet_version}: #{puppet_command}")
219-
result = exec_puppet
244+
result = exec_puppet(logger)
220245

221246
# Success
222247
if (result[:exitcode]).zero?

0 commit comments

Comments
 (0)