Skip to content

Commit

Permalink
Merge pull request #2991 from robertcheramy/2983_fix_rubocop_warnings
Browse files Browse the repository at this point in the history
Fix rubocop warnings
  • Loading branch information
aschaber1 authored Dec 6, 2023
2 parents b1d502f + 34e8fa5 commit e81eb77
Show file tree
Hide file tree
Showing 48 changed files with 141 additions and 219 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ AllCops:
Exclude:
- 'vendor/**/*'

Gemspec/DevelopmentDependencies:
EnforcedStyle: gemspec

Style/StringLiterals:
Enabled: false

Expand Down
95 changes: 6 additions & 89 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-04-14 17:17:03 UTC using RuboCop version 1.48.1.
# on 2023-11-27 13:35:47 UTC using RuboCop version 1.57.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Configuration parameters: AllowComments, AllowNil.
Lint/SuppressedException:
Exclude:
- 'lib/oxidized/input/ssh.rb'
- 'lib/oxidized/input/telnet.rb'
- 'lib/oxidized/nodes.rb'

# Offense count: 62
# Offense count: 64
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 92
Expand All @@ -30,42 +22,18 @@ Metrics/PerceivedComplexity:
Max: 12

# Offense count: 1
Naming/AccessorMethodName:
Exclude:
- 'lib/refinements.rb'

# Offense count: 8
Naming/ConstantName:
Exclude:
- 'extra/rest_client.rb'
- 'lib/oxidized/config.rb'
- 'lib/oxidized/hook.rb'
- 'lib/oxidized/input/ftp.rb'
- 'lib/oxidized/input/input.rb'
- 'lib/oxidized/input/ssh.rb'
- 'lib/oxidized/input/telnet.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
Naming/MemoizedInstanceVariableName:
Exclude:
- 'lib/refinements.rb'

# Offense count: 9
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: PreferredName.
Naming/RescuedExceptionsVariableName:
Exclude:
- 'bin/oxidized'
- 'extra/rest_client.rb'
- 'extra/syslog.rb'
- 'lib/oxidized/cli.rb'
- 'lib/oxidized/config.rb'
- 'lib/oxidized/node.rb'
- 'lib/oxidized/nodes.rb'
- 'lib/oxidized/source/sql.rb'

# Offense count: 2
Rake/DuplicateTask:
Exclude:
Expand All @@ -76,14 +44,13 @@ Security/Eval:
Exclude:
- 'Rakefile'

# Offense count: 3
# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'lib/oxidized/config/vars.rb'
- 'lib/oxidized/nodes.rb'

# Offense count: 2
Style/ClassVars:
Expand All @@ -98,58 +65,14 @@ Style/DoubleNegation:
Exclude:
- 'lib/oxidized/hook/exec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/GlobalStdStream:
Exclude:
- 'lib/oxidized.rb'

# Offense count: 3
Style/OpenStructUse:
Exclude:
- 'lib/oxidized/hook.rb'
- 'lib/oxidized/node.rb'
- 'spec/hook/githubrepo_spec.rb'

# Offense count: 27
# This cop supports safe autocorrection (--autocorrect).
Style/RedundantRegexpCharacterClass:
Exclude:
- 'lib/oxidized/model/axos.rb'
- 'lib/oxidized/model/comtrol.rb'
- 'lib/oxidized/model/fabricos.rb'
- 'lib/oxidized/model/ironware.rb'
- 'lib/oxidized/model/mlnxos.rb'
- 'lib/oxidized/model/nxos.rb'
- 'lib/oxidized/model/slxos.rb'
- 'lib/oxidized/model/sonicos.rb'
- 'lib/oxidized/model/speedtouch.rb'
- 'lib/oxidized/model/telco.rb'
- 'lib/oxidized/model/ucs.rb'
- 'lib/oxidized/model/voltaire.rb'
- 'lib/oxidized/model/zhoneolt.rb'
- 'lib/oxidized/model/zynoscli.rb'

# Offense count: 37
# This cop supports safe autocorrection (--autocorrect).
Style/RedundantRegexpEscape:
Exclude:
- 'lib/oxidized/model/adva.rb'
- 'lib/oxidized/model/airfiber.rb'
- 'lib/oxidized/model/aosw.rb'
- 'lib/oxidized/model/c4cmts.rb'
- 'lib/oxidized/model/dellx.rb'
- 'lib/oxidized/model/eltex.rb'
- 'lib/oxidized/model/enterasys800.rb'
- 'lib/oxidized/model/netonix.rb'
- 'lib/oxidized/model/netscaler.rb'
- 'lib/oxidized/model/openbsd.rb'
- 'lib/oxidized/model/siklu.rb'
- 'lib/oxidized/model/slxos.rb'
- 'lib/oxidized/model/sonicos.rb'
- 'lib/oxidized/model/trango.rb'

# Offense count: 45
# Offense count: 47
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, AllowInnerSlashes.
# SupportedStyles: slashes, percent_r, mixed
Expand All @@ -161,14 +84,8 @@ Style/RegexpLiteral:
Style/SlicingWithRange:
Enabled: false

