github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/docs/3-create-a-jackal-package/7-component-actions.md (about)

     1  import ExampleYAML from '@site/src/components/ExampleYAML';
     2  import Tabs from '@theme/Tabs';
     3  import TabItem from '@theme/TabItem';
     4  
     5  # Component Actions
     6  
     7  Component Actions offer several exec entrypoints that allow a component to perform additional logic at key stages of its lifecycle. These actions are executed within a shell with the same context as the Jackal binary. For a detailed overview of the execution sequence of component actions, please refer to the Jackal [package create lifecycle documentation](./5-package-create-lifecycle.md) and [package deploy lifecycle documentation](../4-deploy-a-jackal-package/1-package-deploy-lifecycle.md). Additionally, you can experiment with the component actions example located in the [Component Actions](../../examples/component-actions/README.md) example page.
     8  
     9  ## Action Sets
    10  
    11  The `component.actions` field includes the following optional keys, also known as `action sets`:
    12  
    13  - `onCreate` - Runs during `jackal package create`.
    14  - `onDeploy` - Runs during `jackal package deploy`.
    15  - `onRemove` - Runs during `jackal package remove`.
    16  
    17  ### Action Set Lists
    18  
    19  These `action sets` contain optional `action lists`. The `onSuccess` and `onFailure` action lists are conditional and rely on the success or failure of previous actions within the same component, as well as the component's lifecycle stages.
    20  
    21  - `before` - sequential list of actions that will run before this component is processed for `create`, `deploy`, or `remove`.
    22  - `after` - sequential list of actions that will run after this component is successfully processed for `create`, `deploy`, or `remove`.
    23  - `onSuccess` - sequential list of actions that will run after **ALL** `after` actions have successfully completed.
    24  - `onFailure` - sequential list of actions that will run after **ANY** error during the above actions or component operations.
    25  
    26  ### Action Set Defaults
    27  
    28  In addition to `action lists`, `action sets` can also specify a `defaults` section that will be applied to all actions in the set. The `defaults` section contains all of the same elements as an action configuration, with the exception of the action specific keys like `cmd`, `description` or `wait`, which are not allowed in the `defaults` section.
    29  
    30  ## Action Configurations
    31  
    32  An `action list` contains an ordered set of `action configurations` that specify what a particular action will do.  In Jackal there are two action types (`cmd` and `wait`), the configuration of which is described below.
    33  
    34  ### Common Action Configuration Keys
    35  
    36  Between all action configurations, there are a few common keys that are common to all of them which are described below:
    37  
    38  - `description` - a description of the action that will replace the default text displayed to the user when the action is running. For example: `description: "File to be created"` would display `Waiting for "File to be created"` instead of `Waiting for "touch test-create-before.txt"`.
    39  - `maxTotalSeconds` - the maximum total time to allow the command to run (default: `0` - no limit for command actions, `300` - 5 minutes for wait actions).
    40  
    41  ### `cmd` Action Configuration
    42  
    43  A `cmd` action executes arbitrary commands or scripts within a shell wrapper. You can use the `cmd` key to define the command(s) to run. This can also be a multi-line script. _You cannot use `cmd` and `wait` in the same action_.
    44  
    45  Within each of the `action` lists (`before`, `after`, `onSuccess`, and `onFailure`), the following action configurations are available:
    46  
    47  - `cmd` - (required if not a wait action) the command to run.
    48  - `dir` - the directory to run the command in, defaults to the current working directory.
    49  - `mute` - whether to mute the realtime output of the command, output is always shown at the end on failure (default: `false`).
    50  - `maxRetries` - the maximum number of times to retry the command if it fails (default: `0` - no retries).
    51  - `env` - an array of environment variables to set for the command in the form of `name=value`.
    52  - `setVariables` - set the standard output of the command to a list of variables that can be used in other actions or components (onDeploy only).
    53  - `shell` - set a preferred shell for the command to run in for a particular operating system (default is `sh` for macOS/Linux and `powershell` for Windows).
    54  
    55  :::info
    56  
    57  By default, multi-line `cmd` blocks will fail if one of the lines errors out; this is analogous to setting `set -e` in a shell script, as documented in the [GNU bash docs](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html).
    58  
    59  :::
    60  
    61  :::note
    62  
    63  Any binaries you execute in your `cmd` actions must exist on the machine they are executed on.  You can bring binaries with a Jackal Package as `files` with the `executable` key set, or take advantage of the `./jackal ` transformation as described in [action transformations](#action-transformations).
    64  
    65  :::
    66  
    67  ### `wait` Action Configuration
    68  
    69  The `wait` action temporarily halts the component stage it's initiated in, either until the specified condition is satisfied or until the maxTotalSeconds time limit is exceeded (which, by default, is set to 5 minutes). To define `wait` parameters, execute the `wait` key; it's essential to note that _you cannot use `cmd` and `wait` in the same action_. Essentially, a `wait` action is _yaml sugar_ for a call to `./jackal tools wait-for`.
    70  
    71  Within each of the `action` lists (`before`, `after`, `onSuccess`, and `onFailure`), the following action configurations are available:
    72  
    73  - `wait` - (required if not a cmd action) the wait parameters.
    74    - `cluster` - perform a wait operation on a Kubernetes resource (kubectl wait).
    75      - `kind` - the kind of resource to wait for (required).
    76      - `name` - the name of the resource to wait for (required), can be a name or label selector.
    77      - `namespace` - the namespace of the resource to wait for.
    78      - `condition` - the condition to wait for (default: `exists`).
    79    - `network` - perform a wait operation on a network resource (curl).
    80      - `protocol` - the protocol to use (i.e. `http`, `https`, `tcp`).
    81      - `address` - the address/port to wait for (required).
    82      - `code` - the HTTP status code to wait for if using `http` or `https`, or `success` to check for any 2xx response code (default: `success`).
    83  
    84  ## Action Examples
    85  
    86  Below are some examples of putting together simple actions at various points in the Jackal lifecycle:
    87  
    88  <Tabs queryString="action-examples">
    89  <TabItem value="Simple onCreate">
    90  
    91  Below is a simple example of an `onCreate` action set that declares `defaults` as well as `before` and `after` action lists:
    92  
    93  <ExampleYAML src={require('../../examples/component-actions/jackal.yaml')} component="on-create" />
    94  </TabItem>
    95  <TabItem value="Failure Handling onDeploy">
    96  
    97  Below is an example of an `onDeploy` action set that demonstrates how you can use `onFailure` actions to perform cleanup tasks or user messaging when an action of component lifecycle step fails:
    98  
    99  <ExampleYAML src={require('../../examples/component-actions/jackal.yaml')} component="on-deploy-with-timeout" />
   100  </TabItem>
   101  <TabItem value="Wait for a Resource">
   102  
   103  Below are examples of waiting for resources to exist or be available within an action using `wait` actions:
   104  
   105  <ExampleYAML src={require('../../examples/component-actions/jackal.yaml')} component="on-create-with-network-wait-action" />
   106  <ExampleYAML src={require('../../examples/component-actions/jackal.yaml')} component="on-deploy-with-wait-action" showLink={false} />
   107  </TabItem>
   108  </Tabs>
   109  
   110  ## Action Transformations
   111  
   112  As you may have noticed mentioned in the `before` action list of the above `Simple onCreate` example, Jackal provides some helpful transformations that help enhance cross-platform compatibility and allow you to better orchestrate Jackal and its components.
   113  
   114  Below are the transformations that Jackal will make on an action before it is ran:
   115  
   116  - Replace `./jackal `&nbsp;with the path to the currently running Jackal executable.
   117    - This allows you to run Jackal in Jackal and is designed to help you use `jackal tools` commands in the air gap.
   118  - Replace common Unix commands and shell syntax with `powershell` / `pwsh` alternatives on Windows.
   119    - This allows commands like `touch` to work on Windows and while not perfect enhances cross-platform capabilities.
   120  - Add `env` entries for all previously declared Jackal `variables`.
   121    - This allows you to use variables in actions and when combined with `setVariables` allows you to chain `variables` from an action for use in later actions or templates.
   122  
   123  <Tabs queryString="action-transformations">
   124  
   125  <TabItem value="Variables onDeploy">
   126  
   127  Within `onDeploy` action lists, you can use the `setVariables` action configuration to set a list of variables that can be used in other actions or components during `jackal package deploy`. The variable value will be assigned in two environment variables: `JACKAL_VAR_{NAME}` and `TF_VAR_{name}`. These values will be accessible in subsequent actions and can be used for templating in `files` or `manifests` in other components as `###JACKAL_VAR_{NAME}###`. This feature allows package authors to define dynamic runtime variables for consumption by other components or actions.
   128  
   129  :::note
   130  
   131  Unlike normal variables, `setVariables` do not need to be defined with the `variables` key at the top of the `jackal.yaml`.
   132  
   133  :::
   134  
   135  <ExampleYAML src={require('../../examples/component-actions/jackal.yaml')} component="on-deploy-with-multiple-variables" />
   136  
   137  </TabItem>
   138  <TabItem value="Jackal in Jackal onRemove">
   139  
   140  Below is an example of an `onRemove` action set that demonstrates how you can use `./jackal `&nbsp;to use Jackal commands like `jackal tools kubectl` to perform actions on systems that might not have the pre-requisite software (like `kubectl`) installed onto them:
   141  
   142  <ExampleYAML src={require('../../examples/component-actions/jackal.yaml')} component="on-remove" />
   143  
   144  </TabItem>
   145  </Tabs>
   146  
   147  ---
   148  
   149  ## Additional Action Use Cases
   150  
   151  Below are a few more use cases from other `examples` and `packages` for how actions can be used:
   152  
   153  <Tabs queryString="action-transformations">
   154  
   155  <TabItem value="Downloading Pre-requisites">
   156  
   157  The below example shows the `kiwix-serve` component from the data injections example which downloads a `.zim` file with an `onCreate.before` action for inclusion into the Jackal package.
   158  
   159  <ExampleYAML src={require('../../examples/kiwix/jackal.yaml')} component="kiwix-serve" />
   160  
   161  </TabItem>
   162  
   163  
   164  <TabItem value="Setting up Dependencies">
   165  
   166  The below example includes the `eksctl` binary and `eks.yaml` file in one component, setting it up in an `onDeploy.after` action and then uses the `eksctl` binary in a second component to create an EKS cluster in an `onDeploy.before` action.
   167  
   168  <ExampleYAML src={require('../../packages/distros/eks/jackal.yaml')} component="load-eksctl" />
   169  <ExampleYAML src={require('../../packages/distros/eks/jackal.yaml')} component="deploy-eks-cluster" showLink={false} />
   170  
   171  </TabItem>
   172  
   173  <TabItem value="Waiting for GitOps Resources">
   174  
   175  The below example shows using a `wait` command to wait for a GitOps deployment to happen after Jackal configures the initial `GitRepository` manifest.  By default Jackal will only track the resources it directly deploys, but adding a `wait` action allows you to control the lifecycle more directly.
   176  
   177  <ExampleYAML src={require('../../examples/podinfo-flux/jackal.yaml')} component="podinfo-via-flux" />
   178  
   179  </TabItem>
   180  
   181  </Tabs>