-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__module__.pm
310 lines (249 loc) · 7.55 KB
/
__module__.pm
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package Rex::Module::Database::Postgres;
use Rex -base;
use Rex::Commands::User;
use Data::Dumper;
our $__configuration = {
user => 'postgres',
home_dir => '/var/lib/pgsql',
data_dir => '/var/lib/pgsql/data',
conf_dir => '/var/lib/pgsql/data',
log_dir => '/var/log/postgresql',
locale => 'en_US.UTF8',
encoding => 'UTF8',
owner => 'postgres',
group => 'postgres',
#authmethod => 'md5',
};
our @__hba = (
{
type => 'host',
database => 'all',
user => 'all',
address => '127.0.0.1/32',
method => 'md5'
},
{
type => 'host',
database => 'all',
user => 'all',
address => '::1/128',
method => 'md5'
}
);
our @__settings = (
{ listen_addresses => "'*'" },
{ dynamic_shared_memory_type => 'posix' },
);
our $__bin_path = {
debian => "/usr/bin",
ubuntu => "/usr/bin",
centos => "/usr/bin",
mageia => "/usr/bin",
};
our $__service_name = {
debian => "postgresql",
ubuntu => "postgresql",
centos => "postgresql",
mageia => "postgresql",
};
our $__package_name = {
debian => "postgresql",
ubuntu => "postgresql",
centos => "postgresql-server",
mageia => "postgresql",
};
our $__contrib_package_name = {
debian => "postgresql-contrib",
ubuntu => "postgresql-contrib",
centos => "postgresql-contrib",
mageia => "postgresql-contrib",
};
task 'setup',
sub {
my $package_name = param_lookup ("package_name", case ( lc(operating_system), $__package_name ));
my $contrib_package_name = param_lookup ("contrib_package_name", case ( lc(operating_system), $__contrib_package_name ));
update_package_db;
my $os = get_operating_system();
pkg $package_name, ensure => "latest";
pkg $contrib_package_name, ensure => "latest";
};
task 'initialize',
sub {
my $postgres_service = param_lookup ("service_name", case ( lc(operating_system), $__service_name ));
initialize_folders ();
initialize_service ();
initialize_configs();
#if ($changed) {
# restart();
#}
# Service Setup
run "systemctl enable $postgres_service";
die("Error enabling service") unless ($? == 0);
};
my @service_actions = qw( start stop restart );
foreach my $service_action (@service_actions) {
task "$service_action" => sub {
my $postgres_service = param_lookup ("service_name", case ( lc(operating_system), $__service_name ));
service $postgres_service => "$service_action";
};
}
sub postgresql_psql {
my $postgres_bin_path = param_lookup ("bin_path", case ( lc(operating_system), $__bin_path ));
my ($user,$password,$host,$db_stmt,$filename) = @_;
return run $postgres_bin_path."/psql -U $user -h $host $db_stmt < $filename",
env => {
PGPASSWORD => "$password",
};
};
# postgresql_sudo_psql: to execute psql command using postgres user
# @sql_query is a string with the SQL query to execute
# @db_stmt is a string with the name of the database
sub postgresql_sudo_psql {
my $postgres_config = getConfiguration();
my $postgres_bin_path = param_lookup ("bin_path", case ( lc(operating_system), $__bin_path ));
my $os = get_operating_system();
my $sql_query = shift;
die("Error missing sql query for psql") unless ($sql_query);
my $db_stmt = shift;
my $user = $postgres_config->{user};
sudo {
command => => sub {
run "$postgres_bin_path/psql $db_stmt -c \"$sql_query\"",
},
user => $user,
continuous_read => sub {
#output to log
Rex::Logger::info(@_, "warn");
},
};
die("Error on psql -c $sql_query") unless ($? == 0);
};
sub initialize_service {
my $postgres_config = getConfiguration();
my $data_dir = $postgres_config->{data_dir};
my $method = 'initialize_service_'.lc(get_operating_system());
&{ \&$method }();
die("Data dir not present, initialization failed") unless (is_dir($data_dir));
}
sub initialize_service_debian {
my $postgres_config = getConfiguration();
my $postgres_bin_path = param_lookup ("bin_path", case ( lc(operating_system), $__bin_path ));
my $locale = $postgres_config->{locale};
my $encoding = $postgres_config->{encoding};
my $user = $postgres_config->{user};
my $data_dir = $postgres_config->{data_dir};
my $log_dir = $postgres_config->{log_dir};
my $os = get_operating_system();
if (!is_dir($data_dir)) {
# Create a new PostgreSQL database cluster
sudo {
command => sub {
say run "$postgres_bin_path/initdb --encoding=$encoding --locale=$locale --pgdata=$data_dir";
},
user => $user,
continuous_read => sub {
#output to log
Rex::Logger::info(@_, "warn");
},
};
}
die("Error on 'initdb' command.") unless ($? == 0);
}
sub initialize_service_ubuntu {
initialize_service_debian();
}
sub initialize_service_centos {
my $postgres_config = getConfiguration();
my $postgres_bin_path = param_lookup ("bin_path", case ( lc(operating_system), $__bin_path ));
my $locale = $postgres_config->{locale};
my $encoding = $postgres_config->{encoding};
my $user = $postgres_config->{user};
my $data_dir = $postgres_config->{data_dir};
my $log_dir = $postgres_config->{log_dir};
my $os = get_operating_system();
if (!is_dir($data_dir)) {
# Create a new PostgreSQL database cluster
run $postgres_bin_path ."/postgresql-setup initdb",
continuous_read => sub {
#output to log
Rex::Logger::info(@_, "warn");
},
env => {
PGSETUP_INITDB_OPTIONS => "--encoding=$encoding --locale=$locale --pgdata=$data_dir",
PGDATA => "$data_dir",
};
}
die("Error on 'postgresql-setup initdb' command.") unless ($? == 0);
};
sub initialize_folders {
my $postgres_config = getConfiguration();
my $owner = $postgres_config->{owner};
my $group = $postgres_config->{group};
my $data_dir = $postgres_config->{data_dir};
my $log_dir = $postgres_config->{log_dir};
# mv data dir to tmp dir
if (is_dir($data_dir)) {
#file $data_dir."_old", ensure => "absent";
#mv ($data_dir, $data_dir."_old");
Rex::Logger::info("DATA dir already exists", "warn");
} else {
#TODO: TO TEST - NOT SURE WHAT's THE IMPACT on Postgres setup
file $data_dir, ensure => "directory",
owner => $owner,
group => $group;
}
file $log_dir, ensure => "directory",
owner => $owner,
group => $group;
};
sub initialize_configs {
my $postgres_config = getConfiguration();
my $user = $postgres_config->{user};
my $postgres_hba = param_lookup ("hba", \@__hba);
my $postgres_settings = param_lookup ("settings", \@__settings);
my $conf_dir = $postgres_config->{conf_dir};
# if config dir doesn't exist, create one
if (!is_dir($conf_dir)) {
file $conf_dir, ensure => "directory";
}
# ensure we don't have repeated settings, remove existing
my %existing_keys;
my @filtered_settings;
foreach my $setting (@{$postgres_settings}) {
foreach my $key (keys %$setting) {
unless ($existing_keys{$key}) {
$existing_keys{$key} = 1;
push @filtered_settings, { $key => $setting->{$key} };
}
}
}
$postgres_settings = \@filtered_settings;
file "$conf_dir/postgresql.conf",
content => template("templates/postgresql.conf.tpl", settings => $postgres_settings);
file "$conf_dir/pg_hba.conf",
content => template("templates/pg_hba.conf.tpl", hba => $postgres_hba);
};
sub isInstalled {
return is_installed(param_lookup ("package_name", case ( lc(operating_system()), $__package_name )));
}
sub getConfiguration {
return param_lookup ("configuration", $__configuration);
}
1;
=pod
=head1 NAME
$::module_name - {{ SHORT DESCRIPTION }}
=head1 DESCRIPTION
{{ LONG DESCRIPTION }}
=head1 USAGE
{{ USAGE DESCRIPTION }}
include qw/Rex::Module::Database::Postgres/;
task yourtask => sub {
Rex::Module::Database::Postgres::setup();
};
=head1 TASKS
=over 4
=item example
This is an example Task. This task just output's the uptime of the system.
=back
=cut