示例#1
0
def setup_databases():
    """
        Create production postgres database using local_settings for the DB
    """

    logger.info("confirming Postgres knows about username:{user} password:{pwd}".format(
        user=env.deploy_user,
        pwd=env.password))
    fabtools.icanhaz.postgres.user(
        env.deploy_user,
        env.password,
        superuser=True,
        createdb=True,
        createrole=True,
        login=True)

    run_as_pg("""psql -U postgres -h {host} -c "ALTER USER {user} password '{pwd}';" """.format(
        user=env.deploy_user,
        pwd=env.password,
        host=os.environ['DATABASE_HOST']
    ))

    run_as_pg("""psql -U postgres -h {host} -c "ALTER USER {user} SUPERUSER;" """.format(
        user=env.deploy_user,
        host=os.environ['DATABASE_HOST']
    ))

    database_name = os.environ['DATABASE_NAME']
    fabtools.icanhaz.postgres.database(database_name, env.deploy_user)
    # the following two lines below should be enabled for postgis 2.0 (also remove template_postgis from above lines)
    psql("CREATE EXTENSION IF NOT EXISTS POSTGIS")
    psql("CREATE EXTENSION IF NOT EXISTS DBLINK")

    psql("ALTER DATABASE {database_name} SET search_path = '$user',public,postgis;".format(database_name=database_name))
示例#2
0
def dump_db(db, schema=False, exclude_schema=False, exclude_table=False, table=False, compress=True, output_dir="/tmp"):
    dump_command = 'pg_dump -U postgres'

    if compress:
        dump_command += " -Fc"

    dump_command += " {db} ".format(db=db)
    output_file = db

    if schema:
        dump_command += " -n {schema} ".format(schema=schema)
        output_file += "_" + schema
    if table:
        dump_command += " -t {table} ".format(table=table)
        output_file += "_" + table
    if exclude_schema:
        dump_command += " -N {schema} ".format(schema=schema)
        output_file += "_NO" + schema
    if exclude_table:
        dump_command += " -T {table} ".format(table=table)
        output_file += "_NO" + table

    output_file += ".dump" if compress else ".sql"
    output = os.path.join(output_dir, output_file)

    dump_command += " -f {output}".format(output)
    run_as_pg(dump_command)

    return output
示例#3
0
def setup_databases():
    """
        Create production postgres database using local_settings for the DB
    """

    logger.info("confirming Postgres knows about username:{user} password:{pwd}".format(
        user=env.deploy_user,
        pwd=env.password))
    fabtools.icanhaz.postgres.user(
        env.deploy_user,
        env.password,
        superuser=True,
        createdb=True,
        createrole=True,
        login=True)

    run_as_pg("""psql -U postgres -h {host} -c "ALTER USER {user} password '{pwd}';" """.format(
        user=env.deploy_user,
        pwd=env.password,
        host=os.environ['DATABASE_HOST']
    ))

    run_as_pg("""psql -U postgres -h {host} -c "ALTER USER {user} SUPERUSER;" """.format(
        user=env.deploy_user,
        host=os.environ['DATABASE_HOST']
    ))

    database_name = os.environ['DATABASE_NAME']
    fabtools.icanhaz.postgres.database(database_name, env.deploy_user)
    # the following two lines below should be enabled for postgis 2.0 (also remove template_postgis from above lines)
    psql("CREATE EXTENSION IF NOT EXISTS POSTGIS")
    psql("CREATE EXTENSION IF NOT EXISTS DBLINK")

    psql("ALTER DATABASE {database_name} SET search_path = '$user',public,postgis;".format(database_name=database_name))
示例#4
0
def deploy_data(build_type='prod'):
    """
    Logs into remote machine and loads the data_load that has been published to the same machine
    The dump file is expected at /srv/datadump/pg_dump.dmp
    """

    # stop any connections to the db
    with settings(warn_only=True):
        sudo('supervisorctl stop all')
        sudo('/etc/init.d/supervisor stop')

    # with cd(get_django_setting(build_type, 'ROOT_PATH')):
    #
    #
    #     fab_cmd = env.virtualenv_directory + '/bin/fab'
    #     sudo('{fab_cmd} localhost fetch_datadump:use_local=True,force_local_db_destroy=True'.format(
    #         fab_cmd=fab_cmd), user=env.deploy_user)

    with postgres_env_password_loaded():
        db_conn_string = build_postgres_conn_string()
        database_name = os.environ['DATABASE_NAME']

        # Some versions of postgres do not have --if-exists, so just ignore the error if it doesn't exist
        with settings(warn_only=True):
            drop_db_connections(database_name)
            run('dropdb {db_conn_string}'.format(
                db_conn_string=db_conn_string))

        run_as_pg('createdb -O {db_user} {db_conn_string}'.format(
            db_user=os.environ['DATABASE_USERNAME'],
            db_conn_string=db_conn_string))

        psql(
            "ALTER DATABASE {database_name} SET search_path = '$user',public,postgis;"
            .format(database_name=database_name))

        with settings(warn_only=True):
            result = run(
                'pg_restore {db_conn_string} -d {dbname} {local_dump_file}'.
                format(db_conn_string=build_postgres_conn_string(omit_db=True),
                       dbname=database_name,
                       local_dump_file='/srv/datadump/pg_dump.dmp'))
        if result.failed:
            print "ERROR: You probably don't have 'calthorpe' ROLE defined. Fix by executing:"
            print "CREATE ROLE calthorpe; GRANT calthorpe to {user};".format(
                user=os.environ['DATABASE_USERNAME'])
            raise SystemExit()

    if os.path.exists('/srv/calthorpe_media'):
        sudo('rm -r /srv/calthorpe_media')
    sudo('cp -R /srv/datadump/media/calthorpe_media /srv/')
    directory_permissions(build_type=build_type)

    # start connections to the db
    sudo('/etc/init.d/supervisor start')
    sudo('supervisorctl start all')
