Skip to content

Commit

Permalink
implement configfile validation (#18)
Browse files Browse the repository at this point in the history
* implement configfile validation

* update Reference file
  • Loading branch information
cyberkov authored Apr 25, 2024
1 parent 7054b9c commit 83e72f0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The following parameters are available in the `otelcol` class:
* [`package_name`](#-otelcol--package_name)
* [`package_ensure`](#-otelcol--package_ensure)
* [`service_name`](#-otelcol--service_name)
* [`service_configcheck`](#-otelcol--service_configcheck)
* [`environment_file`](#-otelcol--environment_file)
* [`run_options`](#-otelcol--run_options)
* [`config_file`](#-otelcol--config_file)
Expand Down Expand Up @@ -93,6 +94,14 @@ Name of the service used

Default value: `$package_name`

##### <a name="-otelcol--service_configcheck"></a>`service_configcheck`

Data type: `Boolean`

Check config before service reloads

Default value: `true`

##### <a name="-otelcol--environment_file"></a>`environment_file`

Data type: `String`
Expand Down
3 changes: 3 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Ensure for the package
# @param service_name
# Name of the service used
# @param service_configcheck
# Check config before service reloads
# @param environment_file
# path of the environment file used for service
# @param run_options
Expand Down Expand Up @@ -52,6 +54,7 @@
String $package_name = 'otelcol',
Enum['present','absent','installed','latest'] $package_ensure = 'installed',
String $service_name = $package_name,
Boolean $service_configcheck = true,
String $environment_file = "/etc/${package_name}/${package_name}.conf",
Optional[String] $run_options = undef,
String $config_file = "/etc/${package_name}/config.yaml",
Expand Down
30 changes: 23 additions & 7 deletions manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,36 @@
# @api private
class otelcol::service (
Stdlib::Ensure::Service $ensure = $otelcol::service_ensure,
String $config_check_command = "${otelcol::service_name} validate --config=${otelcol::config_file}",
Boolean $config_check = $otelcol::service_configcheck,
) {
# include install
include otelcol::install

# systemd::dropin_file { 'otelcol_service':
# unit => 'otelcol.service',
# content => epp('otelcol/otelcol.dropin.epp'),
# filename => 'otelcol_override.conf',
# }
# ~>
if $config_check {
exec { 'otelcol_config_check':
command => $config_check_command,
refreshonly => true,
path => [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
],
}
}

$service_require = $config_check ? {
true => [Exec['otelcol_config_check'], Package['otelcol']],
false => Package['otelcol'],
}

service { 'otelcol':
ensure => $ensure,
name => $otelcol::service_name,
require => Package['otelcol'],
require => $service_require,
subscribe => [Concat['otelcol-config'], File['otelcol-environment']],
}
}
11 changes: 11 additions & 0 deletions spec/classes/otelcol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@
it { is_expected.to contain_service('otelcol').with_ensure('stopped') }
end

context 'with service_configcheck' do
let :params do
{
service_configcheck: true,
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_service('otelcol').that_requires('Exec[otelcol_config_check]') }
end

context 'do not manage Service' do
let :params do
{
Expand Down

0 comments on commit 83e72f0

Please sign in to comment.