django-orchestra/orchestra/contrib/orchestration/models.py

267 lines
9.4 KiB
Python
Raw Permalink Normal View History

2015-10-05 12:09:11 +00:00
import logging
import socket
2015-05-01 17:23:22 +00:00
from django.contrib.contenttypes.fields import GenericForeignKey
2014-05-08 16:59:35 +00:00
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
2014-05-08 16:59:35 +00:00
from django.db import models
from django.utils.encoding import force_str
2015-04-07 15:14:49 +00:00
from django.utils.functional import cached_property
2014-10-07 13:08:59 +00:00
from django.utils.module_loading import autodiscover_modules
from django.utils.translation import gettext_lazy as _
2014-05-08 16:59:35 +00:00
from orchestra.core.validators import validate_ip_address, validate_hostname, OrValidator
from orchestra.models.fields import NullableCharField, MultiSelectField
2014-05-08 16:59:35 +00:00
2015-03-04 21:06:16 +00:00
from . import settings
2014-05-08 16:59:35 +00:00
from .backends import ServiceBackend
2015-10-05 12:09:11 +00:00
logger = logging.getLogger(__name__)
2014-05-08 16:59:35 +00:00
class Server(models.Model):
""" Machine runing daemons (services) """
name = models.CharField(_("name"), max_length=256, unique=True,
help_text=_("Verbose name or hostname of this server."))
2014-07-10 15:19:06 +00:00
address = NullableCharField(_("address"), max_length=256, blank=True,
validators=[OrValidator(validate_ip_address, validate_hostname)],
null=True, unique=True, help_text=_(
"Optional IP address or domain name. If blank, name field will be used for address resolution.<br>"
"If the IP address never changes you can set this field and save DNS requests."))
2014-05-08 16:59:35 +00:00
description = models.TextField(_("description"), blank=True)
os = models.CharField(_("operative system"), max_length=32,
2015-04-05 10:46:24 +00:00
choices=settings.ORCHESTRATION_OS_CHOICES,
default=settings.ORCHESTRATION_DEFAULT_OS)
2015-04-02 16:14:55 +00:00
def __str__(self):
2015-05-05 19:42:55 +00:00
return self.name or str(self.address)
2014-05-08 16:59:35 +00:00
def get_address(self):
if self.address:
return self.address
return self.name
def get_ip(self):
address = self.get_address()
try:
return validate_ip_address(address)
except ValidationError:
return socket.gethostbyname(self.name)
def clean(self):
self.name = self.name.strip()
2021-05-13 08:57:48 +00:00
if self.address:
self.address = self.address.strip()
elif self.name:
validate = OrValidator(validate_ip_address, validate_hostname)
validate_hostname(self.name)
try:
validate(self.name)
except ValidationError as err:
raise ValidationError({
'name': _("Name should be a valid hostname or IP address when address is not provided.")
})
2014-05-08 16:59:35 +00:00
class BackendLog(models.Model):
RECEIVED = 'RECEIVED'
TIMEOUT = 'TIMEOUT'
STARTED = 'STARTED'
SUCCESS = 'SUCCESS'
FAILURE = 'FAILURE'
ERROR = 'ERROR'
REVOKED = 'REVOKED'
ABORTED = 'ABORTED'
2015-05-05 19:42:55 +00:00
NOTHING = 'NOTHING'
# Special state for mocked backendlogs
EXCEPTION = 'EXCEPTION'
2014-05-08 16:59:35 +00:00
STATES = (
(RECEIVED, RECEIVED),
(TIMEOUT, TIMEOUT),
(STARTED, STARTED),
(SUCCESS, SUCCESS),
(FAILURE, FAILURE),
(ERROR, ERROR),
(ABORTED, ABORTED),
2014-05-08 16:59:35 +00:00
(REVOKED, REVOKED),
2015-05-05 19:42:55 +00:00
(NOTHING, NOTHING),
2014-05-08 16:59:35 +00:00
)
2014-05-08 16:59:35 +00:00
backend = models.CharField(_("backend"), max_length=256)
state = models.CharField(_("state"), max_length=16, choices=STATES, default=RECEIVED)
server = models.ForeignKey(Server, verbose_name=_("server"), related_name='execution_logs', on_delete=models.CASCADE)
2014-05-08 16:59:35 +00:00
script = models.TextField(_("script"))
2014-07-17 16:09:24 +00:00
stdout = models.TextField(_("stdout"))
stderr = models.TextField(_("stderr"))
2014-05-08 16:59:35 +00:00
traceback = models.TextField(_("traceback"))
exit_code = models.IntegerField(_("exit code"), null=True)
task_id = models.CharField(_("task ID"), max_length=36, unique=True, null=True,
2015-04-05 10:46:24 +00:00
help_text="Celery task ID when used as execution backend")
created_at = models.DateTimeField(_("created"), auto_now_add=True, db_index=True)
2014-09-26 15:05:20 +00:00
updated_at = models.DateTimeField(_("updated"), auto_now=True)
2014-05-08 16:59:35 +00:00
class Meta:
2014-09-24 20:09:41 +00:00
get_latest_by = 'id'
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-11-16 18:39:31 +00:00
return "%s@%s" % (self.backend, self.server)
2014-05-08 16:59:35 +00:00
@property
def execution_time(self):
2014-09-30 14:46:29 +00:00
return (self.updated_at-self.created_at).total_seconds()
@property
def has_finished(self):
return self.state not in (self.STARTED, self.RECEIVED)
2015-05-12 12:38:40 +00:00
@property
def is_success(self):
return self.state in (self.SUCCESS, self.NOTHING)
2014-09-10 16:53:09 +00:00
def backend_class(self):
return ServiceBackend.get_backend(self.backend)
2014-05-08 16:59:35 +00:00
2015-05-13 12:16:51 +00:00
class BackendOperationQuerySet(models.QuerySet):
def create(self, **kwargs):
instance = kwargs.get('instance')
2015-09-20 10:57:13 +00:00
if instance and 'instance_repr' not in kwargs:
kwargs['instance_repr'] = force_str(instance)[:256]
2015-05-13 12:16:51 +00:00
return super(BackendOperationQuerySet, self).create(**kwargs)
2014-05-08 16:59:35 +00:00
class BackendOperation(models.Model):
"""
Encapsulates an operation, storing its related object, the action and the backend.
"""
log = models.ForeignKey('orchestration.BackendLog', related_name='operations', on_delete=models.CASCADE)
2014-07-10 15:19:06 +00:00
backend = models.CharField(_("backend"), max_length=256)
2014-07-09 16:17:43 +00:00
action = models.CharField(_("action"), max_length=64)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
2015-05-13 12:16:51 +00:00
object_id = models.PositiveIntegerField(null=True)
instance_repr = models.CharField(_("instance representation"), max_length=256)
2015-05-01 17:23:22 +00:00
instance = GenericForeignKey('content_type', 'object_id')
2015-05-13 12:16:51 +00:00
objects = BackendOperationQuerySet.as_manager()
2014-05-08 16:59:35 +00:00
class Meta:
verbose_name = _("Operation")
verbose_name_plural = _("Operations")
2016-02-19 10:11:28 +00:00
index_together = (
('content_type', 'object_id'),
)
2015-04-02 16:14:55 +00:00
def __str__(self):
return '%s.%s(%s)' % (self.backend, self.action, self.instance or self.instance_repr)
2015-04-07 15:14:49 +00:00
@cached_property
2014-07-10 15:19:06 +00:00
def backend_class(self):
return ServiceBackend.get_backend(self.backend)
2014-05-08 16:59:35 +00:00
2014-10-07 13:08:59 +00:00
autodiscover_modules('backends')
2014-05-08 16:59:35 +00:00
class RouteQuerySet(models.QuerySet):
def get_for_operation(self, operation, **kwargs):
cache = kwargs.get('cache', {})
if not cache:
for route in self.filter(is_active=True).select_related('host'):
2015-10-05 12:09:11 +00:00
try:
backend_class = route.backend_class
except KeyError:
logger.warning("Backed '%s' not installed." % route.backend)
else:
for action in backend_class.get_actions():
key = (route.backend, action)
try:
cache[key].append(route)
except KeyError:
cache[key] = [route]
routes = []
backend_cls = operation.backend
key = (backend_cls.get_name(), operation.action)
try:
target_routes = cache[key]
except KeyError:
pass
else:
for route in target_routes:
if route.matches(operation.instance):
routes.append(route)
return routes
2014-05-08 16:59:35 +00:00
class Route(models.Model):
"""
Defines the routing that determine in which server a backend is executed
"""
backend = models.CharField(_("backend"), max_length=256,
2015-04-05 10:46:24 +00:00
choices=ServiceBackend.get_choices())
host = models.ForeignKey(Server, verbose_name=_("host"), related_name='routes', on_delete=models.CASCADE)
2014-05-08 16:59:35 +00:00
match = models.CharField(_("match"), max_length=256, blank=True, default='True',
2015-04-05 10:46:24 +00:00
help_text=_("Python expression used for selecting the targe host, "
"<em>instance</em> referes to the current object."))
run_async = models.BooleanField(default=False,
2015-05-06 14:39:25 +00:00
help_text=_("Whether or not block the request/response cycle waitting this backend to "
"finish its execution. Usually you want slave servers to run asynchronously."))
async_actions = MultiSelectField(max_length=256, blank=True, choices=[],
help_text=_("Specify individual actions to be executed asynchronoulsy."))
2014-05-08 16:59:35 +00:00
# method = models.CharField(_("method"), max_lenght=32, choices=method_choices,
# default=MethodBackend.get_default())
2014-09-30 10:20:11 +00:00
is_active = models.BooleanField(_("active"), default=True)
objects = RouteQuerySet.as_manager()
2014-05-08 16:59:35 +00:00
class Meta:
unique_together = ('backend', 'host')
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-05-08 16:59:35 +00:00
return "%s@%s" % (self.backend, self.host)
2015-04-07 15:14:49 +00:00
@cached_property
2015-03-29 16:10:07 +00:00
def backend_class(self):
return ServiceBackend.get_backend(self.backend)
2015-03-29 16:10:07 +00:00
def clean(self):
if not self.match:
self.match = 'True'
if self.backend:
2015-10-05 12:09:11 +00:00
try:
backend_class = self.backend_class
except KeyError:
raise ValidationError({
'backend': _("Backend '%s' is not installed.") % self.backend
})
backend_model = backend_class.model_class()
2015-03-29 16:10:07 +00:00
try:
obj = backend_model.objects.all()[0]
except IndexError:
return
try:
bool(self.matches(obj))
2015-04-01 15:49:21 +00:00
except Exception as exception:
2015-03-29 16:10:07 +00:00
name = type(exception).__name__
2015-06-09 11:16:36 +00:00
raise ValidationError(': '.join((name, str(exception))))
def action_is_async(self, action):
return action in self.async_actions
2014-07-17 16:09:24 +00:00
def matches(self, instance):
safe_locals = {
2014-10-04 09:29:18 +00:00
'instance': instance,
'obj': instance,
instance._meta.model_name: instance,
2014-07-17 16:09:24 +00:00
}
return eval(self.match, safe_locals)
2014-05-08 16:59:35 +00:00
def enable(self):
self.is_active = True
self.save()
2014-05-08 16:59:35 +00:00
def disable(self):
self.is_active = False
self.save()