Schedule a SWIM Activation Job for Network Devices

Scenario Overview

This use case demonstrates how to schedule a SWIM (Software Image Management) job to activate software images or packages on a network device. Activation jobs apply the software image to the target device, ensuring it operates with the updated configuration.

The operation in the following API references is used in this example.

Prerequisites

Before running this example, ensure the following prerequisites are met:

  1. You have obtained an access token. Refer to the 'Getting Started' section for instructions.
  2. The software image already exists on the target device. You can use the image_distribution.sh script to copy the image to the device if needed.
  3. The device is added to the CNC inventory and its details are collected.

Schedule Activation Job for a Given Device

Update the device ID and image details in the image_activation.json input file, then run the image_activation.sh script from the example directory.

Navigate to the cnc-swim-api-examples directory and run the image_activation.sh script using the following command:

cd cnc-swim-api-examples;./image_activation.sh

Script Details

This script reads input from the image_activation.json file located in the input directory. Upon successful execution, a new activation job is scheduled and the output is saved to the image_activation_schedule_status.json file in the output directory.

#!/bin/bash
# Import common API functions and environment variables
. ./cnc-api-common.sh

image_activation() {
    # Step 1: Ensure the CNC JWT is obtained by running the get-cnc-jwt.sh script.
    # Read the JWT from the file and export it as the AUTH_TOKEN_HDR environment variable.
     export_jwt

    # Step 2: Define the API URL, input file, and output file.
    CNC_API_URL=$CNC_SWIM_API_CTX/activate
    CNC_API_INPUT="@$PRJ/input/image_activation.json"
    CNC_API_OUTPUT="$PRJ/output/image_activation_schedule_status.json"

    # Step 3: Replace the `jobName` field in the input JSON with a unique name using a timestamp.
    # The `jq` command safely modifies the input file.
    jq --arg ts "$(date +%s)" '.activateDeviceImageDetailsListDTO.jobDto.jobName = "Job_SWIM_Activation_" + $ts' input/image_activation.json > temp.json && mv temp.json input/image_activation.json

    # Step 4: Send the POST request to schedule the activation job and store the response in the output file.
    http_post $CNC_API_URL $CNC_API_INPUT $CNC_API_OUTPUT
}
image_activation