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-07-09 16:17:43 +00:00
|
|
|
class MySQLDBBackend(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`;'" % context
|
|
|
|
)
|
|
|
|
self.append(
|
|
|
|
"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'
|
|
|
|
|
|
|
|
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 USER \"%(username)s\"@\"%(host)s\";'" % context
|
|
|
|
)
|
|
|
|
self.append(
|
|
|
|
"mysql -e 'UPDATE mysql.user SET Password=\"%(password)s\" "
|
|
|
|
" WHERE User=\"%(username)s\";'" % context
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def delete(self, database):
|
|
|
|
if database.type == database.MYSQL:
|
|
|
|
context = self.get_context(database)
|
2014-07-09 16:17:43 +00:00
|
|
|
self.append(
|
|
|
|
"mysql -e 'DROP USER \"%(username)s\"@\"%(host)s\";'" % context
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def get_context(self, database):
|
|
|
|
return {
|
|
|
|
'username': database.username,
|
|
|
|
'password': database.password,
|
|
|
|
'host': settings.DATABASES_DEFAULT_HOST,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
model = 'database.Database'
|
|
|
|
resource = ServiceMonitor.DISK
|
|
|
|
verbose_name = _("MySQL disk")
|