Skip to content

Commit

Permalink
Migrate from puppetdbquery to puppetdb::query_facts
Browse files Browse the repository at this point in the history
Bug: T13025
  • Loading branch information
paladox committed Dec 21, 2024
1 parent 02e83c4 commit f97deee
Show file tree
Hide file tree
Showing 23 changed files with 193 additions and 349 deletions.
46 changes: 10 additions & 36 deletions modules/base/manifests/firewall.pp
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,22 @@
source => 'puppet:///modules/base/firewall/main-input-default-drop.conf',
}

$firewall_rules_str = join(
query_facts('Class[Role::Icinga2]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['he-ipv6'] ) {
"${value['networking']['ip']} ${value['networking']['interfaces']['he-ipv6']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Role::Icinga2' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)

ferm::service { 'nrpe':
proto => 'tcp',
port => '5666',
srange => "(${firewall_rules_str})",
}

$firewall_bastion_hosts = join(
query_facts('Class[Base]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['he-ipv6'] ) {
"${value['networking']['ip']} ${value['networking']['interfaces']['he-ipv6']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Base' }
| PQL
$firewall_bastion_hosts = vmlib::generate_firewall_ip($subquery)

ferm::service { 'ssh':
proto => 'tcp',
port => '22',
Expand Down
13 changes: 9 additions & 4 deletions modules/prometheus/manifests/class.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
String $module,
Integer $port,
) {
$servers = query_nodes("Class[${module}] or Define[${module}]")
.flatten()
.unique()
.sort()

$pql = @("PQL")
nodes[certname] {
(resources {type = "Class" and title = "${module}" }
or resources {type = "Define" and title = "${module}" })
order by certname
}
| PQL
$servers = puppetdb_query($pql).map |$resource| { $resource['certname'] }.flatten().unique().sort

file { $dest:
ensure => present,
Expand Down
20 changes: 4 additions & 16 deletions modules/prometheus/manifests/exporter/cadvisor.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@
subscribe => Package['cadvisor'],
}

$firewall_rules_str = join(
query_facts('Class[Prometheus] or Class[Role::Grafana]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Prometheus' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)
ferm::service { 'prometheus cadvisor_exporter':
proto => 'tcp',
port => '4194',
Expand Down
20 changes: 4 additions & 16 deletions modules/prometheus/manifests/exporter/mariadb.pp
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,10 @@
ensure => running,
}

$firewall_rules_str = join(
query_facts('Class[Prometheus]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Prometheus' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)
ferm::service { 'prometheus mysqld_exporter':
proto => 'tcp',
port => '9104',
Expand Down
20 changes: 4 additions & 16 deletions modules/prometheus/manifests/exporter/openldap.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,10 @@
}
}

$firewall_rules_str = join(
query_facts('Class[Role::Prometheus]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Role::Prometheus' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)
ferm::service { 'prometheus openldap_exporter':
proto => 'tcp',
port => '9142',
Expand Down
23 changes: 5 additions & 18 deletions modules/prometheus/manifests/exporter/varnish.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,11 @@
restart => true,
}

$firewall_rules_str = join(
query_facts('Class[Role::Prometheus]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['he-ipv6'] ) {
"${value['networking']['ip']} ${value['networking']['interfaces']['he-ipv6']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Role::Prometheus' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)

ferm::service { 'prometheus varnish_exporter':
proto => 'tcp',
port => $listen_port,
Expand Down
11 changes: 7 additions & 4 deletions modules/prometheus/manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@
refreshonly => true,
}

$servers = query_nodes('Class[Base]')
.flatten()
.unique()
.sort()
$pql = @("PQL")
nodes[certname] {
resources {type = "Class" and title = "Base" }
order by certname
}
| PQL
$servers = puppetdb_query($pql).map |$resource| { $resource['certname'] }.flatten().unique().sort

file { '/etc/prometheus/targets/nodes.yaml':
ensure => present,
Expand Down
16 changes: 16 additions & 0 deletions modules/puppetdb/functions/query_facts.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: Apache-2.0
# @summery query for custome facts for a host and return a hash of facts values keyed to the certname
# @param filter a hash of fact name to fetch
# @param a pql subquery to apply to the query
function puppetdb::query_facts(
Array[String[1]] $filter,
Optional[String[1]] $subquery = undef,
) >> Hash[Stdlib::Fqdn, Hash] {
$_subquery = $subquery ? {
undef => '',
default => " and ${subquery}"
}
$filter_str = $filter.map |$filter| { "\"${filter}\"" }.join(',')
$pql = "facts[certname, name, value] { name in [${filter_str}] ${_subquery} }"
puppetdb::munge_facts(puppetdb_query($pql))
}
14 changes: 14 additions & 0 deletions modules/puppetdb/lib/puppet/functions/puppetdb/munge_facts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: Apache-2.0
Puppet::Functions.create_function(:'puppetdb::munge_facts') do
dispatch :munge_facts do
param 'Array[Hash]', :facts
end

def munge_facts(facts)
facts_out = Hash.new {|h, k| h[k] = {}}
facts.each do |f|
facts_out[f['certname']][f['name']] = f['value']
end
facts_out
end
end
22 changes: 4 additions & 18 deletions modules/role/manifests/burrow.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,10 @@
metrics_addr => '0.0.0.0:9500'
}

$firewall_rules_str = join(
query_facts('Class[Role::Prometheus]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['he-ipv6'] ) {
"${value['networking']['ip']} ${value['networking']['interfaces']['he-ipv6']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Role::Prometheus' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)

# Burrow offers a HTTP REST API
ferm::service { 'burrow-main':
Expand Down
24 changes: 8 additions & 16 deletions modules/role/manifests/changeprop.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@
include role::prometheus::statsd_exporter

# TODO: Restrict beta access at some point once we get working.
$firewall_rules_str = join(
query_facts('Class[Role::Mediawiki] or Class[Role::Mediawiki_task] or Class[Role::Mediawiki_beta] or Class[Role::Icinga2]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
(resources { type = 'Class' and title = 'Role::Mediawik' } or
resources { type = 'Class' and title = 'Role::Mediawiki_task' } or
resources { type = 'Class' and title = 'Role::Mediawiki_beta' } or
resources { type = 'Class' and title = 'Role::Icinga2' })
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)

ferm::service { 'changeprop':
proto => 'tcp',
port => '7200',
Expand Down
18 changes: 4 additions & 14 deletions modules/role/manifests/cloud.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@

class { '::cpufrequtils': }

$firewall_rules_str = join(
query_facts('Class[Role::Cloud]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['vmbr1'] ) {
"${value['networking']['interfaces']['vmbr1']['ip']} ${value['networking']['ip']} ${value['networking']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
resources { type = 'Class' and title = 'Role::Cloud' }
| PQL
$firewall_rules_str = vmlib::generate_firewall_ip($subquery)

ferm::service { 'proxmox port 5900:5999':
proto => 'tcp',
Expand Down
41 changes: 9 additions & 32 deletions modules/role/manifests/irc.pp
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,21 @@
udp_port => '5071',
}

$firewall_irc_rules_str = join(
query_facts('Class[Role::Mediawiki] or Class[Role::Mediawiki_task] or Class[Role::Mediawiki_beta]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$subquery = @("PQL")
(resources { type = 'Class' and title = 'Role::Mediawik' } or
resources { type = 'Class' and title = 'Role::Mediawiki_task' } or
resources { type = 'Class' and title = 'Role::Mediawiki_beta' })
| PQL
$firewall_irc_rules_str = vmlib::generate_firewall_ip($subquery)

ferm::service { 'ircrcbot':
proto => 'udp',
port => '5070',
srange => "(${firewall_irc_rules_str})",
}

$firewall_all_rules_str = join(
query_facts('Class[Base]', ['networking'])
.map |$key, $value| {
if ( $value['networking']['interfaces']['ens19'] and $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens19']['ip']} ${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} elsif ( $value['networking']['interfaces']['ens18'] ) {
"${value['networking']['interfaces']['ens18']['ip']} ${value['networking']['interfaces']['ens18']['ip6']}"
} else {
"${value['networking']['ip']} ${value['networking']['ip6']}"
}
}
.flatten()
.unique()
.sort(),
' '
)
$firewall_all_rules_str = vmlib::generate_firewall_ip()

ferm::service { 'irclogserverbot':
proto => 'udp',
port => '5071',
Expand Down
Loading

0 comments on commit f97deee

Please sign in to comment.