forked from anubia/py_pg_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorchestrator.py
1255 lines (1069 loc) · 49.5 KB
/
orchestrator.py
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from alterer import Alterer
from backer import Backer
from backer import BackerCluster
from configurator import Configurator
from connecter import Connecter
from const.const import Messenger
from const.const import Queries
from db_selector.db_selector import DbSelector
from dir_tools.dir_tools import Dir
from dropper import Dropper
from informer import Informer
from logger.logger import Logger
from replicator import Replicator
from restorer import Restorer
from restorer import RestorerCluster
from scheduler import Scheduler
from terminator import Terminator
from trimmer import Trimmer
from trimmer import TrimmerCluster
from vacuumer import Vacuumer
class Orchestrator:
action = None # The action to do
args = [] # The list of parameters received in console
logger = None # A logger to show and log some messages
def __init__(self, action, args):
self.action = action
self.args = args
self.logger = self.get_logger()
try:
# Create mailer if necessary
if self.args.config_mailer:
self.create_mailer()
except Exception:
pass
# Stop execution if the user running the program is root
Dir.forbid_root(self.logger)
@staticmethod
def show_dbs(dbs_list, logger):
'''
Target:
- show in console and log a list of databases.
Parameters:
- dbs_list: the list of databases to be shown.
- logger: a logger to show and log some messages.
'''
logger.highlight('info', Messenger.ANALIZING_PG_DATA, 'white')
for db in dbs_list: # Para cada BD en PostgreSQL...
message = Messenger.DETECTED_DB.format(dbname=db['datname'])
logger.info(message)
@staticmethod
def get_cfg_vars(config_type, config_path, logger=None):
'''
Target:
- get all the variables stored in a config file with cfg extension.
Parameters:
- config_type: the type of config file which is going to be loaded,
to differ it from the other types which will have different
sections and variables.
- logger: a logger to show and log some messages.
Return:
- the parser which will contain all the config file variables.
'''
configurator = Configurator()
# In the case of logger being None, this is because a logger config
# file is being loaded to specify some settings
if logger:
configurator.load_cfg(config_type, config_path, logger)
else:
configurator.load_cfg(config_type, config_path)
return configurator.parser
def get_logger(self):
'''
Target:
- get a logger object with its variables.
Return:
- a logger to show and log some messages.
'''
# If the user specified a logger config file through console...
if self.args.config_logger:
config_type = 'log'
# Get the variables from the config file, without sending any
# logger!! It would give a redundancy error
parser = Orchestrator.get_cfg_vars(config_type,
self.args.config_logger)
# Overwrite the config variables with the console ones if necessary
if self.args.logger_logfile:
parser.log_vars['log_dir'] = self.args.logger_logfile
if self.args.logger_level:
parser.log_vars['level'] = self.args.logger_level
if self.args.logger_mute:
parser.log_vars['mute'] = self.args.logger_mute
# Create the logger with the specified variables
logger = Logger(parser.log_vars['log_dir'],
parser.log_vars['level'], parser.log_vars['mute'])
# If the user did not specify a logger config file through console...
else:
# Create the logger with the console variables
logger = Logger(self.args.logger_logfile, self.args.logger_level,
self.args.logger_mute)
return logger
def create_mailer(self):
'''
Target:
- get a mailer object with variables to send emails informing
about the results of the program's execution.
Return:
- a mailer which will send emails informing about the results of
the program's execution.
'''
config_type = 'mail'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type,
self.args.config_mailer,
self.logger)
# Create the mailer with the specified variables
self.logger.create_mailer(parser.mail_vars['level'],
parser.mail_vars['name'],
parser.mail_vars['address'],
parser.mail_vars['password'],
parser.mail_vars['to'],
parser.mail_vars['cc'],
parser.mail_vars['bcc'],
parser.mail_vars['server_tag'],
parser.mail_vars['external_ip'],
self.action)
def get_connecter(self):
'''
Target:
- get a connecter object with its variables.
Return:
- a connecter which will allow queries to PostgreSQL.
'''
# If the user specified a connecter config file through console...
if self.args.config_connection:
config_type = 'connect'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type,
self.args.config_connection,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.pg_host:
parser.conn_vars['server'] = self.args.pg_host
if self.args.pg_user:
parser.conn_vars['user'] = self.args.pg_user
if self.args.pg_port:
parser.conn_vars['port'] = self.args.pg_port
# Create the connecter with the specified variables
connecter = Connecter(server=parser.conn_vars['server'],
user=parser.conn_vars['user'],
port=parser.conn_vars['port'],
logger=self.logger)
# If the user did not specify a connecter config file through console..
else:
# Create the connecter with the console variables
connecter = Connecter(server=self.args.pg_host,
user=self.args.pg_user,
port=self.args.pg_port, logger=self.logger)
return connecter
def get_alterer(self, connecter):
'''
Target:
- get an alterer object with variables to change owners of some
PostgreSQL databases.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- an alterer which will change owners of PostgreSQL databases.
'''
# If the user specified an alterer config file through console...
if self.args.config:
config_type = 'alter'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.db_name:
parser.bkp_vars['in_dbs'] = True
elif self.args.old_role:
parser.bkp_vars['old_role'] = self.args.old_role
elif self.args.new_role:
parser.bkp_vars['new_role'] = self.args.new_role
else:
pass
# Create the alterer with the specified variables
alterer = Alterer(connecter, parser.bkp_vars['in_dbs'],
parser.bkp_vars['old_role'],
parser.bkp_vars['new_role'], self.logger)
# If the user did not specify an alterer config file through console...
else:
# Create the alterer with the console variables
alterer = Alterer(connecter, self.args.db_name, self.args.old_role,
self.args.new_role, self.logger)
return alterer
def get_db_backer(self, connecter):
'''
Target:
- get a backer object with variables to backup databases.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a backer which will backup databases.
'''
# If the user specified a backer config file through console...
if self.args.config:
config_type = 'backup'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if
# necessary
if self.args.bkp_path:
parser.bkp_vars['bkp_path'] = self.args.bkp_path
if self.args.group:
parser.bkp_vars['group'] = self.args.group
if self.args.backup_format:
parser.bkp_vars['bkp_type'] = self.args.backup_format
if self.args.db_name:
parser.bkp_vars['in_dbs'] = self.args.db_name
parser.bkp_vars['ex_dbs'] = []
parser.bkp_vars['in_regex'] = ''
parser.bkp_vars['ex_regex'] = ''
if self.args.ex_templates:
parser.bkp_vars['ex_templates'] = True
elif self.args.no_ex_templates:
parser.bkp_vars['ex_templates'] = False
if self.args.vacuum:
parser.bkp_vars['vacuum'] = True
elif self.args.no_vacuum:
parser.bkp_vars['vacuum'] = False
if self.args.db_owner:
parser.bkp_vars['db_owner'] = self.args.db_owner
# Create the backer with the specified variables
backer = Backer(connecter, parser.bkp_vars['bkp_path'],
parser.bkp_vars['group'],
parser.bkp_vars['bkp_type'],
parser.bkp_vars['prefix'],
parser.bkp_vars['in_dbs'],
parser.bkp_vars['in_regex'],
parser.bkp_vars['in_priority'],
parser.bkp_vars['ex_dbs'],
parser.bkp_vars['ex_regex'],
parser.bkp_vars['ex_templates'],
parser.bkp_vars['vacuum'],
parser.bkp_vars['db_owner'], self.logger)
# If the user did not specify a backer config file through console...
else:
if self.args.ex_templates:
ex_templates = True
elif self.args.no_ex_templates:
ex_templates = False
else:
ex_templates = True
if self.args.vacuum:
vacuum = True
elif self.args.no_vacuum:
vacuum = False
else:
vacuum = True
# Create the backer with the console variables
backer = Backer(connecter, bkp_path=self.args.bkp_path,
group=self.args.group,
bkp_type=self.args.backup_format,
in_dbs=self.args.db_name,
ex_templates=ex_templates, vacuum=vacuum,
db_owner=self.args.db_owner,
logger=self.logger)
return backer
def get_cl_backer(self, connecter):
'''
Target:
- get a backer object with variables to backup databases' clusters.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a backer which will backup databases' clusters.
'''
# If the user specified a backer config file through console...
if self.args.config:
config_type = 'backup_all'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.bkp_path:
parser.bkp_vars['bkp_path'] = self.args.bkp_path
if self.args.group:
parser.bkp_vars['group'] = self.args.group
if self.args.backup_format:
parser.bkp_vars['bkp_type'] = self.args.backup_format
if self.args.vacuum:
parser.bkp_vars['vacuum'] = True
elif self.args.no_vacuum:
parser.bkp_vars['vacuum'] = False
# Create the backer with the specified variables
backer = BackerCluster(connecter, parser.bkp_vars['bkp_path'],
parser.bkp_vars['group'],
parser.bkp_vars['bkp_type'],
parser.bkp_vars['prefix'],
parser.bkp_vars['vacuum'], self.logger)
# If the user did not specify a backer config file through console...
else:
if self.args.vacuum:
vacuum = True
elif self.args.no_vacuum:
vacuum = False
else:
vacuum = True
# Create the backer with the console variables
backer = BackerCluster(connecter, bkp_path=self.args.bkp_path,
group=self.args.group,
bkp_type=self.args.backup_format,
vacuum=vacuum, logger=self.logger)
return backer
def get_dropper(self, connecter):
'''
Target:
- get a dropper object with variables to drop some PostgreSQL
databases.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a dropper which will delete some PostgreSQL databases.
'''
# If the user specified a dropper config file through console...
if self.args.config:
config_type = 'drop'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.db_name:
parser.bkp_vars['in_dbs'] = self.args.db_name
# Create the dropper with the specified variables
dropper = Dropper(connecter, parser.bkp_vars['in_dbs'],
self.logger)
# If the user did not specify a dropper config file through console...
else:
# Create the dropper with the console variables
dropper = Dropper(connecter, self.args.db_name, self.logger)
return dropper
def get_informer(self, connecter):
'''
Target:
- get an informer object with variables to show some PostgreSQL
information.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a informer which will get and show some PostgreSQL information.
'''
# Create the informer with the console variables
informer = Informer(connecter, self.args.details_conns,
self.args.details_dbs, self.args.details_users,
self.logger)
return informer
def get_replicator(self, connecter):
'''
Target:
- get a replicator object with variables to clone a PostgreSQL
database.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a replicator which will clone a PostgreSQL database.
'''
# If the user specified a replicator config file through console...
if self.args.config:
config_type = 'replicate'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.db_name:
parser.bkp_vars['new_dbname'] = self.args.db_name[0]
parser.bkp_vars['original_dbname'] = self.args.db_name[1]
# Create the replicator with the specified variables
replicator = Replicator(connecter, parser.bkp_vars['new_dbname'],
parser.bkp_vars['original_dbname'],
self.logger)
# If the user did not specify a replicator config file through
# console...
else:
# Create the replicator with the console variables
replicator = Replicator(connecter, self.args.db_name[0],
self.args.db_name[1], self.logger)
return replicator
def get_db_restorer(self, connecter):
'''
Target:
- get a restorer object with variables to restore a database backup
in PostgreSQL.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a restorer which will restore a PostgreSQL database.
'''
# If the user specified a restorer config file through console...
if self.args.config:
config_type = 'restore'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.db_backup:
parser.bkp_vars['bkp_path'] = self.args.db_backup[0]
parser.bkp_vars['new_dbname'] = self.args.db_backup[1]
# Create the restorer with the specified variables
restorer = Restorer(connecter, parser.bkp_vars['bkp_path'],
parser.bkp_vars['new_dbname'], self.logger)
# If the user did not specify a restorer config file through console...
else:
# Create the restorer with the console variables
restorer = Restorer(connecter, self.args.db_backup[0],
self.args.db_backup[1], self.logger)
return restorer
def get_cl_restorer(self, connecter):
'''
Target:
- get a restorer object with variables to restore a cluster backup
in PostgreSQL.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a restorer which will restore a PostgreSQL cluster.
'''
# If the user specified a restorer config file through console...
if self.args.config:
config_type = 'restore_all'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.cluster_backup:
parser.bkp_vars['bkp_path'] = self.args.cluster_backup
# Create the restorer with the specified variables
restorer = RestorerCluster(connecter, parser.bkp_vars['bkp_path'],
self.logger)
# If the user did not specify a restorer config file through console...
else:
# Create the restorer with the console variables
restorer = Restorer(connecter, self.args.cluster_backup,
self.logger)
return restorer
def get_scheduler(self):
'''
Target:
- get a scheduler object with variables to modify or show the
content of the program's CRON file.
Return:
- a scheduler which will modify or show the content of the
program's CRON file.
'''
# If the user specified a scheduler config file through console...
if self.args.add_config:
config_type = 'schedule'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type,
self.args.add_config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.add:
parser.bkp_vars['time'] = self.args.add[0]
parser.bkp_vars['command'] = self.args.add[1]
# Create the scheduler with the specified variables
scheduler = Scheduler(parser.bkp_vars['time'],
parser.bkp_vars['command'], self.logger)
elif self.args.remove_config:
config_type = 'schedule'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type,
self.args.remove_config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.remove:
parser.bkp_vars['time'] = self.args.remove[0]
parser.bkp_vars['command'] = self.args.remove[1]
# Create the scheduler with the specified variables
scheduler = Scheduler(parser.bkp_vars['time'],
parser.bkp_vars['command'], self.logger)
# If the user did not specify a scheduler config file through console..
elif self.args.add:
scheduler = Scheduler(self.args.add[0], self.args.add[1],
self.logger)
elif self.args.remove:
scheduler = Scheduler(self.args.remove[0], self.args.remove[1],
self.logger)
else:
scheduler = Scheduler(logger=self.logger)
return scheduler
def get_terminator(self, connecter):
'''
Target:
- get a terminator object with variables to terminate connections
to PostgreSQL.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a terminator which will terminate connections to
PostgreSQL.
'''
# If the user specified a terminator config file through console...
if self.args.config:
config_type = 'terminate'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.all:
parser.kill_vars['kill_all'] = True
elif self.args.user:
parser.kill_vars['kill_user'] = self.args.db_name
elif self.args.db_name:
parser.kill_vars['kill_dbs'] = self.args.user
else:
pass
# Create the terminator with the specified variables
terminator = Terminator(connecter,
parser.kill_vars['kill_all'],
parser.kill_vars['kill_user'],
parser.kill_vars['kill_dbs'],
self.logger)
# If the user did not specify a terminator config file through
# console...
else:
# Create the terminator with the console variables
terminator = Terminator(connecter, self.args.all,
self.args.user, self.args.db_name,
self.logger)
return terminator
def get_db_trimmer(self):
'''
Target:
- get a trimmer object with its variables to delete some databases'
backups in a selected directory.
Return:
- a trimmer which will delete databases' backups.
'''
# If the user specified a trimmer config file through console...
if self.args.config:
config_type = 'trim'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.db_name:
parser.bkp_vars['in_dbs'] = self.args.db_name
parser.bkp_vars['ex_dbs'] = []
parser.bkp_vars['in_regex'] = ''
parser.bkp_vars['ex_regex'] = ''
if self.args.bkp_folder:
parser.bkp_vars['bkp_path'] = self.args.bkp_folder
if self.args.prefix:
parser.bkp_vars['prefix'] = self.args.prefix
if self.args.n_backups:
parser.bkp_vars['min_n_bkps'] = self.args.n_backups
if self.args.expiry_days:
parser.bkp_vars['exp_days'] = self.args.expiry_days
if self.args.max_size:
parser.bkp_vars['max_size'] = self.args.max_size
if parser.bkp_vars['pg_warnings']:
connecter = self.get_connecter()
else:
connecter = None
# Create the trimmer with the specified variables
trimmer = Trimmer(parser.bkp_vars['bkp_path'],
parser.bkp_vars['prefix'],
parser.bkp_vars['in_dbs'],
parser.bkp_vars['in_regex'],
parser.bkp_vars['in_priority'],
parser.bkp_vars['ex_dbs'],
parser.bkp_vars['ex_regex'],
parser.bkp_vars['min_n_bkps'],
parser.bkp_vars['exp_days'],
parser.bkp_vars['max_size'],
parser.bkp_vars['pg_warnings'], connecter,
self.logger)
# If the user did not specify a trimmer config file through console...
else:
# There is no option "pg_warnings" in console so it is obligatory
# to connect to PostgreSQL here
connecter = self.get_connecter()
# Create the trimmer with the console variables
trimmer = Trimmer(connecter=connecter,
bkp_path=self.args.bkp_folder,
prefix=self.args.prefix,
in_dbs=self.args.db_name,
min_n_bkps=self.args.n_backups,
exp_days=self.args.expiry_days,
max_size=self.args.max_size, logger=self.logger)
return trimmer
def get_cl_trimmer(self):
'''
Target:
- get a trimmer object with its variables to delete some clusters'
backups in a selected directory.
Return:
- a trimmer which will delete clusters' backups.
'''
# If the user specified a trimmer config file through console...
if self.args.config:
config_type = 'trim_all'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.bkp_folder:
parser.bkp_vars['bkp_path'] = self.args.bkp_folder
if self.args.prefix:
parser.bkp_vars['prefix'] = self.args.prefix
if self.args.n_backups:
parser.bkp_vars['min_n_bkps'] = self.args.n_backups
if self.args.expiry_days:
parser.bkp_vars['exp_days'] = self.args.expiry_days
if self.args.max_size:
parser.bkp_vars['max_size'] = self.args.max_size
# Create the trimmer with the specified variables
trimmer = TrimmerCluster(parser.bkp_vars['bkp_path'],
parser.bkp_vars['prefix'],
parser.bkp_vars['min_n_bkps'],
parser.bkp_vars['exp_days'],
parser.bkp_vars['max_size'], self.logger)
else:
# Create the trimmer with the console variables
trimmer = TrimmerCluster(self.args.bkp_folder, self.args.prefix,
self.args.n_backups,
self.args.expiry_days, self.args.max_size,
self.logger)
return trimmer
def get_vacuumer(self, connecter):
'''
Target:
- get a vacuumer object with variables to vacuum databases in
PostgreSQL.
Parameters:
- connecter: an object with connection parameters to connect to
PostgreSQL.
Return:
- a vacuumer which will vacuum PostgreSQL databases.
'''
# If the user specified a vacuumer config file through console...
if self.args.config:
config_type = 'vacuum'
# Get the variables from the config file
parser = Orchestrator.get_cfg_vars(config_type, self.args.config,
self.logger)
# Overwrite the config variables with the console ones if necessary
if self.args.db_name:
parser.bkp_vars['in_dbs'] = self.args.db_name
parser.bkp_vars['ex_dbs'] = []
parser.bkp_vars['in_regex'] = ''
parser.bkp_vars['ex_regex'] = ''
if self.args.db_owner:
parser.bkp_vars['db_owner'] = self.args.db_owner
# Create the vacuumer with the specified variables
vacuumer = Vacuumer(connecter,
parser.bkp_vars['in_dbs'],
parser.bkp_vars['in_regex'],
parser.bkp_vars['in_priority'],
parser.bkp_vars['ex_dbs'],
parser.bkp_vars['ex_regex'],
parser.bkp_vars['ex_templates'],
parser.bkp_vars['db_owner'],
self.logger)
# If the user did not specify a vacuumer config file through console...
else:
# Create the vacuumer with the console variables
vacuumer = Vacuumer(connecter, in_dbs=self.args.db_name,
db_owner=self.args.db_owner,
logger=self.logger)
return vacuumer
def setup_alterer(self):
'''
Target:
- change the owner of the specified databases in PostgreSQL.
'''
connecter = self.get_connecter()
self.logger.debug(Messenger.BEGINNING_EXE_ALTERER)
alterer = self.get_alterer(connecter)
# Check if the role of user connected to PostgreSQL is superuser
pg_superuser = connecter.is_pg_superuser()
if not pg_superuser:
# Users who are not superusers will only be able to backup the
# databases they own
owner = connecter.user
self.logger.highlight('warning', Messenger.ACTION_DB_NO_SUPERUSER,
'yellow', effect='bold')
else:
owner = ''
# Get PostgreSQL databases' names, connection permissions and owners
dbs_all = connecter.get_pg_dbs_data(ex_templates=False, db_owner=owner)
# Show and log their names
Orchestrator.show_dbs(dbs_all, self.logger)
# Get the target databases in a list
alt_list = DbSelector.get_filtered_dbs(
dbs_all=dbs_all, in_dbs=alterer.in_dbs, logger=self.logger)
# Terminate every connection to the target databases if necessary
if self.args.terminate:
terminator = Terminator(connecter, target_dbs=alt_list,
logger=self.logger)
terminator.terminate_backend_dbs(alt_list)
# Delete the databases
alterer.alter_dbs_owner(alt_list)
# Close connection to PostgreSQL
connecter.pg_disconnect()
def setup_backer(self):
'''
Target:
- executes the backer depending on the type of backup to make, the
role of the user who is connected to PostgreSQL and the rest of
the conditions. It calls a terminator if necessary.
'''
connecter = self.get_connecter()
# Get databases or clusters' backer depending on the option selected
# by the user in console
if self.args.cluster:
self.logger.debug(Messenger.BEGINNING_EXE_CL_BACKER)
backer = self.get_cl_backer(connecter)
else:
self.logger.debug(Messenger.BEGINNING_EXE_DB_BACKER)
backer = self.get_db_backer(connecter)
# If necessary, add group and bkp_path to the mailer to be sent within
# the process information
if self.args.config_mailer:
self.logger.mailer.add_group(backer.group)
path = backer.bkp_path + backer.group
self.logger.mailer.add_bkp_path(path)
# Check if the role of user connected to PostgreSQL is superuser
pg_superuser = connecter.is_pg_superuser()
if not pg_superuser:
if self.args.cluster is False:
# Users who are not superusers will only be able to backup the
# databases they own
backer.db_owner = connecter.user
self.logger.highlight(
'warning', Messenger.ACTION_DB_NO_SUPERUSER,
'yellow', effect='bold')
else: # Backup the cluster can only be made by superuser
self.logger.stop_exe(Messenger.ACTION_CL_NO_SUPERUSER)
# Make the backups
if self.args.cluster is False: # Backup databases
# Get PostgreSQL databases' names, connection permissions and
# owners
dbs_all = connecter.get_pg_dbs_data(backer.ex_templates,
backer.db_owner)
# Show and log their names
Orchestrator.show_dbs(dbs_all, self.logger)
# Get the target databases in a list
bkp_list = DbSelector.get_filtered_dbs(
dbs_all, backer.in_dbs, backer.ex_dbs, backer.in_regex,
backer.ex_regex, backer.in_priority, self.logger)
# Terminate every connection to these target databases if necessary
if self.args.terminate:
terminator = Terminator(connecter, target_dbs=bkp_list,
logger=self.logger)
terminator.terminate_backend_dbs(bkp_list)
backer.backup_dbs(bkp_list) # Make databases' backup
else: # Backup a cluster
# Terminate every connection to any database of the cluster if
# necessary
if self.args.terminate:
terminator = Terminator(connecter, target_all=True,
logger=self.logger)
terminator.terminate_backend_all()
backer.backup_cl() # Make cluster's backup
# Close connection to PostgreSQL
connecter.pg_disconnect()
def setup_dropper(self):
'''
Target:
- delete specified databases in PostgreSQL.
'''
connecter = self.get_connecter()
self.logger.debug(Messenger.BEGINNING_EXE_DROPPER)
dropper = self.get_dropper(connecter)
# Terminate every connection to the target databases if necessary
if self.args.terminate:
terminator = Terminator(connecter, target_dbs=dropper.dbnames,
logger=self.logger)
terminator.terminate_backend_dbs(dropper.dbnames)
# Delete the databases
dropper.drop_pg_dbs(dropper.dbnames)
# Close connection to PostgreSQL
connecter.pg_disconnect()
def setup_informer(self):
'''
Target:
- give information about PostgreSQL to the user.
'''
connecter = self.get_connecter()
self.logger.debug(Messenger.BEGINNING_EXE_INFORMER)
informer = self.get_informer(connecter)
if self.args.details_conns is not None:
informer.show_pg_conns_data()
if self.args.list_conns:
informer.show_pg_connpids()
if self.args.details_dbs is not None:
informer.show_pg_dbs_data()
if self.args.list_dbs:
informer.show_pg_dbnames()
if self.args.details_users is not None:
informer.show_pg_users_data()
if self.args.list_users:
informer.show_pg_usernames()
if self.args.version_pg:
informer.show_pg_version()
if self.args.version_num_pg:
informer.show_pg_nversion()
if self.args.time_start:
informer.show_pg_time_start()
if self.args.time_up:
informer.show_pg_time_up()
# Close connection to PostgreSQL
connecter.pg_disconnect()
def setup_replicator(self):
'''
Target:
- clone a database in PostgreSQL.
'''
connecter = self.get_connecter()
self.logger.debug(Messenger.BEGINNING_EXE_REPLICATOR)
replicator = self.get_replicator(connecter)
pg_superuser = connecter.is_pg_superuser()
if not pg_superuser:
connecter.cursor.execute(Queries.GET_PG_DB_SOME_DATA,
(replicator.original_dbname, ))
db = connecter.cursor.fetchone()
if db['owner'] != connecter.user:
self.logger.stop_exe(Messenger.ACTION_DB_NO_SUPERUSER)
# Terminate every connection to the database which is going to be
# replicated, if necessary
if self.args.terminate:
terminator = Terminator(connecter,
target_dbs=[replicator.original_dbname],
logger=self.logger)
terminator.terminate_backend_dbs([replicator.original_dbname])
# Clone the database
replicator.replicate_pg_db()
# Close connection to PostgreSQL
connecter.pg_disconnect()
def setup_restorer(self):
'''
Target:
- restore a specified backup file as a new database or cluster in
PostgreSQL.
'''
connecter = self.get_connecter()
if self.args.cluster: # Restore a cluster (must be created first)
# Check if the role of user connected to PostgreSQL is
# superuser
pg_superuser = connecter.is_pg_superuser()
if pg_superuser:
restorer = self.get_cl_restorer(connecter)
self.logger.debug(Messenger.BEGINNING_EXE_CL_RESTORER)
restorer.restore_cluster_backup()
else: