django-orchestra/orchestra/contrib/webapps/backends/__init__.py

88 lines
3.4 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
import pkgutil
2014-10-10 14:39:46 +00:00
import textwrap
2014-05-08 16:59:35 +00:00
from .. import settings
2014-05-08 16:59:35 +00:00
class WebAppServiceMixin(object):
model = 'webapps.WebApp'
2015-04-23 14:34:04 +00:00
related_models = (
('webapps.WebAppOption', 'webapp'),
)
directive = None
2015-04-24 11:39:20 +00:00
doc_settings = (settings,
('WEBAPPS_UNDER_CONSTRUCTION_PATH', 'WEBAPPS_MOVE_ON_DELETE_PATH',)
)
2023-07-25 20:16:38 +00:00
def check_webapp_dir(self, context):
self.append(textwrap.dedent("""
# Create webapp dir
CREATED=0
if [[ ! -e %(app_path)s ]]; then
mkdir -p %(app_path)s
CREATED=1
elif [[ -z $( ls -A %(app_path)s ) ]]; then
CREATED=1
fi""") % context
)
2014-05-08 16:59:35 +00:00
def create_webapp_dir(self, context):
self.append(textwrap.dedent("""
# Create webapp dir
CREATED=0
2015-10-15 22:31:54 +00:00
if [[ ! -e %(app_path)s ]]; then
CREATED=1
mkdir -p %(app_path)s
chown %(user)s:%(group)s %(app_path)s
fi""") % context
)
2014-05-08 16:59:35 +00:00
def set_under_construction(self, context):
if context['under_construction_path']:
self.append(textwrap.dedent("""
# Set under construction if needed
2015-10-07 11:44:30 +00:00
if [[ $CREATED == 1 && ! $(ls -A %(app_path)s | head -n1) ]]; then
# Async wait some seconds for other backends to lock app_path or cp under construction
2015-05-12 12:38:40 +00:00
nohup bash -c '
sleep 2
2015-10-07 11:44:30 +00:00
if [[ ! $(ls -A %(app_path)s | head -n1) ]]; then
2015-05-06 14:39:25 +00:00
cp -r %(under_construction_path)s %(app_path)s
2015-10-15 22:31:54 +00:00
chown -R %(user)s:%(group)s %(app_path)s/*
2015-05-12 12:38:40 +00:00
fi' &> /dev/null &
fi""") % context
)
2014-05-08 16:59:35 +00:00
def delete_webapp_dir(self, context):
2015-04-09 14:32:10 +00:00
if context['deleted_app_path']:
self.append(textwrap.dedent("""\
# Move app into WEBAPPS_MOVE_ON_DELETE_PATH, nesting if exists.
deleted_app_path="%(deleted_app_path)s"
while [[ -e $deleted_app_path ]]; do
deleted_app_path="${deleted_app_path}/$(basename ${deleted_app_path})"
done
mv %(app_path)s $deleted_app_path || exit_code=$?
""") % context
)
2015-04-09 14:32:10 +00:00
else:
self.append("rm -fr %(app_path)s" % context)
2014-05-08 16:59:35 +00:00
def get_context(self, webapp):
context = webapp.type_instance.get_directive_context()
context.update({
2023-07-25 20:16:38 +00:00
'user': webapp.sftpuser.username if webapp.target_server.name in settings.WEBAPP_NEW_SERVERS else webapp.get_username(),
'group': webapp.sftpuser.username if webapp.target_server.name in settings.WEBAPP_NEW_SERVERS else webapp.get_groupname(),
2015-03-04 21:06:16 +00:00
'app_name': webapp.name,
2015-04-27 14:54:17 +00:00
'app_type': webapp.type,
2015-04-09 14:32:10 +00:00
'app_path': webapp.get_path(),
2014-05-08 16:59:35 +00:00
'banner': self.get_banner(),
2015-04-24 11:39:20 +00:00
'under_construction_path': settings.WEBAPPS_UNDER_CONSTRUCTION_PATH,
2015-03-27 19:50:54 +00:00
'is_mounted': webapp.content_set.exists(),
2023-07-25 20:16:38 +00:00
'target_server': webapp.target_server,
})
2015-04-09 14:32:10 +00:00
context['deleted_app_path'] = settings.WEBAPPS_MOVE_ON_DELETE_PATH % context
return context
2014-05-08 16:59:35 +00:00
for __, module_name, __ in pkgutil.walk_packages(__path__):
# sorry for the exec(), but Import module function fails :(
2014-05-08 16:59:35 +00:00
exec('from . import %s' % module_name)