add task implementations
This commit is contained in:
parent
e79d58d886
commit
1b8c4ce697
1 changed files with 66 additions and 2 deletions
|
@ -7,10 +7,21 @@ from __future__ import absolute_import, unicode_literals
|
|||
from celery import shared_task
|
||||
from celery.utils.log import get_task_logger
|
||||
|
||||
from gvamysql import settings
|
||||
from MySQLdb import connect
|
||||
|
||||
|
||||
_LOGGER = get_task_logger(__name__)
|
||||
|
||||
|
||||
def _get_connection():
|
||||
return connect(
|
||||
host=settings.GVAMYSQL_DBADMIN_HOST,
|
||||
port=settings.GVAMYSQL_DBADMIN_PORT,
|
||||
user=settings.GVAMYSQL_DBADMIN_USER,
|
||||
password=settings.GVAMYSQL_DBADMIN_PASSWORD)
|
||||
|
||||
|
||||
@shared_task
|
||||
def create_mysql_user(username, password):
|
||||
"""
|
||||
|
@ -22,6 +33,16 @@ def create_mysql_user(username, password):
|
|||
:rtype: str
|
||||
|
||||
"""
|
||||
conn = _get_connection()
|
||||
curs = conn.cursor()
|
||||
curs.execute(
|
||||
"""
|
||||
CREATE USER %(username)s@'%' IDENTIFIED BY %(password)s
|
||||
""",
|
||||
{'username': username, 'password': password}
|
||||
)
|
||||
conn.commit()
|
||||
return username
|
||||
|
||||
|
||||
@shared_task
|
||||
|
@ -35,6 +56,15 @@ def set_mysql_userpassword(username, password):
|
|||
:rtype: boolean
|
||||
|
||||
"""
|
||||
conn = _get_connection()
|
||||
curs = conn.cursor()
|
||||
curs.execute(
|
||||
"""
|
||||
SET PASSWORD FOR %(username)s = PASSWORD(%(password)s)
|
||||
""",
|
||||
{'username': username, 'password': password})
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
|
||||
@shared_task
|
||||
|
@ -47,6 +77,15 @@ def delete_mysql_user(username):
|
|||
:rtype: boolean
|
||||
|
||||
"""
|
||||
conn = _get_connection()
|
||||
curs = conn.cursor()
|
||||
curs.execute(
|
||||
"""
|
||||
DROP USER %(username)s@'%'
|
||||
""",
|
||||
{'username': username})
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
|
||||
@shared_task
|
||||
|
@ -60,15 +99,40 @@ def create_mysql_database(dbname, username):
|
|||
:rtype: str
|
||||
|
||||
"""
|
||||
conn = _get_connection()
|
||||
curs = conn.cursor()
|
||||
curs.execute(
|
||||
"""
|
||||
CREATE DATABASE %(dbname)s CHARACTER SET utf8 COLLATE utf8_german_ci
|
||||
""",
|
||||
{'dbname': dbname})
|
||||
curs.execute(
|
||||
"""
|
||||
GRANT ALL PRIVILEGES ON %(dbname)s.* TO %(username)s@'%'
|
||||
""",
|
||||
{'dbname': dbname, 'username': username})
|
||||
conn.commit()
|
||||
return dbname
|
||||
|
||||
|
||||
@shared_task
|
||||
def delete_mysql_database(dbname):
|
||||
def delete_mysql_database(dbname, username):
|
||||
"""
|
||||
This task deletes an existing MySQL database.
|
||||
This task deletes an existing MySQL database and revokes privileges of the
|
||||
given user on that database.
|
||||
|
||||
:param str dbname: database name
|
||||
:param str username: the user name of an existing MySQL user
|
||||
:return: True if the database has been deleted, False otherwise
|
||||
:rtype: boolean
|
||||
|
||||
"""
|
||||
conn = _get_connection()
|
||||
curs = conn.cursor()
|
||||
curs.execute(
|
||||
"""
|
||||
REVOKE ALL PRIVILEGES ON %(dbname)s.* FROM %(username)s@'%'
|
||||
""",
|
||||
{'dbname': dbname, 'username': username})
|
||||
conn.commit()
|
||||
return True
|
||||
|
|
Loading…
Reference in a new issue