2015-03-10 21:51:10 +00:00
|
|
|
import os
|
2015-04-10 15:03:38 +00:00
|
|
|
from collections import OrderedDict
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from django.db import models
|
2015-03-04 21:06:16 +00:00
|
|
|
from django.utils.functional import cached_property
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-03-04 21:06:16 +00:00
|
|
|
from jsonfield import JSONField
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-05-04 19:52:53 +00:00
|
|
|
from orchestra.core import validators
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
from . import settings
|
2015-03-27 19:50:54 +00:00
|
|
|
from .fields import VirtualDatabaseRelation, VirtualDatabaseUserRelation
|
2015-03-10 16:57:23 +00:00
|
|
|
from .options import AppOption
|
2015-03-04 21:06:16 +00:00
|
|
|
from .types import AppType
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebApp(models.Model):
|
|
|
|
""" Represents a web application """
|
2015-04-16 13:15:21 +00:00
|
|
|
name = models.CharField(_("name"), max_length=128, validators=[validators.validate_name],
|
|
|
|
help_text=_("The app will be installed in %s") % settings.WEBAPPS_BASE_DIR)
|
2015-04-05 10:46:24 +00:00
|
|
|
type = models.CharField(_("type"), max_length=32, choices=AppType.get_choices())
|
2021-04-22 08:28:00 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("Account"), related_name='webapps')
|
2015-03-23 15:36:51 +00:00
|
|
|
data = JSONField(_("data"), blank=True, default={},
|
2015-04-05 10:46:24 +00:00
|
|
|
help_text=_("Extra information dependent of each service."))
|
2021-04-22 08:28:00 +00:00
|
|
|
target_server = models.ForeignKey('orchestration.Server', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("Target Server"), related_name='webapps')
|
2021-01-13 15:41:36 +00:00
|
|
|
comments = models.TextField(default="", blank=True)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-27 19:50:54 +00:00
|
|
|
# CMS webapps usually need a database and dbuser, with these virtual fields we tell the ORM to delete them
|
|
|
|
databases = VirtualDatabaseRelation('databases.Database')
|
|
|
|
databaseusers = VirtualDatabaseUserRelation('databases.DatabaseUser')
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('name', 'account')
|
|
|
|
verbose_name = _("Web App")
|
|
|
|
verbose_name_plural = _("Web Apps")
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2015-03-02 10:37:25 +00:00
|
|
|
return self.name
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-27 19:17:26 +00:00
|
|
|
def get_description(self):
|
|
|
|
return self.get_type_display()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
@cached_property
|
|
|
|
def type_class(self):
|
2015-03-31 12:39:08 +00:00
|
|
|
return AppType.get(self.type)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
@cached_property
|
|
|
|
def type_instance(self):
|
|
|
|
""" Per request lived type_instance """
|
2015-03-11 16:32:33 +00:00
|
|
|
return self.type_class(self)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-27 19:17:26 +00:00
|
|
|
def clean(self):
|
2015-03-04 21:06:16 +00:00
|
|
|
apptype = self.type_instance
|
2015-03-11 16:32:33 +00:00
|
|
|
apptype.validate()
|
2015-04-30 11:24:18 +00:00
|
|
|
a = apptype.clean_data()
|
2015-03-11 16:32:33 +00:00
|
|
|
self.data = apptype.clean_data()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-06-05 09:57:06 +00:00
|
|
|
def get_options(self, **kwargs):
|
2015-06-04 10:23:51 +00:00
|
|
|
options = OrderedDict()
|
2015-04-20 14:23:10 +00:00
|
|
|
qs = WebAppOption.objects.filter(**kwargs)
|
|
|
|
for name, value in qs.values_list('name', 'value').order_by('name'):
|
|
|
|
if name in options:
|
2015-06-22 22:38:39 +00:00
|
|
|
if AppOption.get(name).comma_separated:
|
|
|
|
options[name] = options[name].rstrip(',') + ',' + value.lstrip(',')
|
|
|
|
else:
|
|
|
|
options[name] = max(options[name], value)
|
2015-04-20 14:23:10 +00:00
|
|
|
else:
|
|
|
|
options[name] = value
|
|
|
|
return options
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-24 10:16:46 +00:00
|
|
|
def get_directive(self):
|
2015-03-11 16:32:33 +00:00
|
|
|
return self.type_instance.get_directive()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-05-26 08:15:34 +00:00
|
|
|
def get_base_path(self):
|
2014-05-08 16:59:35 +00:00
|
|
|
context = {
|
2014-10-24 12:12:20 +00:00
|
|
|
'home': self.get_user().get_home(),
|
2015-03-02 10:37:25 +00:00
|
|
|
'app_name': self.name,
|
2014-05-08 16:59:35 +00:00
|
|
|
}
|
2016-05-26 08:15:34 +00:00
|
|
|
return settings.WEBAPPS_BASE_DIR % context
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2016-05-26 08:15:34 +00:00
|
|
|
def get_path(self):
|
|
|
|
path = self.get_base_path()
|
2015-03-10 16:57:23 +00:00
|
|
|
public_root = self.options.filter(name='public-root').first()
|
|
|
|
if public_root:
|
|
|
|
path = os.path.join(path, public_root.value)
|
2015-03-10 21:51:10 +00:00
|
|
|
return os.path.normpath(path.replace('//', '/'))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-24 10:16:46 +00:00
|
|
|
def get_user(self):
|
|
|
|
return self.account.main_systemuser
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def get_username(self):
|
2014-10-24 10:16:46 +00:00
|
|
|
return self.get_user().username
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
def get_groupname(self):
|
|
|
|
return self.get_username()
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebAppOption(models.Model):
|
2021-04-22 08:28:00 +00:00
|
|
|
webapp = models.ForeignKey(WebApp, on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("Web application"), related_name='options')
|
2015-10-01 16:02:26 +00:00
|
|
|
name = models.CharField(_("name"), max_length=128,
|
|
|
|
choices=AppType.get_group_options_choices())
|
2014-05-08 16:59:35 +00:00
|
|
|
value = models.CharField(_("value"), max_length=256)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('webapp', 'name')
|
|
|
|
verbose_name = _("option")
|
|
|
|
verbose_name_plural = _("options")
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-05-08 16:59:35 +00:00
|
|
|
return self.name
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
@cached_property
|
|
|
|
def option_class(self):
|
2015-03-31 12:39:08 +00:00
|
|
|
return AppOption.get(self.name)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-10 11:46:48 +00:00
|
|
|
@cached_property
|
|
|
|
def option_instance(self):
|
|
|
|
""" Per request lived option instance """
|
2015-03-11 16:32:33 +00:00
|
|
|
return self.option_class(self)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean(self):
|
2015-03-11 16:32:33 +00:00
|
|
|
self.option_instance.validate()
|