22
33require 'fileutils'
44require 'json'
5- require 'open3'
65require 'stringio'
76
87require_relative '../catalog-util/bootstrap'
98require_relative '../catalog-util/builddir'
109require_relative '../catalog-util/command'
11- require_relative '../util/puppetversion'
1210require_relative '../catalog-util/facts'
11+ require_relative '../util/puppetversion'
12+ require_relative '../util/scriptrunner'
1313
1414module 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