2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
This module contains the signal handlers of the :py:mod:`userdbs` app.
|
|
|
|
|
2016-09-24 21:57:28 +02:00
|
|
|
The module starts Celery_ tasks.
|
|
|
|
|
|
|
|
.. _Celery: http://www.celeryproject.org/
|
|
|
|
|
2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.db.models.signals import post_delete, post_save
|
|
|
|
from django.dispatch import receiver
|
2023-02-18 22:46:48 +01:00
|
|
|
from passlib.pwd import genword
|
|
|
|
|
|
|
|
from mysqltasks.tasks import (
|
|
|
|
create_mysql_database,
|
|
|
|
create_mysql_user,
|
|
|
|
delete_mysql_database,
|
|
|
|
delete_mysql_user,
|
|
|
|
set_mysql_userpassword,
|
|
|
|
)
|
|
|
|
from pgsqltasks.tasks import (
|
|
|
|
create_pgsql_database,
|
|
|
|
create_pgsql_user,
|
|
|
|
delete_pgsql_database,
|
|
|
|
delete_pgsql_user,
|
|
|
|
set_pgsql_userpassword,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
from taskresults.models import TaskResult
|
|
|
|
|
|
|
|
from .models import DB_TYPES, DatabaseUser, UserDatabase, password_set
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(password_set, sender=DatabaseUser)
|
|
|
|
def handle_dbuser_password_set(sender, instance, password, **kwargs):
|
|
|
|
"""
|
|
|
|
Signal handler triggered by password changes for
|
|
|
|
:py:class:`userdbs.models.DatabaseUser` instances.
|
|
|
|
|
2016-09-24 21:57:28 +02:00
|
|
|
:param sender: the sender of the signal
|
|
|
|
:param instance: the Database user instance
|
|
|
|
:param str password: the new password for the database user
|
|
|
|
|
|
|
|
This signal handler starts Celery_ tasks depending on the db_type value of
|
|
|
|
the database user.
|
|
|
|
|
|
|
|
.. blockdiag::
|
|
|
|
:desctable:
|
|
|
|
|
|
|
|
blockdiag {
|
|
|
|
node_width = 200;
|
|
|
|
|
|
|
|
A -> B;
|
|
|
|
A -> C;
|
|
|
|
|
|
|
|
A [ label = "", shape = beginpoint,
|
|
|
|
description = "this signal handler" ];
|
|
|
|
B [ label = "set mysql userpassword", color = "PowderBlue",
|
|
|
|
description = ":py:func:`set_mysql_userpassword()
|
|
|
|
<mysqltasks.tasks.set_mysql_userpassword>` called with
|
|
|
|
database username and password" ];
|
|
|
|
C [ label = "set pgsql userpassword", color = "DodgerBlue",
|
|
|
|
description = ":py:func:`set_pgsql_userpassword()
|
|
|
|
<pgsqltasks.tasks.set_pgsql_userpassword>` called with
|
|
|
|
database username and password" ];
|
|
|
|
}
|
|
|
|
|
2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
if instance.db_type == DB_TYPES.mysql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_dbuser_password_set",
|
2015-12-07 01:23:07 +01:00
|
|
|
set_mysql_userpassword.s(instance.name, password),
|
2023-02-18 22:46:48 +01:00
|
|
|
"mysql password change",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"MySQL password change has been requested in task %s", taskresult.task_id
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
elif instance.db_type == DB_TYPES.pgsql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_dbuser_password_set",
|
2015-12-07 01:23:07 +01:00
|
|
|
set_pgsql_userpassword.s(instance.name, password),
|
2023-02-18 22:46:48 +01:00
|
|
|
"pgsql password change",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"PostgreSQL password change has been requested in task %s",
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
2023-02-18 22:46:48 +01:00
|
|
|
"Password change has been requested for unknown database %s"
|
|
|
|
" the request has been ignored.",
|
|
|
|
instance.db_type,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=DatabaseUser)
|
|
|
|
def handle_dbuser_created(sender, instance, created, **kwargs):
|
|
|
|
"""
|
|
|
|
Signal handler triggered after the creation of or updates to
|
|
|
|
:py:class:`userdbs.models.DatabaseUser` instances.
|
|
|
|
|
2016-09-24 21:57:28 +02:00
|
|
|
:param sender: the sender of the signal
|
|
|
|
:param instance: the DatabaseUser instance
|
|
|
|
:param bool created: whether this signal handler is called for a newly
|
|
|
|
created instance
|
|
|
|
|
|
|
|
This signal handler starts Celery_ tasks depending on the db_type value of
|
|
|
|
the database user.
|
|
|
|
|
|
|
|
.. blockdiag::
|
|
|
|
:desctable:
|
|
|
|
|
|
|
|
blockdiag {
|
|
|
|
node_width = 200;
|
|
|
|
|
|
|
|
A -> B;
|
|
|
|
A -> C;
|
|
|
|
|
|
|
|
A [ label = "", shape = beginpoint,
|
|
|
|
description = "this signal handler" ];
|
|
|
|
B [ label = "create mysql user", color = "PowderBlue",
|
|
|
|
description = ":py:func:`create_mysql_user()
|
|
|
|
<mysqltasks.tasks.create_mysql_user>` called with database
|
|
|
|
username and password" ];
|
|
|
|
C [ label = "create pgsql user", color = "DodgerBlue",
|
|
|
|
description = ":py:func:`create_pgsql_user
|
|
|
|
<pgsqltasks.tasks.create_pgsql_user>` called with database
|
|
|
|
username and password" ];
|
|
|
|
}
|
|
|
|
|
2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
if created:
|
2023-02-18 22:46:48 +01:00
|
|
|
password = kwargs.get("password", genword())
|
2015-12-07 01:23:07 +01:00
|
|
|
# TODO: send GPG encrypted mail with this information
|
|
|
|
if instance.db_type == DB_TYPES.mysql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_dbuser_created",
|
2015-12-07 01:23:07 +01:00
|
|
|
create_mysql_user.s(instance.name, password),
|
2023-02-18 22:46:48 +01:00
|
|
|
"mysql user creation",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"A new MySQL user %s creation has been requested in task %s",
|
|
|
|
instance.name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
elif instance.db_type == DB_TYPES.pgsql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_dbuser_created",
|
2015-12-07 01:23:07 +01:00
|
|
|
create_pgsql_user.s(instance.name, password),
|
2023-02-18 22:46:48 +01:00
|
|
|
"pgsql user creation",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"A new PostgreSQL user %s creation has been requested in task" " %s",
|
|
|
|
instance.name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
2023-02-18 22:46:48 +01:00
|
|
|
"created DatabaseUser for unknown database type %s", instance.db_type
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.debug(
|
2023-02-18 22:46:48 +01:00
|
|
|
"database user %s has been %s", instance, created and "created" or "updated"
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=DatabaseUser)
|
|
|
|
def handle_dbuser_deleted(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Signal handler triggered after the deletion of
|
|
|
|
:py:class:`userdbs.models.DatabaseUser` instances.
|
|
|
|
|
2016-09-24 21:57:28 +02:00
|
|
|
:param sender: the sender of the signal
|
|
|
|
:param instance: the DatabaseUser instance
|
|
|
|
|
|
|
|
This signal handler starts Celery_ tasks depending on the db_type value of
|
|
|
|
the database user.
|
|
|
|
|
|
|
|
.. blockdiag::
|
|
|
|
:desctable:
|
|
|
|
|
|
|
|
blockdiag {
|
|
|
|
node_width = 200;
|
|
|
|
|
|
|
|
A -> B;
|
|
|
|
A -> C;
|
|
|
|
|
|
|
|
A [ label = "", shape = beginpoint,
|
|
|
|
description = "this signal handler" ];
|
|
|
|
B [ label = "delete mysql user", color = "PowderBlue",
|
|
|
|
description = ":py:func:`delete_mysql_user()
|
|
|
|
<mysqltasks.tasks.delete_mysql_user>` called with username
|
|
|
|
from instance.name" ];
|
|
|
|
C [ label = "delete pgsql user", color = "DodgerBlue",
|
|
|
|
description = ":py:func:`delete_pgsql_user()
|
|
|
|
<pgsqltasks.tasks.delete_pgsql_user>` called with username
|
|
|
|
from instance.name" ];
|
|
|
|
}
|
2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
if instance.db_type == DB_TYPES.mysql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_dbuser_deleted",
|
2015-12-07 01:23:07 +01:00
|
|
|
delete_mysql_user.s(instance.name),
|
2023-02-18 22:46:48 +01:00
|
|
|
"mysql user deletion",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"MySQL user %s deletion has been requested in task %s",
|
|
|
|
instance.name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
elif instance.db_type == DB_TYPES.pgsql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_dbuser_deleted",
|
2015-12-07 01:23:07 +01:00
|
|
|
delete_pgsql_user.s(instance.name),
|
2023-02-18 22:46:48 +01:00
|
|
|
"pgsql user deletion",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"PostgreSQL user %s deletion has been requested in task %s",
|
|
|
|
instance.name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
2023-02-18 22:46:48 +01:00
|
|
|
"deleted DatabaseUser %s for unknown database type %s",
|
|
|
|
instance.name,
|
|
|
|
instance.db_type,
|
|
|
|
)
|
|
|
|
_LOGGER.debug("database user %s has been deleted", instance)
|
2015-12-07 01:23:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=UserDatabase)
|
|
|
|
def handle_userdb_created(sender, instance, created, **kwargs):
|
|
|
|
"""
|
|
|
|
Signal handler triggered after the creation of or updates to
|
|
|
|
:py:class:`userdbs.models.UserDatabase` instances.
|
|
|
|
|
2016-09-24 21:57:28 +02:00
|
|
|
:param sender: the sender of the signal
|
|
|
|
:param instance: the UserDatabase instance
|
|
|
|
:param bool created: whether this signal handler has been called for a
|
|
|
|
newly created instance
|
|
|
|
|
|
|
|
This signal handler starts Celery_ tasks depending on the db_type value of
|
|
|
|
the database user owning the UserDatabase instance.
|
|
|
|
|
|
|
|
.. blockdiag::
|
|
|
|
:desctable:
|
|
|
|
|
|
|
|
blockdiag {
|
|
|
|
node_width = 200;
|
|
|
|
|
|
|
|
A -> B;
|
|
|
|
A -> C;
|
|
|
|
|
|
|
|
A [ label = "", shape = beginpoint,
|
|
|
|
description = "this signal handler" ];
|
|
|
|
B [ label = "create mysql database", color = "PowderBlue",
|
|
|
|
description = ":py:func:`create_mysql_database()
|
|
|
|
<mysqltasks.tasks.create_mysql_database>` called with database
|
|
|
|
name and username" ];
|
|
|
|
C [ label = "create pgsql database", color = "DodgerBlue",
|
|
|
|
description = ":py:func:`create_pgsql_database()
|
|
|
|
<pgsqltasks.tasks.create_pgsql_database>` called with database
|
|
|
|
name and username" ];
|
|
|
|
}
|
2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
if created:
|
|
|
|
if instance.db_user.db_type == DB_TYPES.mysql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_userdb_created",
|
|
|
|
create_mysql_database.s(instance.db_name, instance.db_user.name),
|
|
|
|
"mysql database creation",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"The creation of a new MySQL database %s has been requested in"
|
|
|
|
" task %s",
|
|
|
|
instance.db_name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
elif instance.db_user.db_type == DB_TYPES.pgsql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_userdb_created",
|
|
|
|
create_pgsql_database.s(instance.db_name, instance.db_user.name),
|
|
|
|
"pgsql database creation",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"The creation of a new PostgreSQL database %s has been"
|
|
|
|
" requested in task %s",
|
|
|
|
instance.db_name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
2023-02-18 22:46:48 +01:00
|
|
|
"created UserDatabase for unknown database type %s",
|
|
|
|
instance.db_user.db_type,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.debug(
|
2023-02-18 22:46:48 +01:00
|
|
|
"database %s has been %s", instance, created and "created" or "updated"
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=UserDatabase)
|
|
|
|
def handle_userdb_deleted(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Signal handler triggered after the deletion of
|
|
|
|
:py:class:`userdbs.models.UserDatabase` instances.
|
|
|
|
|
2016-09-24 21:57:28 +02:00
|
|
|
:param sender: the sender of the signal
|
|
|
|
:param instance: the UserDatabase instance
|
|
|
|
|
|
|
|
This signal handler starts Celery_ tasks depending on the db_type value of
|
|
|
|
the database user owning the UserDatabase instance.
|
|
|
|
|
|
|
|
.. blockdiag::
|
|
|
|
:desctable:
|
|
|
|
|
|
|
|
blockdiag {
|
|
|
|
node_width = 200;
|
|
|
|
|
|
|
|
A -> B;
|
|
|
|
A -> C;
|
|
|
|
|
|
|
|
A [ label = "", shape = beginpoint,
|
|
|
|
description = "this signal handler" ];
|
|
|
|
B [ label = "delete mysql database", color = "PowderBlue",
|
|
|
|
description = ":py:func:`delete_mysql_user()
|
|
|
|
<mysqltasks.tasks.delete_mysql_user>` called with database
|
|
|
|
name and username" ];
|
|
|
|
C [ label = "delete pgsql database", color = "DodgerBlue",
|
|
|
|
description = ":py:func:`delete_pgsql_user()
|
|
|
|
<pgsqltasks.tasks.delete_pgsql_user>` called with database
|
|
|
|
name" ];
|
|
|
|
}
|
2015-12-07 01:23:07 +01:00
|
|
|
"""
|
|
|
|
if instance.db_user.db_type == DB_TYPES.mysql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_userdb_deleted",
|
2015-12-07 01:23:07 +01:00
|
|
|
delete_mysql_database.s(instance.db_name, instance.db_user.name),
|
2023-02-18 22:46:48 +01:00
|
|
|
"mysql database deletion",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"The deletion of MySQL database %s has been requested in task %s",
|
|
|
|
instance.db_name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
elif instance.db_user.db_type == DB_TYPES.pgsql:
|
|
|
|
taskresult = TaskResult.objects.create_task_result(
|
2023-02-18 22:46:48 +01:00
|
|
|
"handle_userdb_deleted",
|
2015-12-07 01:23:07 +01:00
|
|
|
delete_pgsql_database.s(instance.db_name),
|
2023-02-18 22:46:48 +01:00
|
|
|
"pgsql database deletion",
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
_LOGGER.info(
|
2023-02-18 22:46:48 +01:00
|
|
|
"The deletion of PostgreSQL database %s has been requested in " " task %s",
|
|
|
|
instance.db_name,
|
|
|
|
taskresult.task_id,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
2023-02-18 22:46:48 +01:00
|
|
|
"deleted UserDatabase %s of unknown type %s",
|
|
|
|
instance.db_name,
|
|
|
|
instance.db_type,
|
|
|
|
)
|
2015-12-07 01:23:07 +01:00
|
|
|
pass
|
2023-02-18 22:46:48 +01:00
|
|
|
_LOGGER.debug("database %s has been deleted", instance)
|