Skip to content

Commit

Permalink
Chefstyle fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 committed Nov 18, 2019
1 parent a975db1 commit 16d6513
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ task :console do
IRB.start
end

task default: [:style, :spec]
task default: %i{style spec}
2 changes: 1 addition & 1 deletion knife-tidy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "knife-tidy/version"
Gem::Specification.new do |s|
s.name = "knife-tidy"
s.version = KnifeTidy::VERSION
s.version = "#{s.version}-pre#{ENV['TRAVIS_BUILD_NUMBER']}" if ENV["TRAVIS"]
s.version = "#{s.version}-pre#{ENV["TRAVIS_BUILD_NUMBER"]}" if ENV["TRAVIS"]
s.authors = ["Jeremy Miller"]
s.email = ["[email protected]"]
s.summary = "Report on stale Chef Server nodes and cookbooks and clean up data integrity issues in a knife-ec-backup object based backup"
Expand Down
5 changes: 3 additions & 2 deletions lib/chef/knife/tidy_backup_clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def fix_self_dependencies(org)
Chef::TidySubstitutions.new(nil, tidy).sub_in_file(
::File.join(cookbook_path, "metadata.rb"),
Regexp.new("^depends +['\"]#{name}['\"]"),
"# depends '#{name}' # knife-tidy was here")
"# depends '#{name}' # knife-tidy was here"
)
end
end

Expand Down Expand Up @@ -396,7 +397,7 @@ def validate_clients_group(org)
existing_group_data = FFI_Yajl::Parser.parse(::File.read(clients_group_path), symbolize_names: false)
existing_group_data["clients"] = [] unless existing_group_data.key?("clients")
if existing_group_data["clients"].length != tidy.client_names(org).length
ui.stdout.puts "REPAIRING: Adding #{(existing_group_data['clients'].length - tidy.client_names(org).length).abs} missing clients into #{org}'s client group file #{clients_group_path}"
ui.stdout.puts "REPAIRING: Adding #{(existing_group_data["clients"].length - tidy.client_names(org).length).abs} missing clients into #{org}'s client group file #{clients_group_path}"
existing_group_data["clients"] = (existing_group_data["clients"] + tidy.client_names(org)).uniq
::File.open(clients_group_path, "w") do |f|
f.write(Chef::JSONCompat.to_json_pretty(existing_group_data))
Expand Down
58 changes: 29 additions & 29 deletions lib/chef/knife/tidy_notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,56 @@ class TidyNotify < Knife
banner "knife tidy notify (options)"

option :smtp_server,
short: "-s SERVER_NAME",
long: "--smtp_server SERVER_NAME",
default: "localhost",
description: "SMTP Server to be used for emailling reports to organization admins (defaults to localhost)"
short: "-s SERVER_NAME",
long: "--smtp_server SERVER_NAME",
default: "localhost",
description: "SMTP Server to be used for emailling reports to organization admins (defaults to localhost)"

option :smtp_port,
short: "-p SMTP_PORT",
long: "--smtp_port SMTP_PORT",
default: 25,
description: "SMTP port to be used for emailling reports to organization admins (defaults to 25)"
short: "-p SMTP_PORT",
long: "--smtp_port SMTP_PORT",
default: 25,
description: "SMTP port to be used for emailling reports to organization admins (defaults to 25)"

option :smtp_helo,
short: "-h SMTP_HELO",
long: "--smtp_helo SMTP_HELO",
default: "localhost",
description: "SMTP HELO to be used for emailling reports to organization admins (defaults to localhost)"
short: "-h SMTP_HELO",
long: "--smtp_helo SMTP_HELO",
default: "localhost",
description: "SMTP HELO to be used for emailling reports to organization admins (defaults to localhost)"

option :smtp_username,
short: "-u SMTP_USERNAME",
long: "--smtp_username SMTP_USERNAME",
description: "SMTP Username to be used for emailling reports to organization admins"
short: "-u SMTP_USERNAME",
long: "--smtp_username SMTP_USERNAME",
description: "SMTP Username to be used for emailling reports to organization admins"

