Skip to content

Commit

Permalink
new chefstyle rules for 0.13.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lamont-granquist committed Jul 8, 2019
1 parent 7e0dc7e commit aa65bdf
Show file tree
Hide file tree
Showing 69 changed files with 226 additions and 129 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ task :console do
IRB.start
end

task default: [:style, :spec]
task default: %i{style spec}
3 changes: 3 additions & 0 deletions lib/ohai/common/dmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ def convenience_keys(dmi)
in_common = Mash.new
next unless records.class.to_s == "Mash"
next unless records.key?("all_records")

records[:all_records].each do |record|
record.each do |field, value|
next if value.class.to_s == "Mash"
next if field.to_s == "application_identifier"
next if field.to_s == "size"
next if field.to_s == "record_id"

translated = field.downcase.gsub(/[^a-z0-9]/, "_")
value = value.strip
if in_common.key?(translated)
Expand All @@ -136,6 +138,7 @@ def convenience_keys(dmi)
end
in_common.each do |field, value|
next if value.nil?

dmi[type][field] = value.strip
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/ohai/dsl/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def set(name, *value)
def from(cmd)
_status, stdout, _stderr = run_command(command: cmd)
return "" if stdout.nil? || stdout.empty?

stdout.strip
end

Expand All @@ -155,6 +156,7 @@ def from_with_regex(cmd, *regex_list)
regex_list.flatten.each do |regex|
_status, stdout, _stderr = run_command(command: cmd)
return "" if stdout.nil? || stdout.empty?

stdout.chomp!.strip
md = stdout.match(regex)
return md[1]
Expand Down Expand Up @@ -210,6 +212,7 @@ def safe_get_attribute(*keys)
unless attrs.nil? || attrs.is_a?(Array) || attrs.is_a?(Hash)
raise TypeError.new("Expected Hash but got #{attrs.class}.")
end

attrs[key]
end
rescue NoMethodError
Expand Down
6 changes: 4 additions & 2 deletions lib/ohai/dsl/plugin/versionvii.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ def require_plugin(*args)

def configuration(option, *options)
return nil if plugin_config.nil? || !plugin_config.key?(option)

value = plugin_config[option]
options.each do |opt|
return nil unless value.key?(opt)

value = value[opt]
end
value
Expand All @@ -169,12 +171,12 @@ def fetch_plugin_config
# ["", "Memory"] => ["Memory"]
# ["", "Network", "", "Listeners"] => ["Network", "Listeners"]
# ["SSH", "Host", "", "Key"] => ["SSH", "Host", "Key"]
parts.delete_if { |part| part.empty? }
parts.delete_if(&:empty?)
# ["DMI"] => :dmi
# ["Memory"] => :memory
# ["Network", "Listeners"] => :network_listeners
# ["SSH", "Host", "Key"] => :ssh_host_key
snake_case_name = parts.map { |part| part.downcase }.join("_").to_sym
snake_case_name = parts.map(&:downcase).join("_").to_sym

# Plugin names in config hashes are auto-vivified, so we check with
# key? to avoid falsely instantiating a configuration hash.
Expand Down
4 changes: 3 additions & 1 deletion lib/ohai/hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ def self.parse_hint_file(filename)
def self.hint?(name)
@hints ||= {}
return @hints[name] if @hints[name]

Ohai.config[:hints_path].each do |path|
filename = File.join(path, "#{name}.json")
next unless File.exist?(filename)

Ohai::Log.trace("Found hint #{name}.json at #{filename}")
@hints[name] = parse_hint_file(filename)
end

Ohai::Log.trace("Did not find hint #{name}.json in the hint path(s): #{Ohai.config[:hints_path].join(', ')} ") unless @hints.key?(name)
Ohai::Log.trace("Did not find hint #{name}.json in the hint path(s): #{Ohai.config[:hints_path].join(", ")} ") unless @hints.key?(name)
@hints[name]
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/ohai/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def load_additional(from)
# @param plugin_path [String]
def load_plugin(plugin_path)
plugin_class = load_plugin_class(plugin_path)
return nil unless plugin_class.kind_of?(Class)
return nil unless plugin_class.is_a?(Class)

if plugin_class < Ohai::DSL::Plugin::VersionVII
load_v7_plugin(plugin_class)
else
Expand Down Expand Up @@ -137,9 +138,10 @@ def collect_v7_plugins
# @return [Ohai::DSL::Plugin::VersionVII] Ohai plugin object
def load_v7_plugin_class(contents, plugin_path)
plugin_class = eval(contents, TOPLEVEL_BINDING, plugin_path) # rubocop: disable Security/Eval
unless plugin_class.kind_of?(Class) && plugin_class < Ohai::DSL::Plugin
unless plugin_class.is_a?(Class) && plugin_class < Ohai::DSL::Plugin
raise Ohai::Exceptions::IllegalPluginDefinition, "Plugin file cannot contain any statements after the plugin definition"
end

