Skip to content

Recipe Action

The recipe action embeds another YAML recipe. This action offers the possibility to substitute variables in the recipe that will be embedded.

Usage

Click on the to learn more about the action's options.

Recipe Action
{% set username=username or "user" -%}

actions:
  - action: recipe
    description: Recipe that will be embedded # (1)!
    recipe: recipes/recipe.yaml # (2)!
    working_dir: upstream # (3)! 
    variables: # (4)! 
      username: {{username}}
  1. [Optional] Recipe description, for documentation purposes
  2. [Required] Relative link to the recipe's YAML file
  3. [Optional] Alternative working directory. This is required to avoid breaking relative links, when embedding a recipe from another location.
  4. [Optional] Substition variables

Implementation

RecipeAction

Bases: Action

Recipe action

Source code in simple_cdd_yaml/actions.py
class RecipeAction(Action):
    """ Recipe action """
    def __init__(self, args_dict):
        super().__init__(args_dict)
        self.args_dict = args_dict
        self.actions = {
            'conf': ConfAction,
            'preseed': PreseedAction,
            'apt': AptAction,
            'overlay': OverlayAction,
            'run': RunAction,
            'extra': ExtraAction,
            'downloads': DownloadsAction,
            'recipe': RecipeAction,
            'debos': DebosAction,
        }

    def create_action(self, action_type, args):
        """ Create a new action """
        try:
            return self.actions[action_type](args)
        except KeyError as exc:
            raise KeyError('Unknown action type!') from exc

    def _load_recipe(self, filename, substitutions=None):
        """ Load the yaml recipe """
        recipe_file = self.input_dir / filename
        full_yaml = load_yaml(recipe_file, substitutions)
        return full_yaml['actions']

    def _working_dir(self, props):
        """ Define the recipe's working dir """
        if working_dir := props.get('working_dir'):
            self.input_dir = pl.Path(working_dir)

    def _get_args(self, props):
        """ Get input arguments """
        if working_dir := props.get('working_dir'):
            return dict(self.args_dict, input=working_dir)
        return dict(self.args_dict)

    def process_actions(self, props):
        """ Perform all actions contained in the recipe """
        self._working_dir(props)
        recipe_filename =  props['recipe']
        substitutions = props.get('variables')
        recipe = self._load_recipe(recipe_filename, substitutions)
        args_dict = self._get_args(props)
        for action_props in recipe:
            action_type = action_props['action']
            action = self.create_action(action_type, args_dict)
            action.execute(action_props)
            self.combine_results(action.result)

    def perform_debos_action(self, props):
        self.process_actions(props)

    def perform_action(self, props):
        self.process_actions(props)

    def get_result(self):
        """ Return results dictionary """
        return self.result