Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql_db: connection test, dump/import state #1289

Merged
merged 1 commit into from
Oct 12, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 34 additions & 4 deletions library/mysql_db
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ options:
- The database state
required: false
default: present
choices: [ "present", "absent" ]
choices: [ "present", "absent", "dump", "import" ]
collation:
description:
- Collation mode
Expand All @@ -63,6 +63,10 @@ options:
- Encoding mode
required: false
default: null
target:
description:
- Where to dump/get the .sql file
required: true
examples:
- code: mysql_db db=bobdata state=present
description: Create a new database with name 'bobdata'
Expand All @@ -78,6 +82,7 @@ author: Mark Theunissen
'''

import ConfigParser
import os
try:
import MySQLdb
except ImportError:
Expand All @@ -98,6 +103,18 @@ def db_delete(cursor, db):
cursor.execute(query)
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):
if encoding:
encoding = " CHARACTER SET %s" % encoding
Expand Down Expand Up @@ -133,7 +150,8 @@ def main():
db=dict(required=True, aliases=['name']),
encoding=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"]),
)
)

Expand All @@ -144,6 +162,7 @@ def main():
encoding = module.params["encoding"]
collation = module.params["collation"]
state = module.params["state"]
target = module.params["target"]

# 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
Expand All @@ -163,17 +182,28 @@ def main():

try:
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:
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()
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")

if (state in ['dump','import']) and target is None:
module.fail_json(msg="with state={0} target is required".format(state))

changed = False
if db_exists(cursor, db):
if state == "absent":
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:
if state == "present":
changed = db_create(cursor, db, encoding, collation)
Expand Down