Debian Codegraph @

lib/puppet/provider/postgresql_replication_slot/ruby.rb

raw
1 # frozen_string_literal: true
2
3 Puppet::Type.type(:postgresql_replication_slot).provide(:ruby) do
4 desc 'For confinement'
5 commands psql: 'psql'
6
7 def self.instances
8 run_sql_command('SELECT * FROM pg_replication_slots;')[0].split("\n").select { |l| l.include?('|') }.map do |l|
9 name, *_others = l.strip.split(%r{\s+\|\s+})
10 new(name: name,
11 ensure: :present)
12 end
13 end
14
15 def self.prefetch(resources)
16 instances.each do |i|
17 slot = resources[i.name]
18 slot.provider = i if slot
19 end
20 end
21
22 def exists?
23 @property_hash[:ensure] == :present
24 end
25
26 def create
27 output = self.class.run_sql_command("SELECT * FROM pg_create_physical_replication_slot('#{resource[:name]}');")
28 raise Puppet::Error, "Failed to create replication slot #{resource[:name]}:\n#{output[0]}" unless output[1].success?
29
30 @property_hash[:ensure] = :present
31 end
32
33 def destroy
34 output = self.class.run_sql_command("SELECT pg_drop_replication_slot('#{resource[:name]}');")
35 raise Puppet::Error, "Failed to destroy replication slot #{resource[:name]}:\n#{output[0]}" unless output[1].success?
36
37 @property_hash[:ensure] = :absent
38 end
39
40 private
41
42 def self.run_sql_command(sql)
43 command = ['psql', '-t', '-c', sql]
44
45 run_command(command, 'postgres', 'postgres')
46 end
47
48 def self.run_command(command, user, group)
49 output = Puppet::Util::Execution.execute(command, uid: user,
50 gid: group,
51 failonfail: false,
52 combine: true,
53 override_locale: true,
54 custom_environment: {})
55 [output, $CHILD_STATUS.dup]
56 end
57 end