# Offense count: 81
# Offense count: 82
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Mode.
Style/StringConcatenation:
Enabled: false

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/ZeroLengthPredicate:
Exclude:
- 'lib/oxidized/core.rb'
4 changes: 2 additions & 2 deletions bin/oxidized
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ trap("INT") { exit } # sinatra will otherwise steal this from us
begin
require_relative '../lib/oxidized/cli'
Oxidized::CLI.new.run
rescue StandardError => error
warn error.to_s
rescue StandardError => e
warn e
debug = Oxidized.config.debug rescue true
raise if debug

Expand Down
4 changes: 2 additions & 2 deletions extra/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Config

begin
CFGS.load
rescue StandardError => error
raise InvalidConfig, "Error loading config: #{error.message}"
rescue StandardError => e
raise InvalidConfig, "Error loading config: #{e.message}"
end

restcfg = CFGS.cfg.rest
Expand Down
4 changes: 2 additions & 2 deletions extra/syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class Config

begin
CFGS.load
rescue StandardError => error
raise InvalidConfig, "Error loading config: #{error.message}"
rescue StandardError => e
raise InvalidConfig, "Error loading config: #{e.message}"
ensure
CFG = CFGS.cfg # convenienence, instead of Config.cfg.password, CFG.password
end
Expand Down
4 changes: 2 additions & 2 deletions lib/oxidized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.logger=(val)
end

def self.setup_logger
FileUtils.mkdir_p(Config::Log) unless File.directory?(Config::Log)
FileUtils.mkdir_p(Config::LOG) unless File.directory?(Config::LOG)
self.logger = if config.has_key?('use_syslog') && config.use_syslog
require 'syslog/logger'
Syslog::Logger.new('oxidized')
Expand All @@ -45,7 +45,7 @@ def self.setup_logger
if config.has_key?('log')
Logger.new(File.expand_path(config.log))
else
Logger.new(STDERR)
Logger.new($stderr)
end
end
logger.level = Logger::INFO unless config.debug
Expand Down
8 changes: 4 additions & 4 deletions lib/oxidized/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def run
begin
Oxidized.logger.info "Oxidized starting, running as pid #{$PROCESS_ID}"
Oxidized.new
rescue StandardError => error
crash error
rescue StandardError => e
crash e
raise
end
end
Expand All @@ -29,8 +29,8 @@ def initialize
end

def crash(error)
Oxidized.logger.fatal "Oxidized crashed, crashfile written in #{Config::Crash}"
File.open Config::Crash, 'w' do |file|
Oxidized.logger.fatal "Oxidized crashed, crashfile written in #{Config::CRASH}"
File.open Config::CRASH, 'w' do |file|
file.puts '-' * 50
file.puts Time.now.utc
file.puts error.message + ' [' + error.class.to_s + ']'
Expand Down
28 changes: 14 additions & 14 deletions lib/oxidized/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ class NoConfig < OxidizedError; end
class InvalidConfig < OxidizedError; end

class Config
Root = ENV['OXIDIZED_HOME'] || File.join(Dir.home, '.config', 'oxidized')
Crash = File.join(ENV['OXIDIZED_LOGS'] || Root, 'crash')
Log = File.join(ENV['OXIDIZED_LOGS'] || Root, 'logs')
InputDir = File.join Directory, %w[lib oxidized input]
OutputDir = File.join Directory, %w[lib oxidized output]
ModelDir = File.join Directory, %w[lib oxidized model]
SourceDir = File.join Directory, %w[lib oxidized source]
HookDir = File.join Directory, %w[lib oxidized hook]
Sleep = 1
ROOT = ENV['OXIDIZED_HOME'] || File.join(Dir.home, '.config', 'oxidized')
CRASH = File.join(ENV['OXIDIZED_LOGS'] || ROOT, 'crash')
LOG = File.join(ENV['OXIDIZED_LOGS'] || ROOT, 'logs')
INPUT_DIR = File.join Directory, %w[lib oxidized input]
OUTPUT_DIR = File.join Directory, %w[lib oxidized output]
MODEL_DIR = File.join Directory, %w[lib oxidized model]
SOURCE_DIR = File.join Directory, %w[lib oxidized source]
HOOK_DIR = File.join Directory, %w[lib oxidized hook]
SLEEP = 1

def self.load(cmd_opts = {})
asetus = Asetus.new(name: 'oxidized', load: false, key_to_s: true, usrdir: Oxidized::Config::Root)
asetus = Asetus.new(name: 'oxidized', load: false, key_to_s: true, usrdir: Oxidized::Config::ROOT)
Oxidized.asetus = asetus

asetus.default.username = 'username'
Expand All @@ -36,9 +36,9 @@ def self.load(cmd_opts = {})
asetus.default.groups = {} # group level configuration
asetus.default.group_map = {} # map aliases of groups to names
asetus.default.models = {} # model level configuration
asetus.default.pid = File.join(Oxidized::Config::Root, 'pid')
asetus.default.pid = File.join(Oxidized::Config::ROOT, 'pid')

