Skip to content

Commit f318a88

Browse files
committed
Preserve existing csr_attributes data
In the event a csr_attributes.yaml file is already present, don't overwrite it; instead, merge in the values we need to any values already present.
1 parent 6dd5afd commit f318a88

File tree

5 files changed

+84
-34
lines changed

5 files changed

+84
-34
lines changed

plans/action/install.pp

+19-28
Original file line numberDiff line numberDiff line change
@@ -179,38 +179,29 @@
179179
upload_path => $upload_tarball_path,
180180
)
181181

182-
# Create csr_attributes.yaml files for the nodes that need them
183-
# There is a problem with OID names in csr_attributes.yaml for some
184-
# installs, e.g. PE 2019.0.1, PUP-9746. Use the raw OIDs for now.
185-
186-
run_task('peadm::mkdir_p_file', $master_target,
187-
path => '/etc/puppetlabs/puppet/csr_attributes.yaml',
188-
content => @("HEREDOC"),
189-
---
190-
extension_requests:
191-
${peadm::oid('peadm_role')}: "puppet/master"
192-
${peadm::oid('peadm_availability_group')}: "A"
193-
| HEREDOC
182+
# Create csr_attributes.yaml files for the nodes that need them. Ensure that
183+
# if a csr_attributes.yaml file is already present, the values we need are
184+
# merged with the existing values.
185+
186+
run_plan('peadm::util::insert_csr_extensions', $master_target,
187+
extensions => {
188+
peadm::oid('peadm_role') => 'puppet/master',
189+
peadm::oid('peadm_availability_group') => 'A',
190+
},
194191
)
195192

196-
run_task('peadm::mkdir_p_file', $puppetdb_database_target,
197-
path => '/etc/puppetlabs/puppet/csr_attributes.yaml',
198-
content => @("HEREDOC"),
199-
---
200-
extension_requests:
201-
${peadm::oid('peadm_role')}: "puppet/puppetdb-database"
202-
${peadm::oid('peadm_availability_group')}: "A"
203-
| HEREDOC
193+
run_plan('peadm::util::insert_csr_extensions', $puppetdb_database_target,
194+
extensions => {
195+
peadm::oid('peadm_role') => 'puppet/puppetdb-database',
196+
peadm::oid('peadm_availability_group') => 'A',
197+
},
204198
)
205199

206-
run_task('peadm::mkdir_p_file', $puppetdb_database_replica_target,
207-
path => '/etc/puppetlabs/puppet/csr_attributes.yaml',
208-
content => @("HEREDOC"),
209-
---
210-
extension_requests:
211-
${peadm::oid('peadm_role')}: "puppet/puppetdb-database"
212-
${peadm::oid('peadm_availability_group')}: "B"
213-
| HEREDOC
200+
run_plan('peadm::util::insert_csr_extensions', $puppetdb_database_replica_target,
201+
extensions => {
202+
peadm::oid('peadm_role') => 'puppet/puppetdb-database',
203+
peadm::oid('peadm_availability_group') => 'B',
204+
},
214205
)
215206

216207
# Get the master installation up and running. The installer will

plans/util/insert_csr_extensions.pp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plan peadm::util::insert_csr_extensions (
2+
TargetSpec $targets,
3+
Hash $extensions,
4+
) {
5+
get_targets($targets).each |$target| {
6+
$csr_attributes_data = ($csr_file = run_task('peadm::read_file', $target,
7+
path => '/etc/puppetlabs/puppet/csr_attributes.yaml',
8+
).first['content']) ? {
9+
undef => { },
10+
default => $csr_file.parseyaml,
11+
}
12+
13+
run_task('peadm::mkdir_p_file', $target,
14+
path => '/etc/puppetlabs/puppet/csr_attributes.yaml',
15+
content => $csr_attributes_data.deep_merge({'extension_requests' => $extensions}).to_yaml,
16+
)
17+
}
18+
}

tasks/read_file.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"input_method": "stdin",
1010
"implementations": [
11-
{"name": "read_file.rb"}
11+
{"name": "read_file.rb", "requirements": ["puppet-agent"]},
12+
{"name": "read_file.sh", "requirements": ["shell"]}
1213
]
1314
}

tasks/read_file.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
require 'json'
44

5-
params = JSON.parse(STDIN.read)
6-
content = File.read(params['path'])
7-
result = { 'content' => content }.to_json
8-
9-
puts result
5+
begin
6+
params = JSON.parse(STDIN.read)
7+
content = File.read(params['path'])
8+
rescue StandardError => err
9+
result = {
10+
'content' => nil,
11+
'error' => err.message,
12+
}
13+
else
14+
result = {
15+
'content' => content
16+
}
17+
ensure
18+
puts result.to_json
19+
end

tasks/read_file.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
main() {
4+
if [ -r "$PT_path" ]; then
5+
cat <<-EOS
6+
{
7+
"content": $(python_cmd -c "import json; print json.dumps(open('$PT_path','r').read())")
8+
}
9+
EOS
10+
else
11+
cat <<-EOS
12+
{
13+
"content": null,
14+
"error": "File does not exist or is not readable"
15+
}
16+
EOS
17+
fi
18+
}
19+
20+
python_cmd() {
21+
if command -v python >/dev/null 2>&1; then
22+
python "$@"
23+
else
24+
python3 "$@"
25+
fi
26+
}
27+
28+
main "$@"
29+
exit_code=$?
30+
exit $exit_code

0 commit comments

Comments
 (0)