Exemplo n.º 1
0
def get_password(client):
    _password = getpass.getpass(
        output.message(
            output.get_subject().INFO,
            'SSH password ' + config['host'][client]['user'] + '@' +
            config['host'][client]['host'] + ': ', False))

    while _password.strip() is '':
        output.message(output.get_subject().WARNING,
                       'Password is empty. Please enter a valid password.',
                       True)

        _password = getpass.getpass(
            output.message(
                output.get_subject().INFO,
                'SSH password ' + config['host'][client]['user'] + '@' +
                config['host'][client]['host'] + ': ', False))

    return _password
Exemplo n.º 2
0
def get_composer_information():
    if os.path.isfile(
            os.path.dirname(os.path.realpath(__file__)) + '/../composer.json'):
        with open(
                os.path.dirname(os.path.realpath(__file__)) +
                '/../composer.json', 'r') as read_file:
            return json.load(read_file)
    else:
        sys.exit(
            output.message(output.get_subject().ERROR,
                           'Local composer information not found', False))
Exemplo n.º 3
0
def check_args_options(args):
    global option
    global default_local_host_file_path
    global default_local_sync_path

    if not args.file is None:
        default_local_host_file_path = args.file

    if not args.verbose is None:
        option['verbose'] = True

    if not args.keepdump is None:
        default_local_sync_path = args.keepdump

        if default_local_sync_path[-1] != '/':
            default_local_sync_path += '/'

        option['keep_dump'] = True
        output.message(output.get_subject().INFO, '"Keep dump" option chosen',
                       True)
Exemplo n.º 4
0
def import_database_dump():
    if (not system.option['is_same_client']):
        prepare_target_database_dump()

    # @ToDo: Enable check_dump feature again
    #     if system.option['check_dump']:
    #         check_target_database_dump()

    if not system.option['keep_dump'] and not system.option['is_same_client']:
        output.message(
            output.get_subject().TARGET,
            'Importing database dump',
            True
        )

        mode.run_command(
            helper.get_command('target', 'mysql') + ' ' + generate_mysql_credentials('target') + ' ' +
            system.config['db']['target'][
                'dbname'] + ' < ' + helper.get_target_dump_dir() + origin_database_dump_file_name,
            mode.get_clients().TARGET
        )
Exemplo n.º 5
0
def run_command(command, client):
    if client == clients.ORIGIN:
        if system.option['verbose']:
            output.message(
                output.get_subject().ORIGIN,
                output.get_bcolors().BLACK + command +
                output.get_bcolors().ENDC, True)
        if is_origin_remote():
            return connect.run_ssh_command_origin(command)
        else:
            return os.system(command)
    elif client == clients.TARGET:
        if system.option['verbose']:
            output.message(
                output.get_subject().TARGET,
                output.get_bcolors().BLACK + command +
                output.get_bcolors().ENDC, True)
        if is_target_remote():
            return connect.run_ssh_command_target(command)
        else:
            return os.system(command)
Exemplo n.º 6
0
def remove_target_database_dump():
    _file_path = helper.get_target_dump_dir() + database.origin_database_dump_file_name

    #
    # Move dump to specified directory
    #
    if system.option['keep_dump']:
        helper.create_local_temporary_data_dir()
        _keep_dump_path = system.default_local_sync_path +  database.origin_database_dump_file_name
        mode.run_command(
            helper.get_command('target',
                               'cp') + ' ' + _file_path + ' ' + _keep_dump_path,
            mode.get_clients().TARGET
        )
        output.message(
            output.get_subject().INFO,
            'Database dump file is saved to: ' + _keep_dump_path,
            True
        )

    #
    # Clean up
    #
    if not system.option['is_same_client']:
        output.message(
            output.get_subject().TARGET,
            'Cleaning up',
            True
        )

        if mode.is_target_remote():
            sftp = ssh_client_target.open_sftp()
            sftp.remove(_file_path)
            sftp.remove(_file_path + '.tar.gz')
            sftp.close()
        else:
            if os.path.isfile(_file_path):
                os.remove(_file_path)
            if os.path.isfile(_file_path + '.tar.gz'):
                os.remove(_file_path + '.tar.gz')
Exemplo n.º 7
0
def put_origin_database_dump(origin_path):
    sftp = ssh_client_target.open_sftp()

    if (mode.get_sync_mode() == mode.get_sync_modes().PROXY):
        _subject = output.get_subject().LOCAL
    else:
        _subject = output.get_subject().ORIGIN

    output.message(
        _subject,
        'Uploading database dump',
        True
    )

    #
    # ToDo: Download speed problems
    # https://github.com/paramiko/paramiko/issues/60
    #
    sftp.put(origin_path + database.origin_database_dump_file_name + '.tar.gz',
             helper.get_target_dump_dir() + database.origin_database_dump_file_name + '.tar.gz', upload_status)
    sftp.close()
    print('')
Exemplo n.º 8
0
def parse_database_credentials(_db_credentials):
    _db_credentials = str(_db_credentials).replace('\\n\'', '')
    # DATABASE_URL=mysql://db-user:1234@db-host:3306/db-name
    _db_credentials = re.findall(r"\/{2}(.+):(.+)@(.+):(\d+)\/(.+)",
                                 _db_credentials)[0]

    if len(_db_credentials) != 5:
        sys.exit(
            output.message(output.get_subject().ERROR,
                           'Mismatch of expected database credentials', False))

    _db_config = {
        'dbname': _db_credentials[4],
        'host': _db_credentials[2],
        'password': _db_credentials[1],
        'port': _db_credentials[3],
        'user': _db_credentials[0],
    }

    return _db_config
Exemplo n.º 9
0
def check_target_database_dump():
    with open(system.default_local_sync_path + origin_database_dump_file_name) as f:
        lines = f.readlines()
        if "-- Dump completed on" not in lines[-1]:
            sys.exit(output.message(output.get_subject().ERROR, 'Dump was not fully transferred', False))
Exemplo n.º 10
0
def remove_temporary_data_dir():
    if os.path.exists(system.default_local_sync_path):
        shutil.rmtree(system.default_local_sync_path)
        output.message(output.get_subject().LOCAL, 'Cleaning up', True)