ansible/library/mysql_db

114 lines
3.6 KiB
Text
Raw Normal View History

2012-07-12 01:00:55 +02:00
#!/usr/bin/python
# (c) 2012, Mark Theunissen <mark.theunissen@gmail.com>
# Sponsored by Four Kitchens http://fourkitchens.com.
#
# 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/>.
import ConfigParser
2012-07-12 01:00:55 +02:00
try:
2012-07-22 01:02:34 +02:00
import MySQLdb
2012-07-12 01:00:55 +02:00
except ImportError:
2012-07-22 01:02:34 +02:00
mysqldb_found = False
else:
mysqldb_found = True
2012-07-12 01:00:55 +02:00
# ===========================================
# MySQL module specific support methods.
#
2012-07-22 01:02:34 +02:00
def db_exists(cursor, db):
2012-07-12 01:00:55 +02:00
res = cursor.execute("SHOW DATABASES LIKE %s", (db,))
return bool(res)
2012-07-22 01:02:34 +02:00
def db_delete(cursor, db):
2012-07-12 01:00:55 +02:00
query = "DROP DATABASE %s" % db
cursor.execute(query)
return True
2012-07-22 01:02:34 +02:00
def db_create(cursor, db):
2012-07-12 01:00:55 +02:00
query = "CREATE DATABASE %s" % db
res = cursor.execute(query)
return True
def load_mycnf():
config = ConfigParser.RawConfigParser()
mycnf = os.path.expanduser('~/.my.cnf')
if not os.path.exists(mycnf):
return False
try:
config.readfp(open(mycnf))
creds = dict(user=config.get('client', 'user'),passwd=config.get('client', 'pass'))
except (ConfigParser.NoOptionError, IOError):
return False
return creds
2012-07-12 01:00:55 +02:00
# ===========================================
# Module execution.
#
2012-07-22 01:02:34 +02:00
def main():
module = AnsibleModule(
argument_spec = dict(
loginuser=dict(default=None),
2012-07-26 18:10:22 +02:00
loginpasswd=dict(default=None),
2012-07-22 01:02:34 +02:00
loginhost=dict(default="localhost"),
db=dict(required=True),
state=dict(default="present", choices=["absent", "present"]),
2012-07-22 01:02:34 +02:00
)
)
if not mysqldb_found:
module.fail_json(msg="the python mysqldb module is required")
db = module.params["db"]
state = module.params["state"]
# Either the caller passes both a username and password with which to connect to
# mysql, or they pass neither and allow this module to read the credentials from
# ~/.my.cnf.
2012-07-26 18:13:10 +02:00
loginpasswd = module.params["loginpasswd"]
loginuser = module.params["loginuser"]
2012-07-26 18:13:10 +02:00
if loginuser is None and loginpasswd is None:
mycnf_creds = load_mycnf()
if mycnf_creds is False:
module.fail_json(msg="incomplete login arguments passed and can't find them in ~/.my.cnf")
else:
loginuser = mycnf_creds["user"]
2012-07-26 18:13:10 +02:00
loginpasswd = mycnf_creds["passwd"]
elif loginpasswd is None or loginuser is None:
module.fail_json(msg="when supplying login arguments, both user and pass must be provided")
2012-07-12 01:00:55 +02:00
try:
2012-07-26 18:13:10 +02:00
db_connection = MySQLdb.connect(host=module.params["loginhost"], user=loginuser, passwd=loginpasswd, db="mysql")
2012-07-12 01:00:55 +02:00
cursor = db_connection.cursor()
except Exception as e:
2012-07-22 01:02:34 +02:00
module.fail_json(msg="unable to connect to database")
2012-07-12 01:00:55 +02:00
changed = False
2012-07-22 01:02:34 +02:00
if db_exists(cursor, db):
2012-07-12 01:00:55 +02:00
if state == "absent":
2012-07-22 01:02:34 +02:00
changed = db_delete(cursor, db)
2012-07-12 01:00:55 +02:00
else:
if state == "present":
2012-07-22 01:02:34 +02:00
changed = db_create(cursor, db)
module.exit_json(changed=changed, db=db)
2012-07-12 01:00:55 +02:00
2012-07-22 01:02:34 +02:00
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()