コード例 #1
0
ファイル: mysql.py プロジェクト: lnielsen/invenio-fabric
def mysql_copy(from_env):
    """
    Copy database from latest available dump.

    Currently it is assumed that the dump file is accessible on the
    same path on both environment host systems. This usually means that
    the dumps are stored on a shared network storage.
    """
    with hide('commands'):
        to_env = env
        from_env = env_get(from_env)

        answers_from = prompt_and_check([
            ("FROM MySQL admin user:"******"user"),
            ("FROM MySQL admin password:"******"password")
        ], mysql_admin_check(from_env.CFG_DATABASE_HOST, from_env.CFG_DATABASE_PORT))

        answers_to = prompt_and_check([
            ("TO MySQL admin user:"******"user"),
            ("TO MySQL admin password:"******"password")
        ], mysql_admin_check(to_env.CFG_DATABASE_HOST, to_env.CFG_DATABASE_PORT))

        # Escape quote characters
        answers_from['password'] = answers_from['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')
        answers_to['password'] = answers_to['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')

        user_pw_from = '-u %(user)s --password=%(password)s' % answers_from if answers_from['password'] else '-u %(user)s' % answers_from
        user_pw_to = '-u %(user)s --password=%(password)s' % answers_to if answers_to['password'] else '-u %(user)s' % answers_to

        ctx = {
            'user_pw_from': user_pw_from,
            'user_pw_to': user_pw_to,
            'from_host' :  from_env.CFG_DATABASE_HOST,
            'from_name' :  from_env.CFG_DATABASE_NAME,
            'from_user' :  from_env.CFG_DATABASE_USER,
            'from_password' : from_env.CFG_DATABASE_PASS,
            'from_port': from_env.CFG_DATABASE_PORT,
            'to_host' :  to_env.CFG_DATABASE_HOST,
            'to_name' :  to_env.CFG_DATABASE_NAME,
            'to_user' :  to_env.CFG_DATABASE_USER,
            'to_password' : to_env.CFG_DATABASE_PASS,
            'to_port': to_env.CFG_DATABASE_PORT,
        }


        puts(">>> You are about to copy:")
        puts(">>>   %(from_user)s@%(from_host)s:%(from_port)s/%(from_name)s" % ctx)
        puts(">>> to:")
        puts(">>>   %(to_user)s@%(to_host)s:%(to_port)s/%(to_name)s" % ctx)

        if confirm("This will erease all data in the latter database and may impact performance on system being copied from. Are you sure you want to proceed?"):
            mysql_dropdb(stored_answers=answers_to)
            mysql_createdb(stored_answers=answers_to)

        local('mysqldump %(user_pw_from)s -h %(from_host)s -P %(from_port)s -f %(from_name)s | mysql %(user_pw_to)s -h %(to_host)s -P %(to_port)s -f %(to_name)s' % ctx)
コード例 #2
0
ファイル: mysql.py プロジェクト: lnielsen/invenio-fabric
def mysql_dropdb(stored_answers=None):
    """
    Drop database and user
    """
    with hide('commands'):
        puts(cyan(">>> Dropping database and user ..." % env))

        answers = prompt_and_check([
            ("MySQL admin user:"******"user"),
            ("MySQL admin password:"******"password")
        ], mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        stored_answers=stored_answers)

        # Escape quote characters
        answers['password'] = answers['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')
        user_pw = '-u %(user)s --password=%(password)s' % answers if answers['password'] else '-u %(user)s' % answers

        ctx = {
            'user_pw': user_pw,
            'host': env.CFG_DATABASE_HOST,
            'name':  env.CFG_DATABASE_NAME,
            'user':  env.CFG_DATABASE_USER,
            'password': env.CFG_DATABASE_PASS,
            'port': env.CFG_DATABASE_PORT,
        }

        ctx['password'] = ctx['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')

        local('mysql %(user_pw)s -h %(host)s -P %(port)s -e "DROP DATABASE IF EXISTS %(name)s"' % ctx)
        with settings(warn_only=True):
            for user_host in env.get('CFG_DATABASE_GRANT_HOSTS', ['%']):
                ctx.update({'user_host': user_host})
                local('mysql %(user_pw)s -h %(host)s -P %(port)s -e "REVOKE ALL PRIVILEGES ON %(name)s.* FROM %(user)s@\'%(user_host)s\'"' % ctx)
        local('mysqladmin %(user_pw)s -h %(host)s -P %(port)s flush-privileges' % ctx)
