Run Scripts on Delegates using Profiles

Updated 1 month ago by Michael Cretzman

A Delegate Profile enables you to run a startup script on the host/container/pod for a Harness Delegate when the Delegate is installed, or any time after that. You can create a single Delegate Profile and apply it to multiple Delegates.

A Delegate Profile is mandatory. The Primary Profile is the default and contains no script. You can add a script to it, or create and apply new Profiles for your Delegate.

In this topic:

Review: Delegate Profile Summary

A Delegate Profile lets you install and run script and software every time a Harness Delegate is added.

To do this, you could create a Delegate Profile containing the commands for installing and running the software.

Each time you add a Delegate, you can apply the profile.

When the Delegate is started, the commands to install and run the software are executed.

For example, a Delegate Profile that installs Terraform:

curl -O -L https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
unzip terraform_0.11.13_linux_amd64.zip
sudo mv terraform /usr/local/bin/
terraform --version

For examples of Delegate Profile scripts, see Common Delegate Profile Scripts.

Primary Profile

Your Harness account comes with a default Primary Profile. When you install a new Delegate and it registers with Harness, the Primary Profile is added to that Delegate automatically.

It cannot be renamed or deleted.

You can add any script to the Primary Profile, and apply it to any Delegate.

Profile Approvals

When you create a Profile you can require that its use be approved by a member of the Harness Account Administrator group.

When this option is selected, and Delegate that uses this Profile will appear with a Pending Approval option in the Harness Delegates page.

If you reject the Delegate, it is not registered with Harness and does not appear in the list.

See Approve or Reject Harness Delegates.

What Can I Run In a Profile?

You can add any commands supported on the host/container/pod running the Delegate. Linux shell commands are most common. If kubectl, Helm, or Docker is running on the host/container/pod where you install the Delegate, then you can use their commands.

The base image for the Delegate is Ubuntu 18.04 or later, and so any default Ubuntu packages may be used in the Delegate Profile script.

All Delegates include cURL and unzip as part of their installation package, and so you may use cURL and unzip in your Delegate Profile scripts without installing them. The following script will work without having to install any packages:

usr/bin/apt-get install -y python
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
./awscli-bundle/install -b ~/bin/aws

When is the Profile Executed?

Delegate Profiles are applied under the following conditions:

  • New Delegate - If you add a Delegate Profile when you create the Delegate, the profile commands are executed after the Delegate is started.
  • Running Delegate - If you apply a Delegate Profile to a running Delegate, either by applying it as a new profile or switching the Delegate’s current profile, the profile commands are executed immediately.
  • Edited Profile - If you edit a Delegate Profile, the new commands in the profile are applied to all Delegates using the profile.

Step 1: Create a Delegate Profile

To create a Delegate Profile, do the following:

  1. In Harness, click Setup.
  2. Click Harness Delegates.
  3. Click Delegate Profiles, and then click Add Delegate Profiles. The Manage Delegate Profile dialog appears.
  4. In Display Name, enter a name for the profile.
  5. In Startup Script, enter the script you want to run when the profile is applied, such as when the Delegate is started.
    For Delegate Requires Approval, see Approve or Reject Harness Delegates.
  6. Click Submit. The profile is created. Now you can apply the profile to new or existing Delegates.

Step 2: Applying a Profile

You can apply a profile in the following ways:

  • Kubernetes and ECS Task Delegate - For a Harness Kubernetes or ECS Task Delegate, you can apply a profile when you download the Kubernetes YAML file or ECS Task Spec for the Delegate, or after the Delegate is installed and running.
  • Shell Script and Docker Image Delegate - For the Harness Shell Script and Docker Image delegates, you download, install, and run the Delegates first, and then apply the profile using the Harness Manager.

To apply a profile to an existing Delegate, do the following:

  1. Locate the Delegate to which you want to apply the profile.
  2. Click the dropdown menu next to Profile and then click the profile you want applied to the Delegate.
  3. In the confirmation dialog, click Confirm.

Notes

  • It might take a few minutes to apply the new profile.
  • When switching profiles, any binaries installed as part of the earlier profile are not removed automatically. If you need to remove them, then restart the Kubernetes pod hosting the Kubernetes Delegate or manually clean up the VM hosting the Shell or Docker delegate.
  • To remove a profile from a Delegate, click Select Profile, and then click another Profile, such as the default Primary Profile. Delegates must always have a profile. You can use the Primary Profile as a blank default.

Profile Logs

You can view the execution log of the Profile script for each Delegate. For example, we'll use a Profile that simply prints out its environment variables using printenv.

The date next to the Profile name is a timestamped link to the script execution log. Click the link to see the log:

Step 3: Manage Profiles

When you update a Delegate Profile, the changes are applied to the Delegates using the profile.

You can also delete a profile and it will be removed from the Delegates using it.

When editing or deleting profiles, any binaries installed as part of the earlier profile are not removed automatically. If you need to remove them, then restart the Kubernetes pod hosting the Kubernetes Delegate or manually clean up the VM hosting the Shell or Docker Delegate.

See Also


How did we do?