@@ -263,115 +263,114 @@ def main
263263 end
264264 devices . collect do |_devicename , device |
265265 # TODO when we drop support for ruby < 2.5 we can remove the extra block here
266- begin
267- device_url = URI . parse ( device . url )
268- # Handle nil scheme & port
269- scheme = "#{ device_url . scheme } ://" if device_url . scheme
270- port = ":#{ device_url . port } " if device_url . port
271-
272- # override local $vardir and $certname
273- Puppet [ :ssldir ] = ::File . join ( Puppet [ :deviceconfdir ] , device . name , 'ssl' )
274- Puppet [ :confdir ] = ::File . join ( Puppet [ :devicedir ] , device . name )
275- Puppet [ :libdir ] = options [ :libdir ] || ::File . join ( Puppet [ :devicedir ] , device . name , 'lib' )
276- Puppet [ :vardir ] = ::File . join ( Puppet [ :devicedir ] , device . name )
277- Puppet [ :certname ] = device . name
278- ssl_context = nil
279-
280- # create device directory under $deviceconfdir
281- Puppet ::FileSystem . dir_mkpath ( Puppet [ :ssldir ] ) unless Puppet ::FileSystem . dir_exist? ( Puppet [ :ssldir ] )
282-
283- # this will reload and recompute default settings and create device-specific sub vardir
284- Puppet . settings . use :main , :agent , :ssl
285-
286- # Workaround for PUP-8736: store ssl certs outside the cache directory to prevent accidental removal and keep the old path as symlink
287- optssldir = File . join ( Puppet [ :confdir ] , 'ssl' )
288- Puppet ::FileSystem . symlink ( Puppet [ :ssldir ] , optssldir ) unless Puppet ::FileSystem . exist? ( optssldir )
289-
290- unless options [ :resource ] || options [ :facts ] || options [ :apply ]
291- # Since it's too complicated to fix properly in the default settings, we workaround for PUP-9642 here.
292- # See https://github.com/puppetlabs/puppet/pull/7483#issuecomment-483455997 for details.
293- # This has to happen after `settings.use` above, so the directory is created and before `setup_host` below, where the SSL
294- # routines would fail with access errors
295- if Puppet . features . root? && !Puppet ::Util ::Platform . windows?
296- user = Puppet ::Type . type ( :user ) . new ( name : Puppet [ :user ] ) . exists? ? Puppet [ :user ] : nil
297- group = Puppet ::Type . type ( :group ) . new ( name : Puppet [ :group ] ) . exists? ? Puppet [ :group ] : nil
298- Puppet . debug ( "Fixing perms for #{ user } :#{ group } on #{ Puppet [ :confdir ] } " )
299- FileUtils . chown ( user , group , Puppet [ :confdir ] ) if user || group
300- end
301266
302- ssl_context = setup_context
267+ device_url = URI . parse ( device . url )
268+ # Handle nil scheme & port
269+ scheme = "#{ device_url . scheme } ://" if device_url . scheme
270+ port = ":#{ device_url . port } " if device_url . port
271+
272+ # override local $vardir and $certname
273+ Puppet [ :ssldir ] = ::File . join ( Puppet [ :deviceconfdir ] , device . name , 'ssl' )
274+ Puppet [ :confdir ] = ::File . join ( Puppet [ :devicedir ] , device . name )
275+ Puppet [ :libdir ] = options [ :libdir ] || ::File . join ( Puppet [ :devicedir ] , device . name , 'lib' )
276+ Puppet [ :vardir ] = ::File . join ( Puppet [ :devicedir ] , device . name )
277+ Puppet [ :certname ] = device . name
278+ ssl_context = nil
279+
280+ # create device directory under $deviceconfdir
281+ Puppet ::FileSystem . dir_mkpath ( Puppet [ :ssldir ] ) unless Puppet ::FileSystem . dir_exist? ( Puppet [ :ssldir ] )
282+
283+ # this will reload and recompute default settings and create device-specific sub vardir
284+ Puppet . settings . use :main , :agent , :ssl
285+
286+ # Workaround for PUP-8736: store ssl certs outside the cache directory to prevent accidental removal and keep the old path as symlink
287+ optssldir = File . join ( Puppet [ :confdir ] , 'ssl' )
288+ Puppet ::FileSystem . symlink ( Puppet [ :ssldir ] , optssldir ) unless Puppet ::FileSystem . exist? ( optssldir )
289+
290+ unless options [ :resource ] || options [ :facts ] || options [ :apply ]
291+ # Since it's too complicated to fix properly in the default settings, we workaround for PUP-9642 here.
292+ # See https://github.com/puppetlabs/puppet/pull/7483#issuecomment-483455997 for details.
293+ # This has to happen after `settings.use` above, so the directory is created and before `setup_host` below, where the SSL
294+ # routines would fail with access errors
295+ if Puppet . features . root? && !Puppet ::Util ::Platform . windows?
296+ user = Puppet ::Type . type ( :user ) . new ( name : Puppet [ :user ] ) . exists? ? Puppet [ :user ] : nil
297+ group = Puppet ::Type . type ( :group ) . new ( name : Puppet [ :group ] ) . exists? ? Puppet [ :group ] : nil
298+ Puppet . debug ( "Fixing perms for #{ user } :#{ group } on #{ Puppet [ :confdir ] } " )
299+ FileUtils . chown ( user , group , Puppet [ :confdir ] ) if user || group
300+ end
301+
302+ ssl_context = setup_context
303303
304- unless options [ :libdir ]
305- Puppet . override ( ssl_context : ssl_context ) do
306- Puppet ::Configurer ::PluginHandler . new . download_plugins ( env ) if Puppet ::Configurer . should_pluginsync?
307- end
304+ unless options [ :libdir ]
305+ Puppet . override ( ssl_context : ssl_context ) do
306+ Puppet ::Configurer ::PluginHandler . new . download_plugins ( env ) if Puppet ::Configurer . should_pluginsync?
308307 end
309308 end
309+ end
310310
311- # this inits the device singleton, so that the facts terminus
312- # and the various network_device provider can use it
313- Puppet ::Util ::NetworkDevice . init ( device )
314-
315- if options [ :resource ]
316- type , name = parse_args ( command_line . args )
317- Puppet . info _ ( "retrieving resource: %{resource} from %{target} at %{scheme}%{url_host}%{port}%{url_path}" ) % { resource : type , target : device . name , scheme : scheme , url_host : device_url . host , port : port , url_path : device_url . path }
318- resources = find_resources ( type , name )
319- if options [ :to_yaml ]
320- data = resources . map do |resource |
321- resource . prune_parameters ( :parameters_to_include => @extra_params ) . to_hiera_hash
322- end . inject ( :merge! )
323- text = YAML . dump ( type . downcase => data )
324- else
325- text = resources . map do |resource |
326- resource . prune_parameters ( :parameters_to_include => @extra_params ) . to_manifest . force_encoding ( Encoding . default_external )
327- end . join ( "\n " )
328- end
329- ( puts text )
330- 0
331- elsif options [ :facts ]
332- Puppet . info _ ( "retrieving facts from %{target} at %{scheme}%{url_host}%{port}%{url_path}" ) % { resource : type , target : device . name , scheme : scheme , url_host : device_url . host , port : port , url_path : device_url . path }
333- remote_facts = Puppet ::Node ::Facts . indirection . find ( name , :environment => env )
334- # Give a proper name to the facts
335- remote_facts . name = remote_facts . values [ 'clientcert' ]
336- renderer = Puppet ::Network ::FormatHandler . format ( :console )
337- puts renderer . render ( remote_facts )
338- 0
339- elsif options [ :apply ]
340- # avoid reporting to server
341- Puppet ::Transaction ::Report . indirection . terminus_class = :yaml
342- Puppet ::Resource ::Catalog . indirection . cache_class = nil
343-
344- require_relative '../../puppet/application/apply'
345- begin
346- Puppet [ :node_terminus ] = :plain
347- Puppet [ :catalog_terminus ] = :compiler
348- Puppet [ :catalog_cache_terminus ] = nil
349- Puppet [ :facts_terminus ] = :network_device
350- Puppet . override ( :network_device => true ) do
351- Puppet ::Application ::Apply . new ( Puppet ::Util ::CommandLine . new ( 'puppet' , [ "apply" , options [ :apply ] ] ) ) . run_command
352- end
353- end
311+ # this inits the device singleton, so that the facts terminus
312+ # and the various network_device provider can use it
313+ Puppet ::Util ::NetworkDevice . init ( device )
314+
315+ if options [ :resource ]
316+ type , name = parse_args ( command_line . args )
317+ Puppet . info _ ( "retrieving resource: %{resource} from %{target} at %{scheme}%{url_host}%{port}%{url_path}" ) % { resource : type , target : device . name , scheme : scheme , url_host : device_url . host , port : port , url_path : device_url . path }
318+ resources = find_resources ( type , name )
319+ if options [ :to_yaml ]
320+ data = resources . map do |resource |
321+ resource . prune_parameters ( :parameters_to_include => @extra_params ) . to_hiera_hash
322+ end . inject ( :merge! )
323+ text = YAML . dump ( type . downcase => data )
354324 else
355- Puppet . info _ ( "starting applying configuration to %{target} at %{scheme}%{url_host}%{port}%{url_path}" ) % { target : device . name , scheme : scheme , url_host : device_url . host , port : port , url_path : device_url . path }
356-
357- overrides = { }
358- overrides [ :ssl_context ] = ssl_context if ssl_context
359- Puppet . override ( overrides ) do
360- configurer = Puppet ::Configurer . new
361- configurer . run ( :network_device => true , :pluginsync => false )
325+ text = resources . map do |resource |
326+ resource . prune_parameters ( :parameters_to_include => @extra_params ) . to_manifest . force_encoding ( Encoding . default_external )
327+ end . join ( "\n " )
328+ end
329+ ( puts text )
330+ 0
331+ elsif options [ :facts ]
332+ Puppet . info _ ( "retrieving facts from %{target} at %{scheme}%{url_host}%{port}%{url_path}" ) % { resource : type , target : device . name , scheme : scheme , url_host : device_url . host , port : port , url_path : device_url . path }
333+ remote_facts = Puppet ::Node ::Facts . indirection . find ( name , :environment => env )
334+ # Give a proper name to the facts
335+ remote_facts . name = remote_facts . values [ 'clientcert' ]
336+ renderer = Puppet ::Network ::FormatHandler . format ( :console )
337+ puts renderer . render ( remote_facts )
338+ 0
339+ elsif options [ :apply ]
340+ # avoid reporting to server
341+ Puppet ::Transaction ::Report . indirection . terminus_class = :yaml
342+ Puppet ::Resource ::Catalog . indirection . cache_class = nil
343+
344+ require_relative '../../puppet/application/apply'
345+ begin
346+ Puppet [ :node_terminus ] = :plain
347+ Puppet [ :catalog_terminus ] = :compiler
348+ Puppet [ :catalog_cache_terminus ] = nil
349+ Puppet [ :facts_terminus ] = :network_device
350+ Puppet . override ( :network_device => true ) do
351+ Puppet ::Application ::Apply . new ( Puppet ::Util ::CommandLine . new ( 'puppet' , [ "apply" , options [ :apply ] ] ) ) . run_command
362352 end
363353 end
364- rescue => detail
365- Puppet . log_exception ( detail )
366- # If we rescued an error, then we return 1 as the exit code
367- 1
368- ensure
369- Puppet [ :libdir ] = libdir
370- Puppet [ :vardir ] = vardir
371- Puppet [ :confdir ] = confdir
372- Puppet [ :ssldir ] = ssldir
373- Puppet [ :certname ] = certname
354+ else
355+ Puppet . info _ ( "starting applying configuration to %{target} at %{scheme}%{url_host}%{port}%{url_path}" ) % { target : device . name , scheme : scheme , url_host : device_url . host , port : port , url_path : device_url . path }
356+
357+ overrides = { }
358+ overrides [ :ssl_context ] = ssl_context if ssl_context
359+ Puppet . override ( overrides ) do
360+ configurer = Puppet ::Configurer . new
361+ configurer . run ( :network_device => true , :pluginsync => false )
362+ end
374363 end
364+ rescue => detail
365+ Puppet . log_exception ( detail )
366+ # If we rescued an error, then we return 1 as the exit code
367+ 1
368+ ensure
369+ Puppet [ :libdir ] = libdir
370+ Puppet [ :vardir ] = vardir
371+ Puppet [ :confdir ] = confdir
372+ Puppet [ :ssldir ] = ssldir
373+ Puppet [ :certname ] = certname
375374 end
376375 end
377376
0 commit comments