コード例 #3
0
ファイル: mysql.py プロジェクト: lnielsen/invenio-fabric
def mysql_createdb(stored_answers=None,):
    """
    Create database and user
    """
    with hide('commands'):
        puts(cyan(">>> Creating database and user ..." % env))

        answers = prompt_and_check([
            ("MySQL admin user:"******"user"),
            ("MySQL admin password:"******"password")
        ], mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        stored_answers=stored_answers)

        # Escape quote characters
        answers['password'] = answers['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')
        user_pw = '-u %(user)s --password=%(password)s' % answers if answers['password'] else '-u %(user)s' % answers

        ctx = {
            'user_pw': user_pw,
            'host':  env.CFG_DATABASE_HOST,
            'name':  env.CFG_DATABASE_NAME,
            'user':  env.CFG_DATABASE_USER,
            'password': env.CFG_DATABASE_PASS,
            'port': env.CFG_DATABASE_PORT,
        }

        ctx['password'] = ctx['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')

        # Run commands
        local('mysql %(user_pw)s -h %(host)s -P %(port)s -e "CREATE DATABASE IF NOT EXISTS %(name)s DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"' % ctx)
        for user_host in env.get('CFG_DATABASE_GRANT_HOSTS', ['%']):
            ctx.update({'user_host': user_host})
            local('mysql %(user_pw)s -h %(host)s -P %(port)s -e "GRANT ALL PRIVILEGES ON %(name)s.* TO %(user)s@\'%(user_host)s\' IDENTIFIED BY \'%(password)s\';"' % ctx)
        local('mysqladmin %(user_pw)s -h %(host)s -P %(port)s flush-privileges' % ctx)
コード例 #4
0
def mysql_load(dumpfile=None, stored_answers=None):
    """
    Load MySQL dump file
    """
    with hide("commands"):
        puts(cyan(">>> Loading database dump..."))

        if not dumpfile:
            dumpfile = os.path.join(env.CFG_DATABASE_DUMPDIR, "%s.sql.gz" % env.CFG_DATABASE_NAME)

        if not os.path.exists(dumpfile):
            abort("File %s does not exists." % dumpfile)

        answers = prompt_and_check(
            [("MySQL admin user:"******"user"), ("MySQL admin password:"******"password")],
            mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
            cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
            stored_answers=stored_answers,
        )

        # Escape quote characters
        answers["password"] = answers["password"].replace("'", "'").replace('"', '"').replace("$", "\\$")
        user_pw = "-u %(user)s --password=%(password)s" % answers if answers["password"] else "-u %(user)s" % answers

        if dumpfile.endswith(".gz"):
            dumpfile_stream = "gunzip -c %s" % dumpfile
        else:
            dumpfile_stream = "cat %s" % dumpfile

        ctx = {
            "user_pw": user_pw,
            "host": env.CFG_DATABASE_HOST,
            "name": env.CFG_DATABASE_NAME,
            "user": env.CFG_DATABASE_USER,
            "password": env.CFG_DATABASE_PASS,
            "port": env.CFG_DATABASE_PORT,
            "dumpfile": dumpfile,
            "dumpfile_stream": dumpfile_stream,
        }

        # Escape quote characters
        ctx["password"] = ctx["password"].replace("'", "'").replace('"', '"').replace("$", "\\$")

        if confirm(
            "This will erease all data in the existing database. Are you sure you want to load %(dumpfile)s?" % ctx
        ):
            mysql_dropdb(stored_answers=answers)
            mysql_createdb(stored_answers=answers)

            local("%(dumpfile_stream)s | mysql %(user_pw)s -h %(host)s -P %(port)s -f %(name)s" % ctx)

        return dumpfile
