Repository: bsquizz/ocdeployer

Issue:
AttributeError: 'LegacyEnvConfigHandler' object has no attribute 'env_dir_name'
ocdeployer deploy -f --sets vmaas --template-dir buildfactory -e builder-env.yml vmaas-qe --secrets-local-dir secrets/sanitized

```
INFO:ocdeployer.deploy:Handling config for service set 'vmaas'

Traceback (most recent call last):

  File "/iqe_venv/bin/ocdeployer", line 8, in <module>

    sys.exit(main())

  File "/iqe_venv/lib64/python3.6/site-packages/click/core.py", line 764, in __call__

    return self.main(*args, **kwargs)

  File "/iqe_venv/lib64/python3.6/site-packages/click/core.py", line 717, in main

    rv = self.invoke(ctx)

  File "/iqe_venv/lib64/python3.6/site-packages/click/core.py", line 1137, in invoke

    return _process_result(sub_ctx.command.invoke(sub_ctx))

  File "/iqe_venv/lib64/python3.6/site-packages/click/core.py", line 956, in invoke

    return ctx.invoke(self.callback, **ctx.params)

  File "/iqe_venv/lib64/python3.6/site-packages/click/core.py", line 555, in invoke

    return callback(*args, **kwargs)

  File "/iqe_venv/lib64/python3.6/site-packages/ocdeployer/__main__.py", line 375, in deploy_to_project

    dry_run=False,

  File "/iqe_venv/lib64/python3.6/site-packages/ocdeployer/deploy.py", line 523, in run

    all_processed_templates[service_set] = self._deploy_service_set(service_set)

  File "/iqe_venv/lib64/python3.6/site-packages/ocdeployer/deploy.py", line 430, in _deploy_service_set

    set_cfg = self._get_service_set_cfg(service_set, dir_path)

  File "/iqe_venv/lib64/python3.6/site-packages/ocdeployer/deploy.py", line 421, in _get_service_set_cfg

    set_env_cfg = self.env_config_handler.get_service_set_env_cfg(dir_path, service_set)

  File "/iqe_venv/lib64/python3.6/site-packages/ocdeployer/env.py", line 190, in get_service_set_env_cfg

    self._get_service_set_vars(service_set_dir, service_set), service_set=service_set

  File "/iqe_venv/lib64/python3.6/site-packages/ocdeployer/env.py", line 139, in _get_service_set_vars

    path = os.path.join(service_set_dir, self.env_dir_name)

AttributeError: 'LegacyEnvConfigHandler' object has no attribute 'env_dir_name'
```

Comments:

Patch:
diff --git a/ocdeployer/env.py b/ocdeployer/env.py
index 7ef3e7a..c644e9a 100644
--- a/ocdeployer/env.py
+++ b/ocdeployer/env.py
@@ -277,15 +277,12 @@ class LegacyEnvConfigHandler(EnvConfigHandler):
     def _get_env_name(file_path):
         return os.path.splitext(os.path.basename(file_path))[0]
 
-    def __init__(self, env_files):
+    def __init__(self, env_files, env_dir_name="env"):
         self.env_files = env_files
-        self._last_service_set = None
         _env_names = [self._get_env_name(fp) for fp in self.env_files]
-        self.env_names = _dedupe_preserve_order(_env_names)
-        if len(_env_names) != len(self.env_names):
-            log.warning("Duplicate env names provided: %s", _env_names)
+        super().__init__(_env_names, env_dir_name)
 
-    def _load_vars_per_env(self):
+    def _load_vars_per_env(self, path=None):
         data = {}
 
         for file_path in self.env_files:
diff --git a/ocdeployer/templates.py b/ocdeployer/templates.py
index 005fea1..4560693 100644
--- a/ocdeployer/templates.py
+++ b/ocdeployer/templates.py
@@ -160,6 +160,17 @@ class Template(object):
         with open(self.path, "r") as f:
             return self._jinja_safe(f.read())
 
+    @staticmethod
+    def _format_oc_parameter(param_value):
+        """
+        Hack around yaml dump behaviour for different datatypes
+        Examples:
+            yaml.dump(True) -> 'true\n...\n'
+            yaml.dump('True') -> "'True'\n"
+            yaml.dump('123') -> "'123'\n"
+        """
+        return yaml.dump(param_value).replace("\n...\n", "").strip()
+
     def _process_via_oc(self, content, parameters=None, label=None):
         """
         Run 'oc process' on the template and update content with the processed output
@@ -176,10 +187,11 @@ class Template(object):
         if not parameters:
             parameters = {}
 
-        # Create set of param strings to pass into 'oc process'
         params_and_vals = {}
         for param_name, param_value in parameters.items():
-            params_and_vals[param_name] = "{}={}".format(param_name, param_value)
+            params_and_vals[param_name] = "{}={}".format(
+                param_name, self._format_oc_parameter(param_value)
+            )
 
         extra_args = []
         # Only insert the parameter if it was defined in the template
diff --git a/requirements.txt b/requirements.txt
index 9630002..8547cb2 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,4 @@
-PyYAML
+PyYAML==5.3
 sh
 prompter
 click


Test patch:
diff --git a/tests/test_deploy.py b/tests/test_deploy.py
index ae53a1e..fcff4f4 100644
--- a/tests/test_deploy.py
+++ b/tests/test_deploy.py
@@ -10,7 +10,7 @@ def patched_runner(env_values, mock_load_vars_per_env, legacy=False):
     if not env_values:
         handler = None
     elif legacy:
-        handler = LegacyEnvConfigHandler(env_files=env_values)
+        handler = LegacyEnvConfigHandler(env_files=env_values, env_dir_name="envTEST")
         handler.env_names = env_values
     else:
         handler = EnvConfigHandler(env_names=env_values, env_dir_name="envTEST")
diff --git a/tests/test_templates.py b/tests/test_templates.py
new file mode 100644
index 0000000..d55d6fe
--- /dev/null
+++ b/tests/test_templates.py
@@ -0,0 +1,19 @@
+import pytest
+
+from ocdeployer.templates import Template
+
+
+@pytest.mark.parametrize(
+    'value,expected',
+    (
+        (True, 'true'),
+        ('True', "'True'"),
+        ('true', "'true'"),
+        ('123', "'123'"),
+        (123, '123'),
+        ('123:123:123', '123:123:123'),
+        ('some text', 'some text')
+    )
+)
+def test_template_oc_param_format(value, expected):
+    assert Template._format_oc_parameter(value) == expected