2015-01-01 14:33:53 +01:00
|
|
|
"""
|
2015-01-04 22:48:42 +01:00
|
|
|
This module defines Celery_ tasks to manage MySQL users and databases.
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
from celery import shared_task
|
|
|
|
from celery.utils.log import get_task_logger
|
|
|
|
|
2020-03-04 19:55:29 +01:00
|
|
|
from mysqltasks import settings
|
2015-01-01 22:17:41 +01:00
|
|
|
from MySQLdb import connect
|
|
|
|
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
_LOGGER = get_task_logger(__name__)
|
|
|
|
|
|
|
|
|
2015-01-01 22:17:41 +01:00
|
|
|
def _get_connection():
|
|
|
|
return connect(
|
|
|
|
host=settings.GVAMYSQL_DBADMIN_HOST,
|
|
|
|
port=settings.GVAMYSQL_DBADMIN_PORT,
|
|
|
|
user=settings.GVAMYSQL_DBADMIN_USER,
|
2015-01-01 22:52:21 +01:00
|
|
|
passwd=settings.GVAMYSQL_DBADMIN_PASSWORD,
|
|
|
|
db='mysql',
|
|
|
|
)
|
2015-01-01 22:17:41 +01:00
|
|
|
|
|
|
|
|
2015-01-01 14:33:53 +01:00
|
|
|
@shared_task
|
|
|
|
def create_mysql_user(username, password):
|
|
|
|
"""
|
|
|
|
This task creates a new MySQL user.
|
|
|
|
|
|
|
|
:param str username: the user name
|
|
|
|
:param str password: the password
|
|
|
|
:return: the created user's name
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
"""
|
2015-01-01 22:17:41 +01:00
|
|
|
conn = _get_connection()
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute(
|
|
|
|
"""
|
2015-01-01 22:54:57 +01:00
|
|
|
CREATE USER %(username)s@'%%' IDENTIFIED BY %(password)s
|
2015-01-01 22:17:41 +01:00
|
|
|
""",
|
|
|
|
{'username': username, 'password': password}
|
|
|
|
)
|
|
|
|
conn.commit()
|
|
|
|
return username
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def set_mysql_userpassword(username, password):
|
|
|
|
"""
|
|
|
|
Set a new password for an existing MySQL user.
|
|
|
|
|
|
|
|
:param str username: the user name
|
|
|
|
:param str password: the password
|
|
|
|
:return: True if the password could be set, False otherwise
|
|
|
|
:rtype: boolean
|
|
|
|
|
|
|
|
"""
|
2015-01-01 22:17:41 +01:00
|
|
|
conn = _get_connection()
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute(
|
|
|
|
"""
|
|
|
|
SET PASSWORD FOR %(username)s = PASSWORD(%(password)s)
|
|
|
|
""",
|
|
|
|
{'username': username, 'password': password})
|
|
|
|
conn.commit()
|
|
|
|
return True
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def delete_mysql_user(username):
|
|
|
|
"""
|
|
|
|
This task deletes an existing MySQL user.
|
|
|
|
|
|
|
|
:param str username: the user name
|
|
|
|
:return: True if the user has been deleted, False otherwise
|
|
|
|
:rtype: boolean
|
|
|
|
|
|
|
|
"""
|
2015-01-01 22:17:41 +01:00
|
|
|
conn = _get_connection()
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute(
|
|
|
|
"""
|
2015-01-01 22:54:57 +01:00
|
|
|
DROP USER %(username)s@'%%'
|
2015-01-01 22:17:41 +01:00
|
|
|
""",
|
|
|
|
{'username': username})
|
|
|
|
conn.commit()
|
|
|
|
return True
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def create_mysql_database(dbname, username):
|
|
|
|
"""
|
|
|
|
This task creates a new MySQL database for the given MySQL user.
|
|
|
|
|
|
|
|
:param str dbname: database name
|
|
|
|
:param str username: the user name of an existing MySQL user
|
|
|
|
:return: the database name
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
"""
|
2015-01-01 22:17:41 +01:00
|
|
|
conn = _get_connection()
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute(
|
|
|
|
"""
|
2015-01-01 23:19:20 +01:00
|
|
|
CREATE DATABASE `%(dbname)s` CHARACTER SET utf8 COLLATE utf8_general_ci
|
2015-01-01 23:14:37 +01:00
|
|
|
""" % {'dbname': dbname})
|
2015-01-01 22:17:41 +01:00
|
|
|
curs.execute(
|
|
|
|
"""
|
2015-01-01 23:14:37 +01:00
|
|
|
GRANT ALL PRIVILEGES ON `%(dbname)s`.* TO %%(username)s@'%%%%'
|
|
|
|
""" % {'dbname': dbname}, {'username': username})
|
2015-01-01 22:17:41 +01:00
|
|
|
conn.commit()
|
|
|
|
return dbname
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
2015-01-01 22:17:41 +01:00
|
|
|
def delete_mysql_database(dbname, username):
|
2015-01-01 14:33:53 +01:00
|
|
|
"""
|
2015-01-01 22:17:41 +01:00
|
|
|
This task deletes an existing MySQL database and revokes privileges of the
|
|
|
|
given user on that database.
|
2015-01-01 14:33:53 +01:00
|
|
|
|
|
|
|
:param str dbname: database name
|
2015-01-01 22:17:41 +01:00
|
|
|
:param str username: the user name of an existing MySQL user
|
2015-01-01 14:33:53 +01:00
|
|
|
:return: True if the database has been deleted, False otherwise
|
|
|
|
:rtype: boolean
|
|
|
|
|
|
|
|
"""
|
2015-01-01 22:17:41 +01:00
|
|
|
conn = _get_connection()
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute(
|
|
|
|
"""
|
2015-01-01 23:14:37 +01:00
|
|
|
REVOKE ALL PRIVILEGES ON `%(dbname)s`.* FROM %%(username)s@'%%%%'
|
|
|
|
""" % {'dbname': dbname}, {'username': username})
|
2015-01-02 00:06:31 +01:00
|
|
|
curs.execute(
|
|
|
|
"""
|
|
|
|
DROP DATABASE `%(dbname)s`
|
|
|
|
""" % {'dbname': dbname})
|
2015-01-01 22:17:41 +01:00
|
|
|
conn.commit()
|
|
|
|
return True
|