Пример #1
0
def database(name,
             owner,
             template='template0',
             encoding='UTF8',
             locale='en_US.UTF-8'):
    """
    Require a PostgreSQL database.

    ::

        from fabtools import require

        require.postgres.database('myapp', owner='dbuser')

    """
    if not database_exists(name):

        if locale not in run('locale -a').split():
            require_locale(locale)
            restarted(_service_name())

        create_database(name,
                        owner,
                        template=template,
                        encoding=encoding,
                        locale=locale)
Пример #2
0
def database(name, owner, template='template0', encoding='UTF8',
             locale='en_US.UTF-8', allow_restart=False):
    """
    Require a PostgreSQL database.

    ::

        from fabtools import require

        require.postgres.database('myapp', owner='dbuser')
    """

    locale_transform = lambda l: l.strip().lower().replace('-', '')

    if not database_exists(name):
        locales = map(
            locale_transform,
            run('locale -a').split()
        )
        if locale_transform(locale) not in locales:
            if not allow_restart:
                abort(
                    'New locale "{}" must be installed and '
                    'postgres must be restarted after that'.format(
                        locale
                    )
                )
            require_locale(locale)
            restarted(_service_name())

        create_database(name, owner, template=template, encoding=encoding,
                        locale=locale)
Пример #3
0
def database(name,
             owner,
             template='template0',
             encoding='UTF8',
             locale='en_US.UTF-8'):
    """
    Require a PostgreSQL database.

    ::

        from fabtools import require

        require.postgres.database('myapp', owner='dbuser')

    """
    if not database_exists(name):

        with watch('/etc/locale.gen') as locales:
            require_locale(locale)
        if locales.changed:
            restarted(_service_name())

        create_database(name,
                        owner,
                        template=template,
                        encoding=encoding,
                        locale=locale)
Пример #4
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)
Пример #5
0
 def restore(self, filename):
     if not confirm('Are you sure to delete database?', default=False):
         return
     with warn_only():
         sudo('sudo -u postgres dropdb {}'.format(self.database))
     funcs.create_database(
         self.database, self.user, locale='ja_JP.utf8')
     sudo('bzip2 -d -c {} | sudo -u postgres psql -d {}'.format(
         filename, self.database))
Пример #6
0
def postgres_create():
    """
    Create postgres objects, including owner, databases, and schemas.
    """
    if not pg_user_exists(env.db_user):
        create_pg_user(env.db_user, password=env.db_password)

    if not database_exists(env.db_name):
        create_database(env.db_name, owner=env.db_user)
Пример #7
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')
Пример #8
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"])
Пример #9
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))
Пример #10
0
def database(name, owner, template="template0", encoding="UTF8", locale="en_US.UTF-8"):
    """
    Require a PostgreSQL database.

    ::

        from fabtools import require

        require.postgres.database('myapp', owner='dbuser')

    """
    if not database_exists(name):

        if locale not in run("locale -a").split():
            require_locale(locale)
            restarted(_service_name())

        create_database(name, owner, template=template, encoding=encoding, locale=locale)
Пример #11
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))
Пример #12
0
def database(name, owner, template='template0', encoding='UTF8',
             locale='en_US.UTF-8'):
    """
    Require a PostgreSQL database.

    ::

        from fabtools import require

        require.postgres.database('myapp', owner='dbuser')

    """
    if not database_exists(name):

        with watch('/etc/locale.gen') as locales:
            require_locale(locale)
        if locales.changed:
            restarted(_service_name())

        create_database(name, owner, template=template, encoding=encoding,
                        locale=locale)
Пример #13
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