plugin_class.sources << plugin_path
@v7_plugin_classes << plugin_class unless @v7_plugin_classes.include?(plugin_class)
plugin_class
Expand All @@ -159,6 +161,7 @@ def load_v7_plugin_class(contents, plugin_path)
parts = e.message.split(/<.*>[:[0-9]+]*: syntax error, /)
parts.each do |part|
next if part.length == 0

logger.warn("Plugin Syntax Error: <#{plugin_path}>: #{part}")
end
rescue Exception, Errno::ENOENT => e
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/mash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def self.from_hash(hash)
#
# @api private
def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
key.is_a?(Symbol) ? key.to_s : key
end

# @param value [Object] The value to convert.
Expand Down
1 change: 1 addition & 0 deletions lib/ohai/mixin/constant_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def recursive_remove_constants(object)
if object.respond_to?(:constants)
object.constants.each do |const|
next unless strict_const_defined?(object, const)

recursive_remove_constants(object.const_get(const))
object.send(:remove_const, const)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/ohai/mixin/ec2_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def best_api_version
if versions.empty?
raise "Mixin EC2: Unable to determine EC2 metadata version (no supported entries found)"
end

versions.last
end
end
Expand Down Expand Up @@ -134,7 +135,7 @@ def fetch_metadata(id = "", api_version = nil)
end

def fetch_dir_metadata(id, api_version)
metadata = Hash.new
metadata = {}
retrieved_metadata = metadata_get(id, api_version)
if retrieved_metadata
retrieved_metadata.split("\n").each do |o|
Expand Down Expand Up @@ -195,11 +196,11 @@ def expand_path(file_name)
# ignore "./" and "../"
path.gsub(%r{/\.\.?(?:/|$)}, "/")
.sub(%r{^\.\.?(?:/|$)}, "")
.sub(%r{^$}, "/")
.sub(/^$/, "/")
end

def metadata_key(key)
key.gsub(/\-|\//, "_")
key.gsub(%r{\-|/}, "_")
end

end
Expand Down
5 changes: 2 additions & 3 deletions lib/ohai/mixin/gce_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def http_get(uri)
conn.get(uri, {
"Metadata-Flavor" => "Google",
"User-Agent" => "chef-ohai/#{Ohai::VERSION}",
}
)
})
end

def fetch_metadata(id = "")
Expand Down Expand Up @@ -76,7 +75,7 @@ def has_trailing_slash?(data)
end

