forked from anubia/py_pg_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacuumer.py
166 lines (138 loc) · 5.88 KB
/
vacuumer.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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import subprocess # To execute some commands in the shell
from casting.casting import Casting
from checker.checker import Checker
from const.const import Default
from const.const import Messenger
from date_tools.date_tools import DateTools
from logger.logger import Logger
class Vacuumer:
in_dbs = [] # List of databases to be included in the process
in_regex = '' # Regular expression which must match the included databases
# Flag which determinates whether inclusion conditions predominate over the
# exclusion ones
in_priority = False
ex_dbs = [] # List of databases to be excluded in the process
ex_regex = '' # Regular expression which must match the excluded databases
# Flag which determinates whether the templates must be included
ex_templates = True
# Use other PostgreSQL user during the backup process (only for superusers)
db_owner = ''
# An object with connection parameters to connect to PostgreSQL
connecter = None
logger = None # Logger to show and log some messages
def __init__(self, connecter=None, in_dbs=[], in_regex='',
in_priority=False, ex_dbs=['postgres'], ex_regex='',
ex_templates=True, db_owner='', logger=None):
if logger:
self.logger = logger
else:
self.logger = Logger()
if connecter:
self.connecter = connecter
else:
self.logger.stop_exe(Messenger.NO_CONNECTION_PARAMS)
if isinstance(in_dbs, list):
self.in_dbs = in_dbs
else:
self.in_dbs = Casting.str_to_list(in_dbs)
if Checker.check_regex(in_regex):
self.in_regex = in_regex
else:
self.logger.stop_exe(Messenger.INVALID_IN_REGEX)
if isinstance(in_priority, bool):
self.in_priority = in_priority
elif Checker.str_is_bool(in_priority):
self.in_priority = Casting.str_to_bool(in_priority)
else:
self.logger.stop_exe(Messenger.INVALID_IN_PRIORITY)
if isinstance(ex_dbs, list):
self.ex_dbs = ex_dbs
else:
self.ex_dbs = Casting.str_to_list(ex_dbs)
if Checker.check_regex(ex_regex):
self.ex_regex = ex_regex
else:
self.logger.stop_exe(Messenger.INVALID_EX_REGEX)
if isinstance(ex_templates, bool):
self.ex_templates = ex_templates
elif Checker.str_is_bool(ex_templates):
self.ex_templates = Casting.str_to_bool(ex_templates)
else:
self.logger.stop_exe(Messenger.INVALID_EX_TEMPLATES)
if db_owner is None:
self.db_owner = Default.DB_OWNER
else:
self.db_owner = db_owner
message = Messenger.VACUUMER_VARS.format(
server=self.connecter.server, user=self.connecter.user,
port=self.connecter.port, in_dbs=self.in_dbs,
in_regex=self.in_regex, in_priority=self.in_priority,
ex_dbs=self.ex_dbs, ex_regex=self.ex_regex,
ex_templates=self.ex_templates, db_owner=self.db_owner)
self.logger.debug(Messenger.VACUUMER_VARS_INTRO)
self.logger.debug(message)
def vacuum_db(self, dbname):
'''
Target:
- vacuum a PostgreSQL database.
Parameters:
- dbname: name of the database which is going to be vacuumed.
Return:
- a boolean which indicates the success of the process.
'''
success = True
# Store the command to do
command = 'vacuumdb {} -U {} -h {} -p {}'.format(
dbname, self.connecter.user, self.connecter.server,
self.connecter.port)
try:
# Execute the command in console
result = subprocess.call(command, shell=True)
if result != 0:
raise Exception()
except Exception as e:
self.logger.debug('Error en la función "vacuum_db": {}.'.format(
str(e)))
success = False
return success
def vacuum_dbs(self, vacuum_list):
'''
Target:
- vacuum a group of PostgreSQL databases.
Parameters:
- vacuum_list: names of the databases which are going to be
vacuumed.
'''
if vacuum_list:
self.logger.highlight('info', Messenger.BEGINNING_VACUUMER,
'white')
for db in vacuum_list:
dbname = db['datname']
message = Messenger.PROCESSING_DB.format(dbname=dbname)
self.logger.highlight('info', message, 'cyan')
# Let the user know whether the database connection is allowed
if not db['datallowconn']:
message = Messenger.FORBIDDEN_DB_CONNECTION.format(
dbname=dbname)
self.logger.highlight('warning', message, 'yellow',
effect='bold')
success = False
else:
start_time = DateTools.get_current_datetime()
# Vacuum the database
success = self.vacuum_db(dbname)
end_time = DateTools.get_current_datetime()
# Get and show the process' duration
diff = DateTools.get_diff_datetimes(start_time, end_time)
if success:
message = Messenger.DB_VACUUMER_DONE.format(dbname=dbname,
diff=diff)
self.logger.highlight('info', message, 'green')
else:
message = Messenger.DB_VACUUMER_FAIL.format(dbname=dbname)
self.logger.highlight('warning', message, 'yellow',
effect='bold')
self.logger.highlight('info', Messenger.VACUUMER_DONE, 'green',
effect='bold')