Skip to content

Commit d954105

Browse files
committed
(PUP-11993) Style/RedundantBegin
This commit enables the Style/RedundantBegin cop and fixes 815 autocorrectable offenses.
1 parent 5f1cc46 commit d954105

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1050
-1268
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,6 @@ Style/OptionalBooleanParameter:
646646
Style/PreferredHashMethods:
647647
Enabled: false
648648

649-
# This cop supports safe auto-correction (--auto-correct).
650-
Style/RedundantBegin:
651-
Enabled: false
652-
653649
# This cop supports safe auto-correction (--auto-correct).
654650
Style/RedundantCondition:
655651
Exclude:

ext/windows/service/daemon.rb

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,23 @@ def service_main(*argsv)
7575

7676
service = self
7777
@run_thread = Thread.new do
78-
begin
79-
while service.running? do
80-
runinterval = service.parse_runinterval(ruby_puppet_cmd)
81-
82-
if service.state == RUNNING or service.state == IDLE
83-
service.log_notice("Executing agent with arguments: #{args}")
84-
pid = Process.create(:command_line => "#{ruby_puppet_cmd} agent --onetime #{args}", :creation_flags => CREATE_NEW_CONSOLE).process_id
85-
service.log_debug("Process created: #{pid}")
86-
else
87-
service.log_debug("Service is paused. Not invoking Puppet agent")
88-
end
89-
90-
service.log_debug("Service worker thread waiting for #{runinterval} seconds")
91-
sleep(runinterval)
92-
service.log_debug('Service worker thread woken up')
78+
while service.running? do
79+
runinterval = service.parse_runinterval(ruby_puppet_cmd)
80+
81+
if service.state == RUNNING or service.state == IDLE
82+
service.log_notice("Executing agent with arguments: #{args}")
83+
pid = Process.create(:command_line => "#{ruby_puppet_cmd} agent --onetime #{args}", :creation_flags => CREATE_NEW_CONSOLE).process_id
84+
service.log_debug("Process created: #{pid}")
85+
else
86+
service.log_debug("Service is paused. Not invoking Puppet agent")
9387
end
94-
rescue Exception => e
95-
service.log_exception(e)
88+
89+
service.log_debug("Service worker thread waiting for #{runinterval} seconds")
90+
sleep(runinterval)
91+
service.log_debug('Service worker thread woken up')
9692
end
93+
rescue Exception => e
94+
service.log_exception(e)
9795
end
9896
@run_thread.join
9997
rescue Exception => e
@@ -142,20 +140,18 @@ def log(msg, level)
142140
end
143141

144142
def report_windows_event(type, id, message)
145-
begin
146-
eventlog = nil
147-
eventlog = Puppet::Util::Windows::EventLog.open("Puppet")
148-
eventlog.report_event(
149-
:event_type => type, # EVENTLOG_ERROR_TYPE, etc
150-
:event_id => id, # 0x01 or 0x02, 0x03 etc.
151-
:data => message # "the message"
152-
)
153-
rescue Exception
154-
# Ignore all errors
155-
ensure
156-
unless eventlog.nil?
157-
eventlog.close
158-
end
143+
eventlog = nil
144+
eventlog = Puppet::Util::Windows::EventLog.open("Puppet")
145+
eventlog.report_event(
146+
:event_type => type, # EVENTLOG_ERROR_TYPE, etc
147+
:event_id => id, # 0x01 or 0x02, 0x03 etc.
148+
:data => message # "the message"
149+
)
150+
rescue Exception
151+
# Ignore all errors
152+
ensure
153+
unless eventlog.nil?
154+
eventlog.close
159155
end
160156
end
161157

@@ -192,24 +188,22 @@ def parse_log_level(puppet_path, cmdline_debug)
192188
private
193189

