2014-10-14 13:50:19 +00:00
|
|
|
import textwrap
|
|
|
|
|
2014-07-09 16:17:43 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
from orchestra.contrib.orchestration import ServiceController, replace
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.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'
|
2015-03-04 21:06:16 +00:00
|
|
|
default_route_match = "database.type == 'mysql'"
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def save(self, database):
|
2014-10-17 10:04:47 +00:00
|
|
|
if database.type != database.MYSQL:
|
|
|
|
return
|
2014-10-15 19:29:58 +00:00
|
|
|
context = self.get_context(database)
|
|
|
|
# Not available on delete()
|
|
|
|
context['owner'] = database.owner
|
|
|
|
self.append(
|
|
|
|
"mysql -e 'CREATE DATABASE `%(database)s`;' || true" % context
|
|
|
|
)
|
2014-10-16 15:11:52 +00:00
|
|
|
# clean previous privileges
|
|
|
|
self.append("""mysql mysql -e 'DELETE FROM db WHERE db = "%(database)s";'""" % context)
|
2014-10-15 19:29:58 +00:00
|
|
|
for user in database.users.all():
|
|
|
|
context.update({
|
|
|
|
'username': user.username,
|
|
|
|
'grant': 'WITH GRANT OPTION' if user == context['owner'] else ''
|
|
|
|
})
|
2014-10-14 13:50:19 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2014-10-15 19:29:58 +00:00
|
|
|
mysql -e 'GRANT ALL PRIVILEGES ON `%(database)s`.* TO "%(username)s"@"%(host)s" %(grant)s;' \
|
2015-03-10 16:57:23 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def delete(self, database):
|
2014-10-17 10:04:47 +00:00
|
|
|
if database.type != database.MYSQL:
|
|
|
|
return
|
2014-10-15 19:29:58 +00:00
|
|
|
context = self.get_context(database)
|
2015-03-25 15:45:04 +00:00
|
|
|
self.append("mysql -e 'DROP DATABASE `%(database)s`;' || exit_code=1" % context)
|
2015-02-25 13:24:11 +00:00
|
|
|
self.append("mysql mysql -e 'DELETE FROM db WHERE db = \"%(database)s\";'" % context)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.append("mysql -e 'FLUSH PRIVILEGES;'")
|
2015-03-27 19:50:54 +00:00
|
|
|
super(MySQLBackend, self).commit()
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def get_context(self, database):
|
2015-04-05 18:02:36 +00:00
|
|
|
context = {
|
2014-05-08 16:59:35 +00:00
|
|
|
'database': database.name,
|
|
|
|
'host': settings.DATABASES_DEFAULT_HOST,
|
|
|
|
}
|
2015-04-05 18:02:36 +00:00
|
|
|
return replace(replace(context, "'", '"'), ';', '')
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
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'
|
2015-03-04 21:06:16 +00:00
|
|
|
default_route_match = "databaseuser.type == 'mysql'"
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-09 17:04:12 +00:00
|
|
|
def save(self, user):
|
2014-10-17 10:04:47 +00:00
|
|
|
if user.type != user.MYSQL:
|
|
|
|
return
|
2014-10-15 19:29:58 +00:00
|
|
|
context = self.get_context(user)
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
mysql -e 'CREATE USER "%(username)s"@"%(host)s";' || true \
|
2015-03-10 16:57:23 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2014-10-15 19:29:58 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
mysql -e 'UPDATE mysql.user SET Password="%(password)s" WHERE User="%(username)s";' \
|
2015-03-10 16:57:23 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-09 17:04:12 +00:00
|
|
|
def delete(self, user):
|
2014-10-17 10:04:47 +00:00
|
|
|
if user.type != user.MYSQL:
|
|
|
|
return
|
2014-10-15 19:29:58 +00:00
|
|
|
context = self.get_context(user)
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
mysql -e 'DROP USER "%(username)s"@"%(host)s";' \
|
2015-03-10 16:57:23 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2014-10-14 13:50:19 +00:00
|
|
|
|
|
|
|
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):
|
2015-04-05 18:02:36 +00:00
|
|
|
context = {
|
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,
|
|
|
|
}
|
2015-04-05 18:02:36 +00:00
|
|
|
return replace(replace(context, "'", '"'), ';', '')
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
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-10-17 10:04:47 +00:00
|
|
|
if db.type != db.MYSQL:
|
|
|
|
return
|
2014-09-26 15:05:20 +00:00
|
|
|
context = self.get_context(db)
|
2014-10-14 13:50:19 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2014-11-18 13:59:21 +00:00
|
|
|
mysql -e 'UPDATE db SET Insert_priv="N", Create_priv="N" WHERE Db="%(db_name)s";'\
|
2015-03-10 16:57:23 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2014-07-16 15:20:16 +00:00
|
|
|
|
|
|
|
def recovery(self, db):
|
2014-10-17 10:04:47 +00:00
|
|
|
if db.type != db.MYSQL:
|
|
|
|
return
|
2014-09-26 15:05:20 +00:00
|
|
|
context = self.get_context(db)
|
2014-10-14 13:50:19 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2014-11-18 13:59:21 +00:00
|
|
|
mysql -e 'UPDATE db SET Insert_priv="Y", Create_priv="Y" WHERE Db="%(db_name)s";'\
|
2015-03-10 16:57:23 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2014-11-18 13:59:21 +00:00
|
|
|
|
|
|
|
def prepare(self):
|
2014-11-21 13:53:39 +00:00
|
|
|
super(MysqlDisk, self).prepare()
|
2014-11-18 13:59:21 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
function monitor () {
|
|
|
|
{ du -bs "/var/lib/mysql/$1" || echo 0; } | awk {'print $1'}
|
|
|
|
}"""))
|
|
|
|
# Slower way
|
|
|
|
#self.append(textwrap.dedent("""\
|
|
|
|
# function monitor () {
|
|
|
|
# mysql -B -e "
|
|
|
|
# SELECT IFNULL(sum(data_length + index_length), 0) 'Size'
|
|
|
|
# FROM information_schema.TABLES
|
|
|
|
# WHERE table_schema = '$1';
|
|
|
|
# " | tail -n 1
|
|
|
|
# }"""))
|
2014-07-16 15:20:16 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
def monitor(self, db):
|
|
|
|
if db.type != db.MYSQL:
|
|
|
|
return
|
|
|
|
context = self.get_context(db)
|
|
|
|
self.append('echo %(db_id)s $(monitor "%(db_name)s")' % context)
|
2014-07-16 15:20:16 +00:00
|
|
|
|
|
|
|
def get_context(self, db):
|
2015-04-05 18:02:36 +00:00
|
|
|
context = {
|
2014-07-16 15:20:16 +00:00
|
|
|
'db_name': db.name,
|
|
|
|
'db_id': db.pk,
|
|
|
|
}
|
2015-04-05 18:02:36 +00:00
|
|
|
return replace(replace(context, "'", '"'), ';', '')
|