Exemplo n.º 1
0
def drop_db(date_check=None, workflow=None):
    """Drop a database.

    You will need to enter the current date and time e.g:
    fab test:hatherleigh_info drop_db:02/02/2015-16:54
    """
    check = datetime.now().strftime('%d/%m/%Y-%H:%M')
    db_name = database_name(env.site_info, workflow)
    print(green("drop '{}' database on '{}'").format(db_name, env.host_string))
    if check == date_check:
        message = "Are you sure you want to drop '{}' on '{}'?".format(
            database_name, env.host_string
        )
        confirm = prompt(
            "To drop the database, enter the name of the current month?"
        )
        check = datetime.now().strftime('%B')
        if not confirm == check:
            abort("exit... (the current month is '{}')".format(check))
        confirm = prompt("Are you sure you want to drop the database (Y/N)?")
        if confirm == 'Y':
            print('deleting...')
            if remote_database_exists(env.site_info, workflow):
                drop_remote_database(env.site_info, workflow)
        else:
            abort("exit... (you did not enter 'Y' to drop the database)")
    else:
        print(
            "You cannot drop a database unless you enter the current date and "
            "time as a parameter e.g:\ndrop_db:{}".format(check)
        )
Exemplo n.º 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'))