Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 1 | # Copyright 2018 Mirantis, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from tcp_tests import logger |
| 16 | |
| 17 | |
| 18 | LOG = logger.logger |
| 19 | |
| 20 | |
| 21 | class BackupRestoreManager(object): |
| 22 | """Helper manager for execution backup restore""" |
| 23 | |
| 24 | backup_cmd = 'backupninja -n --run /etc/backup.d/200.backup.rsync' |
| 25 | |
| 26 | def __init__(self, underlay, salt_api, backup_cmd=None): |
| 27 | self.underlay = underlay |
| 28 | self.__salt_api = salt_api |
| 29 | self.backup_cmd = backup_cmd or self.backup_cmd |
| 30 | |
| 31 | @property |
| 32 | def salt_api(self): |
| 33 | return self.__salt_api |
| 34 | |
| 35 | def create_backup(self, tgt, backup_cmd=backup_cmd): |
| 36 | return self.salt_api.enforce_state(tgt, 'cmd.run', backup_cmd) |
| 37 | |
| 38 | def restore_salt_master(self, tgt): |
| 39 | return self.salt_api.local(tgt, 'salt.master.restore') |
| 40 | |
| 41 | def restore_salt_minion(self, tgt): |
| 42 | return self.salt_api.local(tgt, 'salt.minion.restore') |
| 43 | |
| 44 | def create_mysql_backup_backupninja(self, tgt, ): |
| 45 | rets = [] |
| 46 | res = self.salt_api.enforce_state( |
| 47 | tgt, 'cmd.run', |
| 48 | 'backupninja -n --run /etc/backup.d/101.mysql') |
| 49 | rets.append(res) |
| 50 | res_rsync = self.salt_api.enforce_state(tgt, 'cmd.run') |
| 51 | rets.append(res_rsync) |
| 52 | return rets |
| 53 | |
| 54 | def restore_mysql_backupninja(self, tgt): |
| 55 | # Running this state restores the databases and creates a file |
| 56 | # for every restored database in /root/mysql/flags. |
| 57 | return self.salt_api.local(tgt, 'mysql.client') |
| 58 | |
| 59 | def create_mysql_xtrabackup(self, tgt, backup_cmd=backup_cmd): |
| 60 | # Should be run on mysql master node |
| 61 | return self.salt_api.enforce_state( |
| 62 | tgt, 'cmd.run', '/usr/local/bin/innobackupex-runner.sh') |
| 63 | |
| 64 | def check_mysql_xtrabackup_rsynced(self, tgt='I@xtrabackup:server'): |
| 65 | return self.salt_api.enforce_state( |
| 66 | tgt, 'cmd.run', 'ls /var/backups/mysql/xtrabackup/full') |
| 67 | |
| 68 | def stop_mysql_slave(self, tgt='I@galera:slave'): |
| 69 | return self.salt_api.enforce_state(tgt, 'service.stop mysql') |
| 70 | |
| 71 | def remove_mysql_logs(self, tgt='I@galera:slave'): |
| 72 | return self.salt_api.enforce_state( |
| 73 | tgt, 'cmd.run', 'rm /var/lib/mysql/ib_logfile*') |
| 74 | |
| 75 | def stop_mysql_master(self, tgt='I@galera:master'): |
| 76 | return self.salt_api.enforce_state(tgt, 'service.stop mysql') |
| 77 | |
| 78 | def disconnect_wresp_master(self, tgt='I@galera:master'): |
| 79 | # TODO fins the way updated wresp |
| 80 | return self.salt_api.enforce_state( |
| 81 | tgt, 'cmd.run', 'wsrep_cluster_address=gcomm://') |
| 82 | |
| 83 | def move_dbs_files_to_new_location(self, tgt='I@galera:master'): |
| 84 | cmds = ['mkdir -p /root/mysql/mysql.bak/', |
| 85 | 'mv /var/lib/mysql/* /root/mysql/mysql.bak', |
| 86 | 'rm /var/lib/mysql/.galera_bootstrap'] |
| 87 | rest = [] |
| 88 | for cmd in cmds: |
| 89 | res = self.salt_api.enforce_state(tgt, 'cmd.run', cmd) |
| 90 | rest.append(res) |
| 91 | return rest |
| 92 | |
| 93 | def check_dbs_files_removed(self, tgt='I@galera:master'): |
| 94 | cmds = ['ls /var/lib/mysql/', |
| 95 | 'ls -ld /var/lib/mysql/.?*'] |
| 96 | rest = [] |
| 97 | for cmd in cmds: |
| 98 | res = self.salt_api.enforce_state(tgt, 'cmd.run', cmd) |
| 99 | rest.append(res) |
| 100 | return rest |
| 101 | |
| 102 | # run xtrabackup state on node where bacakup |
| 103 | def run_xtrabackup(self, tgt): |
| 104 | return self.salt_api.local(tgt, 'xtrabackup') |
| 105 | |
| 106 | def start_mysql(self): |
| 107 | tgts = ['I@galera:master', 'I@galera:slave'] |
| 108 | ret = [] |
| 109 | for tgt in tgts: |
| 110 | res = self.salt_api.enforce_state(tgt, 'service.start mysql') |
| 111 | ret.append(res) |
| 112 | return ret |
| 113 | |
| 114 | def check_galera_cluster(self, tgt='I@galera:master'): |
| 115 | return self.salt_api.enforce_state( |
| 116 | tgt, 'mysql.status | grep -A1 wsrep_cluster_size') |
| 117 | |
| 118 | # #################Backup_Restore_Glance################### |
| 119 | |
| 120 | def copy_glance_images_to_backup(self, path_to_backup, |
| 121 | tgt="I@glance:server and *01*"): |
| 122 | cmd = 'cp -a /var/lib/glance/images/. {}'.format(path_to_backup) |
| 123 | return self.salt_api.enforce_state( |
| 124 | tgt, 'cmd.run', cmd) |
| 125 | |
| 126 | def copy_glance_images_from_backup(self, path_to_backup, |
| 127 | tgt="I@glance:server and *01*"): |
| 128 | cmd = 'cp -a {}/. /var/lib/glance/images/'.format(path_to_backup) |
| 129 | return self.salt_api.enforce_state( |
| 130 | tgt, 'cmd.run', cmd) |
| 131 | |
| 132 | def check_images_after_backup(self, tgt="I@keystone:client"): |
| 133 | # TODO If the context of the Glance |
| 134 | # images files is lost, run the following commands: |
| 135 | # salt -C 'I@glance:server' cmd.run |
| 136 | # "chown glance:glance <IMAGE_FILE_NAME>" |
| 137 | # salt -C 'I@glance:server' cmd.run "chmod 640 <IMAGE_FILE_NAME>" |
| 138 | cmd = '. /root/keystonercv3; openstack image list' |
| 139 | return self.salt_api.enforce_state(tgt, 'cmd.run', cmd) |
| 140 | |
| 141 | # #################Backup_Restore_cinder_volumes_and_snapshots### |
| 142 | # TODO Verify that a complete backup was created on |
| 143 | # the MySQL Galera Database Master node |
| 144 | # ls /var/backups/mysql/xtrabackup/full |
| 145 | # TODO(tleontovich): add method to check needed configs |
| 146 | # TODO (tleontovich): check pillars |
| 147 | # TODO (tleontovich): check backup is created, and |
| 148 | # restore restores |