Skip to content

Part 2

In this section, you will update an "environment template" with a second resource template that will have a dependency on the original resource template. You will create these as declarative specs in Git and use Rafay's "Git-to-System" synchronization to load them into your Rafay Org.

High Level Steps

You will implement the steps described below

  1. You will first create and update the files locally on your machine.
  2. You will push them to a new branch in your Git repository.
  3. Once the files are loaded in the new branch, you will merge the new branch into the main branch. The main branch is actively monitored for changes by Rafay's GitOps pipeline.
  4. This will trigger the automated loading of the resources in your Rafay Org.

Info

When you launch an instance of an environment based on this updated environment template it will execute the configured Infrastructure as Code (IaC) file and Python code. In our simple, illustrative example, we will print the values of preset environment variables in the IaC and print "Hello World" from Python.


Step 1: Download Files

In this step, you will download and extract a ZIP archive which contains the specification files of the EM resources for this part of the guide.

Download

  • Download the archive em-gs-part2.zip
  • Extract this archive on your local machine

You will use these files in the subsequent steps.


Step 2: View/Update Workflow Handler

In this step, we will review and update the Workflow Handler declarative spec file. In our example, this resource will execute a simple Python "Hello World" print statement.

  • In your extracted archive from step 1, navigate to em-gs-part2/projects/em-gs/workflowhandlers/
  • View the file named em-wh.yaml in this directory
  • Update any fields to match your environment if different names were used
apiVersion: eaas.envmgmt.io/v1
kind: WorkflowHandler
metadata:
  name: em-wh
  project: em-gs
spec:
  config:
    function:
      language: python
      name: em-wh
      skipBuild: false
      source: "from typing import *\nfrom logging import Logger\n\ndef handle(logger:
        Logger,request: Dict[str, Any]) -> Dict[str, Any]:\n  logger.info(f\"inside
        function handler, request: {request}\", extra={\"request\": request})\n\t\n
        \ logger.info(\"Hello world... \")\n\t\n  return {\n    \"output\": \"Hello
        world\"\n  }\n"
      targetPlatforms:
      - linux/amd64
    type: function

Step 3: View/Update Resource Template

In this step, we will view and update the resource template. This resource template executes Python code that will print "Hello World".

  • In your extracted archive from step 1, navigate to em-gs-part2/projects/em-gs/resourcetemplates/
  • View the file named em-rt-python.yaml in this directory
  • Update any fields to match your environment if different names were used
apiVersion: eaas.envmgmt.io/v1
kind: ResourceTemplate
metadata:
  name: em-rt-python
  project: em-gs
spec:
  provider: custom
  providerOptions:
    custom:
      tasks:
      - agents:
        - name: em-gs
        executeOnce: true
        name: python-code
        onFailure: unspecified
        options: {}
        type: workflowHandler
        workflowHandler:
          name: em-wh
  version: v1
  versionState: draft

Step 4: Create and Merge Git Branch

Now, you will create a new branch in your Git repository and copy the contents of the extracted ZIP archive to the branch. You will then merge this new branch into the main branch where Rafay's GitOps will detect the changes.

Git PR and Merge

  • Create a new branch in your Git repository
  • Click Add file -> Upload files
  • Add the folder named projects that contain the files you viewed/updated in this part of the guide
  • Click Commit changes

Merge Branch

Once the changes are pushed into the new branch, we will now merge the changes into the main branch where Rafay GitOps will detect the changes and create the resources in your Rafay Org.

  • Click Compare & pull request

Merge Branch

  • Click Create pull request

Merge Branch

  • Click Merge pull request

Merge Branch

  • Click Confirm merge

Step 5: Validate Templates

In this step, you will validate that Rafay GitOps created the resources within Environment Manager.

  • In your project, navigate to Environments -> Workflow Handlers

You will see the newly created workflow handler.

Workflow Handler

  • In your project, navigate to Environments -> Resource Templates

You will see the newly created resource template.

Resource Template


Step 6: Update Environment Template

In this step, we will update the environment template through the UI in order to add the new resource template into the environment template and provide a dependency on the new resource template to begin execution only after the first resource template has completed. Note that this references the resource template from the prior step.

  • In your project, navigate to Environments -> Environment Templates
  • Click on the em-et template
  • Edit the existing draft version of the template
  • Go to the Resources tab
  • Click + Resource
  • Select Resource Template for the Resource Kind
  • select em-rt-python for the Resource Template Name
  • Select v1 for the Version
  • Click + Add Dependency Value
  • Select em-rt as the dependency
  • Click Save
  • Click No, Exit

Add Template

  • Click Save as Draft

Optionally, you can go into your Git repository and view the updated environment template file to see the newly added resource template and dependency that was synced back to Git.

Git Sync


Step 7: Create Environment

In this step, we will create an environment using the recently updated environment template. The environment will execute the IaC referred to by the first resource template and then execute the second resource template which will execute the Python code in the workflow handler. In this step, we will validate the successful run by reviewing the log. As part of our Python code, we are printing "Hello World" as an output.

  • In your project, navigate to Environments -> Environments
  • Click Launch on the environment template card
  • Enter a name for the environment
  • Click Save & Deploy

Environment

After a minute, the environment run will have completed.

  • Click Show near Activities (Recent Runs)
  • Expand group.em-rt-python.custom.python-code

You will see the output of the Python code showing "Hello World".

Environment


Recap

In this part, you used Rafay GitOp's Git to System synchronization capability to create an resource template that executed Python code. You then updated the existing environment template to execute this new resource template after the first resource template finishes execution. You then deployed a new instance of an environment based on the recently updated environment template.