Skip to content

Commit f2d7126

Browse files
committed
(puppetlabs#469) 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 puppetlabs#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 6a28860 commit f2d7126

14 files changed

+183
-60
lines changed

REFERENCE.md

Lines changed: 55 additions & 49 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)
@@ -105,10 +106,6 @@
105106
* [`peadm::add_compiler`](#peadm--add_compiler): Proxy plan for peadm::add_compilers.
106107
* [`peadm::add_compilers`](#peadm--add_compilers): Add new compilers to a PE architecture or replace an existing with new configuration.
107108
* [`peadm::add_database`](#peadm--add_database)
108-
* [`peadm::add_replica`](#peadm--add_replica): Add or replace a replica host.
109-
Supported use cases:
110-
1: Adding a replica to an existing primary.
111-
2: The existing replica is broken, we have a fresh new VM we want to provision the replica to.
112109
* [`peadm::backup`](#peadm--backup): Backup puppet primary configuration
113110
* [`peadm::backup_ca`](#peadm--backup_ca)
114111
* [`peadm::convert`](#peadm--convert): Convert an existing PE cluster to a PEAdm-managed cluster
@@ -740,6 +737,24 @@ Data type: `Hash`
740737
A hash of settings to set in the config file. Any keys that are set to
741738
undef will not be included in the config file.
742739

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

745760
Type: Puppet Language
@@ -1832,12 +1847,21 @@ The peadm::add_database class.
18321847

18331848
The following parameters are available in the `peadm::add_database` plan:
18341849

1850+
* [`node_group_environment`](#-peadm--add_database--node_group_environment)
18351851
* [`targets`](#-peadm--add_database--targets)
18361852
* [`primary_host`](#-peadm--add_database--primary_host)
18371853
* [`mode`](#-peadm--add_database--mode)
18381854
* [`begin_at_step`](#-peadm--add_database--begin_at_step)
18391855
* [`is_migration`](#-peadm--add_database--is_migration)
18401856

1857+
##### <a name="-peadm--add_database--node_group_environment"></a>`node_group_environment`
1858+
1859+
Data type: `String[1]`
1860+
1861+
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
1862+
1863+
Default value: `peadm::get_node_group_environment($primary_host)`
1864+
18411865
##### <a name="-peadm--add_database--targets"></a>`targets`
18421866

18431867
Data type: `Peadm::SingleTargetSpec`
@@ -1884,51 +1908,6 @@ Data type: `Optional[Boolean]`
18841908

18851909
Default value: `false`
18861910

1887-
### <a name="peadm--add_replica"></a>`peadm::add_replica`
1888-
1889-
Add or replace a replica host.
1890-
Supported use cases:
1891-
1: Adding a replica to an existing primary.
1892-
2: The existing replica is broken, we have a fresh new VM we want to provision the replica to.
1893-
1894-
#### Parameters
1895-
1896-
The following parameters are available in the `peadm::add_replica` plan:
1897-
1898-
* [`primary_host`](#-peadm--add_replica--primary_host)
1899-
* [`replica_host`](#-peadm--add_replica--replica_host)
1900-
* [`replica_postgresql_host`](#-peadm--add_replica--replica_postgresql_host)
1901-
* [`token_file`](#-peadm--add_replica--token_file)
1902-
1903-
##### <a name="-peadm--add_replica--primary_host"></a>`primary_host`
1904-
1905-
Data type: `Peadm::SingleTargetSpec`
1906-
1907-
- The hostname and certname of the primary Puppet server
1908-
1909-
##### <a name="-peadm--add_replica--replica_host"></a>`replica_host`
1910-
1911-
Data type: `Peadm::SingleTargetSpec`
1912-
1913-
- The hostname and certname of the replica VM
1914-
1915-
##### <a name="-peadm--add_replica--replica_postgresql_host"></a>`replica_postgresql_host`
1916-
1917-
Data type: `Optional[Peadm::SingleTargetSpec]`
1918-
1919-
- The hostname and certname of the host with the replica PE-PosgreSQL database.
1920-
Can be a separate host in an XL architecture, or undef in Standard or Large.
1921-
1922-
Default value: `undef`
1923-
1924-
##### <a name="-peadm--add_replica--token_file"></a>`token_file`
1925-
1926-
Data type: `Optional[String]`
1927-
1928-
- (optional) the token file in a different location than the default.
1929-
1930-
Default value: `undef`
1931-
19321911
### <a name="peadm--backup"></a>`peadm::backup`
19331912

19341913
Backup puppet primary configuration
@@ -2016,6 +1995,7 @@ management using PEAdm.
20161995
The following parameters are available in the `peadm::convert` plan:
20171996

20181997
* [`begin_at_step`](#-peadm--convert--begin_at_step)
1998+
* [`node_group_environment`](#-peadm--convert--node_group_environment)
20191999
* [`primary_host`](#-peadm--convert--primary_host)
20202000
* [`replica_host`](#-peadm--convert--replica_host)
20212001
* [`compiler_hosts`](#-peadm--convert--compiler_hosts)
@@ -2035,6 +2015,14 @@ The step where the plan should start. If not set, it will start at the beginning
20352015

20362016
Default value: `undef`
20372017

2018+
##### <a name="-peadm--convert--node_group_environment"></a>`node_group_environment`
2019+
2020+
Data type: `String[1]`
2021+
2022+
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
2023+
2024+
Default value: `peadm::get_node_group_environment($primary_host)`
2025+
20382026
##### <a name="-peadm--convert--primary_host"></a>`primary_host`
20392027

20402028
Data type: `Peadm::SingleTargetSpec`
@@ -2129,6 +2117,7 @@ The following parameters are available in the `peadm::install` plan:
21292117
* [`final_agent_state`](#-peadm--install--final_agent_state)
21302118
* [`stagingdir`](#-peadm--install--stagingdir)
21312119
* [`uploaddir`](#-peadm--install--uploaddir)
2120+
* [`node_group_environment`](#-peadm--install--node_group_environment)
21322121
* [`primary_host`](#-peadm--install--primary_host)
21332122
* [`replica_host`](#-peadm--install--replica_host)
21342123
* [`compiler_hosts`](#-peadm--install--compiler_hosts)
@@ -2229,6 +2218,14 @@ for offline usage.
22292218

22302219
Default value: `undef`
22312220

2221+
##### <a name="-peadm--install--node_group_environment"></a>`node_group_environment`
2222+
2223+
Data type: `String[1]`
2224+
2225+
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
2226+
2227+
Default value: `peadm::get_node_group_environment($primary_host)`
2228+
22322229
##### <a name="-peadm--install--primary_host"></a>`primary_host`
22332230

22342231
Data type: `Peadm::SingleTargetSpec`
@@ -2723,6 +2720,7 @@ The following parameters are available in the `peadm::upgrade` plan:
27232720
* [`stagingdir`](#-peadm--upgrade--stagingdir)
27242721
* [`uploaddir`](#-peadm--upgrade--uploaddir)
27252722
* [`begin_at_step`](#-peadm--upgrade--begin_at_step)
2723+
* [`node_group_environment`](#-peadm--upgrade--node_group_environment)
27262724
* [`primary_host`](#-peadm--upgrade--primary_host)
27272725
* [`replica_host`](#-peadm--upgrade--replica_host)
27282726
* [`compiler_hosts`](#-peadm--upgrade--compiler_hosts)
@@ -2819,6 +2817,14 @@ The step where the plan should start. If not set, it will start at the beginning
28192817

28202818
Default value: `undef`
28212819

2820+
##### <a name="-peadm--upgrade--node_group_environment"></a>`node_group_environment`
2821+
2822+
Data type: `String[1]`
2823+
2824+
environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
2825+
2826+
Default value: `peadm::get_node_group_environment($primary_host)`
2827+
28222828
##### <a name="-peadm--upgrade--primary_host"></a>`primary_host`
28232829

28242830
Data type: `Peadm::SingleTargetSpec`
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 <[email protected]>
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+
}

manifests/setup/node_manager.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# A load balancer address directing traffic to any of the "B" pool
2424
# compilers. This is used for DR configuration in large and extra large
2525
# architectures.
26+
# @param node_group_environment the environment that will be assigned to all the PE Infra node groups
2627
#
2728
class peadm::setup::node_manager (
2829
String[1] $primary_host,
@@ -36,6 +37,7 @@
3637
Optional[String[1]] $compiler_pool_address = undef,
3738
Optional[String[1]] $internal_compiler_a_pool_address = $server_a_host,
3839
Optional[String[1]] $internal_compiler_b_pool_address = $server_b_host,
40+
String[1] $node_group_environment = 'production',
3941
) {
4042
# "Not-configured" placeholder string. This will be used in places where we
4143
# cannot set an explicit null, and need to supply some kind of value.
@@ -46,6 +48,7 @@
4648
# else.
4749
Node_group {
4850
purge_behavior => none,
51+
environment => $node_group_environment,
4952
}
5053

5154
##################################################

plans/add_database.pp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#
2+
# @param node_group_environment environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
3+
#
14
plan peadm::add_database(
25
Peadm::SingleTargetSpec $targets,
36
Peadm::SingleTargetSpec $primary_host,
@@ -10,6 +13,7 @@
1013
'cleanup-db',
1114
'finalize']] $begin_at_step = undef,
1215
Optional[Boolean] $is_migration = false,
16+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
1317
) {
1418
$primary_target = peadm::get_targets($primary_host, 1)
1519
$postgresql_target = peadm::get_targets($targets, 1)
@@ -98,7 +102,7 @@
98102
run_plan('peadm::subplans::component_install', $postgresql_target,
99103
primary_host => $primary_target,
100104
avail_group_letter => $avail_group_letter,
101-
role => 'puppet/puppetdb-database'
105+
role => 'puppet/puppetdb-database',
102106
)
103107
}
104108

@@ -135,15 +139,17 @@
135139
$host = pick($a_host, $b_host)
136140
out::verbose("In transitive state, setting classification to ${host}")
137141
run_plan('peadm::util::update_classification', $primary_target,
138-
postgresql_a_host => $host,
139-
postgresql_b_host => $host,
140-
peadm_config => $peadm_config
142+
postgresql_a_host => $host,
143+
postgresql_b_host => $host,
144+
peadm_config => $peadm_config,
145+
node_group_environment => $node_group_environment,
141146
)
142147
} else {
143148
run_plan('peadm::util::update_classification', $primary_target,
144-
postgresql_a_host => $avail_group_letter ? { 'A' => $postgresql_host, default => undef },
145-
postgresql_b_host => $avail_group_letter ? { 'B' => $postgresql_host, default => undef },
146-
peadm_config => $peadm_config
149+
postgresql_a_host => $avail_group_letter ? { 'A' => $postgresql_host, default => undef },
150+
postgresql_b_host => $avail_group_letter ? { 'B' => $postgresql_host, default => undef },
151+
peadm_config => $peadm_config,
152+
node_group_environment => $node_group_environment,
147153
)
148154
}
149155
}

plans/add_replica.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# @param replica_postgresql_host - The hostname and certname of the host with the replica PE-PosgreSQL database.
88
# Can be a separate host in an XL architecture, or undef in Standard or Large.
99
# @param token_file - (optional) the token file in a different location than the default.
10+
# @param node_group_environment environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
11+
#
12+
# @param token_file - (optional) the token file in a different location than the default.
1013
plan peadm::add_replica(
1114
# Standard or Large
1215
Peadm::SingleTargetSpec $primary_host,
@@ -17,6 +20,7 @@
1720

1821
# Common Configuration
1922
Optional[String] $token_file = undef,
23+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
2024
) {
2125
$primary_target = peadm::get_targets($primary_host, 1)
2226
$replica_target = peadm::get_targets($replica_host, 1)
@@ -97,6 +101,7 @@
97101
internal_compiler_a_pool_address => $replica_avail_group_letter ? { 'A' => $replica_target.peadm::certname(), default => undef },
98102
internal_compiler_b_pool_address => $replica_avail_group_letter ? { 'B' => $replica_target.peadm::certname(), default => undef },
99103
peadm_config => $peadm_config
104+
node_group_environment => $node_group_environment,
100105
)
101106

102107
# Source list of files on Primary and synchronize to new Replica

plans/convert.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# management using PEAdm.
66
#
77
# @param begin_at_step The step where the plan should start. If not set, it will start at the beginning
8+
# @param node_group_environment environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
89
#
910
plan peadm::convert (
1011
# Standard
@@ -26,6 +27,7 @@
2627
Array[String] $dns_alt_names = [],
2728

2829
Optional[Peadm::ConvertSteps] $begin_at_step = undef,
30+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
2931
) {
3032
peadm::assert_supported_bolt_version()
3133

@@ -316,6 +318,7 @@
316318
compiler_pool_address => $compiler_pool_address,
317319
internal_compiler_a_pool_address => $internal_compiler_a_pool_address,
318320
internal_compiler_b_pool_address => $internal_compiler_b_pool_address,
321+
node_group_environment => $node_group_environment,
319322
require => Class['peadm::setup::node_manager_yaml'],
320323
}
321324

plans/install.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
# Directory the installer tarball will be uploaded to or expected to be in
3232
# for offline usage.
3333
#
34+
# @param node_group_environment environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
35+
#
3436
plan peadm::install (
3537
# Standard
3638
Peadm::SingleTargetSpec $primary_host,
@@ -74,6 +76,7 @@
7476
Peadm::Download_mode $download_mode = 'bolthost',
7577
Boolean $permit_unsafe_versions = false,
7678
String $token_lifetime = '1y',
79+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
7780
) {
7881
# Log parameters for debugging
7982
peadm::log_plan_parameters({
@@ -148,6 +151,7 @@
148151
internal_compiler_b_pool_address => $internal_compiler_b_pool_address,
149152
deploy_environment => $deploy_environment,
150153
ldap_config => $ldap_config,
154+
node_group_environment => $node_group_environment,
151155

152156
# Other
153157
stagingdir => $stagingdir,

plans/subplans/configure.pp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# Configures the state the puppet agent should be in on infrastructure nodes
2121
# after PE is configured successfully.
2222
#
23+
# @param node_group_environment environment for the PEADM specific node groups, if not set it will be gathered from pe.conf or production
24+
#
2325
plan peadm::subplans::configure (
2426
# Standard
2527
Peadm::SingleTargetSpec $primary_host,
@@ -43,7 +45,8 @@
4345

4446
# Other
4547
String $stagingdir = '/tmp',
46-
Enum['running', 'stopped'] $final_agent_state = 'running'
48+
Enum['running', 'stopped'] $final_agent_state = 'running',
49+
String[1] $node_group_environment = peadm::get_node_group_environment($primary_host),
4750
) {
4851
# TODO: get and validate PE version
4952

@@ -106,6 +109,7 @@
106109
compiler_pool_address => $compiler_pool_address,
107110
internal_compiler_a_pool_address => $internal_compiler_a_pool_address,
108111
internal_compiler_b_pool_address => $internal_compiler_b_pool_address,
112+
node_group_environment => $node_group_environment,
109113
require => Class['peadm::setup::node_manager_yaml'],
110114
}
111115
}

0 commit comments

Comments
 (0)