Skip to content

Commit a84bfe9

Browse files
committed
(#469) Upgrade: Assign correct environment to node groups
This checks if a user configured a environment in pe.conf. If that's the case, it will be used for the PEADM-specific node groups. Otherwise we fall back to production. This fixes a timing issue discovered in #469. In situations where the PE infra isn't running in production, we cannot assume that a production environment exists. And a node group can only reference classes from the environment the node group belongs to.
1 parent cdb7fe9 commit a84bfe9

File tree

9 files changed

+112
-10
lines changed

9 files changed

+112
-10
lines changed

REFERENCE.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* [`peadm::file_or_content`](#peadm--file_or_content)
3131
* [`peadm::flatten_compact`](#peadm--flatten_compact)
3232
* [`peadm::generate_pe_conf`](#peadm--generate_pe_conf): Generate a pe.conf file in JSON format
33+
* [`peadm::get_node_group_environment`](#peadm--get_node_group_environment): check if a custom PE environment is set in pe.conf
3334
* [`peadm::get_pe_conf`](#peadm--get_pe_conf)
3435
* [`peadm::get_targets`](#peadm--get_targets): Accept undef or a SingleTargetSpec, and return an Array[Target, 1, 0]. This differs from get_target() in that: - It returns an Array[Target
3536
* [`peadm::log_plan_parameters`](#peadm--log_plan_parameters)
@@ -740,6 +741,24 @@ Data type: `Hash`
740741
A hash of settings to set in the config file. Any keys that are set to
741742
undef will not be included in the config file.
742743

744+
### <a name="peadm--get_node_group_environment"></a>`peadm::get_node_group_environment`
745+
746+
Type: Puppet Language
747+
748+
check if a custom PE environment is set in pe.conf
749+
750+
#### `peadm::get_node_group_environment(Peadm::SingleTargetSpec $primary)`
751+
752+
The peadm::get_node_group_environment function.
753+
754+
Returns: `String` the desired environment for PE specific node groups
755+
756+
##### `primary`
757+
758+
Data type: `Peadm::SingleTargetSpec`
759+
760+
the FQDN for the primary, here we will read the pe.conf from
761+
743762
### <a name="peadm--get_pe_conf"></a>`peadm::get_pe_conf`
744763

745764
Type: Puppet Language
@@ -1845,7 +1864,7 @@ Data type: `String[1]`
18451864

18461865
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
18471866

1848-
Default value: `'production'`
1867+
Default value: `peadm::get_node_group_environment($primary_host)`
18491868

18501869
##### <a name="-peadm--add_database--targets"></a>`targets`
18511870

@@ -1945,7 +1964,7 @@ Data type: `String[1]`
19451964

19461965
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
19471966

1948-
Default value: `'production'`
1967+
Default value: `peadm::get_node_group_environment($primary_host)`
19491968

19501969
### <a name="peadm--backup"></a>`peadm::backup`
19511970

@@ -2853,7 +2872,7 @@ Data type: `String[1]`
28532872

28542873
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
28552874

2856-
Default value: `'production'`
2875+
Default value: `peadm::get_node_group_environment($primary_host)`
28572876

28582877
##### <a name="-peadm--upgrade--primary_host"></a>`primary_host`
28592878

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# @summary check if a custom PE environment is set in pe.conf
3+
#
4+
# @param primary the FQDN for the primary, here we will read the pe.conf from
5+
#
6+
# @return [String] the desired environment for PE specific node groups
7+
#
8+
# @see https://www.puppet.com/docs/pe/latest/upgrade_pe#update_environment
9+
#
10+
# @author Tim Meusel <tim@bastelfreak.de>
11+
#
12+
function peadm::get_node_group_environment(Peadm::SingleTargetSpec $primary) {
13+
$peconf = peadm::get_pe_conf(get_target($primary))
14+
# if both are set, they need to be set to the same value
15+
# if they are not set, we assume that the user runs their infra in production
16+
$pe_install = $peconf['pe_install::install::classification::pe_node_group_environment']
17+
$puppet_enterprise = $peconf['puppet_enterprise::master::recover_configuration::pe_environment']
18+
19+
# check if both are equal
20+
# This also evaluates to true if both are undef
21+
if $pe_install == $puppet_enterprise {
22+
# check if the option isn't undef
23+
# ToDo: A proper regex for allowed characters in an environment would be nice
24+
# https://github.com/puppetlabs/puppet-docs/issues/1158
25+
if $pe_install =~ String[1] {
26+
return $pe_install
27+
} else {
28+
return 'production'
29+
}
30+
} else {
31+
fail("pe_install::install::classification::pe_node_group_environment and puppet_enterprise::master::recover_configuration::pe_environment need to be set to the same value, not '${pe_install}' and '${puppet_enterprise}'")
32+
}
33+
}

plans/add_database.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'cleanup-db',
1414
'finalize']] $begin_at_step = undef,
1515
Optional[Boolean] $is_migration = false,
16-
String[1] $node_group_environment = 'production',
16+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
1717
) {
1818
$primary_target = peadm::get_targets($primary_host, 1)
1919
$postgresql_target = peadm::get_targets($targets, 1)

plans/add_replica.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# Common Configuration
2020
Optional[String] $token_file = undef,
21-
String[1] $node_group_environment = 'production',
21+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
2222
) {
2323
$primary_target = peadm::get_targets($primary_host, 1)
2424
$replica_target = peadm::get_targets($replica_host, 1)

plans/convert_compiler_to_legacy.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Peadm::SingleTargetSpec $primary_host,
44
TargetSpec $legacy_hosts,
55
Optional[Boolean] $remove_pdb = true,
6-
String[1] $node_group_environment = 'production',
6+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
77
) {
88
$primary_target = peadm::get_targets($primary_host, 1)
99
$convert_legacy_compiler_targets = peadm::get_targets($legacy_hosts)

plans/upgrade.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
Boolean $permit_unsafe_versions = false,
6464

6565
Optional[Peadm::UpgradeSteps] $begin_at_step = undef,
66-
String[1] $node_group_environment = 'production',
66+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
6767
) {
6868
# Log parameters for debugging
6969
peadm::log_plan_parameters({
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'peadm::get_node_group_environment' do
6+
include BoltSpec::BoltContext
7+
around(:each) do |example|
8+
in_bolt_context do
9+
example.run
10+
end
11+
end
12+
let(:primary) do
13+
['test-vm.puppet.vm']
14+
end
15+
16+
context 'returns production on empty pe.conf' do
17+
it do
18+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{}' })
19+
is_expected.to run.with_params(primary).and_return('production')
20+
end
21+
end
22+
23+
context 'returns production on non-empty pe.conf with unrelated settings' do
24+
it do
25+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{"foo": true}' })
26+
is_expected.to run.with_params(primary).and_return('production')
27+
end
28+
end
29+
context 'returns environment from pe.conf when set twice correctly' do
30+
it do
31+
pe = '{"pe_install::install::classification::pe_node_group_environment": "foo", "puppet_enterprise::master::recover_configuration::pe_environment": "foo"}'
32+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => pe })
33+
is_expected.to run.with_params(primary).and_return('foo')
34+
end
35+
end
36+
context 'fails when both PE options are different' do
37+
it do
38+
pe = '{"pe_install::install::classification::pe_node_group_environment": "foo", "puppet_enterprise::master::recover_configuration::pe_environment": "bar"}'
39+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => pe })
40+
is_expected.to run.with_params(primary).and_raise_error(Puppet::PreformattedError,
41+
%r{pe_install::install::classification::pe_node_group_environment and puppet_enterprise::master::recover_configuration::pe_environment})
42+
end
43+
end
44+
end

spec/plans/convert_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
allow_apply
2020

2121
expect_task('peadm::cert_data').return_for_targets('primary' => trustedjson).be_called_times(2)
22-
expect_task('peadm::read_file').always_return({ 'content' => '2021.7.9' })
22+
expect_task('peadm::read_file').with_params('path' => '/opt/puppetlabs/server/pe_version').always_return({ 'content' => '2021.7.9' })
23+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{}' })
2324
expect_task('peadm::get_group_rules').return_for_targets('primary' => { '_output' => '{"rules": []}' })
2425
expect_task('peadm::node_group_unpin').with_targets('primary').with_params({ 'node_certnames' => ['pe_compiler_legacy'], 'group_name' => 'PE Master' })
2526
expect_task('peadm::check_legacy_compilers').with_targets('primary').with_params({ 'legacy_compilers' => 'pe_compiler_legacy' }).return_for_targets('primary' => { '_output' => '' })

spec/plans/upgrade_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def allow_standard_non_returning_calls
3737

3838
expect_task('peadm::cert_data').return_for_targets('primary' => trusted_primary).be_called_times(1)
3939
expect_task('peadm::check_pe_master_rules').always_return(pe_rule_check)
40+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{}' })
4041

4142
expect(run_plan('peadm::upgrade',
4243
'primary_host' => 'primary',
@@ -50,7 +51,7 @@ def allow_standard_non_returning_calls
5051
expect_task('peadm::read_file')
5152
.with_params('path' => '/opt/puppetlabs/server/pe_build')
5253
.always_return({ 'content' => '2021.7.3' })
53-
54+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{}' })
5455
expect_task('peadm::cert_data').return_for_targets('primary' => trusted_primary,
5556
'compiler' => trusted_compiler).be_called_times(1)
5657
expect_task('peadm::check_pe_master_rules').always_return(pe_rule_check).be_called_times(1)
@@ -109,7 +110,7 @@ def allow_standard_non_returning_calls
109110
it 'updates pe.conf if r10k_known_hosts is set' do
110111
expect_task('peadm::read_file')
111112
.with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf')
112-
.always_return({ 'content' => <<~PECONF })
113+
.always_return({ 'content' => <<~PECONF }).be_called_times(2)
113114
# spec pe.conf
114115
"puppet_enterprise::puppet_master_host": "%{::trusted.certname}"
115116
PECONF
@@ -132,6 +133,8 @@ def allow_standard_non_returning_calls
132133
expect_out_message.with_params(r10k_warning)
133134
expect_task('peadm::check_pe_master_rules').always_return(pe_rule_check)
134135

136+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{}' })
137+
135138
expect(run_plan('peadm::upgrade',
136139
'primary_host' => 'primary',
137140
'version' => '2023.3.0',
@@ -145,6 +148,8 @@ def allow_standard_non_returning_calls
145148
expect_out_message.with_params(r10k_warning).not_be_called
146149
expect_task('peadm::check_pe_master_rules').always_return(pe_rule_check)
147150

151+
expect_task('peadm::read_file').with_params('path' => '/etc/puppetlabs/enterprise/conf.d/pe.conf').always_return({ 'content' => '{}' })
152+
148153
expect(run_plan('peadm::upgrade',
149154
'primary_host' => 'primary',
150155
'version' => '2023.4.0',

0 commit comments

Comments
 (0)