diff --git a/data/common.yaml b/data/common.yaml index 3506413..ad0e36e 100644 --- a/data/common.yaml +++ b/data/common.yaml @@ -6,7 +6,6 @@ lookup_options: '^promtail::password_file_content$': convert_to: 'Sensitive' -promtail::bin_dir: '/usr/local/bin' promtail::service_ensure: 'running' promtail::service_enable: true promtail::version: 'v2.0.0' diff --git a/files/promtail.service b/files/promtail.service index bce07c2..09d4191 100644 --- a/files/promtail.service +++ b/files/promtail.service @@ -3,10 +3,15 @@ Description=Promtail agent from Grafana Documentation=https://github.com/grafana/loki/tree/master/docs/clients/promtail After=network.target +StartLimitIntervalSec=500 +StartLimitBurst=5 + [Service] ExecStart=/usr/local/bin/promtail -config.file=/etc/promtail/config.yaml ExecReload=/bin/kill -HUP $MAINPID + Restart=on-failure +RestartSec=2s RestartForceExitStatus=SIGPIPE KillMode=control-group diff --git a/hiera.yaml b/hiera.yaml index f2573a8..0b36319 100644 --- a/hiera.yaml +++ b/hiera.yaml @@ -6,7 +6,7 @@ defaults: # Used for any hierarchy level that omits these keys. data_hash: yaml_data # Use the built-in YAML backend. hierarchy: - - name: 'kermel' + - name: 'kernel' path: "kernel/%{facts.kernel}.yaml" - name: 'common' path: 'common.yaml' diff --git a/manifests/config.pp b/manifests/config.pp index 695c6bf..210061a 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -6,20 +6,22 @@ class promtail::config { case $facts['kernel'] { 'Linux': { - $config_dir = '/etc/promtail' + $config_file = "${promtail::config_dir}/config.yaml" + } + 'windows': { + $config_file = "${promtail::config_dir}\\config.yaml" } default: { fail("${facts['kernel']} is not supported") } } - file { $config_dir: + file { $promtail::config_dir: ensure => directory, } - $config_file = "${config_dir}/config.yaml" - concat { $config_file: - ensure => present, - notify => Service['promtail'], + ensure => present, + require => File[$promtail::config_dir], + notify => $promtail::service::service_to_notify, } concat::fragment { 'config_header': diff --git a/manifests/init.pp b/manifests/init.pp index 6e89ff2..67ff53e 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -29,6 +29,15 @@ # @param [Stdlib::Absolutepath] bin_dir # The directory in which to create a symlink to the promtail binary # +# @param [Stdlib::Absolutepath] config_dir +# The directory in which the promtail config is stored +# +# @param [Stdlib::Absolutepath] data_dir +# The directory in which to versions of promtail are stored and updated +# +# @param [Stdlib::Absolutepath] log_file_path +# The directory in logs should be stored (Windows only feature!) +# # @param [String[1]] checksum # The checksum of the promtail binary. # Note: each platform has its own checksum. @@ -123,20 +132,23 @@ # } # class promtail ( - Boolean $service_enable, - Enum['running', 'stopped'] $service_ensure, - Hash $clients_config_hash, - Hash $positions_config_hash, - Hash $scrape_configs_hash, - Stdlib::Absolutepath $bin_dir, - String[1] $checksum, - String[1] $version, - Optional[Hash] $server_config_hash = undef, - Optional[Hash] $target_config_hash = undef, - Optional[Stdlib::Absolutepath] $password_file_path = undef, - Optional[Sensitive[String[1]]] $password_file_content = undef, - Stdlib::HTTPUrl $source_url = 'https://github.com/grafana/loki/releases/download', -) { + Boolean $service_enable, + Enum['running', 'stopped', 'absent'] $service_ensure, + Hash $clients_config_hash, + Hash $positions_config_hash, + Hash $scrape_configs_hash, + Stdlib::Absolutepath $bin_dir = $promtail::params::bin_dir, + Stdlib::Absolutepath $data_dir = $promtail::params::data_dir, + Stdlib::Absolutepath $config_dir = $promtail::params::config_dir, + Optional[Stdlib::Absolutepath] $log_file_path = undef, + String[1] $checksum, + String[1] $version, + Optional[Hash] $server_config_hash = undef, + Optional[Hash] $target_config_hash = undef, + Optional[Stdlib::Absolutepath] $password_file_path = undef, + Optional[Sensitive[String[1]]] $password_file_content = undef, + Stdlib::HTTPUrl $source_url = 'https://github.com/grafana/loki/releases/download', +) inherits promtail::params { Class['promtail::install'] -> Class['promtail::config'] -> Class['promtail::service'] diff --git a/manifests/install.pp b/manifests/install.pp index 3f415c5..e312f8e 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -7,21 +7,27 @@ include archive case $facts['os']['architecture'] { - 'x86_64', 'amd64': { $arch = 'amd64' } - 'aarch64': { $arch = 'arm64' } - 'armv7l': { $arch = 'arm' } - default: { fail("Unsupported kernel architecture: ${facts['os']['architecture']}") } + 'x86_64', 'amd64', 'x64': { $arch = 'amd64' } + 'aarch64': { $arch = 'arm64' } + 'armv7l': { $arch = 'arm' } + default: { fail("Unsupported kernel architecture: ${facts['os']['architecture']}") } } case $facts['kernel'] { 'Linux': { - $data_dir = '/usr/local/promtail_data' if versioncmp($promtail::version, 'v0.3.0') > 0 { $release_file_name = "promtail-linux-${arch}" } else { $release_file_name = "promtail_linux_${arch}" } } + 'windows': { + if versioncmp($promtail::version, 'v0.3.0') > 0 { + $release_file_name = "promtail-windows-${arch}.exe" + } else { + $release_file_name = "promtail_windows_${arch}.exe" + } + } default: { fail("${facts['kernel']} is not yet supported") } } @@ -31,14 +37,20 @@ $archive_type = 'gz' } - $version_dir = "${data_dir}/promtail-${promtail::version}" - $binary_path = "${version_dir}/${release_file_name}" + $version_dir = $facts['kernel'] ? { + 'windows' => "${promtail::data_dir}\\${promtail::version}", + default => "${promtail::data_dir}/promtail-${promtail::version}" + } + $binary_path = $facts['kernel'] ? { + 'windows' => "${version_dir}\\${release_file_name}", + default => "${version_dir}/${release_file_name}" + } - file { [$data_dir, $version_dir]: + file { [$promtail::bin_dir, $promtail::data_dir, $version_dir]: ensure => directory, } - archive { "${binary_path}.gz": + archive { "${binary_path}.${archive_type}": ensure => present, source => "${promtail::source_url}/${promtail::version}/${release_file_name}.${archive_type}", extract => true, @@ -49,17 +61,21 @@ cleanup => false, } + $binary_link_path = $facts['kernel'] ? { + 'windows' => "${promtail::bin_dir}\\promtail.exe", + default => "${promtail::bin_dir}/promtail" + } file { $binary_path: ensure => file, mode => '0755', - require => Archive["${binary_path}.gz"], + require => Archive["${binary_path}.${archive_type}"], ; - "${promtail::bin_dir}/promtail": + $binary_link_path: ensure => link, target => $binary_path, require => File[$binary_path], - notify => Service['promtail'], + notify => $promtail::service::service_to_notify ; } } diff --git a/manifests/params.pp b/manifests/params.pp new file mode 100644 index 0000000..2cdbae3 --- /dev/null +++ b/manifests/params.pp @@ -0,0 +1,20 @@ +# @summary Default promtail variables +# +# Sets default variables per OS +# +# @api private +class promtail::params { + case $facts['kernel'] { + 'Linux': { + $bin_dir = '/usr/local/bin' + $data_dir = '/usr/local/promtail_data' + $config_dir = '/etc/promtail' + } + 'windows': { + $bin_dir = 'C:\\Program Files\\promtail' + $data_dir = "${bin_dir}\\versions" + $config_dir = "${bin_dir}\\config" + } + default: { fail("${facts['kernel']} is not yet supported") } + } +} diff --git a/manifests/service.pp b/manifests/service.pp index e974a04..6295cbb 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -4,6 +4,8 @@ # # @api private class promtail::service { + $service_to_notify = Service['promtail'] + case $facts['kernel'] { 'Linux': { systemd::unit_file { 'promtail.service': @@ -17,6 +19,26 @@ require => Systemd::Unit_file['promtail.service'], } } + 'windows': { + require nssm + + $nssm_presents = $promtail::service_ensure ? { + 'running' => 'present', + default => $promtail::service_ensure + } + nssm::service { 'promtail': + ensure => $nssm_presents, + command => $promtail::install::binary_link_path, + app_parameters => "--config.file=\\\"${promtail::config::config_file}\\\"", + log_file_path => $promtail::log_file_path + } + + service { 'promtail': + ensure => $promtail::service_ensure, + enable => $promtail::service_enable, + require => Nssm::Service['promtail'], + } + } default: { fail("${facts['kernel']} is not supported") } } } diff --git a/metadata.json b/metadata.json index f3f4e90..8cc4afe 100644 --- a/metadata.json +++ b/metadata.json @@ -23,49 +23,38 @@ { "name": "puppetlabs/stdlib", "version_requirement": ">= 6.0.0 < 8.0.0" + }, + { + "name": "mxzinke/nssm", + "version_requirement": ">= 1.0.3 < 2.0.0" } ], "operatingsystem_support": [ { "operatingsystem": "CentOS", - "operatingsystemrelease": [ - "7", - "8" - ] + "operatingsystemrelease": ["7", "8"] }, { "operatingsystem": "OracleLinux", - "operatingsystemrelease": [ - "7", - "8" - ] + "operatingsystemrelease": ["7", "8"] }, { "operatingsystem": "RedHat", - "operatingsystemrelease": [ - "7", - "8" - ] + "operatingsystemrelease": ["7", "8"] }, { "operatingsystem": "Scientific", - "operatingsystemrelease": [ - "7" - ] + "operatingsystemrelease": ["7"] }, { "operatingsystem": "Debian", - "operatingsystemrelease": [ - "9", - "10" - ] + "operatingsystemrelease": ["9", "10"] }, { "operatingsystem": "Ubuntu", - "operatingsystemrelease": [ - "18.04" - ] - } + "operatingsystemrelease": ["18.04"] + }, + { "operatingsystem": "Windows" } ], "requirements": [ {