mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
Merge branch 'pgsql' into develop
Conflicts: salt/modules/postgres.py
This commit is contained in:
commit
aa8b50b92e
@ -16,6 +16,7 @@ Module to provide Postgres compatibility to salt.
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
# Import python libs
|
# Import python libs
|
||||||
|
from datetime import datetime
|
||||||
import pipes
|
import pipes
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -35,23 +36,27 @@ def __virtual__():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def version():
|
def version(user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Return the version of a Postgres server using the output
|
Return the version of a Postgres server.
|
||||||
from the ``psql --version`` cmd.
|
|
||||||
|
|
||||||
CLI Example::
|
CLI Example::
|
||||||
|
|
||||||
salt '*' postgres.version
|
salt '*' postgres.version
|
||||||
'''
|
'''
|
||||||
version_line = __salt__['cmd.run']('psql --version').split("\n")[0]
|
query = 'SELECT setting FROM pg_catalog.pg_settings ' \
|
||||||
name = version_line.split(" ")[1]
|
'WHERE name = \'server_version\''
|
||||||
ver = version_line.split(" ")[2]
|
cmd = _psql_cmd('-c', query, '-t',
|
||||||
return '{0} {1}'.format(name, ver)
|
host=host, user=user, port=port, db=db)
|
||||||
|
ret = __salt__['cmd.run_all'](cmd, runas=runas)
|
||||||
|
|
||||||
def _connection_defaults(user=None, host=None, port=None):
|
for line in ret['stdout'].splitlines():
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
def _connection_defaults(user=None, host=None, port=None, db=None):
|
||||||
'''
|
'''
|
||||||
Returns a tuple of (user, host, port) with config, pillar, or default
|
Returns a tuple of (user, host, port, db) with config, pillar, or default
|
||||||
values assigned to missing values.
|
values assigned to missing values.
|
||||||
'''
|
'''
|
||||||
if not user:
|
if not user:
|
||||||
@ -60,8 +65,11 @@ def _connection_defaults(user=None, host=None, port=None):
|
|||||||
host = __salt__['config.option']('postgres.host')
|
host = __salt__['config.option']('postgres.host')
|
||||||
if not port:
|
if not port:
|
||||||
port = __salt__['config.option']('postgres.port')
|
port = __salt__['config.option']('postgres.port')
|
||||||
|
if not db:
|
||||||
|
db = __salt__['config.option']('postgres.db')
|
||||||
|
|
||||||
|
return (user, host, port, db)
|
||||||
|
|
||||||
return (user, host, port)
|
|
||||||
|
|
||||||
def _psql_cmd(*args, **kwargs):
|
def _psql_cmd(*args, **kwargs):
|
||||||
'''
|
'''
|
||||||
@ -70,16 +78,22 @@ def _psql_cmd(*args, **kwargs):
|
|||||||
Accept optional keyword arguments: user, host and port as well as any
|
Accept optional keyword arguments: user, host and port as well as any
|
||||||
number or positional arguments to be added to the end of command.
|
number or positional arguments to be added to the end of command.
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(kwargs.get('user'),
|
(user, host, port, db) = _connection_defaults(kwargs.get('user'),
|
||||||
kwargs.get('host'),
|
kwargs.get('host'),
|
||||||
kwargs.get('port'))
|
kwargs.get('port'),
|
||||||
cmd = ['psql', '--no-align', '--no-readline', '--no-password']
|
kwargs.get('db'))
|
||||||
|
cmd = [salt.utils.which('psql'),
|
||||||
|
'--no-align',
|
||||||
|
'--no-readline',
|
||||||
|
'--no-password']
|
||||||
if user:
|
if user:
|
||||||
cmd += ['--username', user]
|
cmd += ['--username', user]
|
||||||
if host:
|
if host:
|
||||||
cmd += ['--host', host]
|
cmd += ['--host', host]
|
||||||
if port:
|
if port:
|
||||||
cmd += ['--port', port]
|
cmd += ['--port', port]
|
||||||
|
if db:
|
||||||
|
cmd += ['--dbname', db]
|
||||||
cmd += args
|
cmd += args
|
||||||
cmdstr = ' '.join(map(pipes.quote, cmd))
|
cmdstr = ' '.join(map(pipes.quote, cmd))
|
||||||
return cmdstr
|
return cmdstr
|
||||||
@ -87,41 +101,48 @@ def _psql_cmd(*args, **kwargs):
|
|||||||
|
|
||||||
# Database related actions
|
# Database related actions
|
||||||
|
|
||||||
def db_list(user=None, host=None, port=None, runas=None):
|
def db_list(user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Return a list of databases of a Postgres server using the output
|
Return dictionary with information about databases of a Postgres server.
|
||||||
from the ``psql -l`` query.
|
|
||||||
|
|
||||||
CLI Example::
|
CLI Example::
|
||||||
|
|
||||||
salt '*' postgres.db_list
|
salt '*' postgres.db_list
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
ret = []
|
ret = {}
|
||||||
query = """SELECT datname as "Name", pga.rolname as "Owner", """ \
|
header = ['Name',
|
||||||
"""pg_encoding_to_char(encoding) as "Encoding", datcollate as "Collate", datctype as "Ctype", """ \
|
'Owner',
|
||||||
"""datacl as "Access privileges" FROM pg_database pgd, pg_authid pga WHERE pga.oid = pgd.datdba"""
|
'Encoding',
|
||||||
|
'Collate',
|
||||||
|
'Ctype',
|
||||||
|
'Access privileges']
|
||||||
|
|
||||||
cmd = _psql_cmd('-c', query,
|
query = 'SELECT datname as "Name", pga.rolname as "Owner", ' \
|
||||||
host=host, user=user, port=port)
|
'pg_encoding_to_char(encoding) as "Encoding", ' \
|
||||||
|
'datcollate as "Collate", datctype as "Ctype", ' \
|
||||||
|
'datacl as "Access privileges" FROM pg_database pgd, ' \
|
||||||
|
'pg_authid pga WHERE pga.oid = pgd.datdba'
|
||||||
|
|
||||||
cmdret = __salt__['cmd.run'](cmd, runas=runas)
|
cmd = _psql_cmd('-c', query, '-t',
|
||||||
lines = [x for x in cmdret.splitlines() if len(x.split("|")) == 6]
|
host=host, user=user, port=port, db=db)
|
||||||
if not lines:
|
|
||||||
log.error("no results from postgres.db_list")
|
cmdret = __salt__['cmd.run_all'](cmd, runas=runas)
|
||||||
else:
|
|
||||||
log.debug(lines)
|
if cmdret['retcode'] > 0:
|
||||||
header = [x.strip() for x in lines[0].split("|")]
|
return ret
|
||||||
for line in lines[1:]:
|
|
||||||
line = [x.strip() for x in line.split("|")]
|
for line in cmdret['stdout'].splitlines():
|
||||||
if not line[0] == "":
|
if line.count('|') != 5:
|
||||||
ret.append(list(zip(header[:-1], line[:-1])))
|
log.warning('Unexpected string: {0}'.format(line))
|
||||||
|
continue
|
||||||
|
comps = line.split('|')
|
||||||
|
ret[comps[0]] = dict(zip(header[1:], comps[1:]))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def db_exists(name, user=None, host=None, port=None, runas=None):
|
def db_exists(name, user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Checks if a database exists on the Postgres server.
|
Checks if a database exists on the Postgres server.
|
||||||
|
|
||||||
@ -129,20 +150,16 @@ def db_exists(name, user=None, host=None, port=None, runas=None):
|
|||||||
|
|
||||||
salt '*' postgres.db_exists 'dbname'
|
salt '*' postgres.db_exists 'dbname'
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
databases = db_list(user=user, host=host, port=port, runas=runas)
|
databases = db_list(user=user, host=host, port=port, db=db, runas=runas)
|
||||||
for database in databases:
|
return name in databases
|
||||||
if name == dict(database).get('Name'):
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def db_create(name,
|
def db_create(name,
|
||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
|
db=None,
|
||||||
tablespace=None,
|
tablespace=None,
|
||||||
encoding=None,
|
encoding=None,
|
||||||
locale=None,
|
locale=None,
|
||||||
@ -161,18 +178,6 @@ def db_create(name,
|
|||||||
salt '*' postgres.db_create 'dbname' template=template_postgis
|
salt '*' postgres.db_create 'dbname' template=template_postgis
|
||||||
|
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
# check if db exists
|
|
||||||
if db_exists(name, user, host, port, runas=runas):
|
|
||||||
log.info("DB '{0}' already exists".format(name,))
|
|
||||||
return False
|
|
||||||
|
|
||||||
# check if template exists
|
|
||||||
if template:
|
|
||||||
if not db_exists(template, user, host, port, runas=runas):
|
|
||||||
log.info("template '{0}' does not exist.".format(template, ))
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Base query to create a database
|
# Base query to create a database
|
||||||
query = 'CREATE DATABASE "{0}"'.format(name)
|
query = 'CREATE DATABASE "{0}"'.format(name)
|
||||||
@ -183,9 +188,9 @@ def db_create(name,
|
|||||||
# doesn't get thrown by dashes in the name
|
# doesn't get thrown by dashes in the name
|
||||||
'OWNER': owner and '"{0}"'.format(owner),
|
'OWNER': owner and '"{0}"'.format(owner),
|
||||||
'TEMPLATE': template,
|
'TEMPLATE': template,
|
||||||
'ENCODING': encoding and "'{0}'".format(encoding),
|
'ENCODING': encoding and '\'{0}\''.format(encoding),
|
||||||
'LC_COLLATE': lc_collate and "'{0}'".format(lc_collate),
|
'LC_COLLATE': lc_collate and '\'{0}\''.format(lc_collate),
|
||||||
'LC_CTYPE': lc_ctype and "'{0}'".format(lc_ctype),
|
'LC_CTYPE': lc_ctype and '\'{0}\''.format(lc_ctype),
|
||||||
'TABLESPACE': tablespace,
|
'TABLESPACE': tablespace,
|
||||||
}
|
}
|
||||||
with_chunks = []
|
with_chunks = []
|
||||||
@ -198,18 +203,13 @@ def db_create(name,
|
|||||||
query += ' '.join(with_chunks)
|
query += ' '.join(with_chunks)
|
||||||
|
|
||||||
# Execute the command
|
# Execute the command
|
||||||
cmd = _psql_cmd('-c', query, user=user, host=host, port=port)
|
cmd = _psql_cmd('-c', query, user=user, host=host, port=port, db=db)
|
||||||
__salt__['cmd.run'](cmd, runas=runas)
|
ret = __salt__['cmd.run_all'](cmd, runas=runas)
|
||||||
|
|
||||||
# Check the result
|
return ret['retcode'] == 0
|
||||||
if db_exists(name, user, host, port, runas=runas):
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
log.info("Failed to create DB '{0}'".format(name,))
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def db_remove(name, user=None, host=None, port=None, runas=None):
|
def db_remove(name, user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Removes a databases from the Postgres server.
|
Removes a databases from the Postgres server.
|
||||||
|
|
||||||
@ -217,56 +217,88 @@ def db_remove(name, user=None, host=None, port=None, runas=None):
|
|||||||
|
|
||||||
salt '*' postgres.db_remove 'dbname'
|
salt '*' postgres.db_remove 'dbname'
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
# check if db exists
|
|
||||||
if not db_exists(name, user, host, port, runas=runas):
|
|
||||||
log.info("DB '{0}' does not exist".format(name,))
|
|
||||||
return False
|
|
||||||
|
|
||||||
# db doesnt exist, proceed
|
# db doesnt exist, proceed
|
||||||
query = 'DROP DATABASE {0}'.format(name)
|
query = 'DROP DATABASE {0}'.format(name)
|
||||||
cmd = _psql_cmd('-c', query, user=user, host=host, port=port)
|
cmd = _psql_cmd('-c', query, user=user, host=host, port=port, db=db)
|
||||||
__salt__['cmd.run'](cmd, runas=runas)
|
ret = __salt__['cmd.run_all'](cmd, runas=runas)
|
||||||
if not db_exists(name, user, host, port, runas=runas):
|
return ret['retcode'] == 0
|
||||||
return True
|
|
||||||
else:
|
|
||||||
log.info("Failed to delete DB '{0}'.".format(name, ))
|
|
||||||
return False
|
|
||||||
|
|
||||||
# User related actions
|
# User related actions
|
||||||
|
|
||||||
def user_list(user=None, host=None, port=None, runas=None):
|
def user_list(user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Return a list of users of a Postgres server.
|
Return a dict with information about users of a Postgres server.
|
||||||
|
|
||||||
CLI Example::
|
CLI Example::
|
||||||
|
|
||||||
salt '*' postgres.user_list
|
salt '*' postgres.user_list
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
ret = []
|
ret = {}
|
||||||
query = (
|
header = ['name',
|
||||||
'''SELECT rolname, rolsuper, rolinherit, rolcreaterole, rolcreatedb,
|
'superuser',
|
||||||
rolcatupdate, rolcanlogin, rolreplication, rolconnlimit, rolvaliduntil, rolconfig, oid
|
'inherits privileges',
|
||||||
FROM pg_roles'''
|
'can create roles',
|
||||||
)
|
'can create databases',
|
||||||
cmd = _psql_cmd('-c', query,
|
'can update system catalogs',
|
||||||
host=host, user=user, port=port)
|
'can login',
|
||||||
|
'replication',
|
||||||
|
'connections',
|
||||||
|
'expiry time',
|
||||||
|
'defaults variables']
|
||||||
|
|
||||||
cmdret = __salt__['cmd.run'](cmd, runas=runas)
|
ver = version(user=None,
|
||||||
lines = [x for x in cmdret.splitlines() if len(x.split("|")) == 12]
|
host=None,
|
||||||
log.debug(lines)
|
port=None,
|
||||||
header = [x.strip() for x in lines[0].split("|")]
|
db=None,
|
||||||
for line in lines[1:]:
|
runas=None).split('.')
|
||||||
line = [x.strip() for x in line.split("|")]
|
if len(ver) >= 2 and ver[0] >= 9 and ver[1] >= 1:
|
||||||
if not line[0] == "":
|
query = (
|
||||||
ret.append(list(zip(header[:-1], line[:-1])))
|
'SELECT rolname, rolsuper, rolinherit, rolcreaterole, '
|
||||||
|
'rolcreatedb, rolcatupdate, rolcanlogin, rolreplication, '
|
||||||
|
'rolconnlimit, rolvaliduntil::timestamp(0), rolconfig '
|
||||||
|
'FROM pg_roles'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
query = (
|
||||||
|
'SELECT rolname, rolsuper, rolinherit, rolcreaterole, '
|
||||||
|
'rolcreatedb, rolcatupdate, rolcanlogin, NULL, '
|
||||||
|
'rolconnlimit, rolvaliduntil::timestamp(0), rolconfig '
|
||||||
|
'FROM pg_roles'
|
||||||
|
)
|
||||||
|
cmd = _psql_cmd('-c', query, '-t',
|
||||||
|
host=host, user=user, port=port, db=db)
|
||||||
|
|
||||||
|
cmdret = __salt__['cmd.run_all'](cmd, runas=runas)
|
||||||
|
|
||||||
|
if cmdret['retcode'] > 0:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
for line in cmdret['stdout'].splitlines():
|
||||||
|
comps = line.split('|')
|
||||||
|
# type casting
|
||||||
|
for i in range(1, 8):
|
||||||
|
if comps[i] == 't':
|
||||||
|
comps[i] = True
|
||||||
|
elif comps[i] == 'f':
|
||||||
|
comps[i] = False
|
||||||
|
else:
|
||||||
|
comps[i] = None
|
||||||
|
comps[8] = int(comps[8])
|
||||||
|
if comps[9]:
|
||||||
|
comps[9] = datetime.strptime(comps[9], '%Y-%m-%d %H:%M:%S')
|
||||||
|
else:
|
||||||
|
comps[9] = None
|
||||||
|
if not comps[10]:
|
||||||
|
comps[10] = None
|
||||||
|
ret[comps[0]] = dict(zip(header[1:], comps[1:]))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def user_exists(name, user=None, host=None, port=None, runas=None):
|
|
||||||
|
def user_exists(name, user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Checks if a user exists on the Postgres server.
|
Checks if a user exists on the Postgres server.
|
||||||
|
|
||||||
@ -274,29 +306,20 @@ def user_exists(name, user=None, host=None, port=None, runas=None):
|
|||||||
|
|
||||||
salt '*' postgres.user_exists 'username'
|
salt '*' postgres.user_exists 'username'
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
query = (
|
return name in user_list(user=user,
|
||||||
"SELECT true "
|
host=host,
|
||||||
"FROM pg_roles "
|
port=port,
|
||||||
"WHERE EXISTS "
|
db=db,
|
||||||
"(SELECT rolname WHERE rolname='{role}')".format(role=name)
|
runas=runas)
|
||||||
)
|
|
||||||
cmd = _psql_cmd('-c', query, host=host, user=user, port=port)
|
|
||||||
cmdret = __salt__['cmd.run'](cmd, runas=runas)
|
|
||||||
log.debug(cmdret.splitlines())
|
|
||||||
try:
|
|
||||||
val = cmdret.splitlines()[1]
|
|
||||||
except IndexError:
|
|
||||||
log.error("Invalid PostgreSQL result: '%s'", cmdret)
|
|
||||||
return False
|
|
||||||
return True if val.strip() == 't' else False
|
|
||||||
|
|
||||||
def _role_create(name,
|
def _role_create(name,
|
||||||
login,
|
login,
|
||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
|
db=None,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
encrypted=False,
|
encrypted=False,
|
||||||
@ -309,7 +332,6 @@ def _role_create(name,
|
|||||||
Creates a Postgres role. Users and Groups are both roles in postgres.
|
Creates a Postgres role. Users and Groups are both roles in postgres.
|
||||||
However, users can login, groups cannot.
|
However, users can login, groups cannot.
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
if login:
|
if login:
|
||||||
create_type = 'USER'
|
create_type = 'USER'
|
||||||
@ -317,37 +339,39 @@ def _role_create(name,
|
|||||||
create_type = 'ROLE'
|
create_type = 'ROLE'
|
||||||
|
|
||||||
# check if role exists
|
# check if role exists
|
||||||
if user_exists(name, user, host, port, runas=runas):
|
if user_exists(name, user, host, port, db, runas=runas):
|
||||||
log.info("{0} '{1}' already exists".format(create_type, name,))
|
log.info('{0} \'{1}\' already exists'.format(create_type, name,))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
sub_cmd = 'CREATE {0} "{1}" WITH'.format(create_type, name, )
|
sub_cmd = 'CREATE {0} "{1}" WITH'.format(create_type, name, )
|
||||||
if password:
|
if password:
|
||||||
if encrypted:
|
if encrypted:
|
||||||
sub_cmd = "{0} ENCRYPTED".format(sub_cmd, )
|
sub_cmd = '{0} ENCRYPTED'.format(sub_cmd, )
|
||||||
escaped_password = password.replace("'", "''")
|
escaped_password = password.replace('\'', '\'\'')
|
||||||
sub_cmd = "{0} PASSWORD '{1}'".format(sub_cmd, escaped_password)
|
sub_cmd = '{0} PASSWORD \'{1}\''.format(sub_cmd, escaped_password)
|
||||||
if createdb:
|
if createdb:
|
||||||
sub_cmd = "{0} CREATEDB".format(sub_cmd, )
|
sub_cmd = '{0} CREATEDB'.format(sub_cmd, )
|
||||||
if createuser:
|
if createuser:
|
||||||
sub_cmd = "{0} CREATEUSER".format(sub_cmd, )
|
sub_cmd = '{0} CREATEUSER'.format(sub_cmd, )
|
||||||
if superuser:
|
if superuser:
|
||||||
sub_cmd = "{0} SUPERUSER".format(sub_cmd, )
|
sub_cmd = '{0} SUPERUSER'.format(sub_cmd, )
|
||||||
if replication:
|
if replication:
|
||||||
sub_cmd = "{0} REPLICATION".format(sub_cmd, )
|
sub_cmd = '{0} REPLICATION'.format(sub_cmd, )
|
||||||
if groups:
|
if groups:
|
||||||
sub_cmd = "{0} IN GROUP {1}".format(sub_cmd, groups, )
|
sub_cmd = '{0} IN GROUP {1}'.format(sub_cmd, groups, )
|
||||||
|
|
||||||
if sub_cmd.endswith("WITH"):
|
if sub_cmd.endswith('WITH'):
|
||||||
sub_cmd = sub_cmd.replace(" WITH", "")
|
sub_cmd = sub_cmd.replace(' WITH', '')
|
||||||
|
|
||||||
cmd = _psql_cmd('-c', sub_cmd, host=host, user=user, port=port)
|
cmd = _psql_cmd('-c', sub_cmd, host=host, user=user, port=port, db=db)
|
||||||
return __salt__['cmd.run'](cmd, runas=runas)
|
return __salt__['cmd.run'](cmd, runas=runas)
|
||||||
|
|
||||||
|
|
||||||
def user_create(username,
|
def user_create(username,
|
||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
|
db=None,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
encrypted=False,
|
encrypted=False,
|
||||||
@ -368,6 +392,7 @@ def user_create(username,
|
|||||||
user,
|
user,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
|
db,
|
||||||
createdb,
|
createdb,
|
||||||
createuser,
|
createuser,
|
||||||
encrypted,
|
encrypted,
|
||||||
@ -377,53 +402,56 @@ def user_create(username,
|
|||||||
groups,
|
groups,
|
||||||
runas)
|
runas)
|
||||||
|
|
||||||
|
|
||||||
def _role_update(name,
|
def _role_update(name,
|
||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
createdb=False,
|
db=None,
|
||||||
createuser=False,
|
createdb=False,
|
||||||
encrypted=False,
|
createuser=False,
|
||||||
replication=False,
|
encrypted=False,
|
||||||
password=None,
|
replication=False,
|
||||||
groups=None,
|
password=None,
|
||||||
runas=None):
|
groups=None,
|
||||||
|
runas=None):
|
||||||
'''
|
'''
|
||||||
Updates a postgres role.
|
Updates a postgres role.
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
# check if user exists
|
# check if user exists
|
||||||
if not user_exists(name, user, host, port, runas=runas):
|
if not user_exists(name, user, host, port, db, runas=runas):
|
||||||
log.info("User '{0}' does not exist".format(name,))
|
log.info('User \'{0}\' does not exist'.format(name,))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
sub_cmd = "ALTER ROLE {0} WITH".format(name, )
|
sub_cmd = 'ALTER ROLE {0} WITH'.format(name, )
|
||||||
if password:
|
if password:
|
||||||
sub_cmd = "{0} PASSWORD '{1}'".format(sub_cmd, password)
|
sub_cmd = '{0} PASSWORD \'{1}\''.format(sub_cmd, password)
|
||||||
if createdb:
|
if createdb:
|
||||||
sub_cmd = "{0} CREATEDB".format(sub_cmd, )
|
sub_cmd = '{0} CREATEDB'.format(sub_cmd, )
|
||||||
if createuser:
|
if createuser:
|
||||||
sub_cmd = "{0} CREATEUSER".format(sub_cmd, )
|
sub_cmd = '{0} CREATEUSER'.format(sub_cmd, )
|
||||||
if encrypted:
|
if encrypted:
|
||||||
sub_cmd = "{0} ENCRYPTED".format(sub_cmd, )
|
sub_cmd = '{0} ENCRYPTED'.format(sub_cmd, )
|
||||||
if encrypted:
|
if encrypted:
|
||||||
sub_cmd = "{0} REPLICATION".format(sub_cmd, )
|
sub_cmd = '{0} REPLICATION'.format(sub_cmd, )
|
||||||
|
|
||||||
if sub_cmd.endswith("WITH"):
|
if sub_cmd.endswith('WITH'):
|
||||||
sub_cmd = sub_cmd.replace(" WITH", "")
|
sub_cmd = sub_cmd.replace(' WITH', '')
|
||||||
|
|
||||||
if groups:
|
if groups:
|
||||||
for group in groups.split(','):
|
for group in groups.split(','):
|
||||||
sub_cmd = "{0}; GRANT {1} TO {2}".format(sub_cmd, group, name)
|
sub_cmd = '{0}; GRANT {1} TO {2}'.format(sub_cmd, group, name)
|
||||||
|
|
||||||
cmd = _psql_cmd('-c', sub_cmd, host=host, user=user, port=port)
|
cmd = _psql_cmd('-c', sub_cmd, host=host, user=user, port=port, db=db)
|
||||||
return __salt__['cmd.run'](cmd, runas=runas)
|
return __salt__['cmd.run'](cmd, runas=runas)
|
||||||
|
|
||||||
|
|
||||||
def user_update(username,
|
def user_update(username,
|
||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
|
db=None,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
encrypted=False,
|
encrypted=False,
|
||||||
@ -442,6 +470,7 @@ def user_update(username,
|
|||||||
user,
|
user,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
|
db,
|
||||||
createdb,
|
createdb,
|
||||||
createuser,
|
createuser,
|
||||||
encrypted,
|
encrypted,
|
||||||
@ -450,27 +479,33 @@ def user_update(username,
|
|||||||
groups,
|
groups,
|
||||||
runas)
|
runas)
|
||||||
|
|
||||||
def _role_remove(name, user=None, host=None, port=None, runas=None):
|
|
||||||
|
def _role_remove(name, user=None, host=None, port=None, db=None, runas=None):
|
||||||
'''
|
'''
|
||||||
Removes a role from the Postgres Server
|
Removes a role from the Postgres Server
|
||||||
'''
|
'''
|
||||||
(user, host, port) = _connection_defaults(user, host, port)
|
|
||||||
|
|
||||||
# check if user exists
|
# check if user exists
|
||||||
if not user_exists(name, user, host, port, runas=runas):
|
if not user_exists(name, user, host, port, db, runas=runas):
|
||||||
log.info("User '{0}' does not exist".format(name,))
|
log.info('User \'{0}\' does not exist'.format(name,))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# user exists, proceed
|
# user exists, proceed
|
||||||
sub_cmd = 'DROP ROLE {0}'.format(name)
|
sub_cmd = 'DROP ROLE {0}'.format(name)
|
||||||
cmd = _psql_cmd('-c', sub_cmd, host=host, user=user, port=port)
|
cmd = _psql_cmd('-c', sub_cmd, host=host, user=user, port=port, db=db)
|
||||||
__salt__['cmd.run'](cmd, runas=runas)
|
__salt__['cmd.run'](cmd, runas=runas)
|
||||||
if not user_exists(name, user, host, port, runas=runas):
|
if not user_exists(name, user, host, port, db, runas=runas):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
log.info("Failed to delete user '{0}'.".format(name, ))
|
log.info('Failed to delete user \'{0}\'.'.format(name, ))
|
||||||
|
|
||||||
def user_remove(username, user=None, host=None, port=None, runas=None):
|
|
||||||
|
def user_remove(username,
|
||||||
|
user=None,
|
||||||
|
host=None,
|
||||||
|
port=None,
|
||||||
|
db=None,
|
||||||
|
runas=None):
|
||||||
'''
|
'''
|
||||||
Removes a user from the Postgres server.
|
Removes a user from the Postgres server.
|
||||||
|
|
||||||
@ -478,7 +513,8 @@ def user_remove(username, user=None, host=None, port=None, runas=None):
|
|||||||
|
|
||||||
salt '*' postgres.user_remove 'username'
|
salt '*' postgres.user_remove 'username'
|
||||||
'''
|
'''
|
||||||
return _role_remove(username, user, host, port, runas)
|
return _role_remove(username, user, host, port, db, runas)
|
||||||
|
|
||||||
|
|
||||||
# Group related actions
|
# Group related actions
|
||||||
|
|
||||||
@ -486,6 +522,7 @@ def group_create(groupname,
|
|||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
|
db=None,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
encrypted=False,
|
encrypted=False,
|
||||||
@ -507,6 +544,7 @@ def group_create(groupname,
|
|||||||
user,
|
user,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
|
db,
|
||||||
createdb,
|
createdb,
|
||||||
createuser,
|
createuser,
|
||||||
encrypted,
|
encrypted,
|
||||||
@ -516,10 +554,12 @@ def group_create(groupname,
|
|||||||
groups,
|
groups,
|
||||||
runas)
|
runas)
|
||||||
|
|
||||||
|
|
||||||
def group_update(groupname,
|
def group_update(groupname,
|
||||||
user=None,
|
user=None,
|
||||||
host=None,
|
host=None,
|
||||||
port=None,
|
port=None,
|
||||||
|
db=None,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
encrypted=False,
|
encrypted=False,
|
||||||
@ -538,6 +578,7 @@ def group_update(groupname,
|
|||||||
user,
|
user,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
|
db,
|
||||||
createdb,
|
createdb,
|
||||||
createuser,
|
createuser,
|
||||||
encrypted,
|
encrypted,
|
||||||
@ -546,7 +587,13 @@ def group_update(groupname,
|
|||||||
groups,
|
groups,
|
||||||
runas)
|
runas)
|
||||||
|
|
||||||
def group_remove(groupname, user=None, host=None, port=None, runas=None):
|
|
||||||
|
def group_remove(groupname,
|
||||||
|
user=None,
|
||||||
|
host=None,
|
||||||
|
port=None,
|
||||||
|
db=None,
|
||||||
|
runas=None):
|
||||||
'''
|
'''
|
||||||
Removes a group from the Postgres server.
|
Removes a group from the Postgres server.
|
||||||
|
|
||||||
@ -554,4 +601,4 @@ def group_remove(groupname, user=None, host=None, port=None, runas=None):
|
|||||||
|
|
||||||
salt '*' postgres.group_remove 'groupname'
|
salt '*' postgres.group_remove 'groupname'
|
||||||
'''
|
'''
|
||||||
return _role_remove(groupname, user, host, port, runas)
|
return _role_remove(groupname, user, host, port, db, runas)
|
||||||
|
@ -67,14 +67,14 @@ def present(name,
|
|||||||
ret['comment'] = 'Database {0} is set to be created'.format(name)
|
ret['comment'] = 'Database {0} is set to be created'.format(name)
|
||||||
return ret
|
return ret
|
||||||
if __salt__['postgres.db_create'](name,
|
if __salt__['postgres.db_create'](name,
|
||||||
tablespace=tablespace,
|
tablespace=tablespace,
|
||||||
encoding=encoding,
|
encoding=encoding,
|
||||||
locale=locale,
|
locale=locale,
|
||||||
lc_collate=lc_collate,
|
lc_collate=lc_collate,
|
||||||
lc_ctype=lc_ctype,
|
lc_ctype=lc_ctype,
|
||||||
owner=owner,
|
owner=owner,
|
||||||
template=template,
|
template=template,
|
||||||
runas=runas):
|
runas=runas):
|
||||||
ret['comment'] = 'The database {0} has been created'.format(name)
|
ret['comment'] = 'The database {0} has been created'.format(name)
|
||||||
ret['changes'][name] = 'Present'
|
ret['changes'][name] = 'Present'
|
||||||
else:
|
else:
|
||||||
@ -111,7 +111,6 @@ def absent(name, runas=None):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
# fallback
|
# fallback
|
||||||
ret['comment'] = (
|
ret['comment'] = 'Database {0} is not present, so it cannot ' \
|
||||||
'Database {0} is not present, so it cannot be removed'
|
'be removed'.format(name)
|
||||||
).format(name)
|
|
||||||
return ret
|
return ret
|
||||||
|
@ -10,6 +10,7 @@ The postgres_group module is used to create and manage Postgres groups.
|
|||||||
postgres_group.present
|
postgres_group.present
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def present(name,
|
def present(name,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
@ -107,6 +108,7 @@ def absent(name, runas=None):
|
|||||||
ret['changes'][name] = 'Absent'
|
ret['changes'][name] = 'Absent'
|
||||||
return ret
|
return ret
|
||||||
else:
|
else:
|
||||||
ret['comment'] = 'Group {0} is not present, so it cannot be removed'.format(name)
|
ret['comment'] = 'Group {0} is not present, so it cannot ' \
|
||||||
|
'be removed'.format(name)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
@ -10,6 +10,7 @@ The postgres_users module is used to create and manage Postgres users.
|
|||||||
postgres_user.present
|
postgres_user.present
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def present(name,
|
def present(name,
|
||||||
createdb=False,
|
createdb=False,
|
||||||
createuser=False,
|
createuser=False,
|
||||||
@ -107,6 +108,7 @@ def absent(name, runas=None):
|
|||||||
ret['changes'][name] = 'Absent'
|
ret['changes'][name] = 'Absent'
|
||||||
return ret
|
return ret
|
||||||
else:
|
else:
|
||||||
ret['comment'] = 'User {0} is not present, so it cannot be removed'.format(name)
|
ret['comment'] = 'User {0} is not present, so it cannot ' \
|
||||||
|
'be removed'.format(name)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
Loading…
Reference in New Issue
Block a user