Skip to content

Commit

Permalink
Feature/rcs/fix jinja arm parameters (#95)
Browse files Browse the repository at this point in the history
* Verify puff arm template parameter file

* change internal parameter name

* implement working dir and change jinja apply order

* change working dir name
  • Loading branch information
blueskyjunkie authored Oct 27, 2021
1 parent 4c11afd commit 39fe7e7
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 350 deletions.
18 changes: 10 additions & 8 deletions foodx_devops_tools/deploy_me/_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ def _construct_override_parameters(
async def _do_step_deployment(
this_step: ApplicationDeploymentDefinition,
deployment_data: FlattenedDeployment,
puff_parameter_data: PuffMapPaths,
puff_parameter_paths: PuffMapPaths,
this_context: str,
enable_validation: bool,
) -> None:
step_context = f"{this_context}.{this_step.name}"

log.debug(
f"deployment_data.context, {step_context}, {str(deployment_data.context)}" # noqa: E501
f"deployment_data.context, "
f"{step_context}, {str(deployment_data.context)}"
)
log.debug(
f"deployment_data.data, {step_context}, {str(deployment_data.data)}"
Expand All @@ -187,12 +188,6 @@ async def _do_step_deployment(
deployment_data.context.client,
this_step.resource_group,
)
template_files = deployment_data.construct_deployment_paths(
this_step.arm_file,
this_step.puff_file,
puff_parameter_data[this_step.name],
)
log.debug(f"template files, {template_files}")

await login_service_principal(deployment_data.data.azure_credentials)
if enable_validation:
Expand All @@ -207,6 +202,13 @@ async def _do_step_deployment(
template_parameters = deployment_data.construct_template_parameters()
log.debug(f"template parameters, {step_context}, {template_parameters}")

template_files = deployment_data.construct_deployment_paths(
this_step.arm_file,
this_step.puff_file,
puff_parameter_paths[this_step.name],
)
log.debug(f"template files, {template_files}")

deployment_files = await prepare_deployment_files(
template_files,
template_parameters,
Expand Down
62 changes: 33 additions & 29 deletions foodx_devops_tools/pipeline_config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
from foodx_devops_tools.azure.cloud import AzureCredentials
from foodx_devops_tools.patterns import SubscriptionData
from foodx_devops_tools.utilities.jinja2 import TemplateParameters
from foodx_devops_tools.utilities.templates import (
JINJA_FILE_PREFIX,
ArmTemplateParameters,
ArmTemplates,
TemplateFiles,
)
from foodx_devops_tools.utilities.templates import TemplateFiles, TemplatePaths

from ..deployment import DeploymentTuple
from ._exceptions import PipelineViewError
Expand Down Expand Up @@ -419,6 +414,21 @@ def construct_deployment_name(self: W, step_name: str) -> str:

return result[0:64]

@staticmethod
def __construct_working_directory(
parent_dir: pathlib.Path, working_name: str
) -> pathlib.Path:
working_dir = parent_dir / "working" / working_name

return working_dir

@staticmethod
def __encode_working_name(parameters: TemplateParameters) -> str:
"""Construct a working directory name from template parameters."""
# for now, use a simple predictable, non-empty value
value = "w"
return value

def construct_deployment_paths(
self: W,
specified_arm_file: typing.Optional[pathlib.Path],
Expand All @@ -432,13 +442,16 @@ def construct_deployment_paths(
specified_arm_file:
specified_puff_file:
target_arm_parameter_path:
working_name:
Returns:
Tuple of necessary paths.
Raises:
PipelineViewError: If any errors occur due to undefined deployment
data.
"""
template_parameters = self.construct_template_parameters()
working_name = self.__encode_working_name(template_parameters)
application_name = self.context.application_name
frame_folder = self.data.frame_folder
if not frame_folder:
Expand Down Expand Up @@ -476,28 +489,27 @@ def construct_deployment_paths(
)
log.debug(f"{puff_prompt}, {source_puff_path}")

working_dir = source_puff_path.parent
log.debug(f"working directory, {working_dir}")
working_dir = self.__construct_working_directory(
frame_folder, working_name
)
# Assume arm template parameters file has been specified with any sub
# directories in it's path in puff_map.yml, so only frame_folder is
# used here.
parameters_path = frame_folder / target_arm_parameter_path
parameters_path = working_dir / target_arm_parameter_path
log.debug(f"arm parameters path, {parameters_path}")

log.debug(f"working directory, {working_dir}")
target_arm_template_path = self.__screen_jinja_template(
source_arm_template_path, working_dir, "arm"
)
target_puff_path = self.__screen_jinja_template(
source_puff_path, working_dir, "puff"
source_arm_template_path, working_dir
)

template_files = TemplateFiles(
arm_template=ArmTemplates(
source=source_arm_template_path, target=target_arm_template_path
arm_template=TemplatePaths(
source=source_arm_template_path,
target=target_arm_template_path,
),
arm_template_parameters=ArmTemplateParameters(
source_puff=source_puff_path,
templated_puff=target_puff_path,
arm_template_parameters=TemplatePaths(
source=source_puff_path,
target=parameters_path,
),
)
Expand All @@ -506,18 +518,10 @@ def construct_deployment_paths(

@staticmethod
def __screen_jinja_template(
source_path: pathlib.Path, working_dir: pathlib.Path, keyword: str
source_path: pathlib.Path, working_dir: pathlib.Path
) -> pathlib.Path:
if source_path.name.startswith(JINJA_FILE_PREFIX):
# A jinja template file needs to have a target in the working dir.
target_prompt = f"jinja output {keyword} target"
target_path = working_dir / "{0}".format(
source_path.name.replace(JINJA_FILE_PREFIX, "")
)
else:
target_prompt = f"{keyword} target unchanged from {keyword} source"
target_path = source_path
log.debug(f"{target_prompt}, {target_path}")
# A jinja template file needs to have a target in the working dir.
target_path = working_dir / "{0}".format(source_path.name)

return target_path

Expand Down
4 changes: 4 additions & 0 deletions foodx_devops_tools/utilities/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ class AnsibleVaultError(Exception):

class CommandError(Exception):
"""Problem completing an external command run."""


class TemplateError(Exception):
"""Problem processing puff, jinja templates."""
6 changes: 5 additions & 1 deletion foodx_devops_tools/utilities/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@

"""Export utility related exceptions."""

from ._exceptions import CommandError # noqa: F401
from ._exceptions import ( # noqa: F401
AnsibleVaultError,
CommandError,
TemplateError,
)
Loading

0 comments on commit 39fe7e7

Please sign in to comment.