Skip to content

Commit

Permalink
Merge pull request #137 from galaxyproject/add_global_context
Browse files Browse the repository at this point in the history
Add global context
  • Loading branch information
nuwang committed Oct 27, 2020
2 parents a5f97b3 + 669b7a6 commit 1e41a8e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cloudman/cloudman/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@
}
}

CM_GLOBAL_CONTEXT_PATH = "/opt/cloudman/global_context.yaml"

if os.path.isfile(CM_GLOBAL_CONTEXT_PATH) and os.access(CM_GLOBAL_CONTEXT_PATH, os.R_OK):
import yaml
with open(CM_GLOBAL_CONTEXT_PATH) as f:
print(f"Loading cloudman global context from: {CM_GLOBAL_CONTEXT_PATH}")
CM_GLOBAL_CONTEXT = yaml.load(f)
else:
CM_GLOBAL_CONTEXT = {}

# Allow settings to be overridden in a cloudman/settings_local.py
try:
from cloudman.settings_local import * # noqa
Expand Down
9 changes: 7 additions & 2 deletions cloudman/helmsman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ansible.plugins.filter import ipaddr

from django.apps import apps
from django.conf import settings
from django.db import IntegrityError
from django.db import transaction

Expand Down Expand Up @@ -460,8 +461,12 @@ def screenshot_url(self):
return self.template_obj.screenshot_url

def render_values(self, context):
default_context = self.context or {}
new_context = dict(default_context)
global_context = settings.CM_GLOBAL_CONTEXT
# 1. Start with global context
new_context = {'global': dict(global_context)}
# 2. Add template default context
new_context.update(self.context or {})
# 3. Add user specified context
new_context.update(context or {})
jinja2_env = self._get_jinja2_env()
tmpl = jinja2_env.from_string(
Expand Down
12 changes: 12 additions & 0 deletions cloudman/helmsman/management/commands/helmsman_load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@ def handle(self, *args, **options):
@staticmethod
def process_settings(settings):
if settings.get('repositories'):
print("Processing chart repositories...")
Command.process_helm_repos(settings.get('repositories'))
else:
print("No chart repositories defined.")

if settings.get('template_registries'):
print("Processing template registries...")
Command.process_template_registries(settings.get('template_registries'))
else:
print("No template registries defined.")

if settings.get('install_templates'):
print("Processing install templates...")
TplCommand.process_install_templates(settings.get('install_templates'))
else:
print("No install templates defined.")

if settings.get('charts'):
print("Processing charts in helmsman config...")
Command.process_helm_charts(settings.get('charts'))
else:
print("No charts defined in helmsan config.")

@staticmethod
def process_helm_repos(repositories):
Expand Down
21 changes: 20 additions & 1 deletion cloudman/helmsman/tests/test_helmsman_unit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.auth.models import User
from django.test import TestCase
from django.test import TestCase, override_settings

from helmsman.api import HelmsManAPI
from helmsman.api import HMServiceContext
Expand Down Expand Up @@ -33,3 +33,22 @@ def test_render_values(self):

host_rendered = tpl.render_values(context={'domain': 'example.com'})
self.assertEquals(host_expected, host_rendered)

@override_settings(CM_GLOBAL_CONTEXT={'domain': 'globaldomain.com'})
def test_render_values_global_context(self):
base_tpl = ('hosts:\n'
' - ~\n'
' {%- if not (context.global.domain | ipaddr) %}\n'
' - "{{ context.global.domain }}"\n'
' {%- endif %}')

tpl = self.client.templates.create(
'dummytpl', 'dummyrepo', 'dummychart',
template=base_tpl)

host_expected = ('hosts:\n'
' - ~\n'
' - "globaldomain.com"')

host_rendered = tpl.render_values(context={'domain': 'example.com'})
self.assertEquals(host_expected, host_rendered)

0 comments on commit 1e41a8e

Please sign in to comment.