194190
def load_env(base_dir)
195-
begin
196-
# ENV that uses backward slashes
197-
ENV['FACTER_env_windows_installdir'] = base_dir.tr('/', '\\')
198-
ENV['PL_BASEDIR'] = base_dir.tr('/', '\\')
199-
ENV['PUPPET_DIR'] = File.join(base_dir, 'puppet').tr('/', '\\')
200-
ENV['OPENSSL_CONF'] = File.join(base_dir, 'puppet', 'ssl', 'openssl.cnf').tr('/', '\\')
201-
ENV['SSL_CERT_DIR'] = File.join(base_dir, 'puppet', 'ssl', 'certs').tr('/', '\\')
202-
ENV['SSL_CERT_FILE'] = File.join(base_dir, 'puppet', 'ssl', 'cert.pem').tr('/', '\\')
203-
ENV['Path'] = [
204-
File.join(base_dir, 'puppet', 'bin'),
205-
File.join(base_dir, 'bin'),
206-
].join(';').tr('/', '\\') + ';' + ENV.fetch('Path', nil)
207-
208-
# ENV that uses forward slashes
209-
ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV.fetch('RUBYLIB', nil)}"
210-
rescue => e
211-
log_exception(e)
212-
end
191+
# ENV that uses backward slashes
192+
ENV['FACTER_env_windows_installdir'] = base_dir.tr('/', '\\')
193+
ENV['PL_BASEDIR'] = base_dir.tr('/', '\\')
194+
ENV['PUPPET_DIR'] = File.join(base_dir, 'puppet').tr('/', '\\')
195+
ENV['OPENSSL_CONF'] = File.join(base_dir, 'puppet', 'ssl', 'openssl.cnf').tr('/', '\\')
196+
ENV['SSL_CERT_DIR'] = File.join(base_dir, 'puppet', 'ssl', 'certs').tr('/', '\\')
197+
ENV['SSL_CERT_FILE'] = File.join(base_dir, 'puppet', 'ssl', 'cert.pem').tr('/', '\\')
198+
ENV['Path'] = [
199+
File.join(base_dir, 'puppet', 'bin'),
200+
File.join(base_dir, 'bin'),
201+
].join(';').tr('/', '\\') + ';' + ENV.fetch('Path', nil)
202+
203+
# ENV that uses forward slashes
204+
ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV.fetch('RUBYLIB', nil)}"
205+
rescue => e
206+
log_exception(e)
213207
end
214208
end
215209

lib/puppet/application.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,10 @@ def handle_logdest_arg(arg)
481481
Puppet[:logdest] = arg
482482

483483
logdest.each do |dest|
484-
begin
485-
Puppet::Util::Log.newdestination(dest)
486-
options[:setdest] = true
487-
rescue => detail
488-
Puppet.log_and_raise(detail, _("Could not set logdest to %{dest}.") % { dest: arg })
489-
end
484+
Puppet::Util::Log.newdestination(dest)
485+
options[:setdest] = true
486+
rescue => detail
487+
Puppet.log_and_raise(detail, _("Could not set logdest to %{dest}.") % { dest: arg })
490488
end
491489
end
492490

lib/puppet/application/device.rb

Lines changed: 99 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/puppet/configurer.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,12 @@ def retrieve_new_catalog(facts, query_options)
748748
end
749749

750750
def download_plugins(remote_environment_for_plugins)
751-
begin
752-
@handler.download_plugins(remote_environment_for_plugins)
753-
rescue Puppet::Error => detail
754-
if !Puppet[:ignore_plugin_errors] && Puppet[:usecacheonfailure]
755-
@running_failure = true
756-
else
757-
raise detail
758-
end
751+
@handler.download_plugins(remote_environment_for_plugins)
752+
rescue Puppet::Error => detail
753+
if !Puppet[:ignore_plugin_errors] && Puppet[:usecacheonfailure]
754+
@running_failure = true
755+
else
756+
raise detail
759757
end
760758
end
761759
end

0 commit comments

Comments
 (0)