Skip to content

Commit

Permalink
feat: enable auto sync with parent site (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Aug 22, 2023
1 parent e3c5b75 commit 1c34d7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 84 deletions.
19 changes: 14 additions & 5 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,6 @@
}
]

PROJECTROLES_TEMPLATE_INCLUDE_PATH = env.path(
"PROJECTROLES_TEMPLATE_INCLUDE_PATH",
os.path.join(APPS_DIR, "templates", "include"),
)

CRISPY_TEMPLATE_PACK = "bootstrap4"

# STATIC FILE CONFIGURATION
Expand Down Expand Up @@ -577,6 +572,20 @@ def set_logging(debug):
"PROJECTROLES_INLINE_HEAD_INCLUDE", None
)


PROJECTROLES_TEMPLATE_INCLUDE_PATH = env.path(
"PROJECTROLES_TEMPLATE_INCLUDE_PATH",
os.path.join(APPS_DIR, "templates", "include"),
)

# Settings for syncing remote projects (interval is in minutes)
PROJECTROLES_TARGET_SYNC_ENABLE = env.bool(
"PROJECTROLES_TARGET_SYNC_ENABLE", default=False
)
PROJECTROLES_TARGET_SYNC_INTERVAL = env.int(
"PROJECTROLES_TARGET_SYNC_INTERVAL", default=5
)

# Optional projectroles settings
# PROJECTROLES_SECRET_LENGTH = 32
# PROJECTROLES_HELP_HIGHLIGHT_DAYS = 7
Expand Down
80 changes: 1 addition & 79 deletions kioscadmin/tasks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import json
import logging
import ssl
import urllib.request
from datetime import timedelta

import docker
import docker.errors
import dateutil.parser
from django.urls import reverse

from bgjobs.models import BackgroundJob
from celery.schedules import crontab
Expand All @@ -16,7 +12,7 @@
from django.utils import timezone

from containers.tasks import container_task
from projectroles.models import SODAR_CONSTANTS, RemoteSite
from projectroles.models import SODAR_CONSTANTS

from config.celery import app
from django.contrib import auth
Expand All @@ -40,8 +36,6 @@
connect_docker,
ACTION_TO_EXPECTED_STATE,
)
from projectroles.remote_projects import RemoteProjectAPI
from projectroles.views_api import CORE_API_MEDIA_TYPE, CORE_API_DEFAULT_VERSION

User = auth.get_user_model()
app_settings = AppSettingAPI()
Expand Down Expand Up @@ -242,77 +236,6 @@ def sync_container_state_with_last_user_action(_self):
job.save()


@app.task(bind=True)
def sync_upstream(_self):
if getattr(settings, "PROJECTROLES_DISABLE_CATEGORIES", False):
logger.info(
"Project categories and nesting disabled, " "remote sync disabled"
)
return

if settings.PROJECTROLES_SITE_MODE != SITE_MODE_TARGET:
logger.error("Site not in TARGET mode, unable to sync")
return

try:
site = RemoteSite.objects.get(mode=SITE_MODE_SOURCE)

except RemoteSite.DoesNotExist:
logger.error("No source site defined, unable to sync")
return

if getattr(settings, "PROJECTROLES_ALLOW_LOCAL_USERS", False):
logger.info(
"PROJECTROLES_ALLOW_LOCAL_USERS=True, will sync "
"roles for existing local users"
)

logger.info(
'Retrieving data from remote site "{}" ({})..'.format(
site.name, site.get_url()
)
)

api_url = site.get_url() + reverse(
"projectroles:api_remote_get", kwargs={"secret": site.secret}
)

try:
api_req = urllib.request.Request(api_url)
api_req.add_header(
"accept",
"{}; version={}".format(
CORE_API_MEDIA_TYPE, CORE_API_DEFAULT_VERSION
),
)
response = urllib.request.urlopen(api_req)
remote_data = json.loads(response.read())

except Exception as ex:
helper_text = ""
if (
isinstance(ex, urllib.error.URLError)
and isinstance(ex.reason, ssl.SSLError)
and ex.reason.reason == "WRONG_VERSION_NUMBER"
):
helper_text = " (most likely server cannot handle HTTPS requests)"

logger.error(
"Unable to retrieve data from remote site: {}{}".format(
ex, helper_text
)
)
return

remote_api = RemoteProjectAPI()
try:
remote_api.sync_source_data(site, remote_data)

except Exception as ex:
logger.error("Remote sync cancelled with exception: {}".format(ex))
return


@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **_kwargs):
"""Register periodic tasks"""
Expand All @@ -323,4 +246,3 @@ def setup_periodic_tasks(sender, **_kwargs):
sender.add_periodic_task(
crontab(hour=1, minute=11), sig=stop_inactive_containers.s()
)
sender.add_periodic_task(300, sig=sync_upstream.s())

0 comments on commit 1c34d7a

Please sign in to comment.