Schedule a SWIM Distribution Job for Devices
Scenario Overview
This use case demonstrates how to schedule a SWIM Distribution Job to distribute a software image from CNC to device(s). This step ensures the image exists on the device(s) before scheduling a SWIM Activation Job.
The operation in the following API references is used in this example.
Prerequisites
Before running this example, ensure the following prerequisites are met:
- You have obtained an access token. Refer to the 'Getting Started' section of the CNC SWIM API documentation for instructions.
- The software image exists in the SWIM repository.
- The target device is added to the CNC inventory, and its details are collected.
Schedule Distribution Job for Given Image and Device
To schedule the distribution job, update the image_distribution.json
input file with the required details (e.g., job name, image details, and target device), then run the image_distribution.sh
script from the example directory.
Navigate to the cnc-swim-api-examples
directory and run the image_distribution.sh
script using the following command:
cd cnc-swim-api-examples;./image_distribution.sh
Script Details
This script reads input from the image_distribution.json file located in the input directory. Upon successful execution, a new distribution job is scheduled and the output is saved to the image_distribution_schedule_status.json file in the output directory.
#!/bin/bash
# Import common API functions and environment variables
. ./cnc-api-common.sh
image_distribution() {
# 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: Retrieve collection status summary from the server and store the output in CNC_API_OUTPUT file
CNC_API_URL=$CNC_SWIM_API_CTX/distribute
CNC_API_INPUT="@$PRJ/input/image_distribution.json"
CNC_API_OUTPUT="$PRJ/output/image_distribution_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)" '.softwareImageDistributionDTO.jobDto.jobName = "image_distribute_" + $ts' input/image_distribution.json > temp.json && mv temp.json input/image_distribution.json
# Step 4: Send the POST request to schedule the distribution job and store the response in the output file.
http_post $CNC_API_URL $CNC_API_INPUT $CNC_API_OUTPUT
}
image_distribution