|
|
@ -6,13 +6,12 @@ from ..commands import Command |
|
|
|
|
|
|
|
|
|
|
|
__all__ = ( |
|
|
|
__all__ = ( |
|
|
|
"PGSQL_MAPPINGS", |
|
|
|
"PGSQL_MAPPINGS", |
|
|
|
"pg_create_database", |
|
|
|
"pgsql_create", |
|
|
|
"pg_create_user", |
|
|
|
"pgsql_drop", |
|
|
|
"pg_database_exists", |
|
|
|
"pgsql_dump", |
|
|
|
"pg_drop_database", |
|
|
|
"pgsql_exec", |
|
|
|
"pg_drop_user", |
|
|
|
"pgsql_exists", |
|
|
|
"pg_dump_database", |
|
|
|
"pgsql_user", |
|
|
|
"psql", |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Functions |
|
|
|
# Functions |
|
|
@ -52,11 +51,11 @@ def _get_pgsql_command(name, host="localhost", password=None, port=5432, user="p |
|
|
|
return a |
|
|
|
return a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pg_create_database(name, admin_pass=None, admin_user="postgres", host="localhost", owner=None, port=5432, |
|
|
|
def pgsql_create(database, admin_pass=None, admin_user="postgres", host="localhost", owner=None, port=5432, template=None, |
|
|
|
template=None, **kwargs): |
|
|
|
**kwargs): |
|
|
|
"""Create a PostgreSQL database. |
|
|
|
"""Create a PostgreSQL database. |
|
|
|
|
|
|
|
|
|
|
|
- name (str): The database name. |
|
|
|
- database (str): The database name. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- host (str): The database host name or IP address. |
|
|
@ -77,15 +76,15 @@ def pg_create_database(name, admin_pass=None, admin_user="postgres", host="local |
|
|
|
if template is not None: |
|
|
|
if template is not None: |
|
|
|
base.append("--template=%s" % template) |
|
|
|
base.append("--template=%s" % template) |
|
|
|
|
|
|
|
|
|
|
|
base.append(name) |
|
|
|
base.append(database) |
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pg_create_user(name, admin_pass=None, admin_user="postgres", host="localhost", password=None, port=5432, **kwargs): |
|
|
|
def pgsql_drop(database, admin_pass=None, admin_user="postgres", host="localhost", port=5432, **kwargs): |
|
|
|
"""Create a PostgreSQL user. |
|
|
|
"""Remove a PostgreSQL database. |
|
|
|
|
|
|
|
|
|
|
|
- name (str): The user name. |
|
|
|
- database (str): The database name. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- host (str): The database host name or IP address. |
|
|
@ -96,44 +95,42 @@ def pg_create_user(name, admin_pass=None, admin_user="postgres", host="localhost |
|
|
|
kwargs['sudo'] = False |
|
|
|
kwargs['sudo'] = False |
|
|
|
|
|
|
|
|
|
|
|
# Assemble the command. |
|
|
|
# Assemble the command. |
|
|
|
base = _get_pgsql_command("createuser", host=host, password=admin_pass, port=port) |
|
|
|
base = _get_pgsql_command("dropdb", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
base.append("-DRS") |
|
|
|
base.append(database) |
|
|
|
base.append(name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if password is not None: |
|
|
|
|
|
|
|
base.append("&& psql -h %s -U %s" % (host, admin_user)) |
|
|
|
|
|
|
|
base.append("-c \"ALTER USER %s WITH ENCRYPTED PASSWORD '%s';\"" % (name, password)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pg_database_exists(name, admin_pass=None, admin_user="postgres", host="localhost", port=5432, **kwargs): |
|
|
|
def pgsql_dump(database, admin_pass=None, admin_user="postgres", file_name=None, host="localhost", port=5432, **kwargs): |
|
|
|
"""Determine if a Postgres database exists. |
|
|
|
"""Export a Postgres database. |
|
|
|
|
|
|
|
|
|
|
|
- name (str): The database name. |
|
|
|
- database (str): The database name. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
|
|
|
|
- file_name (str): The name/path of the export file. Defaults the database name plus ``.sql``. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- owner (str): The owner (user/role name) of the new database. |
|
|
|
|
|
|
|
- port (int): The port number of the Postgres service running on the host. |
|
|
|
- port (int): The port number of the Postgres service running on the host. |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
# Postgres commands always run without sudo because the -U may be provided. However, sudo may be required for |
|
|
|
_file_name = file_name or "%s.sql" % database |
|
|
|
# file writing. |
|
|
|
|
|
|
|
# kwargs['sudo'] = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
kwargs.setdefault("register", "%s_db_exists" % name) |
|
|
|
# Postgres commands always run without sudo because the -U may be provided. |
|
|
|
|
|
|
|
# kwargs['sudo'] = False |
|
|
|
|
|
|
|
|
|
|
|
base = _get_pgsql_command("psql", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
# Assemble the command. |
|
|
|
base.append(r"-lqt | cut -d \| -f 1 | grep -qw %s" % name) |
|
|
|
base = _get_pgsql_command("pg_dump", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
|
|
|
|
base.append("--column-inserts") |
|
|
|
|
|
|
|
base.append("--file=%s" % _file_name) |
|
|
|
|
|
|
|
base.append(database) |
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pg_drop_database(name, admin_pass=None, admin_user="postgres", host="localhost", port=5432, **kwargs): |
|
|
|
def pgsql_exec(sql, database="template1", host="localhost", password=None, port=5432, user="postgres", **kwargs): |
|
|
|
"""Remove a PostgreSQL database. |
|
|
|
"""Execute a psql command. |
|
|
|
|
|
|
|
|
|
|
|
- name (str): The database name. |
|
|
|
- sql (str): The SQL to be executed. |
|
|
|
|
|
|
|
- database (str): The database name. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- host (str): The database host name or IP address. |
|
|
@ -144,94 +141,86 @@ def pg_drop_database(name, admin_pass=None, admin_user="postgres", host="localho |
|
|
|
kwargs['sudo'] = False |
|
|
|
kwargs['sudo'] = False |
|
|
|
|
|
|
|
|
|
|
|
# Assemble the command. |
|
|
|
# Assemble the command. |
|
|
|
base = _get_pgsql_command("dropdb", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
base = _get_pgsql_command("psql", host=host, password=password, port=port, user=user) |
|
|
|
base.append(name) |
|
|
|
base.append("--dbname=%s" % database) |
|
|
|
|
|
|
|
base.append('-c "%s"' % sql) |
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pg_drop_user(name, admin_pass=None, admin_user="postgres", host="localhost", port=5432, **kwargs): |
|
|
|
def pgsql_exists(database, admin_pass=None, admin_user="postgres", host="localhost", port=5432, **kwargs): |
|
|
|
"""Remove a Postgres user. |
|
|
|
"""Determine if a Postgres database exists. |
|
|
|
|
|
|
|
|
|
|
|
- name (str): The user name. |
|
|
|
- database (str): The database name. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
|
|
|
|
- owner (str): The owner (user/role name) of the new database. |
|
|
|
- port (int): The port number of the Postgres service running on the host. |
|
|
|
- port (int): The port number of the Postgres service running on the host. |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
# Postgres commands always run without sudo because the -U may be provided. |
|
|
|
# Postgres commands always run without sudo because the -U may be provided. |
|
|
|
kwargs['sudo'] = False |
|
|
|
kwargs['sudo'] = False |
|
|
|
|
|
|
|
kwargs.setdefault("register", "pgsql_db_exists") |
|
|
|
|
|
|
|
|
|
|
|
# Assemble the command. |
|
|
|
base = _get_pgsql_command("psql", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
base = _get_pgsql_command("dropuser", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
base.append(r"-lqt | cut -d \| -f 1 | grep -qw %s" % database) |
|
|
|
base.append(name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pg_dump_database(name, admin_pass=None, admin_user="postgres", file_name=None, host="localhost", port=5432, |
|
|
|
def pgsql_user(name, admin_pass=None, admin_user="postgres", host="localhost", op="create", password=None, port=5432, **kwargs): |
|
|
|
**kwargs): |
|
|
|
"""Work with a PostgreSQL user. |
|
|
|
"""Export a Postgres database. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name (str): The database name. |
|
|
|
- name (str): The user name. |
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- host (str): The host name. |
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
- op (str): The operation to perform: ``create``, ``drop``, ``exists``. |
|
|
|
- file_name (str): The name/path of the export file. Defaults the database name plus ``.sql``. |
|
|
|
- passwd (str): The password for a new user. |
|
|
|
- host (str): The database host name or IP address. |
|
|
|
- password (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
- port (int): The port number of the Postgres service running on the host. |
|
|
|
- port (int): The TCP port number. |
|
|
|
|
|
|
|
- user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
_file_name = file_name or "%s.sql" % name |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Postgres commands always run without sudo because the -U may be provided. |
|
|
|
# Postgres commands always run without sudo because the -U may be provided. |
|
|
|
kwargs['sudo'] = False |
|
|
|
kwargs['sudo'] = False |
|
|
|
|
|
|
|
|
|
|
|
# Assemble the command. |
|
|
|
if op == "create": |
|
|
|
base = _get_pgsql_command("pg_dump", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
kwargs.setdefault("comment", "create %s postgres user" % name) |
|
|
|
base.append("--column-inserts") |
|
|
|
# Assemble the command. |
|
|
|
base.append("--file=%s" % _file_name) |
|
|
|
base = _get_pgsql_command("createuser", host=host, password=admin_pass, port=port) |
|
|
|
base.append(name) |
|
|
|
base.append("-DRS") |
|
|
|
|
|
|
|
base.append(name) |
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
if password is not None: |
|
|
|
|
|
|
|
base.append("&& psql -h %s -U %s" % (host, admin_user)) |
|
|
|
|
|
|
|
base.append("-c \"ALTER USER %s WITH ENCRYPTED PASSWORD '%s';\"" % (name, password)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
elif op == "drop": |
|
|
|
|
|
|
|
kwargs.setdefault("comment", "drop %s postgres user" % name) |
|
|
|
|
|
|
|
base = _get_pgsql_command("dropuser", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
|
|
|
|
base.append(name) |
|
|
|
|
|
|
|
|
|
|
|
def psql(sql, database="template1", host="localhost", password=None, port=5432, user="postgres", **kwargs): |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
"""Execute a psql command. |
|
|
|
elif op == "exists": |
|
|
|
|
|
|
|
kwargs.setdefault("comment", "determine if %s postgres user exits" % name) |
|
|
|
- sql (str): The SQL to be executed. |
|
|
|
kwargs.setdefault("register", "pgsql_use_exists") |
|
|
|
- database (str): The database name. |
|
|
|
|
|
|
|
- admin_pass (str): The password for the user with sufficient access privileges to execute the command. |
|
|
|
|
|
|
|
- admin_user (str): The name of the user with sufficient access privileges to execute the command. |
|
|
|
|
|
|
|
- host (str): The database host name or IP address. |
|
|
|
|
|
|
|
- port (int): The port number of the Postgres service running on the host. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
base = _get_pgsql_command("psql", host=host, password=admin_pass, port=port, user=admin_user) |
|
|
|
# Postgres commands always run without sudo because the -U may be provided. |
|
|
|
|
|
|
|
kwargs['sudo'] = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Assemble the command. |
|
|
|
sql = "SELECT 1 FROM pgsql_roles WHERE rolname='%s'" % name |
|
|
|
base = _get_pgsql_command("psql", host=host, password=password, port=port, user=user) |
|
|
|
base.append('-c "%s"' % sql) |
|
|
|
base.append("--dbname=%s" % database) |
|
|
|
|
|
|
|
base.append('-c "%s"' % sql) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
return Command(" ".join(base), **kwargs) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
raise NameError("Unrecognized or unsupported Postgres user operation: %s" % op) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PGSQL_MAPPINGS = { |
|
|
|
PGSQL_MAPPINGS = { |
|
|
|
'pg.client': psql, |
|
|
|
'pgsql.create': pgsql_create, |
|
|
|
'pg.createdatabase': pg_create_database, |
|
|
|
'pgsql.drop': pgsql_drop, |
|
|
|
'pg.createdb': pg_create_database, |
|
|
|
'pgsql.dump': pgsql_dump, |
|
|
|
'pg.createuser': pg_create_user, |
|
|
|
'pgsql.exists': pgsql_exists, |
|
|
|
'pg.database': pg_create_database, |
|
|
|
'pgsql.sql': pgsql_exec, |
|
|
|
'pg.database_exists': pg_database_exists, |
|
|
|
'pgsql.user': pgsql_user, |
|
|
|
'pg.db': pg_create_database, |
|
|
|
|
|
|
|
'pg.dropdatabase': pg_drop_database, |
|
|
|
|
|
|
|
'pg.dropdb': pg_drop_database, |
|
|
|
|
|
|
|
'pg.dropuser': pg_drop_user, |
|
|
|
|
|
|
|
'pg.dump': pg_dump_database, |
|
|
|
|
|
|
|
'pg.dumpdb': pg_dump_database, |
|
|
|
|
|
|
|
'pg.exists': pg_database_exists, |
|
|
|
|
|
|
|
'pg.user': pg_create_user, |
|
|
|
|
|
|
|
'psql': psql, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|