2016-02-09 12:17:42 +00:00
|
|
|
import importlib
|
|
|
|
import os
|
2015-10-02 11:08:23 +00:00
|
|
|
from functools import lru_cache
|
2015-10-01 16:02:26 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError, ObjectDoesNotExist
|
2014-09-26 19:21:09 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
from orchestra import plugins
|
2015-09-30 13:22:17 +00:00
|
|
|
from orchestra.contrib.databases.models import Database, DatabaseUser
|
2015-04-07 15:14:49 +00:00
|
|
|
from orchestra.contrib.orchestration import Operation
|
2015-10-01 16:02:26 +00:00
|
|
|
from orchestra.contrib.websites.models import Website, WebsiteDirective
|
|
|
|
from orchestra.utils.apps import isinstalled
|
2015-10-29 18:19:00 +00:00
|
|
|
from orchestra.utils.functional import cached
|
2015-09-21 10:28:49 +00:00
|
|
|
from orchestra.utils.python import import_class
|
2014-09-26 19:21:09 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
from . import helpers
|
2014-09-26 19:21:09 +00:00
|
|
|
from .. import settings
|
2015-09-21 10:28:49 +00:00
|
|
|
from ..forms import SaaSPasswordForm
|
2014-11-20 15:34:59 +00:00
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
|
2016-02-09 12:17:42 +00:00
|
|
|
class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount):
|
2015-10-01 16:02:26 +00:00
|
|
|
PROTOCOL_MAP = {
|
|
|
|
'http': (Website.HTTP, (Website.HTTP, Website.HTTP_AND_HTTPS)),
|
|
|
|
'https': (Website.HTTPS_ONLY, (Website.HTTPS, Website.HTTP_AND_HTTPS, Website.HTTPS_ONLY)),
|
|
|
|
}
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
name = None
|
|
|
|
verbose_name = None
|
2015-09-21 10:28:49 +00:00
|
|
|
form = SaaSPasswordForm
|
2015-03-25 15:45:04 +00:00
|
|
|
site_domain = None
|
2015-03-23 15:36:51 +00:00
|
|
|
has_custom_domain = False
|
2014-10-11 12:43:08 +00:00
|
|
|
icon = 'orchestra/icons/apps.png'
|
|
|
|
class_verbose_name = _("Software as a Service")
|
2015-03-23 15:36:51 +00:00
|
|
|
plugin_field = 'service'
|
2015-10-01 16:02:26 +00:00
|
|
|
allow_custom_url = False
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2014-09-26 19:21:09 +00:00
|
|
|
@classmethod
|
2015-10-02 11:08:23 +00:00
|
|
|
@lru_cache()
|
2016-02-09 12:17:42 +00:00
|
|
|
def get_plugins(cls, all=False):
|
|
|
|
if all:
|
|
|
|
for module in os.listdir(os.path.dirname(__file__)):
|
|
|
|
if module not in ('options.py', '__init__.py') and module[-3:] == '.py':
|
|
|
|
importlib.import_module('.'+module[:-3], __package__)
|
|
|
|
plugins = super().get_plugins()
|
|
|
|
else:
|
|
|
|
plugins = []
|
|
|
|
for cls in settings.SAAS_ENABLED_SERVICES:
|
|
|
|
plugins.append(import_class(cls))
|
2014-09-26 19:21:09 +00:00
|
|
|
return plugins
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2016-02-11 14:24:09 +00:00
|
|
|
def get_change_readonly_fields(cls):
|
|
|
|
fields = super(SoftwareService, cls).get_change_readonly_fields()
|
2015-03-25 15:45:04 +00:00
|
|
|
return fields + ('name',)
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-03-25 15:45:04 +00:00
|
|
|
def get_site_domain(self):
|
2015-09-29 12:35:22 +00:00
|
|
|
context = {
|
|
|
|
'site_name': self.instance.name,
|
|
|
|
'name': self.instance.name,
|
|
|
|
}
|
|
|
|
return self.site_domain % context
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def clean(self):
|
|
|
|
if self.allow_custom_url:
|
|
|
|
if self.instance.custom_url:
|
|
|
|
if isinstalled('orchestra.contrib.websites'):
|
|
|
|
helpers.clean_custom_url(self)
|
|
|
|
elif self.instance.custom_url:
|
|
|
|
raise ValidationError({
|
|
|
|
'custom_url': _("Custom URL not allowed for this service."),
|
|
|
|
})
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def clean_data(self):
|
|
|
|
data = super(SoftwareService, self).clean_data()
|
|
|
|
if not self.instance.pk:
|
|
|
|
try:
|
|
|
|
log = Operation.execute_action(self.instance, 'validate_creation')[0]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
else:
|
2015-04-20 14:23:10 +00:00
|
|
|
if log.state != log.SUCCESS:
|
|
|
|
raise ValidationError(_("Validate creation execution has failed."))
|
2015-04-02 16:14:55 +00:00
|
|
|
errors = {}
|
|
|
|
if 'user-exists' in log.stdout:
|
|
|
|
errors['name'] = _("User with this username already exists.")
|
2015-04-07 15:14:49 +00:00
|
|
|
if 'email-exists' in log.stdout:
|
2015-04-02 16:14:55 +00:00
|
|
|
errors['email'] = _("User with this email address already exists.")
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
return data
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def get_directive_name(self):
|
|
|
|
return '%s-saas' % self.name
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def get_directive(self, *args):
|
|
|
|
if not args:
|
|
|
|
instance = self.instance
|
|
|
|
else:
|
|
|
|
instance = args[0]
|
|
|
|
url = urlparse(instance.custom_url)
|
|
|
|
account = instance.account
|
|
|
|
return WebsiteDirective.objects.get(
|
|
|
|
name=self.get_directive_name(),
|
|
|
|
value=url.path,
|
|
|
|
website__protocol__in=self.PROTOCOL_MAP[url.scheme][1],
|
|
|
|
website__domains__name=url.netloc,
|
|
|
|
website__account=account,
|
|
|
|
)
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def get_website(self):
|
|
|
|
url = urlparse(self.instance.custom_url)
|
|
|
|
account = self.instance.account
|
|
|
|
return Website.objects.get(
|
|
|
|
protocol__in=self.PROTOCOL_MAP[url.scheme][1],
|
|
|
|
domains__name=url.netloc,
|
|
|
|
account=account,
|
|
|
|
directives__name=self.get_directive_name(),
|
|
|
|
directives__value=url.path,
|
|
|
|
)
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def create_or_update_directive(self):
|
|
|
|
return helpers.create_or_update_directive(self)
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-01 16:02:26 +00:00
|
|
|
def delete_directive(self):
|
2015-10-08 13:54:39 +00:00
|
|
|
directive = None
|
2015-10-01 16:02:26 +00:00
|
|
|
try:
|
|
|
|
old = type(self.instance).objects.get(pk=self.instance.pk)
|
2015-10-08 13:54:39 +00:00
|
|
|
if old.custom_url:
|
|
|
|
directive = self.get_directive(old)
|
2015-10-01 16:02:26 +00:00
|
|
|
except ObjectDoesNotExist:
|
2015-10-08 13:54:39 +00:00
|
|
|
return
|
|
|
|
if directive is not None:
|
2015-10-01 16:02:26 +00:00
|
|
|
directive.delete()
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-03-23 15:36:51 +00:00
|
|
|
def save(self):
|
2015-10-01 16:02:26 +00:00
|
|
|
# pre instance.save()
|
|
|
|
if isinstalled('orchestra.contrib.websites'):
|
|
|
|
if self.instance.custom_url:
|
|
|
|
self.create_or_update_directive()
|
|
|
|
elif self.instance.pk:
|
|
|
|
self.delete_directive()
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-03-23 15:36:51 +00:00
|
|
|
def delete(self):
|
2015-10-01 16:02:26 +00:00
|
|
|
if isinstalled('orchestra.contrib.websites'):
|
|
|
|
self.delete_directive()
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-03-26 16:00:30 +00:00
|
|
|
def get_related(self):
|
|
|
|
return []
|
2015-09-30 13:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DBSoftwareService(SoftwareService):
|
|
|
|
db_name = None
|
|
|
|
db_user = None
|
2016-02-09 12:17:42 +00:00
|
|
|
abstract = True
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-09-30 13:22:17 +00:00
|
|
|
def get_db_name(self):
|
|
|
|
context = {
|
|
|
|
'name': self.instance.name,
|
|
|
|
'site_name': self.instance.name,
|
|
|
|
}
|
|
|
|
db_name = self.db_name % context
|
|
|
|
# Limit for mysql database names
|
|
|
|
return db_name[:65]
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-09-30 13:22:17 +00:00
|
|
|
def get_db_user(self):
|
|
|
|
return self.db_user
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-10-29 18:19:00 +00:00
|
|
|
@cached
|
2015-09-30 13:22:17 +00:00
|
|
|
def get_account(self):
|
2016-04-30 11:23:13 +00:00
|
|
|
account_model = self.instance._meta.get_field('account')
|
2021-04-22 12:44:47 +00:00
|
|
|
return account_model.remote_field.model.objects.get_main()
|
|
|
|
|
2015-09-30 13:22:17 +00:00
|
|
|
def validate(self):
|
|
|
|
super(DBSoftwareService, self).validate()
|
|
|
|
create = not self.instance.pk
|
|
|
|
if create:
|
|
|
|
account = self.get_account()
|
|
|
|
# Validated Database
|
|
|
|
db_user = self.get_db_user()
|
|
|
|
try:
|
|
|
|
DatabaseUser.objects.get(username=db_user)
|
|
|
|
except DatabaseUser.DoesNotExist:
|
|
|
|
raise ValidationError(
|
|
|
|
_("Global database user for PHPList '%(db_user)s' does not exists.") % {
|
|
|
|
'db_user': db_user
|
|
|
|
}
|
|
|
|
)
|
|
|
|
db = Database(name=self.get_db_name(), account=account)
|
|
|
|
try:
|
|
|
|
db.full_clean()
|
|
|
|
except ValidationError as e:
|
|
|
|
raise ValidationError({
|
|
|
|
'name': e.messages,
|
|
|
|
})
|
2021-04-22 12:44:47 +00:00
|
|
|
|
2015-09-30 13:22:17 +00:00
|
|
|
def save(self):
|
2015-10-01 16:02:26 +00:00
|
|
|
super(DBSoftwareService, self).save()
|
2015-09-30 13:22:17 +00:00
|
|
|
account = self.get_account()
|
|
|
|
# Database
|
|
|
|
db_name = self.get_db_name()
|
|
|
|
db_user = self.get_db_user()
|
|
|
|
db, db_created = account.databases.get_or_create(name=db_name, type=Database.MYSQL)
|
|
|
|
user = DatabaseUser.objects.get(username=db_user)
|
|
|
|
db.users.add(user)
|
|
|
|
self.instance.database_id = db.pk
|