示例#5
0
def drop_databases():
    """
        Drop the databases. This is not part of the normal setup process, rather it's used to recreate the database in development.
    :return:
    """

    db_name = os.environ['DATABASE_NAME']
    for database_name in ['test_{0}'.format(db_name), db_name]:
        with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
            exists = psql("", database_name=database_name).succeeded
            # Drop the database if it exists
        if exists:
            drop_db_connections(database_name)
            run_as_pg("dropdb {db};".format(db=database_name))
示例#6
0
def deploy_data(build_type='prod'):
    """
    Logs into remote machine and loads the data_load that has been published to the same machine
    The dump file is expected at /srv/datadump/pg_dump.dmp
    """

    # stop any connections to the db
    with settings(warn_only=True):
        sudo('supervisorctl stop all')
        sudo('/etc/init.d/supervisor stop')

    # with cd(get_django_setting(build_type, 'ROOT_PATH')):
    #
    #
    #     fab_cmd = env.virtualenv_directory + '/bin/fab'
    #     sudo('{fab_cmd} localhost fetch_datadump:use_local=True,force_local_db_destroy=True'.format(
    #         fab_cmd=fab_cmd), user=env.deploy_user)

    with postgres_env_password_loaded():
        db_conn_string = build_postgres_conn_string()
        database_name = os.environ['DATABASE_NAME']

        # Some versions of postgres do not have --if-exists, so just ignore the error if it doesn't exist
        with settings(warn_only=True):
            drop_db_connections(database_name)
            run('dropdb {db_conn_string}'.format(db_conn_string=db_conn_string))

        run_as_pg('createdb -O {db_user} {db_conn_string}'.format(
            db_user=os.environ['DATABASE_USERNAME'], db_conn_string=db_conn_string))

        psql("ALTER DATABASE {database_name} SET search_path = '$user',public,postgis;".format(database_name=database_name))

        with settings(warn_only=True):
            result = run('pg_restore {db_conn_string} -d {dbname} {local_dump_file}'.format(
                db_conn_string=build_postgres_conn_string(omit_db=True),
                dbname=database_name,
                local_dump_file='/srv/datadump/pg_dump.dmp'))
        if result.failed:
            print "ERROR: You probably don't have 'calthorpe' ROLE defined. Fix by executing:"
            print "CREATE ROLE calthorpe; GRANT calthorpe to {user};".format(user=os.environ['DATABASE_USERNAME'])
            raise SystemExit()

    if os.path.exists('/srv/calthorpe_media'):
        sudo('rm -r /srv/calthorpe_media')
    sudo('cp -R /srv/datadump/media/calthorpe_media /srv/')
    directory_permissions(build_type=build_type)

    # start connections to the db
    sudo('/etc/init.d/supervisor start')
    sudo('supervisorctl start all')
示例#7
0
def switch_to_prod(reverse=False):
    # This should be done in ansible, once there is a specific 'dev'
    # role. Then, we need to override
    # `DjangoTestSuiteRunner.setup_databases`.

    # For more information:
    # http://stackoverflow.com/questions/5917587/django-unit-tests-without-a-db
    # https://docs.djangoproject.com/en/1.5/topics/testing/advanced/#django.test.simple.DjangoTestSuiteRunner.setup_databases
    if reverse:
        # Production needs superuser privileges to run tests
        build_type = 'dev'
        superuser = '******'
    else:
        build_type = 'prod'
        superuser = '******'

    try:
        sudo('rm -f /etc/supervisor/conf.d/calthorpe.conf')
        sudo('rm -f /etc/nginx/sites-available/calthorpe.nginx')

    except Exception as e:
        logger.warning('Failed to remove configuration files: %s', e)

    run_as_pg(
        """psql -h {host} -c "ALTER ROLE {user} {superuser}";""".format(
            user=env.deploy_user,
            superuser=superuser,
            host=os.environ['DATABASE_HOST']
        )
    )

    root_path = get_django_setting(build_type, 'ROOT_PATH')
    sudo('ln -sf {ROOT_PATH}/conf/etc/nginx/sites-available/calthorpe.nginx.{build_type} '
         '/etc/nginx/sites-enabled/calthorpe.nginx '.format(ROOT_PATH=root_path,
                                                            build_type=build_type))
    sudo('ln -sf {ROOT_PATH}/conf/etc/supervisor/conf.d/calthorpe.supervisor.{build_type} '
         ' /etc/supervisor/conf.d/calthorpe.conf'.format(ROOT_PATH=root_path,
                                                         build_type=build_type))

    sudo('supervisorctl stop all')
    sudo('sleep 15')
    sudo('service supervisor restart')
    sudo('service nginx restart')