From 1366184e6faecc7cdaaeffa705d3012ad52ced12 Mon Sep 17 00:00:00 2001 From: Miguel Negron Date: Sat, 1 Jun 2024 15:38:45 +0100 Subject: [PATCH 1/4] lint providers --- resources/providers/config.rb | 305 +++++++++++++++++----------------- 1 file changed, 148 insertions(+), 157 deletions(-) diff --git a/resources/providers/config.rb b/resources/providers/config.rb index 9ef3300..176c94c 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -1,12 +1,8 @@ - -# Cookbook Name:: rb-exporter -# +# Cookbook:: rb-exporter # Provider:: config -# action :add do begin - user = new_resource.user config_dir = new_resource.config_dir arp_ifaces = [] @@ -16,7 +12,7 @@ flush_cache[:before] end - execute "create_user" do + execute 'create_user' do command "/usr/sbin/useradd -r #{user}" ignore_failure true not_if "getent passwd #{user}" @@ -25,156 +21,153 @@ directory config_dir do action :create end - - template "/etc/rsyslog.d/arp.conf" do - source "rsyslog_arp_conf.erb" - owner "root" - group "root" - cookbook "rb-exporter" - mode 0644 + + template '/etc/rsyslog.d/arp.conf' do + source 'rsyslog_arp_conf.erb' + owner 'root' + group 'root' + cookbook 'rb-exporter' + mode '0644' retries 2 notifies :restart, 'service[rsyslog]', :delayed - end - - if node["interfaces"] and !node["interfaces"].empty? - - node["interfaces"].each do |iface_key, orig_iface| - iface = node["interfaces"][iface_key].to_hash.clone - - arp_ifaces.push(iface_key) if iface["arp"] == "true" - - [ "rb-exporter" ].each do |s| - [ "restart", "stop", "start" ].each do |s_action| - execute "#{s_action}_#{s}_#{iface_key}" do - command "/bin/env WAIT=1 /etc/init.d/#{s} #{s_action} #{iface_key}" - ignore_failure false - action :nothing - end - end - end - - if !iface["dstAddress"].empty? - - execute "iface_restart_#{iface_key}" do - command "ifconfig #{iface_key} down && ifconfig #{iface_key} up" - ignore_failure false - action :nothing - end - - template "/etc/sysconfig/network-scripts/ifcfg-#{iface_key}" do - source "ifcfg.erb" - owner "root" - group "root" - cookbook "rb-exporter" - mode 0644 - retries 2 - variables(:iface => iface_key, :iface_type => iface["iface_type"], :iface_ip => iface["iface_ip"], :iface_netmask => iface["iface_netmask"], :iface_gateway => iface["iface_gateway"]) - notifies :run, "execute[iface_restart_#{iface_key}]", :immediately if iface_key != "eth0" - end - - template "/etc/logrotate.d/rb-exporter-#{iface_key}" do - source "rb-exporter_log-rotate.erb" - owner "root" - group "root" - cookbook "rb-exporter" - mode 0644 - retries 2 - variables(:iface => iface_key) - end - - directory "/var/log/rb-exporter/#{iface_key}" do - owner "root" - group "root" - mode 0755 - recursive true - action :create - end - - directory "/etc/rb-exporter/#{iface_key}" do - owner "root" - group "root" - mode 0755 - recursive true - action :create - end - - # Calculate observation_id - observation_id = (iface["observationId"] and !iface["observationId"].empty?) ? iface["observationId"] : nil - observation_id = 4294967295 if observation_id.nil? and iface["type"].downcase.include? "sflow" - - template "/etc/rb-exporter/#{iface_key}/rb-exporter.conf" do - source "rb-exporter_conf.erb" - owner "root" - group "root" - cookbook "rb-exporter" - mode 0644 - retries 2 - variables(:dstAddress => iface["dstAddress"], :type => iface["type"], :ipAddress => node["ipaddress"], :iface => iface_key, :observation_id => observation_id ) - notifies :run, "execute[restart_rb-exporter_#{iface_key}]", :delayed - end - - template "/etc/rb-exporter/#{iface_key}/pretag.map" do - source "rb-exporter_pretag_map.erb" - owner "root" - group "root" - cookbook "rb-exporter" - mode 0644 - retries 2 - variables(:observation_id => observation_id) - notifies :run, "execute[restart_rb-exporter_#{iface_key}]", :delayed - end - else - - directory "/opt/rb/etc/rb-exporter/#{iface_key}" do - recursive true - action :delete - only_if {::Dir.exist?("/opt/rb/etc/rb-exporter/#{iface_key}")} - notifies :run, "execute[stop_rb-exporter_#{iface_key}]", :immediate - end - - file "/etc/logrotate.d/rb-exporter-#{iface_key}" do - action :delete - only_if {::File.exist?("/etc/logrotate.d/rb-exporter-#{iface_key}")} - end - - end - end - end - - template "/etc/sysconfig/arpwatch" do - source "arpwatch.erb" - owner "root" - group "root" - cookbook "rb-exporter" - mode 0644 - retries 2 - variables(:arp_ifaces => arp_ifaces) - notifies :restart, 'service[arpwatch]', :delayed - end - - service "rsyslog" do - service_name "rsyslog" - supports :status => true, :reload => true, :restart => true, :start => true, :enable => true - action [:enable,:start] - ignore_failure true - action([:start, :enable]) - end - - service "arpwatch" do - service_name "arpwatch" - supports :status => true, :reload => true, :restart => true, :start => true, :enable => true - ignore_failure true - action([:start, :enable]) - end - - service "rb-exporter" do - service_name "rb-exporter" - supports :status => true, :reload => true, :restart => true, :start => true, :enable => true - ignore_failure true - action([:start, :enable]) - end - - Chef::Log.info("rb-exporter cookbook has been processed") + end + + if node['interfaces'] && !node['interfaces'].empty? + node['interfaces'].each do |iface_key, _orig_iface| + iface = node['interfaces'][iface_key].to_hash.clone + + arp_ifaces.push(iface_key) if iface['arp'] == 'true' + + ['rb-exporter'].each do |s| + %w(restart stop start).each do |s_action| + execute "#{s_action}_#{s}_#{iface_key}" do + command "/bin/env WAIT=1 /etc/init.d/#{s} #{s_action} #{iface_key}" + ignore_failure false + action :nothing + end + end + end + + if !iface['dstAddress'].empty? + + execute "iface_restart_#{iface_key}" do + command "ifconfig #{iface_key} down && ifconfig #{iface_key} up" + ignore_failure false + action :nothing + end + + template "/etc/sysconfig/network-scripts/ifcfg-#{iface_key}" do + source 'ifcfg.erb' + owner 'root' + group 'root' + cookbook 'rb-exporter' + mode '0644' + retries 2 + variables(iface: iface_key, iface_type: iface['iface_type'], iface_ip: iface['iface_ip'], iface_netmask: iface['iface_netmask'], iface_gateway: iface['iface_gateway']) + notifies :run, "execute[iface_restart_#{iface_key}]", :immediately if iface_key != 'eth0' + end + + template "/etc/logrotate.d/rb-exporter-#{iface_key}" do + source 'rb-exporter_log-rotate.erb' + owner 'root' + group 'root' + cookbook 'rb-exporter' + mode '0644' + retries 2 + variables(iface: iface_key) + end + + directory "/var/log/rb-exporter/#{iface_key}" do + owner 'root' + group 'root' + mode '0755' + recursive true + action :create + end + + directory "/etc/rb-exporter/#{iface_key}" do + owner 'root' + group 'root' + mode '0755' + recursive true + action :create + end + + # Calculate observation_id + observation_id = (iface['observationId'] && !iface['observationId'].empty?) ? iface['observationId'] : nil + observation_id = 4294967295 if observation_id.nil? && iface['type'].downcase.include?('sflow') + + template "/etc/rb-exporter/#{iface_key}/rb-exporter.conf" do + source 'rb-exporter_conf.erb' + owner 'root' + group 'root' + cookbook 'rb-exporter' + mode '0644' + retries 2 + variables(dstAddress: iface['dstAddress'], type: iface['type'], ipAddress: node['ipaddress'], iface: iface_key, observation_id: observation_id) + notifies :run, "execute[restart_rb-exporter_#{iface_key}]", :delayed + end + + template "/etc/rb-exporter/#{iface_key}/pretag.map" do + source 'rb-exporter_pretag_map.erb' + owner 'root' + group 'root' + cookbook 'rb-exporter' + mode '0644' + retries 2 + variables(observation_id: observation_id) + notifies :run, "execute[restart_rb-exporter_#{iface_key}]", :delayed + end + else + directory "/opt/rb/etc/rb-exporter/#{iface_key}" do + recursive true + action :delete + only_if { ::Dir.exist?("/opt/rb/etc/rb-exporter/#{iface_key}") } + notifies :run, "execute[stop_rb-exporter_#{iface_key}]", :immediately + end + + file "/etc/logrotate.d/rb-exporter-#{iface_key}" do + action :delete + only_if { ::File.exist?("/etc/logrotate.d/rb-exporter-#{iface_key}") } + end + end + end + end + + template '/etc/sysconfig/arpwatch' do + source 'arpwatch.erb' + owner 'root' + group 'root' + cookbook 'rb-exporter' + mode '0644' + retries 2 + variables(arp_ifaces: arp_ifaces) + notifies :restart, 'service[arpwatch]', :delayed + end + + service 'rsyslog' do + service_name 'rsyslog' + supports status: true, reload: true, restart: true, start: true, enable: true + action [:enable, :start] + ignore_failure true + action([:start, :enable]) + end + + service 'arpwatch' do + service_name 'arpwatch' + supports status: true, reload: true, restart: true, start: true, enable: true + ignore_failure true + action([:start, :enable]) + end + + service 'rb-exporter' do + service_name 'rb-exporter' + supports status: true, reload: true, restart: true, start: true, enable: true + ignore_failure true + action([:start, :enable]) + end + + Chef::Log.info('rb-exporter cookbook has been processed') rescue => e Chef::Log.error(e.message) end @@ -182,10 +175,8 @@ action :remove do begin - - Chef::Log.info("rb-exporter cookbook has been processed") + Chef::Log.info('rb-exporter cookbook has been processed') rescue => e Chef::Log.error(e.message) end end - From 1901f8689ed452ccf7b76c0539cfed0d79ad8f53 Mon Sep 17 00:00:00 2001 From: Miguel Negron Date: Sat, 1 Jun 2024 15:38:52 +0100 Subject: [PATCH 2/4] lint recipes --- resources/recipes/configure_solo.rb | 12 ++++-------- resources/recipes/default.rb | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/resources/recipes/configure_solo.rb b/resources/recipes/configure_solo.rb index 9ef12ca..a0e7125 100644 --- a/resources/recipes/configure_solo.rb +++ b/resources/recipes/configure_solo.rb @@ -1,12 +1,8 @@ -# -# Cookbook Name:: rb-exporter +# Cookbook:: rb-exporter # Recipe:: configure_solo -# -# Copyright 2017, redborder -# -# All rights reserved - Do Not Redistribute -# +# Copyright:: 2024, redborder +# License:: Affero General Public License, Version 3 -rb_exporter_config "config" do +rb_exporter_config 'config' do action [:add] end diff --git a/resources/recipes/default.rb b/resources/recipes/default.rb index 35aa15d..e1ce43a 100644 --- a/resources/recipes/default.rb +++ b/resources/recipes/default.rb @@ -1,12 +1,8 @@ -# -# Cookbook Name:: rb-exporter +# Cookbook:: rb-exporter # Recipe:: default -# -# Copyright 2016, redborder -# -# All rights reserved - Do Not Redistribute -# +# Copyright:: 2024, redborder +# License:: Affero General Public License, Version 3 -rb_exporter_config "config" do +rb_exporter_config 'config' do action :add end From 883358a23f3513997c8308e46dae5b590be15c23 Mon Sep 17 00:00:00 2001 From: Miguel Negron Date: Sat, 1 Jun 2024 15:38:57 +0100 Subject: [PATCH 3/4] lint resources --- resources/resources/config.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/resources/resources/config.rb b/resources/resources/config.rb index 1f5c94e..110a8e2 100644 --- a/resources/resources/config.rb +++ b/resources/resources/config.rb @@ -1,10 +1,8 @@ -# Cookbook Name:: rb-exporter -# +# Cookbook:: rb-exporter # Resource:: config -# actions :add, :remove default_action :add -attribute :user, :kind_of => String, :default => "rb-exporter" -attribute :config_dir, :kind_of => String, :default => "/etc/rb-exporter" +attribute :user, kind_of: String, default: 'rb-exporter' +attribute :config_dir, kind_of: String, default: '/etc/rb-exporter' From fbd52b19777b3279fe0e7bfa2a807317c6e8265d Mon Sep 17 00:00:00 2001 From: Miguel Negron Date: Sat, 1 Jun 2024 15:39:04 +0100 Subject: [PATCH 4/4] add gemfile for testing --- Gemfile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Gemfile diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..9734e78 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gem 'cookstyle', '= 7.32.1' +gem 'rspec', '= 3.11' +gem 'rubocop', '= 1.25.1'