asetus.default.crash.directory = File.join(Oxidized::Config::Root, 'crashes')
asetus.default.crash.directory = File.join(Oxidized::Config::ROOT, 'crashes')
asetus.default.crash.hostnames = false

asetus.default.stats.history_size = 10
Expand All @@ -58,8 +58,8 @@ def self.load(cmd_opts = {})

begin
asetus.load # load system+user configs, merge to Config.cfg
rescue StandardError => error
raise InvalidConfig, "Error loading config: #{error.message}"
rescue StandardError => e
raise InvalidConfig, "Error loading config: #{e.message}"
end

raise NoConfig, 'edit ~/.config/oxidized/config' if asetus.create
Expand Down
4 changes: 2 additions & 2 deletions lib/oxidized/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(_args)
Oxidized.mgr = Manager.new
Oxidized.hooks = HookManager.from_config(Oxidized.config)
nodes = Nodes.new
raise NoNodesFound, 'source returns no usable nodes' if nodes.size.zero?
raise NoNodesFound, 'source returns no usable nodes' if nodes.empty?

@worker = Worker.new nodes
trap('HUP') { nodes.load }
Expand All @@ -33,7 +33,7 @@ def initialize(_args)

def run
Oxidized.logger.debug "lib/oxidized/core.rb: Starting the worker..."
@worker.work while sleep Config::Sleep
@worker.work while sleep Config::SLEEP
end
end
end
6 changes: 3 additions & 3 deletions lib/oxidized/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HookContext < OpenStruct; end
# RegisteredHook is a container for a Hook instance
RegisteredHook = Struct.new(:name, :hook)

Events = %i[
EVENTS = %i[
node_success
node_fail
post_store
Expand All @@ -32,9 +32,9 @@ def initialize
end

def register(event, name, hook_type, cfg)
unless Events.include? event
unless EVENTS.include? event
raise ArgumentError,
"unknown event #{event}, available: #{Events.join ','}"
"unknown event #{event}, available: #{EVENTS.join ','}"
end

Oxidized.mgr.add_hook(hook_type) || raise("cannot load hook '#{hook_type}', not found")
Expand Down
2 changes: 1 addition & 1 deletion lib/oxidized/input/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Exec < Input

def connect(node)
@node = node
@log = File.open(Oxidized::Config::Log + "/#{@node.ip}-exec", "w") if Oxidized.config.input.debug?
@log = File.open(Oxidized::Config::LOG + "/#{@node.ip}-exec", "w") if Oxidized.config.input.debug?
@node.model.cfg["exec"].each { |cb| instance_exec(&cb) }
end

Expand Down
4 changes: 2 additions & 2 deletions lib/oxidized/input/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Oxidized
require_relative 'cli'

class FTP < Input
RescueFail = {
RESCUE_FAIL = {
debug: [
# Net::SSH::Disconnect,
],
Expand All @@ -18,7 +18,7 @@ class FTP < Input
def connect(node)
@node = node
@node.model.cfg['ftp'].each { |cb| instance_exec(&cb) }
@log = File.open(Oxidized::Config::Log + "/#{@node.ip}-ftp", 'w') if Oxidized.config.input.debug?
@log = File.open(Oxidized::Config::LOG + "/#{@node.ip}-ftp", 'w') if Oxidized.config.input.debug?
@ftp = Net::FTP.new(@node.ip)
@ftp.passive = Oxidized.config.input.ftp.passive
@ftp.login @node.auth[:username], @node.auth[:password]
Expand Down
2 changes: 1 addition & 1 deletion lib/oxidized/input/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def connect(node)
@username = nil
@password = nil
@headers = {}
@log = File.open(Oxidized::Config::Log + "/#{@node.ip}-http", "w") if Oxidized.config.input.debug?
@log = File.open(Oxidized::Config::LOG + "/#{@node.ip}-http", "w") if Oxidized.config.input.debug?
@node.model.cfg["http"].each { |cb| instance_exec(&cb) }

return true unless @main_page && defined?(login)
Expand Down
2 changes: 1 addition & 1 deletion lib/oxidized/input/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PromptUndetect < OxidizedError; end
class Input
include Oxidized::Config::Vars

RescueFail = {
RESCUE_FAIL = {
debug: [
Errno::ECONNREFUSED
],
Expand Down
4 changes: 2 additions & 2 deletions lib/oxidized/input/scp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Oxidized
require_relative 'cli'

class SCP < Input
RescueFail = {
RESCUE_FAIL = {
debug: [
# Net::SSH::Disconnect,
],
Expand All @@ -18,7 +18,7 @@ class SCP < Input
def connect(node)
@node = node
@node.model.cfg['scp'].each { |cb| instance_exec(&cb) }
@log = File.open(Oxidized::Config::Log + "/#{@node.ip}-scp", 'w') if Oxidized.config.input.debug?
@log = File.open(Oxidized::Config::LOG + "/#{@node.ip}-scp", 'w') if Oxidized.config.input.debug?
@ssh = Net::SSH.start(@node.ip, @node.auth[:username], password: @node.auth[:password])
connected?
end
Expand Down
Loading

0 comments on commit e81eb77

Please sign in to comment.