-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathbackup_classification.rb
executable file
·45 lines (38 loc) · 1.31 KB
/
backup_classification.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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