option :smtp_password,
long: "--smtp_password SMTP_PASSWORD",
description: "SMTP Password to be used for emailling reports to organization admins"
long: "--smtp_password SMTP_PASSWORD",
description: "SMTP Password to be used for emailling reports to organization admins"

option :smtp_from,
long: "--smtp_from SMTP_FROM",
description: "SMTP From address to be used for emailling reports to organization admins"
long: "--smtp_from SMTP_FROM",
description: "SMTP From address to be used for emailling reports to organization admins"

option :smtp_use_tls,
long: "--smtp_use_tls",
short: "-t",
default: false,
boolean: true | false,
description: "Whether TLS should be used for emailling reports to organization admins (defaults to false if omitted)"
long: "--smtp_use_tls",
short: "-t",
default: false,
boolean: true | false,
description: "Whether TLS should be used for emailling reports to organization admins (defaults to false if omitted)"

include Knife::TidyBase

def run
reports_dir = tidy.reports_dir
report_file_suffixes = ["_unused_cookbooks.json", "_cookbook_count.json", "_stale_nodes.json"]
# Only grab the files matching the report_file_suffixes
report_files = Dir["#{reports_dir}/*{#{report_file_suffixes.join(',')}}"]
report_files = Dir["#{reports_dir}/*{#{report_file_suffixes.join(",")}}"]

ui.info "Reading from #{tidy.reports_dir} directory"

# Fetch list of organization names from reports directory
begin
org_names = report_files.map { |r_file| r_file.match("#{reports_dir}\/(.*)(#{report_file_suffixes.join('|')})").captures.first }.uniq
org_names = report_files.map { |r_file| r_file.match("#{reports_dir}\/(.*)(#{report_file_suffixes.join("|")})").captures.first }.uniq
rescue NoMethodError
ui.stderr.puts "Failed to parse json reports files. Please ensure your reports are valid."
return
Expand Down Expand Up @@ -120,7 +120,7 @@ def generate_email(report_data, organization, recipients, report_file_suffixes)
mime_boundary = "==Multipart_Boundary_x#{srand}x"
message = <<~MESSAGE_END
From: Knife Tidy <#{config[:smtp_from]}>
To: #{recipients.map { |recipient| "#{recipient[:name]} <#{recipient[:email]}>" }.join(', ')}
To: #{recipients.map { |recipient| "#{recipient[:name]} <#{recipient[:email]}>" }.join(", ")}
MIME-Version: 1.0
Subject: Knife Tidy Cleanup Report for Organization "#{organization}"
Content-Type: multipart/mixed; boundary="#{mime_boundary}";
Expand Down Expand Up @@ -172,13 +172,13 @@ def generate_unused_cookbooks_table(report_data, organization)
table_body = if report_data[organization]["_unused_cookbooks.json"].empty?
"<tr><td colspan='2'>No unused cookbook versions</td></tr>"
else
report_data[organization]["_unused_cookbooks.json"].map { |cookbook_name, cookbook_versions| "<tr><td>#{cookbook_name}</td><td>#{cookbook_versions.join('<br>')}</td></tr>" }.join("\n")
report_data[organization]["_unused_cookbooks.json"].map { |cookbook_name, cookbook_versions| "<tr><td>#{cookbook_name}</td><td>#{cookbook_versions.join("<br>")}</td></tr>" }.join("\n")
end
table_start + header_string + table_body + table_end
end