def sanitize_key(key)
key.gsub(/\-|\//, "_")
key.gsub(%r{\-|/}, "_")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/aix/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# 6390000 20000 63a0000 ba8 /usr/lib/drivers/if_en
# f1000000c0318000 20000 f1000000c0320000 17138 /usr/lib/drivers/random
so.stdout.lines do |line|
if line =~ /\s*([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([a-zA-Z0-9\/\._]+)/
if line =~ %r{\s*([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([a-zA-Z0-9/\._]+)}
modules[$5] = { text: { address: $1, size: $2 }, data: { address: $3, size: $4 } }
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ohai/plugins/aix/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def hex_to_dec_netmask(netmask)
iface[interface][:metric] = $1 if lin =~ /metric\s(\S+)/
else
# We have key value pairs.
if lin =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/
if lin =~ %r{inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(/(\d{1,2}))?}
tmp_addr, tmp_prefix = $1, $3
if tmp_prefix.nil?
netmask = hex_to_dec_netmask($1) if lin =~ /netmask\s(\S+)\s/
Expand All @@ -96,7 +96,7 @@ def hex_to_dec_netmask(netmask)
if lin =~ /broadcast\s(\S+)\s/
iface[interface][:addresses][tmp_addr][:broadcast] = $1
end
elsif lin =~ /inet6 ([a-f0-9\:]+)%?([\d]*)\/?(\d*)?/
elsif lin =~ %r{inet6 ([a-f0-9\:]+)%?([\d]*)/?(\d*)?}
# TODO do we have more properties on inet6 in aix? broadcast
iface[interface][:addresses] ||= Mash.new
iface[interface][:addresses][$1] = { "family" => "inet6", "zone_index" => $2, "prefixlen" => $3 }
Expand Down Expand Up @@ -128,7 +128,7 @@ def hex_to_dec_netmask(netmask)
so_n.stdout.lines.each do |line|
if line =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)/
interface = $6
iface[interface][:routes] = Array.new unless iface[interface][:routes]
iface[interface][:routes] = [] unless iface[interface][:routes]
iface[interface][:routes] << Mash.new( destination: $1, family: family,
via: $2, flags: $3)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ohai/plugins/aix/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
case title
when "network"
next if line =~ /^Interface|^---/

splat = line.strip.split
key = splat[0].downcase
value = {
Expand All @@ -68,6 +69,7 @@
wpars[wpar_name][title][key] = value
when "user-specified routes"
next if line =~ /^Type|^---/

splat = line.strip.split
key = splat[2].downcase
value = {
Expand All @@ -77,6 +79,7 @@
wpars[wpar_name][title][key] = value
when "file systems"
next if line =~ /^MountPoint|^---/

splat = line.strip.split
key = splat[1].downcase
value = {
Expand All @@ -99,6 +102,7 @@
wpars[wpar_name][title]["Privileges"] += privileges.split(",")
when "device exports"
next if line =~ /^Name|^---/

splat = line.strip.split
key = splat[0].downcase
value = {
Expand Down
1 change: 1 addition & 0 deletions lib/ohai/plugins/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def parse_metadata

endpoint_data = fetch_metadata
return nil if endpoint_data.nil?

metadata = initialize_metadata_mash_compute

# blindly add everything in compute to our data structure
Expand Down
11 changes: 7 additions & 4 deletions lib/ohai/plugins/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ def initialize

def add_ipv4_addr(ip, accessibility)
return if ip.nil? # just skip if ip is nil

ipaddr = validate_ip_addr(ip, :ipv4)

case accessibility
when :public
@cloud[:public_ipv4_addrs] ||= Array.new
@cloud[:public_ipv4_addrs] ||= []
@cloud[:public_ipv4_addrs] << ipaddr.to_s
when :private
@cloud[:local_ipv4_addrs] ||= Array.new
@cloud[:local_ipv4_addrs] ||= []
@cloud[:local_ipv4_addrs] << ipaddr.to_s
else
raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
Expand All @@ -69,15 +70,17 @@ def add_ipv4_addr(ip, accessibility)

def add_ipv6_addr(ip, accessibility)
return if ip.nil? # just skip if ip is nil

ipaddr = validate_ip_addr(ip, :ipv6)

raise "ERROR: invalid ipv6 address of '#{ip}' detected. " unless ipaddr.ipv6?

case accessibility
when :public
@cloud[:public_ipv6_addrs] ||= Array.new
@cloud[:public_ipv6_addrs] ||= []
@cloud[:public_ipv6_addrs] << ipaddr.to_s
when :private
@cloud[:local_ipv6_addrs] ||= Array.new
@cloud[:local_ipv6_addrs] ||= []
@cloud[:local_ipv6_addrs] << ipaddr.to_s
else
raise "ERROR: invalid accessibility param of '#{accessibility}'. must be :public or :private."
Expand Down
12 changes: 6 additions & 6 deletions lib/ohai/plugins/cpu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def parse_bsd_dmesg(&block)
cpu_number += 1
when /vendor_id\s+:\s(.+)/
vendor_id = $1
if vendor_id =~ (/IBM\/S390/)
if vendor_id =~ (%r{IBM/S390})
cpuinfo["vendor_id"] = vendor_id
else
cpuinfo[current_cpu]["vendor_id"] = vendor_id
Expand Down Expand Up @@ -173,9 +173,9 @@ def parse_bsd_dmesg(&block)
cpuinfo["model"] = $3.to_i(16).to_s
cpuinfo["stepping"] = $4
# These _should_ match /AMD Features2?/ lines as well
when /FreeBSD\/SMP: Multiprocessor System Detected: (\d*) CPUs/
when %r{FreeBSD/SMP: Multiprocessor System Detected: (\d*) CPUs}
cpuinfo["total"] = $1.to_i
when /FreeBSD\/SMP: (\d*) package\(s\) x (\d*) core\(s\)/
when %r{FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)}
cpuinfo["real"] = $1.to_i
cpuinfo["cores"] = $1.to_i * $2.to_i
end
Expand Down Expand Up @@ -336,8 +336,8 @@ def parse_bsd_dmesg(&block)
cpu["cpustates"] = Mash.new

currentcpu = 0
cpucores = Array.new
cpusockets = Array.new
cpucores = []
cpusockets = []
processor_info.each do |processor|
_desc, instance, _record, keyvalue = processor.split(":")
cpu[instance] ||= Mash.new
Expand Down Expand Up @@ -406,7 +406,7 @@ def parse_bsd_dmesg(&block)
cpu[current_cpu]["model_name"] = processor["name"]
cpu[current_cpu]["description"] = processor["description"]
cpu[current_cpu]["mhz"] = processor["maxclockspeed"].to_s
cpu[current_cpu]["cache_size"] = "#{processor['l2cachesize']} KB"
cpu[current_cpu]["cache_size"] = "#{processor["l2cachesize"]} KB"
end

cpu[:total] = logical_processors
Expand Down
Loading

0 comments on commit aa65bdf

Please sign in to comment.