Merge pull request #1289 from abondis/mysql_migration

mysql_db: connection test, dump/import state
reviewable/pr18780/r1
Michael DeHaan 12 years ago
commit 86b7d4b689

@ -52,7 +52,7 @@ options:
- The database state - The database state
required: false required: false
default: present default: present
choices: [ "present", "absent" ] choices: [ "present", "absent", "dump", "import" ]
collation: collation:
description: description:
- Collation mode - Collation mode
@ -63,6 +63,10 @@ options:
- Encoding mode - Encoding mode
required: false required: false
default: null default: null
target:
description:
- Where to dump/get the .sql file
required: true
examples: examples:
- code: mysql_db db=bobdata state=present - code: mysql_db db=bobdata state=present
description: Create a new database with name 'bobdata' description: Create a new database with name 'bobdata'
@ -78,6 +82,7 @@ author: Mark Theunissen
''' '''
import ConfigParser import ConfigParser
import os
try: try:
import MySQLdb import MySQLdb
except ImportError: except ImportError:
@ -98,6 +103,18 @@ def db_delete(cursor, db):
cursor.execute(query) cursor.execute(query)
return True return True
def db_dump(user, password, db_name, target):
res = os.system("/usr/bin/mysqldump -q -u "+user+ " -p"+password+" "
+db_name+" > "
+target)
return (res == 0)
def db_import(user, password, db_name, target):
res = os.system("/usr/bin/mysql -u "+user+ " -p"+password+" "
+db_name+" < "
+target)
return (res == 0)
def db_create(cursor, db, encoding, collation): def db_create(cursor, db, encoding, collation):
if encoding: if encoding:
encoding = " CHARACTER SET %s" % encoding encoding = " CHARACTER SET %s" % encoding
@ -133,7 +150,8 @@ def main():
db=dict(required=True, aliases=['name']), db=dict(required=True, aliases=['name']),
encoding=dict(default=""), encoding=dict(default=""),
collation=dict(default=""), collation=dict(default=""),
state=dict(default="present", choices=["absent", "present"]), target=dict(default=None),
state=dict(default="present", choices=["absent", "present","dump", "import"]),
) )
) )
@ -144,6 +162,7 @@ def main():
encoding = module.params["encoding"] encoding = module.params["encoding"]
collation = module.params["collation"] collation = module.params["collation"]
state = module.params["state"] state = module.params["state"]
target = module.params["target"]
# Either the caller passes both a username and password with which to connect to # 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 # mysql, or they pass neither and allow this module to read the credentials from
@ -163,17 +182,28 @@ def main():
try: try:
if module.params["login_unix_socket"] != None: if module.params["login_unix_socket"] != None:
db_connection = MySQLdb.connect(host=module.params["login_host"], unix_socket=module.params["login_unix_socket"], user=login_user, passwd=login_password, db="mysql") db_connection = MySQLdb.connect(host=module.params["login_host"], unix_socket=module.params["login_unix_socket"], user=login_user, passwd=login_password, db=db)
else: else:
db_connection = MySQLdb.connect(host=module.params["login_host"], user=login_user, passwd=login_password, db="mysql") db_connection = MySQLdb.connect(host=module.params["login_host"], user=login_user, passwd=login_password, db=db)
cursor = db_connection.cursor() cursor = db_connection.cursor()
except Exception as e: except Exception as e:
module.fail_json(msg="unable to connect, check login_user and login_password are correct, or alternatively check ~/.my.cnf contains credentials") module.fail_json(msg="unable to connect, check login_user and login_password are correct, or alternatively check ~/.my.cnf contains credentials")
if (state in ['dump','import']) and target is None:
module.fail_json(msg="with state={0} target is required".format(state))
changed = False changed = False
if db_exists(cursor, db): if db_exists(cursor, db):
if state == "absent": if state == "absent":
changed = db_delete(cursor, db) changed = db_delete(cursor, db)
elif state == "dump":
changed = db_dump(login_user, login_password, db, target)
if not changed:
module.fail_json(msg="dump failed!")
elif state == "import":
changed = db_import(login_user, login_password, db, target)
if not changed:
module.fail_json(msg="import failed!")
else: else:
if state == "present": if state == "present":
changed = db_create(cursor, db, encoding, collation) changed = db_create(cursor, db, encoding, collation)

Loading…
Cancel
Save