Skip to content

Commit 8080462

Browse files
author
Carme, Pamy
committed
fix: change exception for a warning as the stack validation can fail because docker is not installed
1 parent 4a0384a commit 8080462

5 files changed

Lines changed: 26 additions & 7 deletions

File tree

stackconfig/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from stackconfig.stackconfig import StackConfigCompose
66
from stackconfig.utils.jinja2_utils import render_jijnja2_compose
7+
from stackconfig.utils.yaml_utils import remove_files
78

89

910
@click.command()
@@ -42,16 +43,20 @@
4243
@click.option("--version", help="Set valid version for the final docker-compose file", default=None)
4344
def cli(file, output, j2template=None, j2data=None, version=None):
4445
try:
46+
jinja_files = []
4547
file = list(set(file))
4648
if j2template:
4749
if not j2data:
4850
print(
4951
"WARNING: No yaml data file should be provided with the data to render the jinja template"
5052
)
51-
file = file + render_jijnja2_compose(list(set(j2template)), j2data)
53+
jinja_files = render_jijnja2_compose(list(set(j2template)), j2data)
54+
file = file + jinja_files
5255
stack_config = StackConfigCompose(file, output, version)
5356
stack_config.merge_stack_compose()
5457
print(f"INFO: The docker-compose file was saved in: {output}")
5558
except Exception as exc:
5659
print(f"ERROR: {str(exc)}")
5760
exit(1)
61+
finally:
62+
remove_files(jinja_files)

stackconfig/stackconfig.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from compose.cli.command import get_config_from_options
55

66

7-
from stackconfig.utils.yaml_utils import save_compose, load_compose
7+
from stackconfig.utils.yaml_utils import save_compose, load_compose, remove_files
88
from stackconfig.utils.validate_compose import validate_docker_stack_compose
99

1010

@@ -30,6 +30,8 @@ def merge_stack_compose(self):
3030
save_compose(compose_config_str, name_tmp_file, as_text=True)
3131
self.compose_dict = load_compose(name_tmp_file)
3232

33+
remove_files(name_tmp_file)
34+
3335
if self.version and isinstance(self.version, str):
3436
self.compose_dict["version"] = self.version
3537

stackconfig/utils/jinja2_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from jinja2.nativetypes import NativeEnvironment
44
from jinja2 import Undefined
55

6-
from stackconfig.utils.yaml_utils import save_compose, load_compose
6+
from stackconfig.utils.yaml_utils import load_compose
77

88

99
def render_jijnja2_compose(templates, data_file=None, data_dict=None, jinja2_env=None):
@@ -36,7 +36,8 @@ def render_jijnja2_compose(templates, data_file=None, data_dict=None, jinja2_env
3636
name = f.name
3737
except Exception as err:
3838
raise Exception(
39-
f"Please be sure the template {template} is valid. Error: {str(err)}. Error-type: {type(err).__name__}"
39+
f"Please be sure the template {template} is valid. Error: {str(err)}. Error-type: {type(err).__name__}. "
40+
f"Be sure to escape docker syntax. i.e test-{{{{ '{{.Task.ID}}' }}}}"
4041
)
4142
template_files.append(name)
4243
return template_files

stackconfig/utils/validate_compose.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tempfile
22
import subprocess
33

4-
from stackconfig.utils.yaml_utils import save_compose
4+
from stackconfig.utils.yaml_utils import save_compose, remove_files
55

66

77
def validate_docker_stack_compose(compose_dict):
@@ -15,5 +15,8 @@ def validate_docker_stack_compose(compose_dict):
1515
result = subprocess.getoutput(
1616
f"docker stack deploy -c {name_tmp_file} tmp_stack"
1717
)
18+
remove_files([name_tmp_file])
1819
if "Additional property z_dummy is not allowed" not in result:
19-
raise Exception(f"Invalid compose file: Please check {result}")
20+
print(f"INFO: docker stack validation failed: {result}. "
21+
f"This is only a warning as docker may not be installed or "
22+
f"the user doesnt have perms to execute docker cmds.")

stackconfig/utils/yaml_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import yaml
2+
import os
23

34

45
def save_compose(compose_definition, path_to_save, as_text=False):
@@ -30,7 +31,14 @@ def load_compose(compose_path):
3031
with open(compose_path) as f:
3132
compose_definition = yaml.safe_load(f)
3233
except FileNotFoundError:
33-
print("WARNING no compose file found at: {}".format(compose_path))
34+
print("ERROR: no compose file found at: {}".format(compose_path))
3435
compose_definition = {}
3536

3637
return compose_definition
38+
39+
40+
def remove_files(files_list):
41+
if isinstance(files_list, str):
42+
files_list = [files_list]
43+
for jf in files_list:
44+
os.remove(jf)

0 commit comments

Comments
 (0)