From 94dca7149dab29875202e2f63afc761b45e2fd59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 5 May 2020 14:20:16 +0200 Subject: [PATCH] Refactor/lint (#15) --- .rubocop.yml | 1 + .rubocop_todo.yml | 21 +++++++++++++++++++++ .sync.yml | 6 ++++++ lib/puppet/catalog-diff/comparer.rb | 3 +++ lib/puppet/catalog-diff/compilecatalog.rb | 21 ++++++++------------- lib/puppet/catalog-diff/differ.rb | 3 +++ lib/puppet/catalog-diff/findcatalogs.rb | 3 +++ lib/puppet/catalog-diff/formater.rb | 3 +++ lib/puppet/catalog-diff/preprocessor.rb | 21 +++++++++++++++++++++ lib/puppet/catalog-diff/searchfacts.rb | 5 ++++- lib/puppet/face/catalog/pull.rb | 2 +- manifests/init.pp | 2 +- manifests/viewer.pp | 14 +++++++------- 13 files changed, 82 insertions(+), 23 deletions(-) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index 5307849..2006754 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -79,6 +79,7 @@ Style/TrailingCommaInLiteral: Style/SymbolArray: Description: Using percent style obscures symbolic intent of array's contents. EnforcedStyle: brackets +inherit_from: ".rubocop_todo.yml" RSpec/MessageSpies: EnforcedStyle: receive Style/Documentation: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..d73ee2b --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,21 @@ +--- +Lint/AssignmentInCondition: + Enabled: false +Lint/RescueException: + Enabled: false +Lint/ShadowingOuterLocalVariable: + Enabled: false +Metrics/LineLength: + Enabled: false +Performance/FixedSize: + Enabled: false +Security/MarshalLoad: + Enabled: false +Style/ClassVars: + Enabled: false +Style/MultilineBlockChain: + Enabled: false +Style/NumericPredicate: + Enabled: false +Style/RegexpLiteral: + Enabled: false diff --git a/.sync.yml b/.sync.yml index da68b37..0b4cd63 100644 --- a/.sync.yml +++ b/.sync.yml @@ -2,18 +2,24 @@ ".travis.yml": user: camptocamp secure: "oVFK/jAf7ZbNR8cdo2AcqwnKp1pktpa+EkImXGF2b6L7OxOl2VXxbBKGW1IueUccmDH381XqrMtdbCVgz83u3HdpkHb3PIFmchPgPeIBgevdubyfulHdwYN++nLL9zxRH+FXhW1qmqpvHFS8CHw4Ia2k69gdcQNNlC60WYk1QwxLYTnTaem2nWZsmNZBNgh5TFzTJzsbJQFoyJlUUkbkg7rZig3qSw/otqwh/HUrEA+UUcBv6B6cIeMaKzaGvHWvfdXv8asT+r/kl/048J2FZM1ulQxZE+273Z1xwp4KATx00NqpceFcvHAInbMkgYuwi3nDJo4pKpqKWKvlw1CJm9+uXk9qYgFhaQpmkz5Jc3njiPZadRITkYG4T9dUHlvN47LDJOZNCwr8kjEkakI1Rv/H7tFlxHG/42TJDAs9dqB4aoiflzo4GOBAsx+YtpTmM+1aWvdDjeedxnuVrGn1lgHkibm7kSiNZDdxQ4/Xv/yYmuko8kam/HdlXC7bg0myQE4zdHPAxcn6qnC6zifmssgonaGXlx+3Hmnqqb7F3vv5+w4Nb9VYekVc6JY0slyq5ADvbNkSL2dGx0qlX4dD2puRgZM/NFAGsJ0AANIIDGWBDb3nj8ofvpRs7ab48U2eRiLb+3vhnIK83S4CdNnZH7TidCv9Y6bAcuQkD0qlXUQ=" + ".gitlab-ci.yml": delete: true + ".rubocop.yml": default_configs: require: - rubocop-rspec + inherit_from: .rubocop_todo.yml + + Gemfile: use_litmus: true optional: ":development": - gem: github_changelog_generator condition: Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.2.2') + spec/spec_helper.rb: mock_with: ":rspec" coverage_report: true diff --git a/lib/puppet/catalog-diff/comparer.rb b/lib/puppet/catalog-diff/comparer.rb index efd6787..cf4cf1b 100644 --- a/lib/puppet/catalog-diff/comparer.rb +++ b/lib/puppet/catalog-diff/comparer.rb @@ -1,6 +1,9 @@ require 'puppet/util/diff' require 'digest' +# Puppet::CatalogDiff module Puppet::CatalogDiff + # Comparer providers methods + # to compare resources module Comparer # Creates an array of just the resource titles # it would be records like file["/foo"] diff --git a/lib/puppet/catalog-diff/compilecatalog.rb b/lib/puppet/catalog-diff/compilecatalog.rb index aec996f..77317d4 100644 --- a/lib/puppet/catalog-diff/compilecatalog.rb +++ b/lib/puppet/catalog-diff/compilecatalog.rb @@ -1,6 +1,12 @@ require 'puppet/network/http_pool' +# Puppet::CatalogDiff module Puppet::CatalogDiff + # Puppet::CatalogDiff::CompileCatalog + # allows to retrieve a catalog, using + # v3/catalog, v4/catalog or PuppetDB class CompileCatalog + include Puppet::CatalogDiff::Preprocessor + attr_reader :node_name def initialize(node_name, save_directory, server, certless, catalog_from_puppetdb) @@ -52,19 +58,8 @@ def get_catalog_from_puppetdb(node_name, server) rescue PSON::ParserError => e raise "Error parsing json output of puppetdb catalog query for #{node_name}: #{e.message}\ncontent: #{ret}" end - catalog = catalog[0] - # Fix "data" level in PuppetDB catalog - catalog['resources'] = catalog['resources']['data'] - # Fix edges - new_edges = [] - catalog['edges']['data'].each do |edge| - new_edges << { - 'source' => "#{edge['source_type']}[#{edge['source_title']}]", - 'target' => "#{edge['target_type']}[#{edge['target_title']}]", - } - end - catalog['edges'] = new_edges - catalog + + convert_pdb(catalog) end def compile_catalog(node_name, server, certless) diff --git a/lib/puppet/catalog-diff/differ.rb b/lib/puppet/catalog-diff/differ.rb index 9ccae55..c58539a 100644 --- a/lib/puppet/catalog-diff/differ.rb +++ b/lib/puppet/catalog-diff/differ.rb @@ -9,7 +9,10 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'preprocessor.rb')) require File.expand_path(File.join(File.dirname(__FILE__), 'comparer.rb')) require File.expand_path(File.join(File.dirname(__FILE__), 'formater.rb')) + +# Puppet::CatalogDiff module Puppet::CatalogDiff + # Differ allows to diff two catalogs class Differ include Puppet::CatalogDiff::Preprocessor include Puppet::CatalogDiff::Comparer diff --git a/lib/puppet/catalog-diff/findcatalogs.rb b/lib/puppet/catalog-diff/findcatalogs.rb index d128e03..70a5aa2 100644 --- a/lib/puppet/catalog-diff/findcatalogs.rb +++ b/lib/puppet/catalog-diff/findcatalogs.rb @@ -1,4 +1,7 @@ +# Puppet::CatalogDiff module Puppet::CatalogDiff + # FindCatalogs allows to find + # pairs of catalogs in two directories class FindCatalogs attr_accessor :old_catalog_path, :new_catalog_path diff --git a/lib/puppet/catalog-diff/formater.rb b/lib/puppet/catalog-diff/formater.rb index 9161429..3705ce3 100644 --- a/lib/puppet/catalog-diff/formater.rb +++ b/lib/puppet/catalog-diff/formater.rb @@ -1,5 +1,8 @@ require 'puppet/util/terminal' +# Puppet::CatalogDiff module Puppet::CatalogDiff + # Formater provides methods to format + # resources into diff-able Puppet DSL class Formater def initialize; end diff --git a/lib/puppet/catalog-diff/preprocessor.rb b/lib/puppet/catalog-diff/preprocessor.rb index 551adb3..1556267 100644 --- a/lib/puppet/catalog-diff/preprocessor.rb +++ b/lib/puppet/catalog-diff/preprocessor.rb @@ -1,4 +1,8 @@ +# Puppet::CatalogDiff module Puppet::CatalogDiff + # Puppet::CatalogDiff::Preprocessor + # provides methods to convert catalogs to + # the catalog-diff intermediate format module Preprocessor # capitalize a resource from ["class", "foo::bar"] to Class[Foo::Bar] # @@ -44,4 +48,21 @@ def convert25(resource, collector) end end end + + # Converts PuppetDB catalogs to our intermediate format + def convert_pdb(catalog) + catalog = catalog[0] + # Fix "data" level in PuppetDB catalog + catalog['resources'] = catalog['resources']['data'] + # Fix edges + new_edges = [] + catalog['edges']['data'].each do |edge| + new_edges << { + 'source' => "#{edge['source_type']}[#{edge['source_title']}]", + 'target' => "#{edge['target_type']}[#{edge['target_title']}]", + } + end + catalog['edges'] = new_edges + catalog + end end diff --git a/lib/puppet/catalog-diff/searchfacts.rb b/lib/puppet/catalog-diff/searchfacts.rb index f802ab0..761b9c7 100644 --- a/lib/puppet/catalog-diff/searchfacts.rb +++ b/lib/puppet/catalog-diff/searchfacts.rb @@ -1,7 +1,9 @@ require 'puppet/network/http_pool' require 'uri' require 'json' +# Puppet::CatalogDiff module Puppet::CatalogDiff + # SearchFacts returns facts from local data, Puppet API, or PuppetDB class SearchFacts def initialize(facts) @facts = Hash[facts.split(',').map { |f| f.split('=') }] @@ -102,5 +104,6 @@ def find_nodes_puppetdb(env) end names = filtered.map { |node| node['certname'] } names - end end + end + end end diff --git a/lib/puppet/face/catalog/pull.rb b/lib/puppet/face/catalog/pull.rb index f526263..c17153a 100644 --- a/lib/puppet/face/catalog/pull.rb +++ b/lib/puppet/face/catalog/pull.rb @@ -122,7 +122,7 @@ rescue Exception => e Puppet.err(e.to_s) end - end + end end }.each(&:join) output = {} diff --git a/manifests/init.pp b/manifests/init.pp index 19d64f4..ea19489 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,5 +1,5 @@ class catalog_diff( - $diff_master, + String $diff_master, ) { puppet_auth { 'allow the diff server to retrieve any catalog': ensure => present, diff --git a/manifests/viewer.pp b/manifests/viewer.pp index 72d861b..ac6c3f6 100644 --- a/manifests/viewer.pp +++ b/manifests/viewer.pp @@ -1,9 +1,9 @@ class catalog_diff::viewer ( - $remote = 'https://github.com/camptocamp/puppet-catalog-diff-viewer.git', - $password = 'puppet', - $revision = 'master', - $port = 1495, - $listen_ip = $ipaddress, + String $remote = 'https://github.com/camptocamp/puppet-catalog-diff-viewer.git', + String $password = 'puppet', + String $revision = 'master', + Integer $port = 1495, + String $listen_ip = $::ipaddress, ) { require git @@ -13,7 +13,7 @@ } apache::vhost {"${listen_ip}:${port}": - servername => $fqdn, + servername => $::fqdn, ip => $listen_ip, docroot => '/var/www/diff', ip_based => true, @@ -35,7 +35,7 @@ htpasswd { 'puppet': username => 'puppet', - cryptpasswd => ht_crypt($password,$uuid), + cryptpasswd => ht_crypt($password, $::uuid), target => '/var/www/.htpasswd', }