Separate database logic in subclasses

This commit is contained in:
Einar Forselv 2019-12-03 01:29:41 +01:00
parent a6f088b648
commit 32a3218ac9
2 changed files with 91 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import os
from pathlib import Path
from typing import List
from restic_volume_backup import utils
@ -9,6 +10,7 @@ VOLUME_TYPE_VOLUME = "volume"
class Container:
"""Represents a docker container"""
container_type = None
def __init__(self, data: dict):
self._data = data
@ -74,15 +76,22 @@ class Container:
"""Is backup enabled for this container?"""
return any([
self.volume_backup_enabled,
self.mysql_backup_enabled,
self.mariadb_backup_enabled,
self.postgresql_backup_enabled,
self.database_backup_enabled,
])
@property
def volume_backup_enabled(self) -> bool:
return utils.is_true(self.get_label('restic-volume-backup.volumes'))
@property
def database_backup_enabled(self) -> bool:
"""bool: Is database backup enabled in any shape or form?"""
return any([
self.mysql_backup_enabled,
self.mariadb_backup_enabled,
self.postgresql_backup_enabled,
])
@property
def mysql_backup_enabled(self) -> bool:
return utils.is_true(self.get_label('restic-volume-backup.mysql'))
@ -170,15 +179,20 @@ class Container:
return volumes
def get_mysql_credentials(self):
return {
'host': self.hostname,
'username': self.get_config_env('MYSQL_USER'),
'password': self.get_config_env('MYSQL_PASSWORD'),
'port': "3306",
}
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
raise NotImplementedError("Base container class don't implement this")
def _parse_pattern(self, value):
def ping(self) -> bool:
"""Check the availability of the service"""
raise NotImplementedError("Base container class don't implement this")
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
raise NotImplementedError("Base container class don't implement this")
def _parse_pattern(self, value: str) -> List[str]:
"""list: Safely parse include/exclude pattern from user"""
if not value:
return None
@ -192,6 +206,7 @@ class Container:
return value.split(',')
def __eq__(self, other):
"""Compare container by id"""
if other is None:
return False

View File

@ -0,0 +1,65 @@
from restic_volume_backup.containers import Container
from restic_volume_backup.restic
class MariadbContainer(Container):
container_type = 'mariadb'
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
return {
'host': self.hostname,
'username': self.get_config_env('MYSQL_USER'),
'password': self.get_config_env('MYSQL_PASSWORD'),
'port': "3306",
}
def ping(self) -> bool:
"""Check the availability of the service"""
raise NotImplementedError("Base container class don't implement this")
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
raise NotImplementedError("Base container class don't implement this")
class MysqlContainer(Container):
container_type = 'mysql'
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
return {
'host': self.hostname,
'username': self.get_config_env('MYSQL_USER'),
'password': self.get_config_env('MYSQL_PASSWORD'),
'port': "3306",
}
def ping(self) -> bool:
"""Check the availability of the service"""
raise NotImplementedError("Base container class don't implement this")
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
raise NotImplementedError("Base container class don't implement this")
class PostgresContainer(Container):
container_type = 'postgres'
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
return {
'host': self.hostname,
'username': self.get_config_env('POSTGRES_USER'),
'password': self.get_config_env('POSTGRES_PASSWORD'),
'port': "5432",
}
def ping(self) -> bool:
"""Check the availability of the service"""
raise NotImplementedError("Base container class don't implement this")
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
raise NotImplementedError("Base container class don't implement this")