mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-07-20 07:14:25 +02:00
Activated loading of env depending on if it exist
This commit is contained in:
parent
45624037b1
commit
85195e01f9
14
filter_plugins/has_env.py
Normal file
14
filter_plugins/has_env.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def has_env(application_id, base_dir='.'):
|
||||||
|
"""
|
||||||
|
Check if env.j2 exists under roles/{{ application_id }}/templates/env.j2
|
||||||
|
"""
|
||||||
|
path = os.path.join(base_dir, 'roles', application_id, 'templates', 'env.j2')
|
||||||
|
return os.path.isfile(path)
|
||||||
|
|
||||||
|
class FilterModule(object):
|
||||||
|
def filters(self):
|
||||||
|
return {
|
||||||
|
'has_env': has_env,
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
{# Base for docker services #}
|
{# Base for docker services #}
|
||||||
|
|
||||||
restart: {{docker_restart_policy}}
|
restart: {{docker_restart_policy}}
|
||||||
|
{% if application_id | has_env %}
|
||||||
env_file:
|
env_file:
|
||||||
- "{{docker_compose.files.env}}"
|
- "{{docker_compose.files.env}}"
|
||||||
|
{% endif %}
|
||||||
logging:
|
logging:
|
||||||
driver: journald
|
driver: journald
|
||||||
|
|
||||||
|
35
tests/unit/filter_plugins/test_has_env.py
Normal file
35
tests/unit/filter_plugins/test_has_env.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import unittest
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
# Import the filter directly
|
||||||
|
from filter_plugins.has_env import has_env
|
||||||
|
|
||||||
|
class TestHasEnvFilter(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
# Create a test directory structure
|
||||||
|
self.base_dir = './testdata'
|
||||||
|
self.app_with_env = 'app_with_env'
|
||||||
|
self.app_without_env = 'app_without_env'
|
||||||
|
os.makedirs(os.path.join(self.base_dir, 'roles', self.app_with_env, 'templates'), exist_ok=True)
|
||||||
|
os.makedirs(os.path.join(self.base_dir, 'roles', self.app_without_env, 'templates'), exist_ok=True)
|
||||||
|
|
||||||
|
# Create an empty env.j2 file
|
||||||
|
with open(os.path.join(self.base_dir, 'roles', self.app_with_env, 'templates', 'env.j2'), 'w') as f:
|
||||||
|
f.write('')
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
# Clean up the test data
|
||||||
|
if os.path.exists(self.base_dir):
|
||||||
|
shutil.rmtree(self.base_dir)
|
||||||
|
|
||||||
|
def test_env_exists(self):
|
||||||
|
"""Test that has_env returns True if env.j2 exists."""
|
||||||
|
self.assertTrue(has_env(self.app_with_env, base_dir=self.base_dir))
|
||||||
|
|
||||||
|
def test_env_not_exists(self):
|
||||||
|
"""Test that has_env returns False if env.j2 does not exist."""
|
||||||
|
self.assertFalse(has_env(self.app_without_env, base_dir=self.base_dir))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user