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 |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 16 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 17 | |
| 18 | |
| 19 | LOG = logger.logger |
| 20 | |
| 21 | |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 22 | class BackupRestoreManager(ExecuteCommandsMixin): |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 23 | """Helper manager for execution backup restore""" |
| 24 | |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 25 | def __init__(self, config, underlay, salt_api): |
| 26 | self.__config = config |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 27 | self.underlay = underlay |
| 28 | self.__salt_api = salt_api |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 29 | super(BackupRestoreManager, self).__init__(config, underlay) |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 30 | |
| 31 | @property |
| 32 | def salt_api(self): |
| 33 | return self.__salt_api |
| 34 | |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 35 | def get_node_name(self, tgt): |
| 36 | res = [node_name for node_name in |
| 37 | self.underlay.node_names() if tgt in node_name] |
| 38 | assert len(res) > 0, 'Can not find node name by tgt {}'.format(tgt) |
| 39 | return res[0] |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 40 | |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 41 | def create_backup(self, tgt, backup_cmd=None): |
| 42 | if not backup_cmd: |
| 43 | backup_cmd = 'backupninja -n --run /etc/backup.d/200.backup.rsync' |
| 44 | step = {'cmd': backup_cmd, 'node_name': self.get_node_name(tgt)} |
| 45 | self.execute_command(step, 'Running backup command') |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 46 | |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 47 | def check_file_exists(self, tgt, file_path=None): |
| 48 | if not file_path: |
| 49 | file_path = '/etc/backup.d/200.backup.rsync' |
| 50 | cmd = 'test -f {}'.format(file_path) |
| 51 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 52 | self.execute_command(step, 'Check file {} exists'.format(file_path)) |
| 53 | |
| 54 | def delete_dirs_files(self, tgt, file_path='/etc/pki/ca/salt_master_ca/'): |
| 55 | cmd = 'rm -rf {}'.format(file_path) |
| 56 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 57 | self.execute_command(step, 'Delete {}'.format(file_path)) |
| 58 | |
| 59 | def restore_salt(self, tgt): |
| 60 | cmd = 'salt-call state.sls salt.master.restore,salt.minion.restore' |
| 61 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 62 | self.execute_command(step, 'Restore salt master') |
| 63 | |
| 64 | def ping_minions(self, tgt): |
| 65 | cmd = 'salt "*" test.ping' |
| 66 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 67 | self.execute_command(step, 'Ping minions') |
| 68 | |
| 69 | def verify_salt_master_restored(self, tgt): |
| 70 | cmd = "salt -t2 '*' saltutil.refresh_pillar" |
| 71 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 72 | self.execute_command(step, |
| 73 | 'Verify that the Salt Master node is restored') |
| 74 | step = {'cmd': 'ls -la /etc/pki/ca/salt_master_ca/', |
| 75 | 'node_name': self.get_node_name(tgt)} |
| 76 | self.execute_command(step, |
| 77 | 'Check pki files exists') |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 78 | |
| 79 | def create_mysql_backup_backupninja(self, tgt, ): |
| 80 | rets = [] |
| 81 | res = self.salt_api.enforce_state( |
| 82 | tgt, 'cmd.run', |
| 83 | 'backupninja -n --run /etc/backup.d/101.mysql') |
| 84 | rets.append(res) |
| 85 | res_rsync = self.salt_api.enforce_state(tgt, 'cmd.run') |
| 86 | rets.append(res_rsync) |
| 87 | return rets |
| 88 | |
| 89 | def restore_mysql_backupninja(self, tgt): |
| 90 | # Running this state restores the databases and creates a file |
| 91 | # for every restored database in /root/mysql/flags. |
| 92 | return self.salt_api.local(tgt, 'mysql.client') |
| 93 | |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 94 | def create_mysql_xtrabackup(self, tgt, backup_cmd=None): |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 95 | # Should be run on mysql master node |
| 96 | return self.salt_api.enforce_state( |
| 97 | tgt, 'cmd.run', '/usr/local/bin/innobackupex-runner.sh') |
| 98 | |
| 99 | def check_mysql_xtrabackup_rsynced(self, tgt='I@xtrabackup:server'): |
| 100 | return self.salt_api.enforce_state( |
| 101 | tgt, 'cmd.run', 'ls /var/backups/mysql/xtrabackup/full') |
| 102 | |
| 103 | def stop_mysql_slave(self, tgt='I@galera:slave'): |
| 104 | return self.salt_api.enforce_state(tgt, 'service.stop mysql') |
| 105 | |
| 106 | def remove_mysql_logs(self, tgt='I@galera:slave'): |
| 107 | return self.salt_api.enforce_state( |
| 108 | tgt, 'cmd.run', 'rm /var/lib/mysql/ib_logfile*') |
| 109 | |
| 110 | def stop_mysql_master(self, tgt='I@galera:master'): |
| 111 | return self.salt_api.enforce_state(tgt, 'service.stop mysql') |
| 112 | |
| 113 | def disconnect_wresp_master(self, tgt='I@galera:master'): |
Tatyana Leontovich | 835465f | 2018-06-14 16:42:23 +0300 | [diff] [blame] | 114 | # TODO finds the way updated wresp |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 115 | return self.salt_api.enforce_state( |
| 116 | tgt, 'cmd.run', 'wsrep_cluster_address=gcomm://') |
| 117 | |
| 118 | def move_dbs_files_to_new_location(self, tgt='I@galera:master'): |
| 119 | cmds = ['mkdir -p /root/mysql/mysql.bak/', |
| 120 | 'mv /var/lib/mysql/* /root/mysql/mysql.bak', |
| 121 | 'rm /var/lib/mysql/.galera_bootstrap'] |
| 122 | rest = [] |
| 123 | for cmd in cmds: |
| 124 | res = self.salt_api.enforce_state(tgt, 'cmd.run', cmd) |
| 125 | rest.append(res) |
| 126 | return rest |
| 127 | |
| 128 | def check_dbs_files_removed(self, tgt='I@galera:master'): |
| 129 | cmds = ['ls /var/lib/mysql/', |
| 130 | 'ls -ld /var/lib/mysql/.?*'] |
| 131 | rest = [] |
| 132 | for cmd in cmds: |
| 133 | res = self.salt_api.enforce_state(tgt, 'cmd.run', cmd) |
| 134 | rest.append(res) |
| 135 | return rest |
| 136 | |
| 137 | # run xtrabackup state on node where bacakup |
| 138 | def run_xtrabackup(self, tgt): |
| 139 | return self.salt_api.local(tgt, 'xtrabackup') |
| 140 | |
| 141 | def start_mysql(self): |
| 142 | tgts = ['I@galera:master', 'I@galera:slave'] |
| 143 | ret = [] |
| 144 | for tgt in tgts: |
| 145 | res = self.salt_api.enforce_state(tgt, 'service.start mysql') |
| 146 | ret.append(res) |
| 147 | return ret |
| 148 | |
| 149 | def check_galera_cluster(self, tgt='I@galera:master'): |
| 150 | return self.salt_api.enforce_state( |
| 151 | tgt, 'mysql.status | grep -A1 wsrep_cluster_size') |
| 152 | |
| 153 | # #################Backup_Restore_Glance################### |
| 154 | |
| 155 | def copy_glance_images_to_backup(self, path_to_backup, |
Tatyana Leontovich | afe8f95 | 2018-06-20 15:33:03 +0300 | [diff] [blame] | 156 | tgt="ctl03"): |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 157 | cmd = 'cp -a /var/lib/glance/images/. {}'.format(path_to_backup) |
Tatyana Leontovich | afe8f95 | 2018-06-20 15:33:03 +0300 | [diff] [blame] | 158 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 159 | return self.execute_command(step, 'Copy glance images to backup') |
| 160 | |
| 161 | def get_image_uud(self, tgt='ctl03'): |
| 162 | cmd = (". /root/keystonercv3; " |
| 163 | "openstack image list -c ID| awk 'NR==4'| cut -d '|' -f 2") |
| 164 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 165 | res = self.execute_command(step, 'Get uuid of image on fs', |
| 166 | return_res=True) |
| 167 | return res |
| 168 | |
| 169 | def delete_image_from_fs(self, uuid, tgt="ctl03"): |
| 170 | cmd = ('cd /var/lib/glance/images/; rm {}'.format(uuid)) |
| 171 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 172 | self.execute_command(step, 'Delete image before restore') |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 173 | |
| 174 | def copy_glance_images_from_backup(self, path_to_backup, |
Tatyana Leontovich | afe8f95 | 2018-06-20 15:33:03 +0300 | [diff] [blame] | 175 | tgt="ctl03"): |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 176 | cmd = 'cp -a {}/. /var/lib/glance/images/'.format(path_to_backup) |
Tatyana Leontovich | afe8f95 | 2018-06-20 15:33:03 +0300 | [diff] [blame] | 177 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 178 | return self.execute_command(step, 'Copy to glance') |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 179 | |
Tatyana Leontovich | afe8f95 | 2018-06-20 15:33:03 +0300 | [diff] [blame] | 180 | def check_image_on_fs(self, uuid, tgt="ctl03"): |
| 181 | cmd = ('ls /var/lib/glance/images/ | grep {}'.format(uuid)) |
| 182 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 183 | self.execute_command(step, 'Check image exists after restore') |
Tatyana Leontovich | 2675691 | 2018-06-06 20:11:05 +0300 | [diff] [blame] | 184 | |
Tatyana Leontovich | afe8f95 | 2018-06-20 15:33:03 +0300 | [diff] [blame] | 185 | def check_image_after_backup(self, uuid, tgt="ctl03"): |
| 186 | cmd = ('. /root/keystonercv3; ' |
| 187 | 'openstack image save {} --file test'.format(uuid)) |
| 188 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 189 | self.execute_command(step, 'Save image after backup') |
Tatyana Leontovich | 83a5d6c | 2018-07-03 21:59:55 +0300 | [diff] [blame] | 190 | |
| 191 | def create_cirros(self, tgt='ctl03'): |
| 192 | cmd = ('wget http://download.cirros-cloud.' |
| 193 | 'net/0.3.4/cirros-0.3.4-i386-disk.img; . ' |
| 194 | '/root/keystonercv3; glance --timeout 120 ' |
| 195 | 'image-create --name cirros --visibility ' |
| 196 | 'public --disk-format qcow2 --container-format ' |
| 197 | 'bare --progress < /root/cirros-0.3.4-i386-disk.img') |
| 198 | step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)} |
| 199 | self.execute_command(step, 'Create image before run') |