Automating Code Deployment
This tutorial teaches you how to use a script to automate DevOps tasks. In order to complete this tutorial, you need a valid Control-M endPoint and API token
Before you begin
Ensure that you have set up your environment, as described in Setting Up the Prerequisites.
Step 1: Set the Control-M Environment
The first task when starting to work with Control-M Automation API is to configure the Control-M environment that you are going to use. An environment is a combination of an endPoint
An endPoint looks like the following:
https://<controlmEndPointHost>:8443/automation-api
Let's add an environment and name it
The command below shows you how to do this. In this command, you specify a valid API token, as described in Authentication Service.
ctm environment add ciEnvironment "https://<controlmEndPointHost>:8443/automation-api" "<token>"
> ctm environment add ciEnvironment "https://[controlmEndPointHost]:8443/automation-api" "[ControlmUser]" "[ControlmPassword]"
info: Environment 'ciEnvironment' was created
info: ciEnvironment:
{"endPoint":"https://<controlmEndPointHost>:8443/automation-api","user":"[ControlmUser]"}
You can also deploy to a workbench environment. In such a case, the endpoint is https://localhost:8443/automation-api. The user name and password are both workbench.
Step 2: Access the Tutorial Samples
Go to the directory where the tutorial sample is located:
cd automation-api-quickstart/control-m/102-automate-code-deployment
Step 3: Deploy to ciEnvironment
Deploy the code to a specific environment. The "-e" is used to specify a destination environment that differs from the default environment.
Ensure that the version of the destination environment matches the version of the CLI.
The command below shows you how to do this and demonstrates a response:
> ctm deploy AutomationAPISampleFlow.json -e ciEnvironment
[
{
"deploymentFile": "AutomationAPISampleFlow.json",
"successfulFoldersCount": 0,
"successfulSmartFoldersCount": 1,
"successfulSubFoldersCount": 0,
"successfulJobsCount": 2,
"successfulConnectionProfilesCount": 0,
"successfulDriversCount": 0,
"isDeployDescriptorValid": false,
"deployedFolders": [
"AutomationAPISampleFlow"
]
}
]
Step 4: Retrieve jobs from ciEnvironment Back to the Development Environment Using Deploy Descriptor
You can now retrieve the jobs from the
The following command shows how to retrieve the jobs and folders from the
ctm deploy jobs::get -s "server=*&folder=*" -e ciEnvironment > ciEnvironmentJobs.json
Typically, the two environments (in this case,
{
"DeployDescriptor":
[
{
"Comment": "Set run as user in Defaults to the Dev automation user",
"ApplyOn": {
"@":"Defaults"
},
"Property" :"RunAs",
"Assign" : "workbench"
},
{
"Comment": "Modify Application property to comply with Development environment",
"Property" :"Application",
"Replace" : [ {"(.*)" : "Dev$1"} ]
},
{
"Comment": "Distribute jobs across hosts available in Development environment based on job names",
"Property": "Host",
"Source": "@",
"Replace": [
{ "Command.*" : "workbench"},
{ "Script.*" : "workbench"}
]
}
]
}
Use the deploy transform command to debug the modifications:
ctm deploy transform ciEnvironmentJobs.json DeployDescriptor.json -e workbench
The following output is returned. Note that the name of the application now begins with "Dev", and the two hosts are now
{
"Defaults" : {
"Application" : "DevSampleApp",
"SubApplication" : "SampleSubApp",
"RunAs" : "workbench",
"Job" : {
"When" : {
"Months" : [ "JAN", "OCT", "DEC" ],
"MonthDays" : [ "22", "1", "11" ],
"WeekDays" : [ "MON", "TUE", "WED", "THU", "FRI" ],
"FromTime" : "0300",
"ToTime" : "2100"
},
"ActionIfFailure" : {
"Type" : "If",
"CompletionStatus" : "NOTOK",
"mailToTeam" : {
"Type" : "Mail",
"Message" : "%%JOBNAME failed",
"To" : "team@mycomp.com"
}
}
}
},
"AutomationAPISampleFlow" : {
"Type" : "Folder",
"Comment" : "Code reviewed by John",
"CommandJob" : {
"Type" : "Job:Command",
"Command" : "echo my 1st job",
"Host" : "workbench"
},
"ScriptJob" : {
"Type" : "Job:Script",
"FilePath" : "SCRIPT_PATH",
"FileName" : "SCRIPT_NAME",
"Host" : "workbench"
},
"Flow" : {
"Type" : "Flow",
"Sequence" : [ "CommandJob", "ScriptJob" ]
}
}
}
To do the actual deployment to the Development environment, use the following command:
ctm deploy ciEnvironmentJobs.json DeployDescriptor.json -e workbench
Step 5: Automate Deployments
Let's automate the deployment of Control-M object definitions from the source directory to
#!/bin/bash
for f in *.json; do
echo "Deploying file $f";
ctm deploy $f -e ciEnvironment;
done
This code can be used in Jenkins to push Git changes to Control-M.
Step 6: Automate Deployments with a Python Script
You can automate the deployment of Control-M object definitions from the source directory to
import requests # pip install requests if you don't have it already
import urllib3
urllib3.disable_warnings() # disable warnings when creating unverified requests
endPoint = 'https://<controlmEndPointHost>:8443/automation-api'
user = '<ControlMUser>'
passwd = '<ControlMPassword>'
# -----------------
# login
r_login = requests.post(endPoint + '/session/login', json={"username": user, "password": passwd}, verify=False)
print(r_login.content)
print(r_login.status_code)
if r_login.status_code != requests.codes.ok:
exit(1)
token = r_login.json()['token']
# -----------------
# Built
uploaded_files = [
('definitionsFile', ('Jobs.json', open('c:\\src\ctmdk\Jobs.json', 'rb'), 'application/json'))
]
r = requests.post(endPoint + '/deploy', files=uploaded_files, headers={'Authorization': 'Bearer ' + token}, verify=False)
print(r.content)
print(r.status_code)
exit(r.status_code == requests.codes.ok)
Where to Go from Here
To learn more about what you can do with the Control-M Automation API, read through Code Reference and Services.