mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
8.4 KiB
Plaintext
268 lines
8.4 KiB
Plaintext
13 years ago
|
#!/usr/bin/python
|
||
13 years ago
|
# -*- coding: utf-8 -*-
|
||
13 years ago
|
|
||
|
# (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/>.
|
||
|
|
||
12 years ago
|
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.
|
||
|
version_added: "0.6"
|
||
|
options:
|
||
|
name:
|
||
|
description:
|
||
|
- name of the database to add or remove
|
||
|
required: true
|
||
|
default: null
|
||
|
login_user:
|
||
|
description:
|
||
|
- The username used to authenticate with
|
||
|
required: false
|
||
|
default: null
|
||
|
login_password:
|
||
|
description:
|
||
12 years ago
|
- The password used to authenticate with
|
||
12 years ago
|
required: false
|
||
|
default: null
|
||
|
login_host:
|
||
|
description:
|
||
|
- Host running the database
|
||
|
required: false
|
||
|
default: localhost
|
||
12 years ago
|
login_unix_socket:
|
||
|
description:
|
||
|
- The path to a Unix domain socket for local connections
|
||
|
required: false
|
||
|
default: null
|
||
12 years ago
|
state:
|
||
|
description:
|
||
|
- The database state
|
||
|
required: false
|
||
|
default: present
|
||
12 years ago
|
choices: [ "present", "absent", "dump", "import" ]
|
||
12 years ago
|
collation:
|
||
|
description:
|
||
|
- Collation mode
|
||
|
required: false
|
||
|
default: null
|
||
|
encoding:
|
||
|
description:
|
||
|
- Encoding mode
|
||
|
required: false
|
||
|
default: null
|
||
12 years ago
|
target:
|
||
|
description:
|
||
12 years ago
|
- Where to dump/get the C(.sql) file
|
||
12 years ago
|
required: false
|
||
12 years ago
|
examples:
|
||
12 years ago
|
- code: "mysql_db: db=bobdata state=present"
|
||
12 years ago
|
description: Create a new database with name 'bobdata'
|
||
12 years ago
|
notes:
|
||
12 years ago
|
- Requires the MySQLdb Python package on the remote host. For Ubuntu, this
|
||
12 years ago
|
is as easy as apt-get install python-mysqldb. (See M(apt).)
|
||
12 years ago
|
- Both I(login_password) and I(login_user) are required when you are
|
||
12 years ago
|
passing credentials. If none are present, the module will attempt to read
|
||
|
the credentials from C(~/.my.cnf), and finally fall back to using the MySQL
|
||
12 years ago
|
default login of C(root) with no password.
|
||
12 years ago
|
requirements: [ ConfigParser ]
|
||
|
author: Mark Theunissen
|
||
|
'''
|
||
|
|
||
13 years ago
|
import ConfigParser
|
||
12 years ago
|
import os
|
||
13 years ago
|
try:
|
||
13 years ago
|
import MySQLdb
|
||
13 years ago
|
except ImportError:
|
||
13 years ago
|
mysqldb_found = False
|
||
|
else:
|
||
|
mysqldb_found = True
|
||
13 years ago
|
|
||
|
# ===========================================
|
||
|
# MySQL module specific support methods.
|
||
|
#
|
||
|
|
||
13 years ago
|
def db_exists(cursor, db):
|
||
13 years ago
|
res = cursor.execute("SHOW DATABASES LIKE %s", (db,))
|
||
|
return bool(res)
|
||
|
|
||
13 years ago
|
def db_delete(cursor, db):
|
||
12 years ago
|
query = "DROP DATABASE `%s`" % db
|
||
13 years ago
|
cursor.execute(query)
|
||
|
return True
|
||
|
|
||
12 years ago
|
def db_dump(host, user, password, db_name, target):
|
||
12 years ago
|
res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" "
|
||
12 years ago
|
+db_name+" > "
|
||
|
+target)
|
||
|
return (res == 0)
|
||
|
|
||
12 years ago
|
def db_import(host, user, password, db_name, target):
|
||
12 years ago
|
res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" "
|
||
12 years ago
|
+db_name+" < "
|
||
|
+target)
|
||
|
return (res == 0)
|
||
|
|
||
13 years ago
|
def db_create(cursor, db, encoding, collation):
|
||
|
if encoding:
|
||
|
encoding = " CHARACTER SET %s" % encoding
|
||
|
if collation:
|
||
|
collation = " COLLATE %s" % collation
|
||
12 years ago
|
query = "CREATE DATABASE `%s`%s%s" % (db, encoding, collation)
|
||
13 years ago
|
res = cursor.execute(query)
|
||
|
return True
|
||
|
|
||
12 years ago
|
def strip_quotes(s):
|
||
|
""" Remove surrounding single or double quotes
|
||
|
|
||
|
>>> print strip_quotes('hello')
|
||
|
hello
|
||
|
>>> print strip_quotes('"hello"')
|
||
|
hello
|
||
|
>>> print strip_quotes("'hello'")
|
||
|
hello
|
||
|
>>> print strip_quotes("'hello")
|
||
|
'hello
|
||
|
|
||
|
"""
|
||
|
single_quote = "'"
|
||
|
double_quote = '"'
|
||
|
|
||
|
if s.startswith(single_quote) and s.endswith(single_quote):
|
||
|
s = s.strip(single_quote)
|
||
|
elif s.startswith(double_quote) and s.endswith(double_quote):
|
||
|
s = s.strip(double_quote)
|
||
|
return s
|
||
|
|
||
|
|
||
|
def config_get(config, section, option):
|
||
|
""" Calls ConfigParser.get and strips quotes
|
||
|
|
||
|
See: http://dev.mysql.com/doc/refman/5.0/en/option-files.html
|
||
|
"""
|
||
|
return strip_quotes(config.get(section, option))
|
||
|
|
||
|
|
||
13 years ago
|
def load_mycnf():
|
||
|
config = ConfigParser.RawConfigParser()
|
||
|
mycnf = os.path.expanduser('~/.my.cnf')
|
||
13 years ago
|
if not os.path.exists(mycnf):
|
||
|
return False
|
||
13 years ago
|
try:
|
||
13 years ago
|
config.readfp(open(mycnf))
|
||
12 years ago
|
except (IOError):
|
||
|
return False
|
||
|
# We support two forms of passwords in .my.cnf, both pass= and password=,
|
||
|
# as these are both supported by MySQL.
|
||
|
try:
|
||
12 years ago
|
passwd = config_get(config, 'client', 'password')
|
||
12 years ago
|
except (ConfigParser.NoOptionError):
|
||
|
try:
|
||
12 years ago
|
passwd = config_get(config, 'client', 'pass')
|
||
12 years ago
|
except (ConfigParser.NoOptionError):
|
||
|
return False
|
||
|
try:
|
||
12 years ago
|
creds = dict(user=config_get(config, 'client', 'user'),passwd=passwd)
|
||
12 years ago
|
except (ConfigParser.NoOptionError):
|
||
13 years ago
|
return False
|
||
|
return creds
|
||
|
|
||
13 years ago
|
# ===========================================
|
||
|
# Module execution.
|
||
|
#
|
||
|
|
||
13 years ago
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
13 years ago
|
login_user=dict(default=None),
|
||
|
login_password=dict(default=None),
|
||
|
login_host=dict(default="localhost"),
|
||
12 years ago
|
login_unix_socket=dict(default=None),
|
||
13 years ago
|
db=dict(required=True, aliases=['name']),
|
||
13 years ago
|
encoding=dict(default=""),
|
||
|
collation=dict(default=""),
|
||
12 years ago
|
target=dict(default=None),
|
||
|
state=dict(default="present", choices=["absent", "present","dump", "import"]),
|
||
13 years ago
|
)
|
||
|
)
|
||
|
|
||
|
if not mysqldb_found:
|
||
|
module.fail_json(msg="the python mysqldb module is required")
|
||
|
|
||
|
db = module.params["db"]
|
||
13 years ago
|
encoding = module.params["encoding"]
|
||
|
collation = module.params["collation"]
|
||
13 years ago
|
state = module.params["state"]
|
||
12 years ago
|
target = module.params["target"]
|
||
13 years ago
|
|
||
|
# 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.
|
||
13 years ago
|
login_password = module.params["login_password"]
|
||
|
login_user = module.params["login_user"]
|
||
|
if login_user is None and login_password is None:
|
||
13 years ago
|
mycnf_creds = load_mycnf()
|
||
|
if mycnf_creds is False:
|
||
13 years ago
|
login_user = "root"
|
||
|
login_password = ""
|
||
13 years ago
|
else:
|
||
13 years ago
|
login_user = mycnf_creds["user"]
|
||
|
login_password = mycnf_creds["passwd"]
|
||
|
elif login_password is None or login_user is None:
|
||
|
module.fail_json(msg="when supplying login arguments, both login_user and login_password must be provided")
|
||
12 years ago
|
login_host = module.params["login_host"]
|
||
13 years ago
|
|
||
12 years ago
|
if state in ['dump','import']:
|
||
|
if target is None:
|
||
12 years ago
|
module.fail_json(msg="with state=%s target is required" % (state))
|
||
12 years ago
|
connect_to_db = db
|
||
|
else:
|
||
|
connect_to_db = 'mysql'
|
||
13 years ago
|
try:
|
||
12 years ago
|
if module.params["login_unix_socket"]:
|
||
12 years ago
|
db_connection = MySQLdb.connect(host=module.params["login_host"], unix_socket=module.params["login_unix_socket"], user=login_user, passwd=login_password, db=connect_to_db)
|
||
12 years ago
|
else:
|
||
12 years ago
|
db_connection = MySQLdb.connect(host=module.params["login_host"], user=login_user, passwd=login_password, db=connect_to_db)
|
||
13 years ago
|
cursor = db_connection.cursor()
|
||
12 years ago
|
except Exception, e:
|
||
13 years ago
|
module.fail_json(msg="unable to connect, check login_user and login_password are correct, or alternatively check ~/.my.cnf contains credentials")
|
||
13 years ago
|
|
||
13 years ago
|
changed = False
|
||
13 years ago
|
if db_exists(cursor, db):
|
||
13 years ago
|
if state == "absent":
|
||
13 years ago
|
changed = db_delete(cursor, db)
|
||
12 years ago
|
elif state == "dump":
|
||
12 years ago
|
changed = db_dump(login_host, login_user, login_password, db, target)
|
||
12 years ago
|
if not changed:
|
||
|
module.fail_json(msg="dump failed!")
|
||
|
elif state == "import":
|
||
12 years ago
|
changed = db_import(login_host, login_user, login_password, db, target)
|
||
12 years ago
|
if not changed:
|
||
|
module.fail_json(msg="import failed!")
|
||
13 years ago
|
else:
|
||
|
if state == "present":
|
||
13 years ago
|
changed = db_create(cursor, db, encoding, collation)
|
||
13 years ago
|
|
||
|
module.exit_json(changed=changed, db=db)
|
||
13 years ago
|
|
||
13 years ago
|
# this is magic, see lib/ansible/module_common.py
|
||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||
|
main()
|