-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathbase.py
547 lines (423 loc) · 17.7 KB
/
base.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import logging
from collections import Iterable
from util import get_credentials_for
from django.utils.translation import ugettext_lazy as _
from .errors import ConnectionError
LOG = logging.getLogger(__name__)
__all__ = ['BaseDriver', 'DatabaseStatus', 'DatabaseInfraStatus']
class BaseDriver(object):
"""
BaseDriver interface
"""
ENV_CONNECTION = 'DATABASEINFRA_CONNECTION'
# List of reserved database names for this driver that cannot be used
RESERVED_DATABASES_NAME = []
# must be overwritten by subclasses
default_port = 0
def __init__(self, *args, **kwargs):
if 'databaseinfra' in kwargs:
self.databaseinfra = kwargs.get('databaseinfra')
self.name = self.databaseinfra.engine.name
else:
raise TypeError(_("DatabaseInfra is not defined"))
def _masters_size_in_bytes(self, field_lookup):
masters = self.get_master_instance()
if not masters:
return 0
masters = [masters] if not isinstance(masters, Iterable) else masters
return sum(map(lambda m: getattr(m, field_lookup) or 0, masters))
@property
def ports(self):
raise NotImplementedError()
@property
def replication_topology(self):
return self.databaseinfra.plan.replication_topology
@property
def replication_topology_driver(self):
import util
return util.get_replication_topology_instance(
self.replication_topology.class_path
)
@property
def masters_total_size_in_bytes(self):
"""
Return total size of all masters instances on infra.
"""
return self._masters_size_in_bytes('total_size_in_bytes')
@property
def masters_used_size_in_bytes(self):
"""
Return used size of all masters instances on infra.
"""
return self._masters_size_in_bytes('used_size_in_bytes')
@property
def set_require_ssl_for_users(self):
return False
@property
def set_require_ssl_for_databaseinfra(self):
return False
def test_connection(self, credential=None):
""" Tests the connection to the database """
raise NotImplementedError()
def get_connection(self, database=None):
""" Connection string for this databaseinfra """
raise NotImplementedError()
def get_connection_dns(self, database=None):
""" Connection string for this databaseinfra """
raise NotImplementedError()
def get_user(self):
return self.databaseinfra.user
def _pass_if_connection_error(self, func, *func_args, **func_kwargs):
try:
func(*func_args, **func_kwargs)
except ConnectionError:
pass
except Exception as e:
error = e.message
if (len(error) >= 2 and
error[0] == 2002 and
"Can't connect to " in error[1]):
return
raise e
def try_remove_database(self, database):
self._pass_if_connection_error(self.remove_database, database)
def try_remove_user(self, credential):
self._pass_if_connection_error(self.remove_user, credential)
def try_update_user(self, credential):
self._pass_if_connection_error(self.update_user, credential)
def get_password(self):
return self.databaseinfra.password
def check_status(self):
""" Check if databaseinfra is working. If not working, raises subclass
of GenericDriverError """
raise NotImplementedError()
def info(self):
""" Returns a mapping with same attributes of databaseinfra """
raise NotImplementedError()
def get_total_size_from_instance(self, instance):
"""
Method used in update_infra_instances_sizes.
Return total size in bytes from instance.
"""
raise NotImplementedError()
def get_used_size_from_instance(self, instance):
"""
Method used in update_infra_instances_sizes.
Return used size in bytes from instance.
"""
raise NotImplementedError()
def update_infra_instances_sizes(self):
updated_instances = []
for instance in self.get_database_instances():
if instance.is_alive:
instance.used_size_in_bytes = self.get_used_size_from_instance(
instance
)
instance.total_size_in_bytes = (
self.get_total_size_from_instance(instance))
instance.save(update_fields=['used_size_in_bytes', 'total_size_in_bytes'])
updated_instances.append("{} - OK\n".format(instance.dns))
else:
updated_instances.append("{} - ERROR\n".format(instance.dns))
return updated_instances
def get_master_instance_total_size_in_gb(self, instance=None):
""" Return total size of a instance.
If instance not passed the total of first master instance
will be returned.
"""
from logical.models import GB_FACTOR
if instance is None:
instance = self.get_master_instance()
if instance and isinstance(instance, Iterable):
instance = instance[0]
if hasattr(instance, 'total_size_in_bytes'):
return (instance.total_size_in_bytes or 0) * GB_FACTOR
return 0
def create_user(self, credential):
raise NotImplementedError()
def update_user(self, credential):
raise NotImplementedError()
def remove_user(self, credential):
raise NotImplementedError()
def list_users(self, instance=None):
"""
this method should return a list of the users in the instance
Ex.: ["mary", "john", "michael"]
"""
raise NotImplementedError()
def create_database(self, database):
raise NotImplementedError()
def remove_database(self, database):
raise NotImplementedError()
def list_databases(self):
"""
list databases in a databaseinfra
this method should return a list of the databases names in the instance
Ex.: ["mary", "john", "michael"]
"""
raise NotImplementedError()
def import_databases(self, databaseinfra):
"""import databases already created in a databaseinfra"""
raise NotImplementedError()
def get_client(self, instance):
raise NotImplementedError()
def lock_database(self, client):
raise NotImplementedError()
def lock_database_ssh(self, instance, client):
return self.lock_database(client)
def unlock_database(self, client):
raise NotImplementedError()
def unlock_database_ssh(self, instance, client):
return self.unlock_database(client)
def check_instance_is_eligible_for_backup(self, instance):
raise NotImplementedError()
def check_instance_is_master(self, instance, default_timeout=False):
raise NotImplementedError()
def initialization_script_path(self, host=None):
raise NotImplementedError()
def deprecated_files(self,):
raise NotImplementedError()
def remove_deprectaed_files(self,):
return str().join(
["\nrm -f " + self.data_dir() + file
for file in self.deprecated_files()]
)
def data_dir(self, ):
raise NotImplementedError()
def get_replication_info(self, instance):
raise NotImplementedError()
def is_replication_ok(self, instance):
raise NotImplementedError()
def switch_master(self, instance=None, preferred_slave_instance=None):
raise NotImplementedError()
def switch_master_with_stepdowntime(self, instance=None, preferred_slave_instance=None, stepdown_time=60):
raise NotImplementedError()
def get_database_instances(self, ):
driver_name = self.name.upper()
instances = [instance
if instance.instance_type == instance.__getattribute__(
driver_name) else None
for instance in self.databaseinfra.instances.all()]
return filter(None, instances)
def get_non_database_instances(self, ):
driver_name = self.name.upper()
instances = [instance
if instance.instance_type != instance.__getattribute__(
driver_name) else None
for instance in self.databaseinfra.instances.all()]
return filter(None, instances)
def get_master_instance(self, ignore_instance=None, default_timeout=False):
instances = self.get_database_instances()
if ignore_instance:
instances.remove(ignore_instance)
for instance in instances:
try:
if self.check_instance_is_master(
instance, default_timeout=default_timeout):
return instance
if instance.hostname.future_host:
instance.address = instance.hostname.future_host.address
if self.check_instance_is_master(
instance, default_timeout=default_timeout):
return instance
except ConnectionError:
continue
return None
def get_master_instance2(self, ignore_instance=None, default_timeout=False):
instances = self.get_database_instances()
if ignore_instance:
instances.remove(ignore_instance)
for instance in instances:
try:
if self.check_instance_is_master(
instance, default_timeout=default_timeout):
return instance
except ConnectionError:
continue
return None
def get_slave_instances(self, ):
instances = self.get_database_instances()
master = self.get_master_instance()
try:
instances.remove(master)
except ValueError:
raise Exception("Master could not be detected")
return instances
def get_temporary_instances(self):
instances = [instance if instance.temporary is True else None
for instance in self.databaseinfra.instances.all()]
return filter(None, instances)
def start_slave(self, instance):
pass
def stop_slave(self, instance):
pass
def agents_command(self, host, command,
no_output=False, raise_if_error=False):
for agent in self.get_database_agents():
script = host.commands.exec_service_command(
service_name=agent,
action=command,
no_output=no_output
)
host.ssh.run_script(script, raise_if_error=raise_if_error)
def start_agents(self, host, no_output=False, raise_if_error=False):
self.agents_command(
host, "start", no_output=no_output, raise_if_error=raise_if_error)
def stop_agents(self, host, no_output=False, raise_if_error=False):
self.agents_command(
host, "stop", no_output=no_output, raise_if_error=raise_if_error)
def check_replication_and_switch(self, instance, attempts=100,
check_is_master_attempts=5,
preferred_slave_instance=None):
from time import sleep
for attempt in range(0, attempts):
if self.is_replication_ok(instance):
self.switch_master(instance, preferred_slave_instance)
LOG.info("Switch master returned ok...")
check_is_master_attempts_count = check_is_master_attempts
while self.check_instance_is_master(instance,
default_timeout=False):
if check_is_master_attempts_count == 0:
break
check_is_master_attempts_count -= 1
sleep(10)
else:
return
raise Exception("Could not change master")
LOG.info("Waiting 10s to check replication...")
sleep(10)
raise Exception(
"Could not switch master because of replication's delay"
)
def check_replication_and_switch_with_stepdown_time(self, instance, attempts=100,
check_is_master_attempts=5,
preferred_slave_instance=None,
stepdown_time=60):
LOG.info("Check Replication with StepDown time of %s seconds", stepdown_time)
from time import sleep
for attempt in range(0, attempts):
if self.is_replication_ok(instance):
self.switch_master_with_stepdowntime(instance, preferred_slave_instance, stepdown_time)
LOG.info("Switch master returned ok...")
check_is_master_attempts_count = check_is_master_attempts
while self.check_instance_is_master(instance,
default_timeout=False):
if check_is_master_attempts_count == 0:
break
check_is_master_attempts_count -= 1
sleep(10)
else:
return
raise Exception("Could not change master")
LOG.info("Waiting 10s to check replication...")
sleep(10)
raise Exception(
"Could not switch master because of replication's delay"
)
def get_database_agents(self):
""" Returns database agents list"""
raise NotImplementedError()
def get_default_database_port(self):
""" Returns database default port"""
raise NotImplementedError()
def get_default_instance_type(self):
""" This method Returns default instance type. It must be implemented
for every new type added to Instance Model, even subclasses of existent
types."""
raise NotImplementedError()
@property
def database_key(self):
return None
@property
def replica_set_name(self):
return None
def get_configuration(self):
raise NotImplementedError
def set_configuration(self, instance, name, value):
raise NotImplementedError
def get_database_process_name(self):
""" Returns OS database process name"""
raise NotImplementedError
def initialization_parameters(self, instance):
return {}
def configuration_parameters(self, instance):
return {}
def master_parameters(self, instance, master):
return {}
def configuration_parameters_for_log_resize(self, instance):
return {}
def configuration_parameters_migration(self, instance):
return self.configuration_parameters(instance)
def start_replication_parameters(self, instance):
return {}
@classmethod
def topology_name(cls):
return []
def get_master_for(self, instance):
return self.get_master_instance()
def build_new_infra_auth(self):
raise NotImplementedError()
def set_user_require_ssl(self, credential):
raise NotImplementedError()
def set_user_not_require_ssl(self, credential):
raise NotImplementedError()
def set_replication_user_require_ssl(self, instance=None):
raise NotImplementedError()
def set_replication_user_not_require_ssl(self, instance=None):
raise NotImplementedError()
def set_replication_require_ssl(self, instance=None, ca_path=None):
raise NotImplementedError()
def set_replication_not_require_ssl(self, instance=None, ca_path=None):
raise NotImplementedError()
def create_metric_collector_user(self, username, password):
raise NotImplementedError
def remove_metric_collector_user(self, username):
raise NotImplementedError
def get_metric_collector_user(self, username):
return username
def get_metric_collector_password(self, password):
return password
def get_start_pty_default(self):
return False
@property
def credential_type(self):
raise NotImplementedError()
def get_initial_infra_credentials(self):
credential = get_credentials_for(
environment=self.databaseinfra.environment,
credential_type=self.credential_type
)
initusers = credential.get_parameters_by_group('inituser')
if len(initusers) == 0:
raise Exception('There is no initial user in database credentials')
for init_user, init_password in initusers.items():
break
return init_user, init_password
def get_final_infra_credentials(self):
credential = get_credentials_for(
environment=self.databaseinfra.environment,
credential_type=self.credential_type
)
return credential.user, credential.password
def create_db_params_changes(self, database):
raise NotImplementedError('Not implemented for Driver')
class DatabaseStatus(object):
def __init__(self, database_model):
self.database_model = database_model
self.used_size_in_bytes = -1
self.total_size_in_bytes = -1
self.is_alive = False
@property
def name(self):
return self.database_model.name
class DatabaseInfraStatus(object):
def __init__(self, databaseinfra_model):
self.databaseinfra_model = databaseinfra_model
self.version = None
self.used_size_in_bytes = -1
self.databases_status = {}
def get_database_status(self, database_name):
""" Return DatabaseStatus of one specific database """
return self.databases_status.get(database_name, None)