示例#1
0
 def test_create_user_with_no_connection_limit(self, _run_as_pg):
     from fabtools import postgres
     postgres.create_user('foo', 'bar', connection_limit=-1)
     expected = (
         'psql -c "CREATE USER foo NOSUPERUSER NOCREATEDB NOCREATEROLE '
         'INHERIT LOGIN CONNECTION LIMIT -1 UNENCRYPTED PASSWORD \'bar\';"')
     self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#2
0
 def test_create_user_with_no_options(self, _run_as_pg):
     from fabtools import postgres
     postgres.create_user('foo', 'bar')
     expected = (
         'psql -c "CREATE USER foo NOSUPERUSER NOCREATEDB NOCREATEROLE '
         'INHERIT LOGIN PASSWORD \'bar\';"')
     self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#3
0
def user(
    name,
    password,
    superuser=False,
    createdb=False,
    createrole=False,
    inherit=True,
    login=True,
    connection_limit=None,
    encrypted_password=False,
):
    """
    Require the existence of a PostgreSQL user.

    The password and options provided will only be applied when creating
    a new user (existing users will *not* be modified).

    ::

        from fabtools import require

        require.postgres.user('dbuser', password='******')

        require.postgres.user('dbuser2', password='******',
            createdb=True, createrole=True, connection_limit=20)

    """
    if not user_exists(name):
        create_user(
            name, password, superuser, createdb, createrole, inherit, login, connection_limit, encrypted_password
        )
示例#4
0
def user(name,
         password,
         superuser=False,
         createdb=False,
         createrole=False,
         inherit=True,
         login=True,
         connection_limit=None,
         encrypted_password=False):
    """
    Require the existence of a PostgreSQL user.

    The password and options provided will only be applied when creating
    a new user (existing users will *not* be modified).

    ::

        from fabtools import require

        require.postgres.user('dbuser', password='******')

        require.postgres.user('dbuser2', password='******',
            createdb=True, create_role=True, connection_limit=20)

    """
    if not user_exists(name):
        create_user(name, password, superuser, createdb, createrole, inherit,
                    login, connection_limit, encrypted_password)
示例#5
0
 def test_create_user_with_no_connection_limit(self, _run_as_pg):
     from fabtools import postgres
     postgres.create_user('foo', 'bar', connection_limit=-1)
     expected = (
         'psql -c "CREATE USER foo NOSUPERUSER NOCREATEDB NOCREATEROLE '
         'INHERIT LOGIN CONNECTION LIMIT -1 UNENCRYPTED PASSWORD \'bar\';"')
     self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#6
0
 def test_create_user_with_no_options(self, _run_as_pg):
     from fabtools import postgres
     postgres.create_user('foo', 'bar')
     expected = (
         'psql -c "CREATE USER foo NOSUPERUSER NOCREATEDB NOCREATEROLE '
         'INHERIT LOGIN PASSWORD \'bar\';"')
     self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#7
0
def setup_postgres():
    """Initial postgres setup."""
    if not db.user_exists(env.database_user):
        if 'database_pw' not in env:
            prompt('PostgreSQL database password:'******'database_pw')
        create_user(env.database_user, password=env.database_pw, encrypted_password=True)
    if not database_exists(env.database_name):
        create_database(env.database_name, env.database_user)
示例#8
0
def test_create_and_drop_user(postgres_server):

    from fabtools.postgres import create_user, drop_user, user_exists

    create_user('alice', password='******')
    assert user_exists('alice')

    drop_user('alice')
    assert not user_exists('alice')
示例#9
0
    def test_create_user_with_no_options(self, _run_as_pg):
        from fabtools import postgres

        postgres.create_user("foo", "bar")
        expected = (
            'psql -c "CREATE USER foo NOSUPERUSER NOCREATEDB NOCREATEROLE '
            "INHERIT LOGIN UNENCRYPTED PASSWORD 'bar';\""
        )
        self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#10
0
def test_create_and_drop_user(postgres_server):

    from fabtools.postgres import create_user, drop_user, user_exists

    create_user('alice', password='******')
    assert user_exists('alice')

    drop_user('alice')
    assert not user_exists('alice')
示例#11
0
 def test_create_user_with_custom_options(self, _run_as_pg):
     from fabtools import postgres
     postgres.create_user('foo', 'bar', superuser=True, createdb=True,
                          createrole=True, inherit=False, login=False,
                          connection_limit=20, encrypted_password=True)
     expected = (
         'psql -c "CREATE USER "\'"foo"\'" SUPERUSER CREATEDB CREATEROLE '
         'NOINHERIT NOLOGIN CONNECTION LIMIT 20 '
         'ENCRYPTED PASSWORD \'bar\';"')
     self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#12
