2012-07-12 01:00:55 +02:00
#!/usr/bin/python
2012-08-03 03:29:10 +02:00
# -*- coding: utf-8 -*-
2012-07-12 01:00:55 +02:00
# (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/>.
2012-09-29 16:15:52 +02:00
DOCUMENTATION = '''
- - -
module : mysql_db
short_description : Add or remove MySQL databases from a remote host .
description :
- Add or remove MySQL databases from a remote host .
2013-11-28 03:23:03 +01:00
version_added : " 0.6 "
2012-09-29 16:15:52 +02:00
options :
name :
description :
- name of the database to add or remove
2015-12-08 18:09:50 +01:00
- name = all May only be provided if I ( state ) is C ( dump ) or C ( import ) .
2014-09-30 01:18:58 +02:00
- if name = all Works like - - all - databases option for mysqldump ( Added in 2.0 )
2012-09-29 16:15:52 +02:00
required : true
default : null
2013-07-14 13:11:03 +02:00
aliases : [ db ]
2012-09-29 16:15:52 +02:00
state :
description :
- The database state
required : false
default : present
2012-10-11 05:28:42 +02:00
choices : [ " present " , " absent " , " dump " , " import " ]
2012-09-29 16:15:52 +02:00
collation :
description :
2015-10-31 19:24:32 +01:00
- Collation mode ( sorting ) . This only applies to new table / databases and does not update existing ones , this is a limitation of MySQL .
2012-09-29 16:15:52 +02:00
required : false
default : null
encoding :
description :
2016-04-14 17:17:13 +02:00
- Encoding mode to use , examples include C ( utf8 ) or C ( latin1_swedish_ci )
2012-09-29 16:15:52 +02:00
required : false
default : null
2012-10-11 05:28:42 +02:00
target :
description :
2014-02-26 12:56:24 +01:00
- Location , on the remote host , of the dump file to read from or write to . Uncompressed SQL
2015-10-28 21:35:51 +01:00
files ( C ( . sql ) ) as well as bzip2 ( C ( . bz2 ) ) , gzip ( C ( . gz ) ) and xz ( Added in 2.0 ) compressed files are supported .
2013-03-05 23:11:12 +01:00
required : false
2016-04-12 11:33:58 +02:00
single_transaction :
description :
- Execute the dump in a single transaction
required : false
default : false
version_added : " 2.1 "
quick :
description :
- Option used for dumping large tables
required : false
2016-04-13 11:15:48 +02:00
default : true
2016-04-12 11:33:58 +02:00
version_added : " 2.1 "
2015-12-08 18:09:50 +01:00
author : " Ansible Core Team "
2016-04-11 07:46:39 +02:00
requirements :
- mysql ( command line binary )
- mysqldump ( command line binary )
notes :
- Requires the python - mysqldb package on the remote host , as well as mysql and mysqldump binaries .
2015-11-11 05:13:48 +01:00
extends_documentation_fragment : mysql
2012-09-29 16:15:52 +02:00
'''
2013-06-14 11:53:43 +02:00
EXAMPLES = '''
# Create a new database with name 'bobdata'
2013-07-14 13:11:03 +02:00
- mysql_db : name = bobdata state = present
2014-02-26 12:56:24 +01:00
# Copy database dump file to remote host and restore it to database 'my_db'
- copy : src = dump . sql . bz2 dest = / tmp
- mysql_db : name = my_db state = import target = / tmp / dump . sql . bz2
2014-09-30 01:18:58 +02:00
# Dumps all databases to hostname.sql
- mysql_db : state = dump name = all target = / tmp / { { inventory_hostname } } . sql
# Imports file.sql similiar to mysql -u <username> -p <password> < hostname.sql
- mysql_db : state = import name = all target = / tmp / { { inventory_hostname } } . sql
2013-06-14 11:53:43 +02:00
'''
2012-10-11 05:28:42 +02:00
import os
2014-03-13 04:15:56 +01:00
import pipes
2014-09-30 01:11:45 +02:00
import stat
2015-05-25 14:40:15 +02:00
import subprocess
2015-11-11 05:13:48 +01:00
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 ) :
2014-08-04 13:15:58 +02:00
res = cursor . execute ( " SHOW DATABASES LIKE %s " , ( db . replace ( " _ " , " \ _ " ) , ) )
2012-07-12 01:00:55 +02:00
return bool ( res )
2012-07-22 01:02:34 +02:00
def db_delete ( cursor , db ) :
2014-11-25 10:46:09 +01:00
query = " DROP DATABASE %s " % mysql_quote_identifier ( db , ' database ' )
2012-07-12 01:00:55 +02:00
cursor . execute ( query )
return True
2016-04-12 11:33:58 +02:00
def db_dump ( module , host , user , password , db_name , target , all_databases , port , config_file , socket = None , ssl_cert = None , ssl_key = None , ssl_ca = None , single_transaction = None , quick = None ) :
2013-10-16 15:51:08 +02:00
cmd = module . get_bin_path ( ' mysqldump ' , True )
2015-11-11 05:13:48 +01:00
# If defined, mysqldump demands --defaults-extra-file be the first option
2015-12-17 20:35:44 +01:00
if config_file :
cmd + = " --defaults-extra-file= %s " % pipes . quote ( config_file )
2015-11-11 05:13:48 +01:00
if user is not None :
cmd + = " --user= %s " % pipes . quote ( user )
if password is not None :
cmd + = " --password= %s " % pipes . quote ( password )
if ssl_cert is not None :
cmd + = " --ssl-cert= %s " % pipes . quote ( ssl_cert )
if ssl_key is not None :
cmd + = " --ssl-key= %s " % pipes . quote ( ssl_key )
if ssl_cert is not None :
cmd + = " --ssl-ca= %s " % pipes . quote ( ssl_ca )
2013-10-16 15:51:08 +02:00
if socket is not None :
2014-03-13 04:15:56 +01:00
cmd + = " --socket= %s " % pipes . quote ( socket )
2013-10-16 15:59:31 +02:00
else :
2015-03-03 23:23:07 +01:00
cmd + = " --host= %s --port= %i " % ( pipes . quote ( host ) , port )
2014-09-30 01:18:58 +02:00
if all_databases :
cmd + = " --all-databases "
else :
cmd + = " %s " % pipes . quote ( db_name )
2016-04-12 11:33:58 +02:00
if single_transaction :
cmd + = " --single-transaction=true "
if quick :
cmd + = " --quick "
2015-05-25 18:22:08 +02:00
path = None
2013-09-30 20:09:32 +02:00
if os . path . splitext ( target ) [ - 1 ] == ' .gz ' :
2015-05-25 18:22:08 +02:00
path = module . get_bin_path ( ' gzip ' , True )
2013-09-30 20:09:32 +02:00
elif os . path . splitext ( target ) [ - 1 ] == ' .bz2 ' :
2015-05-25 18:22:08 +02:00
path = module . get_bin_path ( ' bzip2 ' , True )
2015-05-12 23:28:18 +02:00
elif os . path . splitext ( target ) [ - 1 ] == ' .xz ' :
2015-05-25 18:22:08 +02:00
path = module . get_bin_path ( ' xz ' , True )
if path :
cmd = ' %s | %s > %s ' % ( cmd , path , pipes . quote ( target ) )
2013-09-30 20:09:32 +02:00
else :
2014-03-13 04:15:56 +01:00
cmd + = " > %s " % pipes . quote ( target )
2015-05-25 18:22:08 +02:00
2014-03-13 04:15:56 +01:00
rc , stdout , stderr = module . run_command ( cmd , use_unsafe_shell = True )
2013-10-16 15:51:08 +02:00
return rc , stdout , stderr
2012-10-11 05:28:42 +02:00
2015-11-11 05:13:48 +01:00
def db_import ( module , host , user , password , db_name , target , all_databases , port , config_file , socket = None , ssl_cert = None , ssl_key = None , ssl_ca = None ) :
2014-06-17 23:37:14 +02:00
if not os . path . exists ( target ) :
return module . fail_json ( msg = " target %s does not exist on the host " % target )
2015-05-28 05:27:51 +02:00
cmd = [ module . get_bin_path ( ' mysql ' , True ) ]
2015-11-11 05:13:48 +01:00
# --defaults-file must go first, or errors out
2015-12-17 20:35:44 +01:00
if config_file :
cmd . append ( " --defaults-extra-file= %s " % pipes . quote ( config_file ) )
2015-05-28 05:27:51 +02:00
if user :
cmd . append ( " --user= %s " % pipes . quote ( user ) )
if password :
cmd . append ( " --password= %s " % pipes . quote ( password ) )
2013-10-16 15:51:08 +02:00
if socket is not None :
2015-05-28 05:27:51 +02:00
cmd . append ( " --socket= %s " % pipes . quote ( socket ) )
2015-11-11 05:13:48 +01:00
if ssl_cert is not None :
cmd . append ( " --ssl-cert= %s " % pipes . quote ( ssl_cert ) )
if ssl_key is not None :
cmd . append ( " --ssl-key= %s " % pipes . quote ( ssl_key ) )
if ssl_cert is not None :
cmd . append ( " --ssl-ca= %s " % pipes . quote ( ssl_ca ) )
2013-10-16 15:59:31 +02:00
else :
2015-05-28 05:27:51 +02:00
cmd . append ( " --host= %s " % pipes . quote ( host ) )
cmd . append ( " --port= %i " % port )
2014-09-30 01:18:58 +02:00
if not all_databases :
2015-05-28 05:27:51 +02:00
cmd . append ( " -D " )
cmd . append ( pipes . quote ( db_name ) )
2015-05-25 21:22:49 +02:00
comp_prog_path = None
2013-09-30 20:09:32 +02:00
if os . path . splitext ( target ) [ - 1 ] == ' .gz ' :
2015-05-25 21:10:12 +02:00
comp_prog_path = module . get_bin_path ( ' gzip ' , required = True )
2013-09-30 20:09:32 +02:00
elif os . path . splitext ( target ) [ - 1 ] == ' .bz2 ' :
2015-05-25 21:10:12 +02:00
comp_prog_path = module . get_bin_path ( ' bzip2 ' , required = True )
2015-05-12 23:28:18 +02:00
elif os . path . splitext ( target ) [ - 1 ] == ' .xz ' :
2015-05-25 21:10:12 +02:00
comp_prog_path = module . get_bin_path ( ' xz ' , required = True )
2015-05-25 21:22:49 +02:00
if comp_prog_path :
2015-05-25 21:10:12 +02:00
p1 = subprocess . Popen ( [ comp_prog_path , ' -dc ' , target ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE )
2015-05-28 05:27:51 +02:00
p2 = subprocess . Popen ( cmd , stdin = p1 . stdout , stdout = subprocess . PIPE , stderr = subprocess . PIPE )
2015-05-25 14:40:15 +02:00
( stdout2 , stderr2 ) = p2 . communicate ( )
p1 . stdout . close ( )
p1 . wait ( )
if p1 . returncode != 0 :
stderr1 = p1 . stderr . read ( )
return p1 . returncode , ' ' , stderr1
else :
return p2 . returncode , stdout2 , stderr2
2013-09-30 20:09:32 +02:00
else :
2015-05-28 05:27:51 +02:00
cmd = ' ' . join ( cmd )
2014-03-13 04:15:56 +01:00
cmd + = " < %s " % pipes . quote ( target )
2014-03-30 19:39:00 +02:00
rc , stdout , stderr = module . run_command ( cmd , use_unsafe_shell = True )
2015-05-25 21:22:49 +02:00
return rc , stdout , stderr
2012-10-11 05:28:42 +02:00
2012-07-31 11:56:29 +02:00
def db_create ( cursor , db , encoding , collation ) :
2014-11-25 10:46:09 +01:00
query_params = dict ( enc = encoding , collate = collation )
query = [ ' CREATE DATABASE %s ' % mysql_quote_identifier ( db , ' database ' ) ]
2012-07-31 11:56:29 +02:00
if encoding :
2014-11-25 10:46:09 +01:00
query . append ( " CHARACTER SET %(enc)s " )
2012-07-31 11:56:29 +02:00
if collation :
2014-11-25 10:46:09 +01:00
query . append ( " COLLATE %(collate)s " )
query = ' ' . join ( query )
res = cursor . execute ( query , query_params )
2012-07-12 01:00:55 +02:00
return True
# ===========================================
# Module execution.
#
2012-07-22 01:02:34 +02:00
def main ( ) :
module = AnsibleModule (
argument_spec = dict (
2012-07-31 00:15:24 +02:00
login_user = dict ( default = None ) ,
2016-04-11 16:57:24 +02:00
login_password = dict ( default = None , no_log = True ) ,
2012-07-31 00:15:24 +02:00
login_host = dict ( default = " localhost " ) ,
2015-03-03 23:23:07 +01:00
login_port = dict ( default = 3306 , type = ' int ' ) ,
2012-08-03 12:34:55 +02:00
login_unix_socket = dict ( default = None ) ,
2014-07-11 04:37:46 +02:00
name = dict ( required = True , aliases = [ ' db ' ] ) ,
2012-07-31 11:56:29 +02:00
encoding = dict ( default = " " ) ,
collation = dict ( default = " " ) ,
2016-04-11 16:57:24 +02:00
target = dict ( default = None , type = ' path ' ) ,
2012-10-11 05:28:42 +02:00
state = dict ( default = " present " , choices = [ " absent " , " present " , " dump " , " import " ] ) ,
2016-04-11 16:57:24 +02:00
ssl_cert = dict ( default = None , type = ' path ' ) ,
ssl_key = dict ( default = None , type = ' path ' ) ,
ssl_ca = dict ( default = None , type = ' path ' ) ,
2016-03-10 22:06:39 +01:00
connect_timeout = dict ( default = 30 , type = ' int ' ) ,
2016-04-11 16:57:24 +02:00
config_file = dict ( default = " ~/.my.cnf " , type = ' path ' ) ,
2016-04-12 11:33:58 +02:00
single_transaction = dict ( default = False , type = ' bool ' ) ,
2016-04-13 11:15:48 +02:00
quick = dict ( default = True , type = ' bool ' ) ,
2016-01-26 15:05:49 +01:00
) ,
supports_check_mode = True
2012-07-22 01:02:34 +02:00
)
if not mysqldb_found :
module . fail_json ( msg = " the python mysqldb module is required " )
2014-07-11 04:37:46 +02:00
db = module . params [ " name " ]
2012-07-31 11:56:29 +02:00
encoding = module . params [ " encoding " ]
collation = module . params [ " collation " ]
2012-07-22 01:02:34 +02:00
state = module . params [ " state " ]
2012-10-11 05:28:42 +02:00
target = module . params [ " target " ]
2014-09-30 01:11:45 +02:00
socket = module . params [ " login_unix_socket " ]
2015-03-03 23:23:07 +01:00
login_port = module . params [ " login_port " ]
if login_port < 0 or login_port > 65535 :
module . fail_json ( msg = " login_port must be a valid unix port number (0-65535) " )
2015-11-11 05:13:48 +01:00
ssl_cert = module . params [ " ssl_cert " ]
ssl_key = module . params [ " ssl_key " ]
ssl_ca = module . params [ " ssl_ca " ]
2016-03-10 22:06:39 +01:00
connect_timeout = module . params [ ' connect_timeout ' ]
2015-11-11 05:13:48 +01:00
config_file = module . params [ ' config_file ' ]
login_password = module . params [ " login_password " ]
login_user = module . params [ " login_user " ]
login_host = module . params [ " login_host " ]
2016-04-12 11:33:58 +02:00
single_transaction = module . params [ " single_transaction " ]
quick = module . params [ " quick " ]
2012-07-25 23:31:12 +02:00
2012-10-12 22:26:59 +02:00
if state in [ ' dump ' , ' import ' ] :
if target is None :
2013-01-10 19:24:23 +01:00
module . fail_json ( msg = " with state= %s target is required " % ( state ) )
2015-06-30 23:22:50 +02:00
if db == ' all ' :
2014-09-30 01:18:58 +02:00
db = ' mysql '
all_databases = True
else :
all_databases = False
2012-10-12 22:26:59 +02:00
else :
2014-09-30 01:18:58 +02:00
if db == ' all ' :
module . fail_json ( msg = " name is not allowed to equal ' all ' unless state equals import, or dump. " )
2012-07-12 01:00:55 +02:00
try :
2016-03-10 22:06:39 +01:00
cursor = mysql_connect ( module , login_user , login_password , config_file , ssl_cert , ssl_key , ssl_ca ,
connect_timeout = connect_timeout )
2016-05-18 16:07:21 +02:00
except Exception :
e = get_exception ( )
2015-11-11 05:13:48 +01:00
if os . path . exists ( config_file ) :
module . fail_json ( msg = " unable to connect to database, check login_user and login_password are correct or %s has the credentials. Exception message: %s " % ( config_file , e ) )
2014-07-11 20:57:45 +02:00
else :
2015-11-11 05:13:48 +01:00
module . fail_json ( msg = " unable to find %s . Exception message: %s " % ( config_file , e ) )
2012-07-12 01:00:55 +02:00
2012-07-25 23:31:12 +02:00
changed = False
2015-12-17 20:35:44 +01:00
if not os . path . exists ( config_file ) :
config_file = None
2012-07-22 01:02:34 +02:00
if db_exists ( cursor , db ) :
2012-07-12 01:00:55 +02:00
if state == " absent " :
2016-01-26 15:05:49 +01:00
if module . check_mode :
changed = True
else :
try :
changed = db_delete ( cursor , db )
2016-05-18 16:07:21 +02:00
except Exception :
e = get_exception ( )
2016-01-26 15:05:49 +01:00
module . fail_json ( msg = " error deleting database: " + str ( e ) )
2012-10-11 05:28:42 +02:00
elif state == " dump " :
2016-01-26 15:05:49 +01:00
if module . check_mode :
module . exit_json ( changed = True , db = db )
2013-10-16 15:51:08 +02:00
else :
2016-01-26 15:05:49 +01:00
rc , stdout , stderr = db_dump ( module , login_host , login_user ,
login_password , db , target , all_databases ,
login_port , config_file , socket , ssl_cert , ssl_key , ssl_ca )
if rc != 0 :
module . fail_json ( msg = " %s " % stderr )
else :
module . exit_json ( changed = True , db = db , msg = stdout )
2012-10-11 05:28:42 +02:00
elif state == " import " :
2016-01-26 15:05:49 +01:00
if module . check_mode :
module . exit_json ( changed = True , db = db )
2013-10-16 15:51:08 +02:00
else :
2016-01-26 15:05:49 +01:00
rc , stdout , stderr = db_import ( module , login_host , login_user ,
login_password , db , target , all_databases ,
login_port , config_file , socket , ssl_cert , ssl_key , ssl_ca )
if rc != 0 :
module . fail_json ( msg = " %s " % stderr )
else :
module . exit_json ( changed = True , db = db , msg = stdout )
2012-07-12 01:00:55 +02:00
else :
if state == " present " :
2016-01-26 15:05:49 +01:00
if module . check_mode :
changed = True
else :
try :
changed = db_create ( cursor , db , encoding , collation )
2016-05-18 16:07:21 +02:00
except Exception :
e = get_exception ( )
2016-01-26 15:05:49 +01:00
module . fail_json ( msg = " error creating database: " + str ( e ) )
2012-07-22 01:02:34 +02:00
module . exit_json ( changed = changed , db = db )
2012-07-12 01:00:55 +02:00
2013-12-02 21:13:49 +01:00
# import module snippets
2013-12-02 21:11:23 +01:00
from ansible . module_utils . basic import *
2014-11-25 10:46:09 +01:00
from ansible . module_utils . database import *
2015-11-11 05:13:48 +01:00
from ansible . module_utils . mysql import *
2014-11-25 10:46:09 +01:00
if __name__ == ' __main__ ' :
main ( )