Skip to content

Add peadm::backup plan #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 86 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
655e4f7
(SOLARCH-564) first shot at returning node classification
davidsandilands Nov 26, 2021
ae9a065
(SOLARCH-564) mistake on default string
davidsandilands Nov 26, 2021
d93dd9e
(SOLARCH-564) extra bracket
davidsandilands Nov 26, 2021
1688fa1
(SOLARCH-564) cat walked over keyboard and added S to line
davidsandilands Nov 26, 2021
584273e
(SOLARCH-564) missed out the unless which actually runs it
davidsandilands Nov 29, 2021
ff5a5fe
(SOLARCH-564) missing an end
davidsandilands Nov 29, 2021
b505bf2
(SOLARCH-564) adjusting message string to sub in variavble correctly
davidsandilands Nov 29, 2021
dae5021
(SOLARCH-564) changing variable choice to directory and not file name…
davidsandilands Nov 29, 2021
9612691
(SOLARCH-564) correcting the string
davidsandilands Nov 29, 2021
7184389
(SOLARCH-564) correcting variable name
davidsandilands Nov 29, 2021
64a9199
(SOLARCH-564) correcting variable
davidsandilands Nov 29, 2021
70330be
(SOLARCH-564) correcting string
davidsandilands Nov 29, 2021
8779539
(SOLARCH-564) correcting comment
davidsandilands Nov 29, 2021
62b3c74
(SOLARCH-564) changing to a patter to check it starts and ends with a /
davidsandilands Nov 29, 2021
5c6f37f
(SOLARCH-564) first draft of plan
davidsandilands Dec 7, 2021
845842f
SOLARCH-564 test running simply classification and cert backup
davidsandilands Dec 16, 2021
2775535
SOLARCH-564 removing over complication of type
davidsandilands Dec 17, 2021
3d2dbc4
SOLARCH-564 correcting linting error and variable error
davidsandilands Dec 17, 2021
1d7041c
SOLARCH-564 testing database backup command
davidsandilands Dec 17, 2021
d758b1c
(SOLARCH-564) missing a target
davidsandilands Dec 17, 2021
3841eb4
(SOLARCH-564) testing a lamda of database selection and names
davidsandilands Dec 17, 2021
fe8670b
SOLARCH-564 corrected database to backup array
davidsandilands Dec 17, 2021
bcc3746
SOLARCH-564 correcting type
davidsandilands Dec 17, 2021
aa085ae
SOLARCH-564 changing to allow backup on postgres external db
davidsandilands Dec 17, 2021
49263ea
SOLARCH-564 correcting primary host
davidsandilands Dec 17, 2021
05648bb
(SOLARCH-564) changing directory to not end with a slash
davidsandilands Dec 22, 2021
7ba8732
(SOLARCH-564) changing path to absolutepath to check its a valid dire…
davidsandilands Dec 22, 2021
2977c6d
(SOLARCH-564) wasn't thinking just keep it as a string
davidsandilands Dec 22, 2021
948243f
(SOLARCH-564) adding basic plan testing and correcting a lint error
davidsandilands Dec 23, 2021
a3b61ea
(SOLARCH-716) updated default version to latest LTS .8
davidsandilands Dec 23, 2021
3b3b5b5
(SOLARCH-716) updated peadm to be compatible with 2.x puppetlabs/service
davidsandilands Dec 23, 2021
11927ab
Merge pull request #227 from puppetlabs/SOLARCH-716
davidsandilands Dec 29, 2021
748975e
Fix failing table output tests
reidmv Jan 4, 2022
131def2
Merge pull request #228 from puppetlabs/fix-table-tests
davidsandilands Jan 4, 2022
327fe83
Update plans/backup.pp
davidsandilands Jan 5, 2022
836e022
Update plans/backup.pp
davidsandilands Jan 5, 2022
cd22f57
Update plans/backup.pp
davidsandilands Jan 5, 2022
d4c42e6
Update plans/backup.pp
davidsandilands Jan 5, 2022
671c026
(SOLARCH-564) first shot at returning node classification
davidsandilands Nov 26, 2021
6d86ec4
(SOLARCH-564) mistake on default string
davidsandilands Nov 26, 2021
99e40b1
(SOLARCH-564) extra bracket
davidsandilands Nov 26, 2021
d48e9e3
(SOLARCH-564) cat walked over keyboard and added S to line
davidsandilands Nov 26, 2021
cfa422a
(SOLARCH-564) missed out the unless which actually runs it
davidsandilands Nov 29, 2021
cee21ae
(SOLARCH-564) missing an end
davidsandilands Nov 29, 2021
0bce9f0
(SOLARCH-564) adjusting message string to sub in variavble correctly
davidsandilands Nov 29, 2021
6052092
(SOLARCH-564) changing variable choice to directory and not file name…
davidsandilands Nov 29, 2021
cfa58ea
(SOLARCH-564) correcting the string
davidsandilands Nov 29, 2021
c3f5ca7
(SOLARCH-564) correcting variable name
davidsandilands Nov 29, 2021
27a9589
(SOLARCH-564) correcting variable
davidsandilands Nov 29, 2021
48b3f61
(SOLARCH-564) correcting string
davidsandilands Nov 29, 2021
b6131a8
(SOLARCH-564) correcting comment
davidsandilands Nov 29, 2021
69ec0a1
(SOLARCH-564) changing to a patter to check it starts and ends with a /
davidsandilands Nov 29, 2021
91c009a
(SOLARCH-564) first draft of plan
davidsandilands Dec 7, 2021
9f9604a
SOLARCH-564 test running simply classification and cert backup
davidsandilands Dec 16, 2021
7f8818b
SOLARCH-564 removing over complication of type
davidsandilands Dec 17, 2021
595403b
SOLARCH-564 correcting linting error and variable error
davidsandilands Dec 17, 2021
1ab2ab1
SOLARCH-564 testing database backup command
davidsandilands Dec 17, 2021
55f7ff1
(SOLARCH-564) missing a target
davidsandilands Dec 17, 2021
537dcb4
(SOLARCH-564) testing a lamda of database selection and names
davidsandilands Dec 17, 2021
32ae115
SOLARCH-564 corrected database to backup array
davidsandilands Dec 17, 2021
1df38bd
SOLARCH-564 correcting type
davidsandilands Dec 17, 2021
d4b73e9
SOLARCH-564 changing to allow backup on postgres external db
davidsandilands Dec 17, 2021
4d6d913
SOLARCH-564 correcting primary host
davidsandilands Dec 17, 2021
0df924c
(SOLARCH-564) changing directory to not end with a slash
davidsandilands Dec 22, 2021
0416cb3
(SOLARCH-564) changing path to absolutepath to check its a valid dire…
davidsandilands Dec 22, 2021
126d181
(SOLARCH-564) wasn't thinking just keep it as a string
davidsandilands Dec 22, 2021
d7b5f70
(SOLARCH-564) adding basic plan testing and correcting a lint error
davidsandilands Dec 23, 2021
5d7fa60
Update plans/backup.pp
davidsandilands Jan 5, 2022
11c9420
Update plans/backup.pp
davidsandilands Jan 5, 2022
9a95a22
Update plans/backup.pp
davidsandilands Jan 5, 2022
dcd942a
Update plans/backup.pp
davidsandilands Jan 5, 2022
f6b135f
Merge branch 'SOLARCH-564' of https://github.com/puppetlabs/puppetlab…
davidsandilands Jan 5, 2022
5e04881
(SOLARCH-564) adding in creation of backup directory via apply
davidsandilands Jan 5, 2022
5ee9f04
(SOLARCH-564) updating for all backups to go to backup directory
davidsandilands Jan 5, 2022
f2f6a5b
(SOLARCH-564) add time to allow multiple backups on a day
davidsandilands Jan 5, 2022
cbb6738
(SOLARCH-564) making dates consistent
davidsandilands Jan 5, 2022
60daf3b
(SOLARCH-564) updated with secret keys for ldap and orchestrator and …
davidsandilands Jan 7, 2022
ba7bb6f
(SOLARCH-564) updating to output if test failed
davidsandilands Jan 7, 2022
4848ace
(SOLARCH-564) removing needless comma
davidsandilands Jan 7, 2022
dc3e95c
(SOLARCH-564) dropped part of key names in error
davidsandilands Jan 7, 2022
0d318ea
(SOLARCH-564) inserting deliberate error to test exit code
davidsandilands Jan 7, 2022
8451276
(SOLARCH-564) changing approach so it exits properly
davidsandilands Jan 7, 2022
fdf795d
(SOLARCH-564) test succesful for failure correcting to correct certif…
davidsandilands Jan 7, 2022
06796df
(SOLARCH-564) fixing plan spec with limitiation of timestamps
davidsandilands Jan 10, 2022
0be2aec
(SOLARCH-564) backup classification was in error using server status API
davidsandilands Jan 14, 2022
f5b08c3
(solarch-564) removing parsing
davidsandilands Jan 20, 2022
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
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
"name": "puppetlabs/service",
"version_requirement": ">= 1.3.0 < 2.0.0"
"version_requirement": ">= 1.3.0 < 3.0.0"
}
],
"operatingsystem_support": [
Expand Down
87 changes: 87 additions & 0 deletions plans/backup.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# @summary Backup the core user settings for puppet infrastructure
#
# This plan can backup data as outlined at insert doc
#
plan peadm::backup (
# Standard
Peadm::SingleTargetSpec $primary_host,
Optional[Peadm::SingleTargetSpec] $replica_host = undef,

# Large
Optional[TargetSpec] $compiler_hosts = undef,

# Extra Large
Optional[Peadm::SingleTargetSpec] $primary_postgresql_host = undef,
Optional[Peadm::SingleTargetSpec] $replica_postgresql_host = undef,

# Which data to backup
Boolean $backup_orchestrator = true,
Boolean $backup_rbac = true,
Boolean $backup_activity = true,
Boolean $backup_ca_ssl = true,
Boolean $backup_puppetdb = false,
Boolean $backup_classification = true,
String $output_directory = '/tmp',
){

$timestamp = Timestamp.new().strftime('%F_%T')
$backup_directory = "${output_directory}/pe-backup-${timestamp}"
# Create backup folder
apply_prep($primary_host)
apply($primary_host){
file { $backup_directory :
ensure => 'directory',
owner => 'root',
group => 'pe-postgres',
mode => '0770'
}
}
# Create an array of the names of databases and whether they have to be backed up to use in a lambda later
$database_to_backup = [ $backup_orchestrator, $backup_activity, $backup_rbac, $backup_puppetdb]
$database_names = [ 'pe-orchestrator' , 'pe-activity' , 'pe-rbac' , 'pe-puppetdb' ]

peadm::assert_supported_bolt_version()

# Ensure input valid for a supported architecture
$arch = peadm::assert_supported_architecture(
$primary_host,
$replica_host,
$primary_postgresql_host,
$replica_postgresql_host,
$compiler_hosts,
)

if $backup_classification {
out::message('# Backing up classification')
run_task('peadm::backup_classification', $primary_host,
directory => $backup_directory,
)
}

if $backup_ca_ssl {
out::message('# Backing up ca and ssl certificates')
run_command("/opt/puppetlabs/bin/puppet-backup create --dir=${backup_directory} --scope=certs", $primary_host)
}

# Check if /etc/puppetlabs/console-services/conf.d/secrets/keys.json exists and if so back it up
out::message('# Backing up ldap secret key if it exists')
run_command("test -f /etc/puppetlabs/console-services/conf.d/secrets/keys.json && cp -rp /etc/puppetlabs/console-services/conf.d/secrets/keys.json ${backup_directory} || echo secret ldap key doesnt exist" , $primary_host) # lint:ignore:140chars

# IF backing up orchestrator back up the secrets too /etc/puppetlabs/orchestration-services/conf.d/secrets/
if $backup_orchestrator {
out::message('# Backing up orchestrator secret keys')
run_command("cp -rp /etc/puppetlabs/orchestration-services/conf.d/secrets ${backup_directory}/", $primary_host)
}

$database_to_backup.each |Integer $index, Boolean $value | {
if $value {
out::message("# Backing up database ${database_names[$index]}")
# If the primary postgresql host is set then pe-puppetdb needs to be remotely backed up to primary.
if $database_names[$index] == 'pe-puppetdb' and $primary_postgresql_host {
run_command("sudo -u pe-puppetdb /opt/puppetlabs/server/bin/pg_dump \"sslmode=verify-ca host=${primary_postgresql_host} sslcert=/etc/puppetlabs/puppetdb/ssl/${primary_host}.cert.pem sslkey=/etc/puppetlabs/puppetdb/ssl/${primary_host}.private_key.pem sslrootcert=/etc/puppetlabs/puppet/ssl/certs/ca.pem dbname=pe-puppetdb\" -f /tmp/puppetdb_$(date +%F_%T).bin" , $primary_host) # lint:ignore:140chars
} else {
run_command("sudo -u pe-postgres /opt/puppetlabs/server/bin/pg_dump -Fc \"${database_names[$index]}\" -f \"${backup_directory}/${database_names[$index]}_$(date +%F_%T).bin\"" , $primary_host) # lint:ignore:140chars
}
}
}
}
2 changes: 1 addition & 1 deletion plans/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

# Common Configuration
String $console_password,
String $version = '2019.8.5',
String $version = '2019.8.8',
Optional[Array[String]] $dns_alt_names = undef,
Optional[String] $compiler_pool_address = undef,
Optional[String] $internal_compiler_a_pool_address = undef,
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/plans/failed_table.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+-----------------+------------------+--------------------------+--------+
+------------------------------------------------------------------------+
| Failed Service Status |
+-----------------+------------------+--------------------------+--------+
| Cluster | Service | Url | Status |
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/plans/passed_table.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+-----------------+---------------------------+------------------+-------------+
+------------------------------------------------------------------------------+
| Operational Service Status |
+-----------------+---------------------------+------------------+-------------+
| Cluster | Service | Url | Status |
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/plans/summary_table.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+-----------------+----------+
+----------------------------+
| Overall Status: degraded |
+-----------------+----------+
| Cluster | Status |
Expand Down
20 changes: 20 additions & 0 deletions spec/plans/backup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe 'peadm::backup' do
include BoltSpec::Plans
let(:params) { { 'primary_host' => 'primary' } }

it 'runs with default params' do
allow_apply_prep
allow_apply
expect_out_message.with_params('# Backing up ca and ssl certificates')
# The commands all have a timestamp in them and frankly its prooved to hard with bolt spec to work this out
allow_any_command
expect_out_message.with_params('# Backing up database pe-orchestrator')
expect_out_message.with_params('# Backing up database pe-activity')
expect_out_message.with_params('# Backing up database pe-rbac')
expect_out_message.with_params('# Backing up classification')
expect_task('peadm::backup_classification')
expect(run_plan('peadm::backup', params)).to be_ok
end
end
13 changes: 13 additions & 0 deletions tasks/backup_classification.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"puppet_task_version": 1,
"supports_noop": false,
"description": "A task to call the classification api and write to file",
"parameters": {
"directory": {
"type": "String",
"description": "The directory to write the classification output to. Directory must exist",
"default": "/tmp"
}
},
"input_method": "stdin"
}
45 changes: 45 additions & 0 deletions tasks/backup_classification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/opt/puppetlabs/puppet/bin/ruby

# Puppet Task Name: backup_classification
require 'net/https'
require 'uri'
require 'json'
require 'puppet'

# BackupClassiciation task class
class BackupClassification
def initialize(params)
@params = params
end

def execute!
File.write("#{@params['directory']}/classification_backup.json", return_classification)
puts "Classification written to #{@params['directory']}/classification_backup.json"
end

private

def https_client
client = Net::HTTP.new('localhost', '4433')
client.use_ssl = true
client.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
client.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]))
client.verify_mode = OpenSSL::SSL::VERIFY_NONE
client
end

def return_classification
classification = https_client
classification_request = Net::HTTP::Get.new('/classifier-api/v1/groups')

classification.request(classification_request).body
end
end
# Run the task unless an environment flag has been set, signaling not to. The
# environment flag is used to disable auto-execution and enable Ruby unit
# testing of this task.
unless ENV['RSPEC_UNIT_TEST_MODE']
Puppet.initialize_settings
task = BackupClassification.new(JSON.parse(STDIN.read))
task.execute!
end