Refactoring of mysql, msserver and mongo code.
Implements blueprint: application-hook-refactoring Change-Id: I6df7ae22ec17479dc926cc4e764091487544b5fbchanges/02/280602/14
parent
77453bdae7
commit
e001119d9e
@ -0,0 +1,35 @@
|
||||
# (c) Copyright 2015,2016 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from freezer.mode import mode
|
||||
|
||||
|
||||
class DefaultMode(mode.Mode):
|
||||
|
||||
def __init__(self, conf):
|
||||
pass
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return "default"
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
return "1.0"
|
||||
|
||||
def release(self):
|
||||
pass
|
||||
|
||||
def prepare(self):
|
||||
pass
|
@ -0,0 +1,35 @@
|
||||
# (c) Copyright 2015,2016 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import abc
|
||||
import six
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class Mode(object):
|
||||
@abc.abstractproperty
|
||||
def name(self):
|
||||
pass
|
||||
|
||||
@abc.abstractproperty
|
||||
def version(self):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def prepare(self):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def release(self):
|
||||
pass
|
@ -0,0 +1,55 @@
|
||||
# (c) Copyright 2015,2016 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
|
||||
from freezer.mode import mode
|
||||
|
||||
|
||||
class MongoDbMode(mode.Mode):
|
||||
"""
|
||||
Execute the necessary tasks for file system backup mode
|
||||
"""
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return "mongo"
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
return "1.0"
|
||||
|
||||
def release(self):
|
||||
pass
|
||||
|
||||
def prepare(self):
|
||||
pass
|
||||
|
||||
def __init__(self, conf):
|
||||
try:
|
||||
import pymongo
|
||||
except ImportError:
|
||||
raise ImportError('please install pymongo module')
|
||||
|
||||
logging.info('[*] MongoDB backup is being executed...')
|
||||
logging.info('[*] Checking is the localhost is Master/Primary...')
|
||||
# todo unhardcode this
|
||||
mongodb_port = '27017'
|
||||
local_hostname = conf.hostname
|
||||
db_host_port = '{0}:{1}'.format(local_hostname, mongodb_port)
|
||||
mongo_client = pymongo.MongoClient(db_host_port)
|
||||
master_dict = dict(mongo_client.admin.command("isMaster"))
|
||||
if master_dict['me'] != master_dict['primary']:
|
||||
raise Exception('[*] localhost {0} is not Master/Primary,\
|
||||
exiting...'.format(local_hostname))
|
@ -0,0 +1,71 @@
|
||||
# (c) Copyright 2015,2016 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from freezer import config
|
||||
from freezer.mode import mode
|
||||
|
||||
|
||||
class MysqlMode(mode.Mode):
|
||||
"""
|
||||
Execute a MySQL DB backup. currently only backup with lvm snapshots
|
||||
are supported. This mean, just before the lvm snap vol is created,
|
||||
the db tables will be flushed and locked for read, then the lvm create
|
||||
command will be executed and after that, the table will be unlocked and
|
||||
the backup will be executed. It is important to have the available in
|
||||
backup_args.mysql_conf the file where the database host, name, user,
|
||||
password and port are set.
|
||||
"""
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return "mysql"
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
return "1.0"
|
||||
|
||||
def release(self):
|
||||
if not self.released:
|
||||
self.released = True
|
||||
self.cursor.execute('UNLOCK TABLES')
|
||||
self.mysql_db_inst.commit()
|
||||
self.cursor.close()
|
||||
self.mysql_db_inst.close()
|
||||
|
||||
def prepare(self):
|
||||
self.released = False
|
||||
self.cursor = self.mysql_db_inst.cursor()
|
||||
self.cursor.execute('FLUSH TABLES WITH READ LOCK')
|
||||
self.mysql_db_inst.commit()
|
||||
|
||||
def __init__(self, conf):
|
||||
try:
|
||||
import pymysql as MySQLdb
|
||||
except ImportError:
|
||||
raise ImportError('Please install PyMySQL module')
|
||||
|
||||
with open(conf.mysql_conf, 'r') as mysql_file_fd:
|
||||
parsed_config = config.ini_parse(mysql_file_fd.read())
|
||||
# Initialize the DB object and connect to the db according to
|
||||
# the db mysql backup file config
|
||||
self.released = False
|
||||
try:
|
||||
self.mysql_db_inst = MySQLdb.connect(
|
||||
host=parsed_config.get("host", False),
|
||||
port=int(parsed_config.get("port", 3306)),
|
||||
user=parsed_config.get("user", False),
|
||||
passwd=parsed_config.get("password", False))
|
||||
self.cursor = None
|
||||
except Exception as error:
|
||||
raise Exception('[*] MySQL: {0}'.format(error))
|
@ -0,0 +1,77 @@
|
||||
# (c) Copyright 2015,2016 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
|
||||
from freezer import config
|
||||
from freezer.mode import mode
|
||||
from freezer import utils
|
||||
from freezer import winutils
|
||||
|
||||
|
||||
class SqlserverMode(mode.Mode):
|
||||
"""
|
||||
Execute a SQL Server DB backup. Currently only backups with shadow
|
||||
copy are supported. This mean, as soon as the shadow copy is created
|
||||
the db writes will be blocked and a checkpoint will be created, as soon
|
||||
as the backup finish the db will be unlocked and the backup will be
|
||||
uploaded. A sql_server.conf_file is required for this operation.
|
||||
"""
|
||||
def __init__(self, conf):
|
||||
self.released = False
|
||||
with open(conf.sql_server_conf, 'r') as sql_conf_file_fd:
|
||||
self.sql_server_instance = \
|
||||
config.ini_parse(sql_conf_file_fd.read())["instance"]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return "sqlserver"
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
return "1.0"
|
||||
|
||||
def stop_sql_server(self):
|
||||
""" Stop a SQL Server instance to
|
||||
perform the backup of the db files """
|
||||
|
||||
logging.info('[*] Stopping SQL Server for backup')
|
||||
with winutils.DisableFileSystemRedirection():
|
||||
cmd = 'net stop "SQL Server ({0})"'\
|
||||
.format(self.sql_server_instance)
|
||||
(out, err) = utils.create_subprocess(cmd)
|
||||
if err != '':
|
||||
raise Exception('[*] Error while stopping SQL Server,'
|
||||
', error {0}'.format(err))
|
||||
|
||||
def start_sql_server(self):
|
||||
""" Start the SQL Server instance after the backup is completed """
|
||||
|
||||
with winutils.DisableFileSystemRedirection():
|
||||
cmd = 'net start "SQL Server ({0})"'.format(
|
||||
self.sql_server_instance)
|
||||
(out, err) = utils.create_subprocess(cmd)
|
||||
if err != '':
|
||||
raise Exception('[*] Error while starting SQL Server'
|
||||
', error {0}'.format(err))
|
||||
logging.info('[*] SQL Server back to normal')
|
||||
|
||||
def prepare(self):
|
||||
self.stop_sql_server()
|
||||
self.released = False
|
||||
|
||||
def release(self):
|
||||
if not self.released:
|
||||
self.released = True
|
||||
self.start_sql_server()
|
Loading…
Reference in New Issue