forked from express42/postgresql_lwrp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.rb
More file actions
185 lines (162 loc) · 8.85 KB
/
Copy pathhelpers.rb
File metadata and controls
185 lines (162 loc) · 8.85 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#
# Cookbook Name:: postgresql
# Library:: helpers
#
# Author:: LLC Express 42 (info@express42.com)
#
# Copyright (C) 2012-2014 LLC Express 42
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
class Chef
# Postgresql modules
module Postgresql
# Helpers module
module Helpers
def pg_installed?(pkg_name)
dpkg_status = Mixlib::ShellOut.new("dpkg-query -W -f='${Status}\n' #{pkg_name} 2>/dev/null | grep -c -q 'ok installed'")
dpkg_status.run_command
if dpkg_status.exitstatus == 0
false
else
true
end
end
def systemd_used?
systemd_checker = Mixlib::ShellOut.new('file /sbin/init')
systemd_checker.run_command
if systemd_checker.stdout =~ /systemd/
return true
else
return false
end
end
def exec_in_pg_cluster(cluster_version, cluster_name, *cluster_database, sql)
return [nil, "PostgreSQL cluster #{cluster_name} not running!"] unless pg_running?(cluster_version, cluster_name)
pg_port = get_pg_port(cluster_version, cluster_name)
psql_status = Mixlib::ShellOut.new("echo -n \"#{sql};\" | su -c 'psql -t -p #{pg_port} #{cluster_database.first}' postgres")
psql_status.run_command
[psql_status.stdout, psql_status.stderr]
end
def get_pg_port(cluster_version, cluster_name)
return [nil, nil] unless pg_running?(cluster_version, cluster_name)
postmaster_content = ::File.open("/var/lib/postgresql/#{cluster_version}/#{cluster_name}/postmaster.pid").readlines
postmaster_content[3].to_i
end
def need_to_restart?(allow_restart_cluster, first_time)
if allow_restart_cluster == :first
return first_time
elsif allow_restart_cluster == :always
return true
end
false
end
def pg_running?(cluster_version, cluster_name)
pg_status = Mixlib::ShellOut.new("su -c '/usr/lib/postgresql/#{cluster_version}/bin/pg_ctl \
-D /var/lib/postgresql/#{cluster_version}/#{cluster_name} status' postgres")
pg_status.run_command
if pg_status.stdout =~ /server\ is\ running/
return true
else
return false
end
end
def create_user(cluster_version, cluster_name, cluster_user, options)
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, 'SELECT usename FROM pg_user')
fail "postgresql create_user: can't get users list\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stderr.empty?
if stdout.include? cluster_user
log("postgresql create_user: user '#{cluster_user}' already exists, skiping")
return nil
else
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, "CREATE USER \\\"#{cluster_user}\\\" #{options.map { |t| t.join(' ') }.join(' ')}")
fail "postgresql create_user: can't create user #{cluster_user}\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stdout.include?("CREATE ROLE\n")
log("postgresql create_user: user '#{cluster_user}' created")
end
end
def create_database(cluster_version, cluster_name, cluster_database, options)
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, 'SELECT datname FROM pg_database')
fail "postgresql create_database: can't get database list\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stderr.empty?
if stdout.gsub(/\s+/, ' ').split(' ').include? cluster_database
log("postgresql create_database: database '#{cluster_database}' already exists, skiping")
return nil
else
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, "CREATE DATABASE \\\"#{cluster_database}\\\" #{options.map { |t| t.join(' ') }.join(' ')}")
fail "postgresql create_database: can't create database #{cluster_database}\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stdout.include?("CREATE DATABASE\n")
log("postgresql create_database: database '#{cluster_database}' created")
end
end
def extension_available?(cluster_version, cluster_name, extension)
stdout, _stderr = exec_in_pg_cluster(cluster_version, cluster_name, 'SELECT name FROM pg_available_extensions')
if stdout.include? extension
return true
else
return false
end
end
def install_extension(cluster_version, cluster_name, cluster_database, extension, options)
fail "extension '#{extension}' is not available, please use \'pgxn_extension\' resource to install the extention" unless extension_available?(cluster_version, cluster_name, extension)
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, cluster_database, 'SELECT extname FROM pg_extension')
fail "postgresql install_extension: can't get extensions list\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stderr.empty?
if stdout.include? extension
log("postgresql install: extension '#{extension}' already installed, skiping")
return nil
else
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, cluster_database, "CREATE EXTENSION \\\"#{extension}\\\" #{options.map { |t| t.join(' ') }.join(' ')}")
fail "postgresql install_extension: can't install extension #{extension}\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stdout.include?("CREATE EXTENSION\n")
log("postgresql install_extension: extension '#{extension}' installed")
end
end
def pgxn_install_extension(cluster_version, cluster_name, params, options)
pgxn_status = Mixlib::ShellOut.new("pgxn install '#{params[:name]}'='#{params[:version]}' --sudo --#{params[:stage]}")
pgxn_status.run_command
stdout, stderr = exec_in_pg_cluster(cluster_version, cluster_name, params[:db], 'SELECT extname FROM pg_extension')
fail "postgresql install_extension: can't get extensions list\nSTDOUT: #{stdout}\nSTDERR: #{stderr}" unless stderr.empty?
if stdout.include? params[:name].downcase
log("postgresql install: extension '#{params[:name]}' already installed, skipping")
return nil
else
pgxn_status = Mixlib::ShellOut.new("sudo -u postgres pgxn load '#{params[:name]}'='#{params[:version]}' -d #{params[:db]} --#{params[:stage]} #{options.map { |t| t.join(' ') }.join(' ')}")
pgxn_status.run_command
fail "postgresql install_extension: can't install extension #{params[:name]}\nSTDOUT: #{pgxn_status.stdout}\nSTDERR: #{pgxn_status.stderr}" unless pgxn_status.stdout.include?('CREATE EXTENSION')
log("postgresql install_extension: extension '#{params[:name]}' installed")
end
end
def configuration_hacks(configuration, cluster_version)
configuration['unix_socket_directory'] ||= '/var/run/postgresql' if cluster_version.to_f < 9.3
configuration['unix_socket_directories'] ||= '/var/run/postgresql' if cluster_version.to_f >= 9.3
configuration.delete('wal_receiver_status_interval') if cluster_version.to_f < 9.1
configuration.delete('hot_standby_feedback') if cluster_version.to_f < 9.1
end
def cloud_backup_configuration_hacks(configuration, cluster_name, cluster_version, wal_e_bin)
configuration['archive_command'] = "envdir /etc/wal-e.d/#{cluster_name}-#{cluster_version}/env/ #{wal_e_bin} wal-push %p"
end
def params_validation(provider, credentials)
case provider
when 's3'
required_params = [:AWS_ACCESS_KEY_ID, :AWS_SECRET_ACCESS_KEY, :WALE_S3_PREFIX]
when 'swift'
required_params = [:SWIFT_AUTHURL, :SWIFT_TENANT, :SWIFT_USER, :SWIFT_PASSWORD, :WALE_SWIFT_PREFIX]
when 'azure'
required_params = [:WABS_ACCOUNT_NAME, :WABS_ACCESS_KEY, :WALE_WABS_PREFIX]
end
required_params - credentials.keys.map { |key| key.upcase.to_sym }
end
end
end
end