Skip to content

Commit

Permalink
add ensure parameter to prometheus::daemon
Browse files Browse the repository at this point in the history
make it possible to also remove resources created by
prometheus::daemon. This can then be passed through by all
classes that use prometheus::daemon.
  • Loading branch information
TheMeier committed Jan 4, 2025
1 parent 01d6556 commit c1db34f
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 25 deletions.
20 changes: 19 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -15206,6 +15206,7 @@ The following parameters are available in the `prometheus::daemon` defined type:
* [`init_style`](#-prometheus--daemon--init_style)
* [`proxy_server`](#-prometheus--daemon--proxy_server)
* [`proxy_type`](#-prometheus--daemon--proxy_type)
* [`ensure`](#-prometheus--daemon--ensure)
* [`group`](#-prometheus--daemon--group)
* [`manage_bin_link`](#-prometheus--daemon--manage_bin_link)
* [`purge`](#-prometheus--daemon--purge)
Expand Down Expand Up @@ -15233,7 +15234,7 @@ Complete URL corresponding to the where the release binary archive can be downlo

##### <a name="-prometheus--daemon--notify_service"></a>`notify_service`

Data type: `Any`
Data type: `Variant[Type[Exec],Type[Service],Undef]`

The service to notify when something changes in this define

Expand Down Expand Up @@ -15405,6 +15406,14 @@ Optional proxy server type (none|http|https|ftp)

Default value: `undef`

##### <a name="-prometheus--daemon--ensure"></a>`ensure`

Data type: `Enum['present', 'absent']`

Whether to install or remove the instance

Default value: `'present'`

##### <a name="-prometheus--daemon--group"></a>`group`

Data type: `String[1]`
Expand Down Expand Up @@ -15515,6 +15524,7 @@ The following parameters are available in the `prometheus::scrape_job` defined t
* [`targets`](#-prometheus--scrape_job--targets)
* [`labels`](#-prometheus--scrape_job--labels)
* [`collect_dir`](#-prometheus--scrape_job--collect_dir)
* [`ensure`](#-prometheus--scrape_job--ensure)

##### <a name="-prometheus--scrape_job--job_name"></a>`job_name`

Expand Down Expand Up @@ -15546,6 +15556,14 @@ NOTE: this is a prometheus setting and will be overridden during collection.

Default value: `undef`

##### <a name="-prometheus--scrape_job--ensure"></a>`ensure`

Data type: `Enum['present', 'absent']`

Whether the scrape job should be present or absent.

Default value: `'present'`

## Data types

### <a name="Prometheus--GsUri"></a>`Prometheus::GsUri`
Expand Down
91 changes: 70 additions & 21 deletions manifests/daemon.pp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
# Optional proxy server, with port number if needed. ie: https://example.com:8080
# @param proxy_type
# Optional proxy server type (none|http|https|ftp)
# @param ensure
# Whether to install or remove the instance
define prometheus::daemon (
String[1] $version,
Prometheus::Uri $real_download_url,
$notify_service,
Variant[Type[Exec],Type[Service],Undef] $notify_service,
String[1] $user,
String[1] $group,
Prometheus::Install $install_method = $prometheus::install_method,
Expand Down Expand Up @@ -86,18 +88,22 @@
Stdlib::Absolutepath $usershell = $prometheus::usershell,
Optional[String[1]] $proxy_server = undef,
Optional[Enum['none', 'http', 'https', 'ftp']] $proxy_type = undef,
Enum['present', 'absent'] $ensure = 'present',
) {
$real_package_ensure = $ensure ? { 'absent' => 'absent', default => $package_ensure }
$real_service_ensure = $ensure ? { 'absent' => 'stopped', default => $service_ensure }

case $install_method {
'url': {
if $download_extension == '' {
file { "/opt/${name}-${version}.${os}-${arch}":
ensure => directory,
ensure => stdlib::ensure($ensure, 'directory'),
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0755',
}
-> archive { "/opt/${name}-${version}.${os}-${arch}/${name}":
ensure => present,
ensure => $ensure,
source => $real_download_url,
checksum_verify => false,
before => File["/opt/${name}-${version}.${os}-${arch}/${name}"],
Expand All @@ -106,7 +112,7 @@
}
} else {
archive { "/tmp/${name}-${version}.${download_extension}":
ensure => present,
ensure => $ensure,
extract => true,
extract_path => $extract_path,
source => $real_download_url,
Expand All @@ -120,13 +126,14 @@
}
}
file { $archive_bin_path:
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555',
ensure => stdlib::ensure($ensure, 'file'),
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555',
}
if $manage_bin_link {
file { "${bin_dir}/${bin_name}":
ensure => link,
ensure => stdlib::ensure($ensure, 'link'),
notify => $notify_service,
target => $archive_bin_path,
require => File[$archive_bin_path],
Expand All @@ -135,7 +142,7 @@
}
'package': {
package { $package_name:
ensure => $package_ensure,
ensure => $real_package_ensure,
notify => $notify_service,
}
if $manage_user {
Expand All @@ -148,47 +155,60 @@
if $manage_user {
# if we manage the service, we need to reload it if our user changes
# important for cases where another group gets added
if $manage_service {
if $manage_service and $real_service_ensure == 'running' {
User[$user] ~> $notify_service
}
ensure_resource('user', [$user], {
ensure => 'present',
ensure => $ensure,
system => true,
groups => $extra_groups,
shell => $usershell,
})

if $manage_group {
Group[$group] -> User[$user]
if $ensure == 'present' {
Group[$group] -> User[$user]
} else {
User[$user] -> Group[$group]
Service[$name] -> User[$user]
}
}
}
if $manage_group {
ensure_resource('group', [$group], {
ensure => 'present',
ensure => $ensure,
system => true,
})
}

case $init_style { # lint:ignore:case_without_default
'upstart': {
file { "/etc/init/${name}.conf":
ensure => stdlib::ensure($ensure, 'file'),
mode => '0444',
owner => 'root',
group => 'root',
content => template('prometheus/daemon.upstart.erb'),
notify => $notify_service,
}
file { "/etc/init.d/${name}":
ensure => link,
ensure => stdlib::ensure($ensure, 'file'),
target => '/lib/init/upstart-job',
owner => 'root',
group => 'root',
mode => '0755',
}
if $notify_service !~ Undef {
if $ensure == 'present' {
File["/etc/init/${name}.conf"] ~> $notify_service
} else {
$notify_service -> File["/etc/init/${name}.conf"]
}
}
}
'systemd': {
include 'systemd'
systemd::manage_unit { "${name}.service":
ensure => $ensure,
unit_entry => {
'Description' => "Prometheus ${name}",
'Wants' => 'network-online.target',
Expand All @@ -206,34 +226,61 @@
install_entry => {
'WantedBy' => 'multi-user.target',
},
notify => $notify_service,
}
if $notify_service !~ Undef {
if $ensure == 'present' {
Systemd::Manage_unit["${name}.service"] ~> $notify_service
} else {
$notify_service -> Systemd::Manage_unit["${name}.service"]
}
}
}
'sysv': {
file { "/etc/init.d/${name}":
ensure => stdlib::ensure($ensure, 'file'),
mode => '0555',
owner => 'root',
group => 'root',
content => template('prometheus/daemon.sysv.erb'),
notify => $notify_service,
}
if $notify_service !~ Undef {
if $ensure == 'present' {
File["/etc/init.d/${name}"] ~> $notify_service
} else {
$notify_service -> File["/etc/init.d/${name}"]
}
}
}
'sles': {
file { "/etc/init.d/${name}":
ensure => stdlib::ensure($ensure, 'file'),
mode => '0555',
owner => 'root',
group => 'root',
content => template('prometheus/daemon.sles.erb'),
notify => $notify_service,
}
if $notify_service !~ Undef {
if $ensure == 'present' {
File["/etc/init.d/${name}"] ~> $notify_service
} else {
$notify_service -> File["/etc/init.d/${name}"]
}
}
}
'launchd': {
file { "/Library/LaunchDaemons/io.${name}.daemon.plist":
ensure => stdlib::ensure($ensure, 'file'),
mode => '0644',
owner => 'root',
group => 'wheel',
content => template('prometheus/daemon.launchd.erb'),
notify => $notify_service,
}
if $notify_service !~ Undef {
if $ensure == 'present' {
File["/Library/LaunchDaemons/io.${name}.daemon.plist"] ~> $notify_service
} else {
$notify_service -> File["/Library/LaunchDaemons/io.${name}.daemon.plist"]
}
}
}
'none': {}
Expand All @@ -247,7 +294,7 @@
$env_vars_merged = $env_vars
}

if $install_method == 'package' and $package_ensure in ['absent', 'purged'] {
if $install_method == 'package' and $real_package_ensure == 'absent' {
# purge the environment file if the package is removed
#
# this is to make sure we can garbage-collect the files created by
Expand All @@ -263,6 +310,7 @@
# those files to be present, even if empty, so it's critical that
# the file not get removed
file { "${env_file_path}/${name}":
ensure => stdlib::ensure($ensure, 'file'),
mode => '0644',
owner => 'root',
group => '0', # Darwin uses wheel
Expand Down Expand Up @@ -290,7 +338,7 @@

if $manage_service {
service { $name:
ensure => $service_ensure,
ensure => $real_service_ensure,
name => $init_selector,
enable => $service_enable,
provider => $real_provider,
Expand All @@ -303,6 +351,7 @@
}

@@prometheus::scrape_job { "${scrape_job_name}_${scrape_host}_${scrape_port}":
ensure => $ensure,
job_name => $scrape_job_name,
targets => ["${scrape_host}:${scrape_port}"],
labels => $scrape_job_labels,
Expand Down
5 changes: 4 additions & 1 deletion manifests/scrape_job.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# @param collect_dir
# Directory used for collecting scrape definitions.
# NOTE: this is a prometheus setting and will be overridden during collection.
# @param ensure
# Whether the scrape job should be present or absent.
define prometheus::scrape_job (
String[1] $job_name,
Array[String[1]] $targets,
Hash[String[1], String[1]] $labels = {},
Stdlib::Absolutepath $collect_dir = undef,
Enum['present', 'absent'] $ensure = 'present',
) {
$config = stdlib::to_yaml([
{
Expand All @@ -26,7 +29,7 @@
},
])
file { "${collect_dir}/${job_name}_${name}.yaml":
ensure => file,
ensure => stdlib::ensure($ensure, 'file'),
owner => 'root',
group => $prometheus::group,
mode => $prometheus::config_mode,
Expand Down
Loading

0 comments on commit c1db34f

Please sign in to comment.