0
 def test_create_user_with_custom_options(self, _run_as_pg):
     from fabtools import postgres
     postgres.create_user('foo', 'bar', superuser=True, createdb=True,
                          createrole=True, inherit=False, login=False,
                          connection_limit=20, encrypted_password=True)
     expected = (
         'psql -c "CREATE USER foo SUPERUSER CREATEDB CREATEROLE '
         'NOINHERIT NOLOGIN CONNECTION LIMIT 20 '
         'ENCRYPTED PASSWORD \'bar\';"')
     self.assertEqual(expected, _run_as_pg.call_args[0][0])
示例#13
0
    def run(self):
        user = env['user']
        if self.user:
            user = self.user

        if not funcs.user_exists(user):
            funcs.create_user(user, self.password)

        if not funcs.database_exists(self.database):
            funcs.create_database(
                self.database, user, locale='ja_JP.utf8')
示例#14
0
def server_prepare():
    """ installing all necessary packages """
    apt_packages = ('libpq-dev', 'python3-dev',
                    'libssl-dev', 'lib32ncurses5-dev', 'python3',
                    'postgresql', 'postgis',)
    sudo('apt-get install %s' % " ".join(apt_packages))
    sudo('easy_install pip')
    """ db routines """
    db_env = DB[env.environment]
    if not postgres.user_exists(db_env["user"]):
        postgres.create_user(db_env["user"], db_env["password"],
            createdb=True, createrole=True)
    if not postgres.database_exists(db_env['db']):
        postgres.create_database(db_env['db'], owner=db_env["user"])
示例#15
0
def set_db_permissions():
    """Set the db so user wsgi has all permissions.
    """
    user = "******"
    dbname = "changelog"
    if not user_exists(user):
        create_user(user, password="******")
    if not database_exists(dbname):
        create_database(dbname, user)
    grant_sql = "GRANT ALL ON schema public to %s;" % user
    # assumption is env.repo_alias is also database name
    run('psql %s -c "%s"' % (dbname, grant_sql))
    grant_sql = "GRANT ALL ON ALL TABLES IN schema public to %s;" % user
    # assumption is env.repo_alias is also database name
    run('psql %s -c "%s"' % (dbname, grant_sql))
    grant_sql = "GRANT ALL ON ALL SEQUENCES IN schema public to %s;" % user
    run('psql %s -c "%s"' % (dbname, grant_sql))
示例#16
0
def set_db_permissions():
    """Set the db so user wsgi has all permissions.
    """
    user = '******'
    dbname = 'changelog'
    if not user_exists(user):
        create_user(user, password='******')
    if not database_exists(dbname):
        create_database(dbname, user)
    grant_sql = 'GRANT ALL ON schema public to %s;' % user
    # assumption is env.repo_alias is also database name
    run('psql %s -c "%s"' % (dbname, grant_sql))
    grant_sql = (
        'GRANT ALL ON ALL TABLES IN schema public to %s;' % user)
    # assumption is env.repo_alias is also database name
    run('psql %s -c "%s"' % (dbname, grant_sql))
    grant_sql = (
        'GRANT ALL ON ALL SEQUENCES IN schema public to %s;' % user)
    run('psql %s -c "%s"' % (dbname, grant_sql))
示例#17
0
def create_pgsql_user(username, password=None):
    if postgres.user_exists(username):
        raise KeyError("Username {} is taken".format(username))

    if password is None:
        password = gen_pass(10)
    postgres.create_user(username, password=password, createdb=True)

    db_not_created = True
    db_name = ""
    while db_not_created:
        db_name = gen_pass(12) + "_DB"
        if not postgres.database_exists(db_name):
            postgres.create_database(db_name, owner=username)
            db_not_created = False

    print c.blue("Username="******"Password="******"DB Name =", bold=True),\
        c.green(db_name, bold=True)
    return username, password, db_name
示例#18
0
def add_pg_user():
    print(green("Adding Postgres user - " + settings.DB_USER))
    #print(postgres.user_exists(DB_USER)) # doesn't check correctly
    if not postgres.user_exists(settings.DB_USER):
        postgres.create_user(settings.DB_USER, password=settings.DB_PASSWORD, encrypted_password=True)
示例#19
0
def postgresql_user_create():
    if not postgres.user_exists(DATABASE_OWNER):
        postgres.create_user(DATABASE_OWNER, DATABASE_OWNER_PASSWORD)
示例#20
0
def setup_postgres_user():
    """Set up the postgresql instance."""
    create_user("wsgi", "")
示例#21
0
def setup_postgres_user():
    """Set up the postgresql instance."""
    create_user('wsgi', '')
示例#22
0
def setup_postgres_user():
    """Set up the postgresql instance."""
    create_user('wsgi', '')