Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/fix lint #2

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'cookstyle', '= 7.32.1'
gem 'rspec', '= 3.11'
gem 'rubocop', '= 1.25.1'
305 changes: 148 additions & 157 deletions resources/providers/config.rb
Original file line number Diff line number Diff line change
@@ -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 = []
Expand All @@ -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}"
Expand All @@ -25,167 +21,162 @@
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
end

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

12 changes: 4 additions & 8 deletions resources/recipes/configure_solo.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 4 additions & 8 deletions resources/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 3 additions & 5 deletions resources/resources/config.rb
Original file line number Diff line number Diff line change
@@ -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'
Loading