Skip to content

Commit e3985c5

Browse files
committed
allow promtail installation via package
1 parent e92208d commit e3985c5

File tree

5 files changed

+124
-57
lines changed

5 files changed

+124
-57
lines changed

REFERENCE.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ The following parameters are available in the `promtail` class:
122122
* [`password_file_path`](#password_file_path)
123123
* [`password_file_content`](#password_file_content)
124124
* [`source_url`](#source_url)
125+
* [`install_method`](#install_method)
126+
* [`package_ensure`](#package_ensure)
127+
* [`package_name`](#package_name)
125128

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

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

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

234+
##### <a name="install_method"></a>`install_method`
235+
236+
Data type: `Enum['package', 'archive']`
237+
238+
The way how promtail shall be installed, supported are archive (binary from GitHub) or package from an existing repo
239+
240+
Default value: `'archive'`
241+
242+
##### <a name="package_ensure"></a>`package_ensure`
243+
244+
Data type: `Enum['installed', 'absent']`
245+
246+
The desired ensure state of the package. Only used if `$install_method` is set to `package`
247+
248+
Default value: `'installed'`
249+
250+
##### <a name="package_name"></a>`package_name`
251+
252+
Data type: `String[1]`
253+
254+
The name of the package. Only used if `$install_method` is set to `package`
255+
256+
Default value: `'promtail'`
257+
231258
## Functions
232259

233260
### <a name="promtailstrip_yaml_header"></a>`promtail::strip_yaml_header`

manifests/init.pp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@
6060
# @param [Stdlib::HTTPUrl] source_url
6161
# The URL from which promtail packages can be downloaded
6262
#
63+
# @param install_method
64+
# The way how promtail shall be installed, supported are archive (binary from GitHub) or package from an existing repo
65+
#
66+
# @param package_ensure
67+
# The desired ensure state of the package. Only used if `$install_method` is set to `package`
68+
#
69+
# @param package_name
70+
# The name of the package. Only used if `$install_method` is set to `package`
6371
# @example
6472
# include promtail
6573
#
@@ -129,13 +137,16 @@
129137
Hash $positions_config_hash,
130138
Hash $scrape_configs_hash,
131139
Stdlib::Absolutepath $bin_dir,
132-
String[1] $checksum,
133140
String[1] $version,
141+
Optional[String[1]] $checksum = undef,
134142
Optional[Hash] $server_config_hash = undef,
135143
Optional[Hash] $target_config_hash = undef,
136144
Optional[Stdlib::Absolutepath] $password_file_path = undef,
137145
Optional[Sensitive[String[1]]] $password_file_content = undef,
138146
Stdlib::HTTPUrl $source_url = 'https://github.com/grafana/loki/releases/download',
147+
Enum['package', 'archive'] $install_method = 'archive',
148+
Enum['installed', 'absent'] $package_ensure = 'installed',
149+
String[1] $package_name = 'promtail',
139150
) {
140151
Class['promtail::install']
141152
-> Class['promtail::config']

manifests/install.pp

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,69 @@
44
#
55
# @api private
66
class promtail::install {
7-
include archive
7+
if $promtail::install_method == 'archive' {
8+
assert_type(String[1], $promtail::checksum)
9+
include archive
810

9-
case $facts['os']['architecture'] {
10-
'x86_64', 'amd64': { $arch = 'amd64' }
11-
'aarch64': { $arch = 'arm64' }
12-
'armv7l': { $arch = 'arm' }
13-
default: { fail("Unsupported kernel architecture: ${facts['os']['architecture']}") }
14-
}
11+
case $facts['os']['architecture'] {
12+
'x86_64', 'amd64': { $arch = 'amd64' }
13+
'aarch64': { $arch = 'arm64' }
14+
'armv7l': { $arch = 'arm' }
15+
default: { fail("Unsupported kernel architecture: ${facts['os']['architecture']}") }
16+
}
1517

16-
case $facts['kernel'] {
17-
'Linux': {
18-
$data_dir = '/usr/local/promtail_data'
19-
if versioncmp($promtail::version, 'v0.3.0') > 0 {
20-
$release_file_name = "promtail-linux-${arch}"
21-
} else {
22-
$release_file_name = "promtail_linux_${arch}"
18+
case $facts['kernel'] {
19+
'Linux': {
20+
$data_dir = '/usr/local/promtail_data'
21+
if versioncmp($promtail::version, 'v0.3.0') > 0 {
22+
$release_file_name = "promtail-linux-${arch}"
23+
} else {
24+
$release_file_name = "promtail_linux_${arch}"
25+
}
2326
}
27+
default: { fail("${facts['kernel']} is not yet supported") }
2428
}
25-
default: { fail("${facts['kernel']} is not yet supported") }
26-
}
2729

28-
if versioncmp($promtail::version, 'v1.0.0') > 0 {
29-
$archive_type = 'zip'
30-
} else {
31-
$archive_type = 'gz'
32-
}
30+
if versioncmp($promtail::version, 'v1.0.0') > 0 {
31+
$archive_type = 'zip'
32+
} else {
33+
$archive_type = 'gz'
34+
}
3335

34-
$version_dir = "${data_dir}/promtail-${promtail::version}"
35-
$binary_path = "${version_dir}/${release_file_name}"
36+
$version_dir = "${data_dir}/promtail-${promtail::version}"
37+
$binary_path = "${version_dir}/${release_file_name}"
3638

37-
file { [$data_dir, $version_dir]:
38-
ensure => directory,
39-
}
39+
file { [$data_dir, $version_dir]:
40+
ensure => directory,
41+
}
4042

41-
archive { "${binary_path}.gz":
42-
ensure => present,
43-
source => "${promtail::source_url}/${promtail::version}/${release_file_name}.${archive_type}",
44-
extract => true,
45-
extract_path => $version_dir,
46-
creates => $binary_path,
47-
checksum => $promtail::checksum,
48-
checksum_type => 'sha256',
49-
cleanup => false,
50-
}
43+
archive { "${binary_path}.gz":
44+
ensure => present,
45+
source => "${promtail::source_url}/${promtail::version}/${release_file_name}.${archive_type}",
46+
extract => true,
47+
extract_path => $version_dir,
48+
creates => $binary_path,
49+
checksum => $promtail::checksum,
50+
checksum_type => 'sha256',
51+
cleanup => false,
52+
}
5153

52-
file {
53-
$binary_path:
54-
ensure => file,
55-
mode => '0755',
56-
require => Archive["${binary_path}.gz"],
57-
;
58-
"${promtail::bin_dir}/promtail":
59-
ensure => link,
60-
target => $binary_path,
61-
require => File[$binary_path],
62-
notify => Service['promtail'],
63-
;
54+
file {
55+
$binary_path:
56+
ensure => file,
57+
mode => '0755',
58+
require => Archive["${binary_path}.gz"],
59+
;
60+
"${promtail::bin_dir}/promtail":
61+
ensure => link,
62+
target => $binary_path,
63+
require => File[$binary_path],
64+
notify => Service['promtail'],
65+
;
66+
}
67+
} elsif $promtail::install_method == 'package' {
68+
package { $promtail::package_name:
69+
ensure => $promtail::package_ensure,
70+
}
6471
}
6572
}

manifests/service.pp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
class promtail::service {
77
case $facts['kernel'] {
88
'Linux': {
9-
systemd::unit_file { 'promtail.service':
10-
source => 'puppet:///modules/promtail/promtail.service',
11-
notify => Service['promtail'],
9+
if $promtail::install_method == 'archive' {
10+
systemd::unit_file { 'promtail.service':
11+
source => 'puppet:///modules/promtail/promtail.service',
12+
notify => Service['promtail'],
13+
}
1214
}
1315

1416
service { 'promtail':
15-
ensure => $promtail::service_ensure,
16-
enable => $promtail::service_enable,
17-
require => Systemd::Unit_file['promtail.service'],
17+
ensure => $promtail::service_ensure,
18+
enable => $promtail::service_enable,
1819
}
1920
}
2021
default: { fail("${facts['kernel']} is not supported") }

spec/classes/install_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
describe 'promtail::install' do
44
on_supported_os.each do |os, os_facts|
55
context "on #{os}" do
6+
let(:facts) { os_facts }
67
context 'with valid params' do
7-
let(:facts) { os_facts }
88
let(:pre_condition) do
99
"class { 'promtail':
1010
password_file_path => '/etc/promtail/.gc_pw',
@@ -45,9 +45,30 @@
4545
}"
4646
end
4747

48-
it { is_expected.to compile }
48+
it { is_expected.to compile.with_all_deps }
4949
it { is_expected.to contain_file('/usr/local/bin/promtail').with_ensure('link') }
5050
it { is_expected.to contain_file('/usr/local/promtail_data').with_ensure('directory') }
51+
it { is_expected.to contain_file('/usr/local/promtail_data/promtail-v2.0.0/promtail-linux-amd64').with_ensure('file') }
52+
it { is_expected.to contain_file('/usr/local/promtail_data/promtail-v2.0.0').with_ensure('directory') }
53+
it { is_expected.to contain_archive('/usr/local/promtail_data/promtail-v2.0.0/promtail-linux-amd64.gz') }
54+
it { is_expected.not_to contain_package('promtail') }
55+
end
56+
57+
context 'with package based installation' do
58+
let(:pre_condition) do
59+
"class { 'promtail':
60+
server_config_hash => {},
61+
clients_config_hash => {},
62+
positions_config_hash => {},
63+
scrape_configs_hash => {},
64+
install_method => 'package',
65+
}"
66+
end
67+
68+
it { is_expected.to compile.with_all_deps }
69+
it { is_expected.not_to contain_file('/usr/local/bin/promtail') }
70+
it { is_expected.not_to contain_file('/usr/local/promtail_data') }
71+
it { is_expected.to contain_package('promtail') }
5172
end
5273
end
5374
end

0 commit comments

Comments
 (0)