2012-07-26 15:02:28 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-07-26 15:02:28 +00:00
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import psycopg2
|
|
|
|
except ImportError:
|
|
|
|
postgresqldb_found = False
|
|
|
|
else:
|
|
|
|
postgresqldb_found = True
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# PostgreSQL module specific support methods.
|
|
|
|
#
|
|
|
|
|
2012-08-22 17:20:51 +00:00
|
|
|
def set_owner(cursor, db, owner):
|
|
|
|
query = "ALTER DATABASE %s OWNER TO %s" % (db, owner)
|
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def db_owned_by(cursor, db, user):
|
2012-08-22 18:34:24 +00:00
|
|
|
query = """SELECT * FROM pg_database JOIN pg_user ON datdba = usesysid
|
2012-08-22 17:20:51 +00:00
|
|
|
WHERE usename = %(user)s and datname = %(db)s"""
|
|
|
|
cursor.execute(query, {'db':db, 'user':user})
|
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
2012-07-26 15:02:28 +00:00
|
|
|
def db_exists(cursor, db):
|
|
|
|
query = "SELECT * FROM pg_database WHERE datname=%(db)s"
|
|
|
|
cursor.execute(query, {'db': db})
|
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
|
|
|
def db_delete(cursor, db):
|
2012-08-22 17:20:51 +00:00
|
|
|
if db_exists(cursor, db):
|
|
|
|
query = "DROP DATABASE %s" % db
|
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2012-07-26 15:02:28 +00:00
|
|
|
|
2012-07-31 09:56:29 +00:00
|
|
|
def db_create(cursor, db, owner, template, encoding):
|
2012-08-22 17:20:51 +00:00
|
|
|
if not db_exists(cursor, db):
|
|
|
|
if owner:
|
|
|
|
owner = " OWNER %s" % owner
|
|
|
|
if template:
|
|
|
|
template = " TEMPLATE %s" % template
|
|
|
|
if encoding:
|
|
|
|
encoding = " ENCODING '%s'" % encoding
|
|
|
|
query = "CREATE DATABASE %s%s%s%s" % (db, owner, template, encoding)
|
|
|
|
cursor.execute(query)
|
|
|
|
return True
|
|
|
|
elif owner and not db_owned_by(cursor, db, owner):
|
|
|
|
return set_owner(cursor, db, owner)
|
|
|
|
else:
|
|
|
|
return False
|
2012-07-26 15:02:28 +00:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2012-07-29 16:47:44 +00:00
|
|
|
login_user=dict(default="postgres"),
|
|
|
|
login_password=dict(default=""),
|
|
|
|
login_host=dict(default=""),
|
2012-09-05 16:18:30 +00:00
|
|
|
port=dict(default="5432"),
|
2012-08-01 04:21:36 +00:00
|
|
|
db=dict(required=True, aliases=['name']),
|
2012-07-31 09:56:29 +00:00
|
|
|
owner=dict(default=""),
|
|
|
|
template=dict(default=""),
|
|
|
|
encoding=dict(default=""),
|
2012-07-26 15:02:28 +00:00
|
|
|
state=dict(default="present", choices=["absent", "present"]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not postgresqldb_found:
|
|
|
|
module.fail_json(msg="the python psycopg2 module is required")
|
|
|
|
|
|
|
|
db = module.params["db"]
|
2012-09-05 16:18:30 +00:00
|
|
|
port = module.params["port"]
|
2012-07-31 09:56:29 +00:00
|
|
|
owner = module.params["owner"]
|
|
|
|
template = module.params["template"]
|
|
|
|
encoding = module.params["encoding"]
|
2012-07-26 15:02:28 +00:00
|
|
|
state = module.params["state"]
|
|
|
|
changed = False
|
2012-08-14 20:53:18 +00:00
|
|
|
|
|
|
|
# To use defaults values, keyword arguments must be absent, so
|
|
|
|
# check which values are empty and don't include in the **kw
|
|
|
|
# dictionary
|
|
|
|
params_map = {
|
|
|
|
"login_host":"host",
|
|
|
|
"login_user":"user",
|
2012-09-05 16:18:30 +00:00
|
|
|
"login_password":"password",
|
|
|
|
"port":"port"
|
2012-08-14 20:53:18 +00:00
|
|
|
}
|
|
|
|
kw = dict( (params_map[k], v) for (k, v) in module.params.iteritems()
|
|
|
|
if k in params_map and v != '' )
|
2012-07-26 15:02:28 +00:00
|
|
|
try:
|
2012-08-14 20:53:18 +00:00
|
|
|
db_connection = psycopg2.connect(database="template1", **kw)
|
2012-07-26 15:02:28 +00:00
|
|
|
# Enable autocommit so we can create databases
|
2012-08-14 20:55:47 +00:00
|
|
|
if psycopg2.__version__ >= '2.4.2':
|
|
|
|
db_connection.autocommit = True
|
|
|
|
else:
|
|
|
|
db_connection.set_isolation_level(psycopg2
|
|
|
|
.extensions
|
|
|
|
.ISOLATION_LEVEL_AUTOCOMMIT)
|
2012-07-26 15:02:28 +00:00
|
|
|
cursor = db_connection.cursor()
|
2012-08-13 21:01:05 +00:00
|
|
|
except Exception, e:
|
2012-07-26 15:02:28 +00:00
|
|
|
module.fail_json(msg="unable to connect to database: %s" % e)
|
|
|
|
|
|
|
|
try:
|
2012-08-22 17:20:51 +00:00
|
|
|
if state == "absent":
|
|
|
|
changed = db_delete(cursor, db)
|
|
|
|
elif state == "present":
|
|
|
|
changed = db_create(cursor, db, owner, template, encoding)
|
2012-08-13 21:01:05 +00:00
|
|
|
except Exception, e:
|
2012-07-26 15:02:28 +00:00
|
|
|
module.fail_json(msg="Database query failed: %s" % e)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, db=db)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|