Skip to Content
Technical Articles
Author's profile photo Kiril Gavrailov

SAP Automation Pilot 101 – Writing Scripts

Hey, I haven’t blogged in a while, so I am happy to get back to one of my favorite hobbies – writing blog posts. Today I will introduce one of the brand new features we are having in SAP Automation Pilot – writing scripts within the Pilot.

Why is this important?

In our daily operations tasks, we try to automate almost everything. Why? Because it saves us time – there are many boring, repetitive tasks that we don’t want to do manually, simply because computers are much better than us in this and automation allows us to concentrate on more important work in our backlog. One of the most common tools for this is scripting – having low-level scripting running on our operating systems allows us to achieve a lot in a relatively fast manner. However, it brings a lot of trouble with it – we have to maintain the code, run it, orchestrate the input and output, etc.

With Automation Pilot, you can delegate the execution and orchestration of your existing scripts to the service itself; even more, you can write your own scripts within the Automation Pilot’s UI.

 

How?

To make it easier to explain, I will build an example from scratch – a simple script that restarts many Cloud Foundry-based microservices.

In Automation Pilot, I will create a command called RestartApps.

I will define three inputs – password, region, and user. I need the user and the password to authenticate before the Cloud Foundry and the region to point where my apps are.

I will also define two output keys –  the exitCode and output- to understand how my script was completed and potentially troubleshoot. Also, I can use them within other commands and, based on this output, to take decisions and orchestrate additional steps.

 

Now let’s add a new command of type ExecuteScript, and give it the name “RestartApps.”

Then select the newly created button, and let’s write our script by hitting the Edit button.

 

Now it is time to write my script – I have chosen Bash as a scripting language. However, Automation Pilot supports also  – node.js, python, and Perl.

Firstly, I will map my input parameters (user, password, and region) to environment variables. I am going to click on the Environment button and add the user and the region within it.

Now it is time for the password. As it is a sensitive string, I don’t want to keep it as a variable, so I will pass it via STDIN; I will click on the STDIN tab and add map my password parameter; I will also encode it with base64 as this is requested by the cf client.

Now it is time to write my script – which consists of simple CF CLI invocations which will restart my microservices.

Here’s the script in a copy-paste format

#!/usr/bin/env bash

read -t 5 PASSWORD 

cf api "${API}"
cf auth "${USER}" "${PASSWORD}"
cf target -o mycforg -s prod # replace 'mycforg' and 'prod' with your org and space

#replace the ones below with the apps you want to restart
cf restart backend &
cf restart app-backend-push &
cf restart app &
cf restart app-retriever &
cf restart app-retriever-backup

 

Lastly, I will configure acceptable exit codes and timeout (by default, the timeout is 30 seconds, I will give it a good 300 seconds).

Finally, when I hit the Update button, my command should look like this.

 

With this command now, I can restart all of the microservices, share the command with my colleagues, and include it in broader scenarios. And this is just a glimpse of the capabilities of Automation Pilot. Stay tuned for more blog posts.

 

What’s next?

You can subscribe to SAP Automation Pilot and use it, then give us your feedback. We are constantly looking to improve the service.

You can also check out our upcoming OpenSAP course and learn more about Automation Pilot, SAP Alert Notification, and many other services which can help you setup your DevOps processes better.

Explore additional blog posts right here

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Philip MUGGLESTONE
      Philip MUGGLESTONE

      Hi Kiril Gavrailov

      Nice blog. Thanks.

      However one aspect appears to be missing - how to set the values of the two output keys (exitCode number & output array) with the results of the cf commands?

      Thanks,

      Philip

      Author's profile photo Biser Simeonov
      Biser Simeonov

      Hi Philip MUGGLESTONE ,

      I have reviewed the blog-post scenario and you have a point about setting the values of the two output keys: "exitCode" and "output". In order to do so you need to:

      1) Go to the command configuration screen. Then click on the "output" block followed by the "Edit" button.

      2) Then you can add the expressions as shown below and click on the "Update" button to save the changes applied.

      automation-pilot-output-values

      Please note, before triggering the script there is another to-do in case you did not specify already an input value for input key "region". To do so, from the command configuration section click on the "Input" block, then click on the button "Add" to add Additional Values and you can fill in the fields displayed (see an example underneath) and click on the "Update" button.

      additional-inputs

      All should be in place to trigger your command.

      Please let me hear back from your side in case there are further questions and/or comments.

       

      Kind regards,

      Biser

       

      Author's profile photo Philip MUGGLESTONE
      Philip MUGGLESTONE

      Hi Biser Simeonov,

      Thanks.

      I have it working and you can see an example in these hands-on video tutorials:

      https://www.youtube.com/watch?v=76wPwL3Ldtg&list=PLkzo92owKnVybjwraUeSsMip0YTQnq013&index=9

      https://www.youtube.com/watch?v=i6syVCuVqp0&list=PLkzo92owKnVybjwraUeSsMip0YTQnq013&index=10

      with more context in this accompanying blog:

      DevOps with SAP Business Technology Platform

      Kind regards,

      Philip