def generate_node_table(report_data, organization)
table_start = "<h2>Stale Nodes</h2><p>This table contains nodes that have not checked in to the Chef Server in #{report_data[organization]['_stale_nodes.json']['threshold_days']} days.<p><table border='1' cellpadding='1' cellspacing='0'>"
table_start = "<h2>Stale Nodes</h2><p>This table contains nodes that have not checked in to the Chef Server in #{report_data[organization]["_stale_nodes.json"]["threshold_days"]} days.<p><table border='1' cellpadding='1' cellspacing='0'>"
table_end = "</table>"
header_string = "<tr><th>Node Name</th></tr>"
table_body = if report_data[organization]["_stale_nodes.json"].empty? || report_data[organization]["_stale_nodes.json"]["count"] == 0
Expand Down
2 changes: 2 additions & 0 deletions lib/chef/knife/tidy_server_clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def clean_cookbooks(org)
queue = Chef::Util::ThreadedJobQueue.new
unused_cookbooks_file = ::File.join(tidy.reports_dir, "#{org}_unused_cookbooks.json")
return unless ::File.exist?(unused_cookbooks_file)

ui.stdout.puts "INFO: Cleaning cookbooks for Org: #{org}, using #{unused_cookbooks_file}"
unused_cookbooks = FFI_Yajl::Parser.parse(::File.read(unused_cookbooks_file), symbolize_names: true)
unused_cookbooks.keys.each do |cookbook|
Expand All @@ -115,6 +116,7 @@ def clean_nodes(org)
queue = Chef::Util::ThreadedJobQueue.new
stale_nodes_file = ::File.join(tidy.reports_dir, "#{org}_stale_nodes.json")
return unless ::File.exist?(stale_nodes_file)

ui.stdout.puts "INFO: Cleaning stale nodes for Org: #{org}, using #{stale_nodes_file}"
stale_nodes = FFI_Yajl::Parser.parse(::File.read(stale_nodes_file), symbolize_names: true)
stale_nodes[:list].each do |node|
Expand Down
6 changes: 4 additions & 2 deletions lib/chef/knife/tidy_server_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run

nodes.each do |node|
# If the node hasn't checked in.
if !node["chef_packages"]
unless node["chef_packages"]
# If the node is under an hour old.
if (Time.now.to_i - node["ohai_time"].to_i) < 3600
unconverged_recent_nodes << node["name"]
Expand Down Expand Up @@ -207,8 +207,9 @@ def check_cookbook_list(cb_list, cb, version)
else
versions_not_satisfied.push(v)
end

if v == cb_list[cb].last
ui.warn("Pin of #{cb} #{version} not satisfied by current versions of cookbook: [#{versions_not_satisfied.join(', ')}]")
ui.warn("Pin of #{cb} #{version} not satisfied by current versions of cookbook: [#{versions_not_satisfied.join(", ")}]")
end
end
else
Expand All @@ -221,6 +222,7 @@ def check_environment_pins(used_cookbooks, pins, cb_list)
pins.each do |cb, versions|
versions.each do |version|
next if version == "<= 0.0.0"

if used_cookbooks[cb]
# This pinned cookbook is in the used list, now check for a matching version.
used_cookbooks[cb].each do |v|
Expand Down
1 change: 1 addition & 0 deletions lib/chef/tidy_acls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def validate_acls
actors_groups = acl_actors_groups(acl)
actors_groups[:actors].each do |actor|
next if actor == "pivotal"

if ambiguous_actor?(actor)
fix_ambiguous_actor(actor)
elsif missing_from_members?(actor)
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/tidy_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def unique_email
end

def save_user(user)
::File.open(::File.join(users_path, "#{user['username']}.json"), "w+") do |f|
::File.open(::File.join(users_path, "#{user["username"]}.json"), "w+") do |f|
f.write(FFI_Yajl::Encoder.encode(user, pretty: true))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/tidy_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(root_url)
end

def self.from_chef_server_url(url)
url = url.gsub(/\/organizations\/+[^\/]+\/*$/, "")
url = url.gsub(%r{/organizations/+[^/]+/*$}, "")
Chef::Server.new(url)
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/chef/tidy_substitutions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def boiler_plate
FileUtils.cp(bp, ::File.join(Dir.pwd, "substitutions.json"))
end

def revert
end
def revert; end

def sub_in_file(path, search, replace)
temp_file = Tempfile.new("tidy")
Expand Down

0 comments on commit 16d6513

Please sign in to comment.