def undo(self, workflow_dict):
        LOG.info("Running undo...")
        try:
            databaseinfra = workflow_dict['databaseinfra']

            for host in workflow_dict['stoped_hosts']:
                LOG.info('Starting database on host {}'.format(host))
                return_code, output = use_database_initialization_script(databaseinfra=databaseinfra,
                                                                         host=host,
                                                                         option='start')

                if return_code != 0:
                    LOG.warn(str(output))

                instance = host.instance_set.all()[0]
                start_slave(instance=instance)

            LOG.info('Wainting 30 seconds to setting flipper IPs')
            sleep(30)
            LOG.info('Setting read IP on {}'.format(workflow_dict['host']))
            set_infra_read_ip(slave_host=workflow_dict['host'],
                              infra_name=databaseinfra.name)

            LOG.info('Setting write IP on {}'.format(workflow_dict['not_primary_hosts'][0]))
            set_infra_write_ip(master_host=workflow_dict['not_primary_hosts'][0],
                               infra_name=databaseinfra.name)

            return True
        except Exception:
            traceback = full_stack()

            workflow_dict['exceptions']['error_codes'].append(DBAAS_0021)
            workflow_dict['exceptions']['traceback'].append(traceback)

            return False
    def do(self, workflow_dict):
        try:

            databaseinfra = workflow_dict['databaseinfra']

            master_host = workflow_dict['host']
            master_instance = Instance.objects.get(hostname=master_host)

            secondary_host = workflow_dict['not_primary_hosts'][0]
            secondary_instance = Instance.objects.get(hostname=secondary_host)

            master_log_file, master_log_pos = get_replication_information_from_file(
                host=master_host)

            return_code, output = use_database_initialization_script(
                databaseinfra=databaseinfra, host=master_host, option='start')
            if return_code != 0:
                raise Exception(str(output))

            return_code, output = use_database_initialization_script(
                databaseinfra=databaseinfra,
                host=secondary_host,
                option='start')
            if return_code != 0:
                raise Exception(str(output))

            LOG.info("Waiting 1 minute to continue")
            sleep(60)

            change_master_to(instance=master_instance,
                             master_host=secondary_host.address,
                             bin_log_file=master_log_file,
                             bin_log_position=master_log_pos)

            change_master_to(instance=secondary_instance,
                             master_host=master_host.address,
                             bin_log_file=master_log_file,
                             bin_log_position=master_log_pos)

            start_slave(instance=master_instance)
            start_slave(instance=secondary_instance)

            LOG.info("Waiting 30 seconds to continue")
            sleep(30)
            set_infra_read_ip(slave_host=workflow_dict['host'],
                              infra_name=databaseinfra.name)

            set_infra_write_ip(
                master_host=workflow_dict['not_primary_hosts'][0],
                infra_name=databaseinfra.name)

            return True
        except Exception:
            traceback = full_stack()

            workflow_dict['exceptions']['error_codes'].append(DBAAS_0020)
            workflow_dict['exceptions']['traceback'].append(traceback)

            return False
    def do(self, workflow_dict):
        try:

            databaseinfra = workflow_dict['databaseinfra']

            master_host = workflow_dict['host']
            master_instance = Instance.objects.get(hostname=master_host)

            secondary_host = workflow_dict['not_primary_hosts'][0]
            secondary_instance = Instance.objects.get(hostname=secondary_host)

            master_log_file, master_log_pos = get_replication_information_from_file(host=master_host)

            return_code, output = use_database_initialization_script(databaseinfra=databaseinfra,
                                                                     host=master_host,
                                                                     option='start')
            if return_code != 0:
                raise Exception(str(output))

            return_code, output = use_database_initialization_script(databaseinfra=databaseinfra,
                                                                     host=secondary_host,
                                                                     option='start')
            if return_code != 0:
                raise Exception(str(output))

            LOG.info("Waiting 1 minute to continue")
            sleep(60)

            change_master_to(instance=master_instance,
                             master_host=secondary_host.address,
                             bin_log_file=master_log_file,
                             bin_log_position=master_log_pos)

            change_master_to(instance=secondary_instance,
                             master_host=master_host.address,
                             bin_log_file=master_log_file,
                             bin_log_position=master_log_pos)

            start_slave(instance=master_instance)
            start_slave(instance=secondary_instance)

            LOG.info("Waiting 30 seconds to continue")
            sleep(30)
            set_infra_read_ip(slave_host=workflow_dict['host'],
                              infra_name=databaseinfra.name)

            set_infra_write_ip(master_host=workflow_dict['not_primary_hosts'][0],
                               infra_name=databaseinfra.name)

            return True
        except Exception:
            traceback = full_stack()

            workflow_dict['exceptions']['error_codes'].append(DBAAS_0020)
            workflow_dict['exceptions']['traceback'].append(traceback)

            return False
    def undo(self, workflow_dict):
        LOG.info("Running undo...")
        try:
            databaseinfra = workflow_dict['databaseinfra']

            for host in workflow_dict['stoped_hosts']:
                LOG.info('Starting database on host {}'.format(host))
                return_code, output = use_database_initialization_script(
                    databaseinfra=databaseinfra, host=host, option='start')

                if return_code != 0:
                    LOG.warn(str(output))

                instance = host.instance_set.all()[0]
                start_slave(instance=instance)

            LOG.info('Wainting 30 seconds to setting flipper IPs')
            sleep(30)
            LOG.info('Setting read IP on {}'.format(workflow_dict['host']))
            set_infra_read_ip(slave_host=workflow_dict['host'],
                              infra_name=databaseinfra.name)

            LOG.info('Setting write IP on {}'.format(
                workflow_dict['not_primary_hosts'][0]))
            set_infra_write_ip(
                master_host=workflow_dict['not_primary_hosts'][0],
                infra_name=databaseinfra.name)

            return True
        except Exception:
            traceback = full_stack()

            workflow_dict['exceptions']['error_codes'].append(DBAAS_0021)
            workflow_dict['exceptions']['traceback'].append(traceback)

            return False