refacter: tidy facts
- create a facts module, move all facts to this module
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# arpa_fact.rb
|
||||
|
||||
require 'facter'
|
||||
|
||||
Facter.add(:arpa) do
|
||||
setcode do
|
||||
arpa_info = {}
|
||||
Facter.value(:networking)['interfaces'].each do |interface_name, values|
|
||||
next unless values.key?('ip')
|
||||
|
||||
ip_address = values['ip']
|
||||
reversed_ip_parts = ip_address.split('.').reverse
|
||||
addr = "#{reversed_ip_parts.join('.')}.in-addr.arpa"
|
||||
|
||||
trimmed_ip_parts = reversed_ip_parts[1..]
|
||||
zone = "#{trimmed_ip_parts.join('.')}.in-addr.arpa"
|
||||
|
||||
arpa_info[interface_name] = {
|
||||
'zone' => zone,
|
||||
'addr' => addr
|
||||
}
|
||||
end
|
||||
arpa_info
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# set country based on the subnet
|
||||
|
||||
Facter.add('country') do
|
||||
setcode do
|
||||
# use facts['networking']['ip']to find the promary IP address
|
||||
ip = Facter.value(:networking)['ip']
|
||||
|
||||
# subnet to region mapping
|
||||
subnet_to_country = {
|
||||
'198.18.17.0/24' => 'au'
|
||||
}
|
||||
|
||||
require 'ipaddr'
|
||||
|
||||
# Find the region for the IP
|
||||
country = 'stateless' # default to 'stateless' if no country matches
|
||||
subnet_to_country.each do |subnet, country_initial|
|
||||
if IPAddr.new(subnet).include?(IPAddr.new(ip))
|
||||
country = country_initial
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
country
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# create an enc_role_path fact from enc_role, to be used by hiera.yaml
|
||||
#
|
||||
# roles::infra::dns::resolver becomes roles/infra/dns/resolver
|
||||
Facter.add(:enc_role_path) do
|
||||
setcode do
|
||||
enc_role = Facter.value(:enc_role)
|
||||
if enc_role
|
||||
enc_role_path = enc_role.gsub('::', '/')
|
||||
enc_role_path
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# split the enc_role fact into different tiers
|
||||
#
|
||||
# e.g.
|
||||
# enc_role_tier2: roles::infra::dns::resolver -> infra
|
||||
Facter.add(:enc_role_tier1) do
|
||||
setcode do
|
||||
role = Facter.value(:enc_role)
|
||||
if role
|
||||
parts = role.split('::')
|
||||
parts[1] if parts.size > 1
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# split the enc_role fact into different tiers
|
||||
# e.g.
|
||||
# enc_role_tier2: roles::infra::dns::resolver -> dns
|
||||
Facter.add(:enc_role_tier2) do
|
||||
setcode do
|
||||
role = Facter.value(:enc_role)
|
||||
if role
|
||||
parts = role.split('::')
|
||||
parts[2] if parts.size > 2
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# split the enc_role fact into different tiers
|
||||
# e.g.
|
||||
# enc_role_tier3: roles::infra::dns::resolver -> resolver
|
||||
Facter.add(:enc_role_tier3) do
|
||||
setcode do
|
||||
role = Facter.value(:enc_role)
|
||||
if role
|
||||
parts = role.split('::')
|
||||
parts[3] if parts.size > 3
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# create a boolean for when the mariadb service is active
|
||||
require 'English'
|
||||
|
||||
Facter.add('mariadb_active') do
|
||||
setcode do
|
||||
system('systemctl is-active --quiet mariadb')
|
||||
$CHILD_STATUS.exitstatus.zero?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# check if the /etc/my.cnf.d/server.cnf file exists,
|
||||
# open it and search for the 'datadir =' option
|
||||
# store the datadir value as this fact
|
||||
require 'facter'
|
||||
|
||||
Facter.add('mariadb_datapath') do
|
||||
setcode do
|
||||
if File.exist?('/etc/my.cnf.d/server.cnf')
|
||||
datadir = nil
|
||||
File.foreach('/etc/my.cnf.d/server.cnf') do |line|
|
||||
match = line.match(/^\s*datadir\s*=\s*(.+)\s*$/)
|
||||
if match
|
||||
datadir = match[1].strip
|
||||
break
|
||||
end
|
||||
end
|
||||
datadir
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# check if the mariadb server exists
|
||||
# check if the mariadb_datapath fact exists, else set /var/lib/mysql as the datapath
|
||||
# check if the galera grastate.dat file exists, identifying if galera is boostrapped
|
||||
require 'facter'
|
||||
|
||||
if system('systemctl is-active --quiet mariadb')
|
||||
|
||||
Facter.add('mariadb_galera_active') do
|
||||
setcode do
|
||||
mariadb_datapath = Facter.value(:mariadb_datapath) || '/var/lib/mysql'
|
||||
File.exist?("#{mariadb_datapath}/grastate.dat")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# create boolean for if mariadb is installed based of the default service file
|
||||
Facter.add('mariadb_installed') do
|
||||
setcode do
|
||||
File.exist?('/usr/lib/systemd/system/mariadb.service')
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# check all datadirs for minio are initialised
|
||||
|
||||
require 'yaml'
|
||||
Facter.add('minio_datadirs_initialised') do
|
||||
setcode do
|
||||
yaml_file_path = '/opt/puppetlabs/facter/facts.d/minio_facts.yaml'
|
||||
|
||||
# check if the YAML file exists first
|
||||
next false unless File.exist?(yaml_file_path)
|
||||
|
||||
minio_facts = YAML.load_file(yaml_file_path)
|
||||
dev_count = minio_facts['minio_blockdev_count']
|
||||
datadir = minio_facts['minio_datadir']
|
||||
|
||||
# check datadir if no blockdevices are used, otherwise check the store locations
|
||||
if dev_count.zero?
|
||||
Dir.exist?(datadir)
|
||||
else
|
||||
(1..dev_count).all? do |number|
|
||||
Dir.exist?("#{datadir}/store#{number}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# check that the minio group exists
|
||||
|
||||
require 'yaml'
|
||||
Facter.add('minio_group_exists') do
|
||||
setcode do
|
||||
yaml_file_path = '/opt/puppetlabs/facter/facts.d/minio_facts.yaml'
|
||||
|
||||
# check if the YAML file exists first
|
||||
next false unless File.exist?(yaml_file_path)
|
||||
|
||||
minio_facts = YAML.load_file(yaml_file_path)
|
||||
group_name = minio_facts['minio_group']
|
||||
|
||||
group_exists = system("getent group #{group_name} >/dev/null 2>&1")
|
||||
|
||||
group_exists
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'facter'
|
||||
require 'yaml'
|
||||
|
||||
Facter.add('minio_pool_dns') do
|
||||
setcode do
|
||||
yaml_file_path = '/opt/puppetlabs/facter/facts.d/minio_facts.yaml'
|
||||
|
||||
# check if the YAML file exists
|
||||
next {} unless File.exist?(yaml_file_path)
|
||||
|
||||
# load data from YAML
|
||||
data = YAML.load_file(yaml_file_path)
|
||||
minio_members = data['minio_members']
|
||||
minio_region = data['minio_region']
|
||||
minio_pool = data['minio_pool']
|
||||
domain = Facter.value(:networking)['domain']
|
||||
|
||||
# create result hash
|
||||
result = {}
|
||||
|
||||
# Check CNAME for each node_id from 1 to minio_members
|
||||
(1..minio_members).each do |node_id|
|
||||
cname_target = "#{minio_region}-#{minio_pool}-#{node_id}.#{domain}"
|
||||
command = "host #{cname_target} > /dev/null 2>&1"
|
||||
|
||||
# Using system method to execute the command
|
||||
# It returns true if the command gives exit status 0 (success), otherwise false
|
||||
result[cname_target] = system(command)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# check that the minio user exists
|
||||
|
||||
require 'yaml'
|
||||
Facter.add('minio_user_exists') do
|
||||
setcode do
|
||||
yaml_file_path = '/opt/puppetlabs/facter/facts.d/minio_facts.yaml'
|
||||
|
||||
# check if the YAML file exists first
|
||||
next false unless File.exist?(yaml_file_path)
|
||||
|
||||
minio_facts = YAML.load_file(yaml_file_path)
|
||||
user_name = minio_facts['minio_user']
|
||||
|
||||
user_exists = system("id #{user_name} >/dev/null 2>&1")
|
||||
|
||||
user_exists
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# skip if mysql isnt installed or active
|
||||
if system('which mysql > /dev/null 2>&1') && system('systemctl is-active --quiet mariadb')
|
||||
|
||||
# export mysql wsrep status
|
||||
wsrep_status = `mysql -e "SHOW STATUS LIKE 'wsrep%';"`
|
||||
|
||||
# loop over the output
|
||||
wsrep_status.each_line do |line|
|
||||
# skip the line unless it starts with 'wsrep_'
|
||||
next unless line.match(/^wsrep_/)
|
||||
|
||||
key, value = line.split("\t")
|
||||
Facter.add("mysql_#{key.strip}") do
|
||||
setcode do
|
||||
value.strip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# set region based on the subnet
|
||||
|
||||
Facter.add('region') do
|
||||
setcode do
|
||||
# use facts['networking']['ip']to find the promary IP address
|
||||
ip = Facter.value(:networking)['ip']
|
||||
|
||||
# subnet to region mapping
|
||||
subnet_to_region = {
|
||||
'198.18.17.0/24' => 'drw1'
|
||||
}
|
||||
|
||||
require 'ipaddr'
|
||||
|
||||
# Find the region for the IP
|
||||
region = 'lost' # default to 'lost' if no region matches
|
||||
subnet_to_region.each do |subnet, region_name|
|
||||
if IPAddr.new(subnet).include?(IPAddr.new(ip))
|
||||
region = region_name
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
region
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user