Beispiel #1
0
def drop_db_role():
    print(green("drop '{}' database role").format(env.site_info.db_name))
    db_name = env.site_info.db_name
    if remote_user_exists(env.site_info):
        print('deleting...')
        drop_remote_user(env.site_info)
        print("deleted '{}'".format(db_name))
    else:
        print("Cannot delete '{}' role.  It does not exist.".format(db_name))
Beispiel #2
0
def create_db(table_space=None, workflow=None):
    """Create a database.

    e.g:
    fab test:hatherleigh_info create_db

    Note: table space 'cbs' is the name we have given to the Rackspace Cloud
    Block Storage volume.  If you are not using cloud block storage, then don't
    use the 'table_space' parameter e.g:

    fab test:hatherleigh_info create_db:cbs
    """
    db_name = database_name(env.site_info, workflow)
    print(green("create '{}' on '{}'").format(db_name, env.host_string))
    if env.site_info.is_mysql:
        # TODO
        # Note: these commands will not work if the root user has a password!
        # For more information, see:
        # Securing the Initial MySQL Accounts:
        # http://docs.oracle.com/cd/E17952_01/refman-5.1-en/default-privileges.html
        # Setting mysql root password the first time:
        # https://github.com/saltstack/salt/issues/5918
        # I have a task on my list to set the password automatically
        run('mysql -u root -e "CREATE USER \'{}\'@\'localhost\' IDENTIFIED BY \'{}\';"'.format(
            site_info.db_user(), site_info.password()
            )
        )
        run('mysql -u root -e "CREATE DATABASE {};"'.format(database_name))
        # I had loads of problems with this one.  bash evaluates back-ticks
        # first.  I think I solved the issue by adding 'shell=False' to 'run'.
        command = (
            "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, "
            "ALTER, LOCK TABLES, CREATE TEMPORARY TABLES ON \`{}\`.* TO "
            "'{}'@'localhost' IDENTIFIED BY '{}';".format(
                database_name, site_info.db_user(), site_info.password()
            )
        )
        run('mysql -u root -e "{}"'.format(command), shell=False)
    else:
        if not remote_user_exists(env.site_info):
            remote_user_create(env.site_info)
        remote_database_create(env.site_info, table_space, workflow)
    print(green('done'))