django-orchestra-test/orchestra/apps/databases/backends.py

117 lines
3.6 KiB
Python
Raw Normal View History

import textwrap
2014-07-09 16:17:43 +00:00
from django.utils.translation import ugettext_lazy as _
from orchestra.apps.orchestration import ServiceController
from orchestra.apps.resources import ServiceMonitor
2014-05-08 16:59:35 +00:00
from . import settings
2014-10-07 13:08:59 +00:00
class MySQLBackend(ServiceController):
2014-05-08 16:59:35 +00:00
verbose_name = "MySQL database"
model = 'databases.Database'
def save(self, database):
if database.type == database.MYSQL:
context = self.get_context(database)
2014-07-09 16:17:43 +00:00
self.append(
"mysql -e 'CREATE DATABASE `%(database)s`;' || true" % context
2014-07-09 16:17:43 +00:00
)
self.append(textwrap.dedent("""\
mysql -e 'GRANT ALL PRIVILEGES ON `%(database)s`.* TO "%(owner)s"@"%(host)s" WITH GRANT OPTION;' \
""" % context
))
2014-05-08 16:59:35 +00:00
def delete(self, database):
if database.type == database.MYSQL:
context = self.get_context(database)
self.append("mysql -e 'DROP DATABASE `%(database)s`;'" % context)
def commit(self):
self.append("mysql -e 'FLUSH PRIVILEGES;'")
def get_context(self, database):
return {
'owner': database.owner.username,
'database': database.name,
'host': settings.DATABASES_DEFAULT_HOST,
}
2014-07-09 16:17:43 +00:00
class MySQLUserBackend(ServiceController):
2014-05-08 16:59:35 +00:00
verbose_name = "MySQL user"
model = 'databases.DatabaseUser'
2014-10-09 17:04:12 +00:00
def save(self, user):
if user.type == user.MYSQL:
context = self.get_context(user)
self.append(textwrap.dedent("""\
mysql -e 'CREATE USER "%(username)s"@"%(host)s";' || true \
""" % context
))
self.append(textwrap.dedent("""\
mysql -e 'UPDATE mysql.user SET Password="%(password)s" WHERE User="%(username)s";' \
""" % context
))
2014-05-08 16:59:35 +00:00
2014-10-09 17:04:12 +00:00
def delete(self, user):
if user.type == user.MYSQL:
2014-05-08 16:59:35 +00:00
context = self.get_context(database)
self.append(textwrap.dedent("""\
mysql -e 'DROP USER "%(username)s"@"%(host)s";' \
""" % context
))
def commit(self):
self.append("mysql -e 'FLUSH PRIVILEGES;'")
2014-05-08 16:59:35 +00:00
2014-10-09 17:04:12 +00:00
def get_context(self, user):
2014-05-08 16:59:35 +00:00
return {
2014-10-09 17:04:12 +00:00
'username': user.username,
'password': user.password,
2014-05-08 16:59:35 +00:00
'host': settings.DATABASES_DEFAULT_HOST,
}
2014-10-15 12:47:28 +00:00
# TODO https://docs.djangoproject.com/en/1.7/ref/signals/#m2m-changed
2014-07-09 16:17:43 +00:00
class MySQLPermissionBackend(ServiceController):
2014-05-08 16:59:35 +00:00
model = 'databases.UserDatabaseRelation'
verbose_name = "MySQL permission"
2014-07-09 16:17:43 +00:00
class MysqlDisk(ServiceMonitor):
2014-09-14 09:52:45 +00:00
model = 'databases.Database'
2014-07-09 16:17:43 +00:00
verbose_name = _("MySQL disk")
2014-07-16 15:20:16 +00:00
def exceeded(self, db):
2014-09-26 15:05:20 +00:00
context = self.get_context(db)
self.append(textwrap.dedent("""\
mysql -e 'UPDATE db SET Insert_priv="N", Create_priv="N" WHERE Db="%(db_name)s";' \
""" % context
))
2014-07-16 15:20:16 +00:00
def recovery(self, db):
2014-09-26 15:05:20 +00:00
context = self.get_context(db)
self.append(textwrap.dedent("""\
mysql -e 'UPDATE db SET Insert_priv="Y", Create_priv="Y" WHERE Db="%(db_name)s";' \
""" % context
))
2014-07-16 15:20:16 +00:00
def monitor(self, db):
2014-09-26 15:05:20 +00:00
context = self.get_context(db)
self.append(textwrap.dedent("""\
echo %(db_id)s $(mysql -B -e '"
SELECT sum( data_length + index_length ) "Size"
FROM information_schema.TABLES
WHERE table_schema = "gisp"
GROUP BY table_schema;' | tail -n 1) \
""" % context
))
2014-07-16 15:20:16 +00:00
def get_context(self, db):
return {
'db_name': db.name,
'db_id': db.pk,
}