Ejemplo n.º 1
0
def install_mysql(choice="all"):
    """
    Installs a local instance of MySQL of the web instance. This will only work
    if the number of web instances is one.

    choice: Indicates which assets to create/install:
        'mysql'      -> just install MySQL; don't create the databases
        'site_db'    -> just create the site database
        'bundles_db' -> just create the bundle service database
        'all' or ''  -> install all three
    """
    require("configuration")
    if len(env.roledefs["web"]) != 1:
        raise Exception("Task install_mysql requires exactly one web instance.")

    if choice == "mysql":
        choices = {"mysql"}
    elif choice == "site_db":
        choices = {"site_db"}
    elif choice == "bundles_db":
        choices = {"bundles_db"}
    elif choice == "all":
        choices = {"mysql", "site_db", "bundles_db"}
    else:
        raise ValueError("Invalid choice: %s. Valid choices are: 'build', 'web' or 'all'." % (choice))

    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    dba_password = configuration.getDatabaseAdminPassword()

    if "mysql" in choices:
        sudo("DEBIAN_FRONTEND=noninteractive apt-get -q -y install mysql-server")
        sudo("mysqladmin -u root password {0}".format(dba_password))

    if "site_db" in choices:
        db_name = configuration.getDatabaseName()
        db_user = configuration.getDatabaseUser()
        db_password = configuration.getDatabasePassword()
        cmds = [
            "create database {0};".format(db_name),
            "create user '{0}'@'localhost' IDENTIFIED BY '{1}';".format(db_user, db_password),
            "GRANT ALL PRIVILEGES ON {0}.* TO '{1}'@'localhost' WITH GRANT OPTION;".format(db_name, db_user),
        ]
        run('mysql --user=root --password={0} --execute="{1}"'.format(dba_password, " ".join(cmds)))

    if "bundles_db" in choices:
        db_name = configuration.getBundleServiceDatabaseName()
        db_user = configuration.getBundleServiceDatabaseUser()
        db_password = configuration.getBundleServiceDatabasePassword()
        cmds = [
            "create database {0};".format(db_name),
            "create user '{0}'@'localhost' IDENTIFIED BY '{1}';".format(db_user, db_password),
            "GRANT ALL PRIVILEGES ON {0}.* TO '{1}'@'localhost' WITH GRANT OPTION;".format(db_name, db_user),
        ]
        run('mysql --user=root --password={0} --execute="{1}"'.format(dba_password, " ".join(cmds)))
Ejemplo n.º 2
0
def put_mysql_dump_to_new_database():
    '''Puts dubmped database to new location'''
    require('configuration')
    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    db_host = "localhost"
    db_database = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    backup_directory = os.path.dirname(os.path.realpath(__file__))

    put(local_path='{}/competitiondump.sql.gz'.format(backup_directory),
        remote_path='/home/azureuser/db_dump.sql.gz',
        use_sudo=True)
Ejemplo n.º 3
0
def put_mysql_dump_to_new_database():
    '''Puts dubmped database to new location'''
    require('configuration')
    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    db_host = "localhost"
    db_database = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    backup_directory = os.path.dirname(os.path.realpath(__file__))

    put(local_path='{}/competitiondump.sql.gz'.format(backup_directory),
        remote_path='/home/azureuser/db_dump.sql.gz',
        use_sudo=True)
Ejemplo n.º 4
0
def install_mysql(choice='all'):
    """
    Installs a local instance of MySQL of the web instance. This will only work
    if the number of web instances is one.

    choice: Indicates which assets to create/install:
        'mysql'      -> just install MySQL; don't create the databases
        'website_db' -> just create the website database
        'bundles_db' -> just create the bundle service database
        'all' or ''  -> install everything
    """
    require('configuration')
    if len(env.roledefs['web']) != 1:
        raise Exception("Task install_mysql requires exactly one web instance.")

    if choice == 'mysql':
        choices = {'mysql'}
    elif choice == 'website_db':
        choices = {'website_db'}
    elif choice == 'bundles_db':
        choices = {'bundles_db'}
    elif choice == 'all':
        choices = {'mysql', 'website_db', 'bundles_db'}
    else:
        raise ValueError("Invalid choice: %s. Valid choices are: 'build', 'web' or 'all'." % (choice))

    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    dba_password = configuration.getDatabaseAdminPassword()

    if 'mysql' in choices:
        sudo('DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server')
        sudo('mysqladmin -u root password {0}'.format(dba_password))

    if 'website_db' in choices:
        db_name = configuration.getDatabaseName()
        db_user = configuration.getDatabaseUser()
        db_password = configuration.getDatabasePassword()
        cmds = ["create database {0};".format(db_name),
                "create user '{0}'@'localhost' IDENTIFIED BY '{1}';".format(db_user, db_password),
                "GRANT ALL PRIVILEGES ON {0}.* TO '{1}'@'localhost' WITH GRANT OPTION;".format(db_name, db_user)]
        run('mysql --user=root --password={0} --execute="{1}"'.format(dba_password, " ".join(cmds)))

    if 'bundles_db' in choices:
        db_name = configuration.getBundleServiceDatabaseName()
        db_user = configuration.getBundleServiceDatabaseUser()
        db_password = configuration.getBundleServiceDatabasePassword()
        cmds = ["create database {0};".format(db_name),
                "create user '{0}'@'localhost' IDENTIFIED BY '{1}';".format(db_user, db_password),
                "GRANT ALL PRIVILEGES ON {0}.* TO '{1}'@'localhost' WITH GRANT OPTION;".format(db_name, db_user)]
        run('mysql --user=root --password={0} --execute="{1}"'.format(dba_password, " ".join(cmds)))