コード例 #5
0
ファイル: mysql.py プロジェクト: lnielsen/invenio-fabric
def mysql_load(dumpfile=None, stored_answers=None):
    """
    Load MySQL dump file
    """
    with hide('commands'):
        puts(cyan(">>> Loading database dump..."))

        if not dumpfile:
            dumpfile = os.path.join(env.CFG_DATABASE_DUMPDIR, "%s.sql.gz" % env.CFG_DATABASE_NAME)

        if not exists_local(dumpfile):
            abort("File %s does not exists." % dumpfile)

        answers = prompt_and_check([
            ("MySQL admin user:"******"user"),
            ("MySQL admin password:"******"password")
        ], mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        stored_answers=stored_answers)

        # Escape quote characters
        answers['password'] = answers['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')
        user_pw = '-u %(user)s --password=%(password)s' % answers if answers['password'] else '-u %(user)s' % answers

        if dumpfile.endswith(".gz"):
            dumpfile_stream = "gunzip -c %s" % dumpfile
        else:
            dumpfile_stream = "cat %s" % dumpfile

        ctx = {
            'user_pw': user_pw,
            'host':  env.CFG_DATABASE_HOST,
            'name':  env.CFG_DATABASE_NAME,
            'user':  env.CFG_DATABASE_USER,
            'password': env.CFG_DATABASE_PASS,
            'port': env.CFG_DATABASE_PORT,
            'dumpfile': dumpfile,
            'dumpfile_stream': dumpfile_stream,
        }

        # Escape quote characters
        ctx['password'] = ctx['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')

        if confirm("This will erease all data in the existing database. Are you sure you want to load %(dumpfile)s?" % ctx):
            mysql_dropdb(stored_answers=answers)
            mysql_createdb(stored_answers=answers)

            sudo_local('%(dumpfile_stream)s | mysql %(user_pw)s -h %(host)s -P %(port)s -f %(name)s' % ctx, user=env.CFG_INVENIO_USER)

        return dumpfile
コード例 #6
0
def mysql_createdb(stored_answers=None):
    """
    Create database and user
    """
    with hide("commands"):
        puts(cyan(">>> Creating database and user ..." % env))

        answers = prompt_and_check(
            [("MySQL admin user:"******"user"), ("MySQL admin password:"******"password")],
            mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
            cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
            stored_answers=stored_answers,
        )

        # Escape quote characters
        answers["password"] = answers["password"].replace("'", "'").replace('"', '"').replace("$", "\\$")
        user_pw = "-u %(user)s --password=%(password)s" % answers if answers["password"] else "-u %(user)s" % answers

        ctx = {
            "user_pw": user_pw,
            "host": env.CFG_DATABASE_HOST,
            "name": env.CFG_DATABASE_NAME,
            "user": env.CFG_DATABASE_USER,
            "password": env.CFG_DATABASE_PASS,
            "port": env.CFG_DATABASE_PORT,
        }

        ctx["password"] = ctx["password"].replace("'", "'").replace('"', '"').replace("$", "\\$")

        # Run commands
        local(
            'mysql %(user_pw)s -h %(host)s -P %(port)s -e "CREATE DATABASE IF NOT EXISTS %(name)s DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"'
            % ctx
        )
        local(
            "mysql %(user_pw)s -h %(host)s -P %(port)s -e \"GRANT ALL PRIVILEGES ON %(name)s.* TO %(user)s@localhost IDENTIFIED BY '%(password)s';\""
            % ctx
        )
        local("mysqladmin %(user_pw)s -h %(host)s -P %(port)s flush-privileges" % ctx)
コード例 #7
0
def mysql_dropdb(stored_answers=None):
    """
    Drop database and user
    """
    with hide("commands"):
        puts(cyan(">>> Dropping database and user ..." % env))

        answers = prompt_and_check(
            [("MySQL admin user:"******"user"), ("MySQL admin password:"******"password")],
            mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
            cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
            stored_answers=stored_answers,
        )

        # Escape quote characters
        answers["password"] = answers["password"].replace("'", "'").replace('"', '"').replace("$", "\\$")
        user_pw = "-u %(user)s --password=%(password)s" % answers if answers["password"] else "-u %(user)s" % answers

        ctx = {
            "user_pw": user_pw,
            "host": env.CFG_DATABASE_HOST,
            "name": env.CFG_DATABASE_NAME,
            "user": env.CFG_DATABASE_USER,
            "password": env.CFG_DATABASE_PASS,
            "port": env.CFG_DATABASE_PORT,
        }

        ctx["password"] = ctx["password"].replace("'", "'").replace('"', '"').replace("$", "\\$")

        local('mysql %(user_pw)s -h %(host)s -P %(port)s -e "DROP DATABASE IF EXISTS %(name)s"' % ctx)
        with settings(warn_only=True):
            local(
                'mysql %(user_pw)s -h %(host)s -P %(port)s -e "REVOKE ALL PRIVILEGES ON %(name)s.* FROM %(user)s@localhost"'
                % ctx
            )
        local("mysqladmin %(user_pw)s -h %(host)s -P %(port)s flush-privileges" % ctx)