Creating a JSON file listing the Integration Build's Components

Using the list of agents currently active in a specific environment

The script below retrieves the list of all the active instances of your application’s components from a specific environment and creates from it an Integration Build on Sealights Dashboard.
It provides a “picture” of the testing environment (Lab) at this given point in time.

This solution is aimed to illustrate the concept and is based on the following assumptions:

  • All the components are currently live in the specific environment, up and running as well as appearing in the Cockpit Agent Monitor page

  • No component is designed with a “lazy loading” mechanism bringing it when necessary (on-demand). This restriction applies to front-end components as well (since they’re actively reporting only when the application is opened on the client-side browser).

If one of the assumptions above is not valid in your configuration, it may end up with incorrect data.

#!/bin/bash DOMAIN="mycompany.sealights.co" SL_API_TOKEN=`cat sl_api_token.txt` SL_AGENT_TOKEN=`cat sl_agent_token.txt` INTEGRATION_BUILD_NAME="myIntegrationBuildSample" QA_ENV="QA-01" #Retrieve the Sealights Generated LabID based on App and Environment name SL_LABID=`curl -sX GET "https://$DOMAIN/sl-api/v1/lab-ids?appName=$INTEGRATION_BUILD_NAME&branchName=$QA_ENV&buildType=integration" \ -H "Authorization: Bearer $SL_API_TOKEN" -H "accept: application/json" | jq --raw-output .data.labIds[0].labId` echo "Sealights' LabID is $SL_LABID" #Prepare list of microservices in environment curl -sX GET "https://$DOMAIN/sl-api/v1/agents/live" -H "Authorization: Bearer $SL_API_TOKEN" -H "accept: application/json">liveagents.txt cat liveagents.txt | jq -c --arg LABID_PARAM $SL_LABID '.data[] | select(.labId==$LABID_PARAM and .type=="TestListener")' | jq -s 'group_by(.bsid)[] | {appName: .[0].appName, branch: .[0].branchName, build: .[0].buildName}' | jq -s>sl-integration-components.json echo "List of components generated for the $INTEGRATION_BUILD_NAME on $QA_ENV" cat sl-integration-components.json #Download the java agent if not installed already if [ ! -f "sealights-java-latest.zip" ]; then echo "Downloading Sealights Agent..." wget -nv https://agents.sealights.co/sealights-java/sealights-java-latest.zip unzip -oq sealights-java-latest.zip echo "Local agent version is now:" `cat sealights-java-version.txt` fi #Report new Integration Build to SL ("Picture" of ms deployed in the environment) echo "Reporting new version of $INTEGRATION_BUILD_NAME (for $QA_ENV) to Sealights" java -jar sl-build-scanner.jar -config -token $SL_AGENT_TOKEN -appname $INTEGRATION_BUILD_NAME -branchname $QA_ENV -buildname `date +"%y%m%d_%H%M"` -pi "*integration.build*" -buildsessionidfile integrationBSID.txt java -jar sl-build-scanner.jar -scan -token $SL_AGENT_TOKEN -buildsessionidfile integrationBSID.txt -componentfile sl-integration-components.json echo Script is complete: $QA_ENV can be tested.

Sample script for a Jenkins Pipeline job

Jenkins Pipeline scripts support readJSON and writeJSON and the sample code below allows you to create a JSON file for integration build declaration.

Plugin Pipeline Utility Steps Plugin needs to be installed on your Jenkins instance

pipeline { agent any stages { stage ("Creating JSON") { steps { script{ def sldata = readJSON text: '[{ "component_name": "frontend", "buildSessionId": "123" },{ "component_name": "backend", "buildSessionId": "456" }]' writeJSON(file: 'sl-ib-components.json', json: sldata ) } } } stage('Validate JSON') { steps { sh "cat sl-ib-components.json | jq ." } } } }

The sample code above is OS agnostic and should work for Jenkins instances installed on Windows or Linux servers.