Ejemplo n.º 5
0
def get_database_dump():
    '''Saves backups to $CODALAB_MYSQL_BACKUP_DIR/launchdump-year-month-day-hour-min-second.sql.gz'''
    require('configuration')
    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    db_host = "localhost"
    db_name = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    dump_file_name = 'competitiondump.sql.gz'

    run('mysqldump --host=%s --user=%s --password=%s %s --port=3306 | gzip > /tmp/%s'
        % (db_host, db_user, db_password, db_name, dump_file_name))
    backup_directory = os.path.dirname(os.path.realpath(__file__))

    get('%s' % dump_file_name, backup_directory)
Ejemplo n.º 6
0
def get_database_dump():
    '''Saves backups to $CODALAB_MYSQL_BACKUP_DIR/launchdump-year-month-day-hour-min-second.sql.gz'''
    require('configuration')
    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    db_host = "localhost"
    db_name = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    dump_file_name = 'launchdump-%s.sql.gz' % datetime.datetime.now().strftime(
        '%Y-%m-%d-%H-%M-%S')

    run('mysqldump --host=%s --user=%s --password=%s %s --port=3306 | gzip > /tmp/%s'
        % (db_host, db_user, db_password, db_name, dump_file_name))

    backup_dir = os.environ.get("CODALAB_MYSQL_BACKUP_DIR", "")
    get('/tmp/%s' % dump_file_name, backup_dir)
Ejemplo n.º 7
0
def get_database_dump():
    """Saves backups to $CODALAB_MYSQL_BACKUP_DIR/launchdump-year-month-day-hour-min-second.sql.gz"""
    require("configuration")
    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    db_host = "localhost"
    db_name = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    dump_file_name = "launchdump-%s.sql.gz" % datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")

    run(
        "mysqldump --host=%s --user=%s --password=%s %s --port=3306 | gzip > /tmp/%s"
        % (db_host, db_user, db_password, db_name, dump_file_name)
    )

    backup_dir = os.environ.get("CODALAB_MYSQL_BACKUP_DIR", "")
    get("/tmp/%s" % dump_file_name, backup_dir)
Ejemplo n.º 8
0
def get_database_dump():
    '''Saves backups to $CODALAB_MYSQL_BACKUP_DIR/launchdump-year-month-day-hour-min-second.sql.gz'''
    require('configuration')
    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    db_host = "localhost"
    db_name = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    dump_file_name = 'competitiondump.sql.gz'

    run('mysqldump --host=%s --user=%s --password=%s %s --port=3306 | gzip > /tmp/%s' % (
        db_host,
        db_user,
        db_password,
        db_name,
        dump_file_name)
        )
    backup_directory = os.path.dirname(os.path.realpath(__file__))

    get('%s' % dump_file_name, backup_directory)
Ejemplo n.º 9
0
def install_mysql():
    """
    Installs a local instance of MySQL of the web instance. This will only work
    if the number of web instances is one.
    """
    require('configuration')
    if len(env.roledefs['web']) <> 1:
        raise(Exception("Task install_mysql requires exactly one web instance."))

    configuration = DeploymentConfig(env.cfg_label, env.cfg_path)
    dba_password = configuration.getDatabaseAdminPassword()
    db_name = configuration.getDatabaseName()
    db_user = configuration.getDatabaseUser()
    db_password = configuration.getDatabasePassword()

    sudo('DEBIAN_FRONTEND=noninteractive apt-get -q -y install mysql-server')
    sudo('mysqladmin -u root password {0}'.format(dba_password))

    cmds = ["create database {0};".format(db_name),
            "create user '{0}'@'localhost' IDENTIFIED BY '{1}';".format(db_user, db_password),
            "GRANT ALL PRIVILEGES ON {0}.* TO '{1}'@'localhost' WITH GRANT OPTION;".format(db_name, db_user) ]
    run('mysql --user=root --password={0} --execute="{1}"'.format(dba_password, " ".join(cmds)))