Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ The following parameters are available in the `promtail` class:
* [`password_file_path`](#password_file_path)
* [`password_file_content`](#password_file_content)
* [`source_url`](#source_url)
* [`install_method`](#install_method)
* [`package_ensure`](#package_ensure)
* [`package_name`](#package_name)

##### <a name="service_enable"></a>`service_enable`

Expand Down Expand Up @@ -228,6 +231,30 @@ The URL from which promtail packages can be downloaded

Default value: `'https://github.com/grafana/loki/releases/download'`

##### <a name="install_method"></a>`install_method`

Data type: `Enum['package', 'archive']`

The way how promtail shall be installed, supported are archive (binary from GitHub) or package from an existing repo

Default value: `'archive'`

##### <a name="package_ensure"></a>`package_ensure`

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

The desired ensure state of the package. Only used if `$install_method` is set to `package`

Default value: `'installed'`

##### <a name="package_name"></a>`package_name`

Data type: `String[1]`

The name of the package. Only used if `$install_method` is set to `package`

Default value: `'promtail'`

## Functions

### <a name="promtailstrip_yaml_header"></a>`promtail::strip_yaml_header`
Expand Down
13 changes: 12 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
# @param [Stdlib::HTTPUrl] source_url
# The URL from which promtail packages can be downloaded
#
# @param install_method
# The way how promtail shall be installed, supported are archive (binary from GitHub) or package from an existing repo
#
# @param package_ensure
# The desired ensure state of the package. Only used if `$install_method` is set to `package`
#
# @param package_name
# The name of the package. Only used if `$install_method` is set to `package`
# @example
# include promtail
#
Expand Down Expand Up @@ -129,13 +137,16 @@
Hash $positions_config_hash,
Hash $scrape_configs_hash,
Stdlib::Absolutepath $bin_dir,
String[1] $checksum,
String[1] $version,
Optional[String[1]] $checksum = undef,
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',
Enum['package', 'archive'] $install_method = 'archive',
Enum['installed', 'absent'] $package_ensure = 'installed',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also allow latest or simply any valid string which would allow pinning versions.

String[1] $package_name = 'promtail',
) {
Class['promtail::install']
-> Class['promtail::config']
Expand Down
103 changes: 55 additions & 48 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,69 @@
#
# @api private
class promtail::install {
include archive
if $promtail::install_method == 'archive' {
assert_type(String[1], $promtail::checksum)
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']}") }
}
case $facts['os']['architecture'] {
'x86_64', 'amd64': { $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}"
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}"
}
}
default: { fail("${facts['kernel']} is not yet supported") }
}
default: { fail("${facts['kernel']} is not yet supported") }
}

if versioncmp($promtail::version, 'v1.0.0') > 0 {
$archive_type = 'zip'
} else {
$archive_type = 'gz'
}
if versioncmp($promtail::version, 'v1.0.0') > 0 {
$archive_type = 'zip'
} else {
$archive_type = 'gz'
}

$version_dir = "${data_dir}/promtail-${promtail::version}"
$binary_path = "${version_dir}/${release_file_name}"
$version_dir = "${data_dir}/promtail-${promtail::version}"
$binary_path = "${version_dir}/${release_file_name}"

file { [$data_dir, $version_dir]:
ensure => directory,
}
file { [$data_dir, $version_dir]:
ensure => directory,
}

archive { "${binary_path}.gz":
ensure => present,
source => "${promtail::source_url}/${promtail::version}/${release_file_name}.${archive_type}",
extract => true,
extract_path => $version_dir,
creates => $binary_path,
checksum => $promtail::checksum,
checksum_type => 'sha256',
cleanup => false,
}
archive { "${binary_path}.gz":
ensure => present,
source => "${promtail::source_url}/${promtail::version}/${release_file_name}.${archive_type}",
extract => true,
extract_path => $version_dir,
creates => $binary_path,
checksum => $promtail::checksum,
checksum_type => 'sha256',
cleanup => false,
}

file {
$binary_path:
ensure => file,
mode => '0755',
require => Archive["${binary_path}.gz"],
;
"${promtail::bin_dir}/promtail":
ensure => link,
target => $binary_path,
require => File[$binary_path],
notify => Service['promtail'],
;
file {
$binary_path:
ensure => file,
mode => '0755',
require => Archive["${binary_path}.gz"],
;
"${promtail::bin_dir}/promtail":
ensure => link,
target => $binary_path,
require => File[$binary_path],
notify => Service['promtail'],
;
}
} elsif $promtail::install_method == 'package' {
package { $promtail::package_name:
ensure => $promtail::package_ensure,
}
}
}
13 changes: 7 additions & 6 deletions manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
class promtail::service {
case $facts['kernel'] {
'Linux': {
systemd::unit_file { 'promtail.service':
source => 'puppet:///modules/promtail/promtail.service',
notify => Service['promtail'],
if $promtail::install_method == 'archive' {
systemd::unit_file { 'promtail.service':
source => 'puppet:///modules/promtail/promtail.service',
notify => Service['promtail'],
}
}
Comment on lines +9 to 14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if $promtail::install_method == 'archive' {
systemd::unit_file { 'promtail.service':
source => 'puppet:///modules/promtail/promtail.service',
notify => Service['promtail'],
}
}
if $promtail::install_method == 'archive' {
systemd::unit_file { 'promtail.service':
source => 'puppet:///modules/promtail/promtail.service',
before => Service['promtail'],
notify => Service['promtail'],
}
}


service { 'promtail':
ensure => $promtail::service_ensure,
enable => $promtail::service_enable,
require => Systemd::Unit_file['promtail.service'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line being removed?

Copy link

@zipkid zipkid Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use a package the systemd unit file would be managed by the package and not as a resource in Puppet.
See block #9-13 in this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that it would make sense then to add a before param to the unit file then...

ensure => $promtail::service_ensure,
enable => $promtail::service_enable,
}
}
default: { fail("${facts['kernel']} is not supported") }
Expand Down
26 changes: 24 additions & 2 deletions spec/classes/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
describe 'promtail::install' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

context 'with valid params' do
let(:facts) { os_facts }
let(:pre_condition) do
"class { 'promtail':
password_file_path => '/etc/promtail/.gc_pw',
Expand Down Expand Up @@ -45,9 +46,30 @@
}"
end

it { is_expected.to compile }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_file('/usr/local/bin/promtail').with_ensure('link') }
it { is_expected.to contain_file('/usr/local/promtail_data').with_ensure('directory') }
it { is_expected.to contain_file('/usr/local/promtail_data/promtail-v2.0.0/promtail-linux-amd64').with_ensure('file') }
it { is_expected.to contain_file('/usr/local/promtail_data/promtail-v2.0.0').with_ensure('directory') }
it { is_expected.to contain_archive('/usr/local/promtail_data/promtail-v2.0.0/promtail-linux-amd64.gz') }
it { is_expected.not_to contain_package('promtail') }
end

context 'with package based installation' do
let(:pre_condition) do
"class { 'promtail':
server_config_hash => {},
clients_config_hash => {},
positions_config_hash => {},
scrape_configs_hash => {},
install_method => 'package',
}"
end

it { is_expected.to compile.with_all_deps }
it { is_expected.not_to contain_file('/usr/local/bin/promtail') }
it { is_expected.not_to contain_file('/usr/local/promtail_data') }
it { is_expected.to contain_package('promtail') }
end
end
end
Expand Down