A question I often get from clients is how certain operational tasks, like restarts of application servers, can be done through XL Deploy. They want XL Deploy to be the ‘one stop shop’, where deployments and simple operational tasks alike can be started.
XL Deploy has a concept for that calledcontrol tasks. A good resource to start out from isthis blog post, which defines a good, generic framework to start out this specific use case. Browse through this blog post before reading on, it’s a short and concise read.
The following use case was handed to me: I want to execute a restart on an entire tree of Tomcat servers. The application servers are on hosts, and they are split in a folder hierarchy, like this:
So in this case, executing the bulk restart on the folder would restart Tomcat servers on host1, host2, host3 and host4, but executing the bulk restart on the subfolder would only restart the Tomcat servers on host1 and host4. Since ‘stop’ and ‘start’ are control tasks themselves, it’s like having a master control task that starts child control tasks.
First, let’s start with defining a new control task on folders in XL Deploy. You simply do that by placing a snipplet like this in the synthetic.xml of your plugin (or in the ext/ folder):
description=”Performs a stop / start task on multiple Tomcat servers”
task-description=”Performs a stop / start task on multiple Tomcat servers”
This tells XL Deploy that on every folder you can execute a control task called ‘bulkRestartTomcat’ that will invoke a script that runs on the XL Deploy server.
Let’s explore how to find the correct servers in the jython script. You do that by invoking the query method on the repository service (API docs). Here we specify that we want to look for only configuration items (nodes) with type ‘tomcat.Server’, or in other words, Tomcat servers. The thirds parameter specifies that we only want to include nodes that are children of the folder the user right clicked on. For the rest we don’t have to specify anything.
containers = repositoryService.query(Type.valueOf("tomcat.Server"), None, thisCi.id, None, None, None, 0, -1) print "Found Tomcat containers: " + str(containers)
For each of the results, we want to execute the ‘stop’ control task, and wait for that to finish.
tasks =  for container in containers: tasks.append(run_control_task(container, "stop")) wait_for_tasks_to_finish(tasks)
And once all have been stopped, we want to start them up again.
tasks =  for container in containers: tasks.append(run_control_task(container, "start")) wait_for_tasks_to_finish(tasks)
To find the full source including all the details, click here .
As you can see in this job log, it found three Tomcat servers on the host1 server:
Found Tomcat containers: [Infrastructure/folder/subfolder/host1/tc1 [tomcat.Server], Infrastructure/folder/subfolder/host1/tc2 [tomcat.Server], Infrastructure/folder/subfolder/host1/tc3 [tomcat.Server]]
Executing Control task [stop] for Infrastructure/folder/subfolder/host1/tc1, task id 2617a12a-3e09-4e06-ba1a-dd9f334f36b8
Executing Control task [stop] for Infrastructure/folder/subfolder/host1/tc2, task id 8b6e8125-12fc-42c7-b285-ab7e5b8da050
Executing Control task [stop] for Infrastructure/folder/subfolder/host1/tc3, task id a30e3271-f230-4854-b733-d40dc82d400f
Waiting for task 2617a12a-3e09-4e06-ba1a-dd9f334f36b8 to finish
Executing Control task [start] for Infrastructure/folder/subfolder/host1/tc1, task id f51d8c13-515c-4b1d-8669-f45c867e2701
Executing Control task [start] for Infrastructure/folder/subfolder/host1/tc2, task id 965bdea6-a662-489c-8369-a4754ad84d33
Executing Control task [start] for Infrastructure/folder/subfolder/host1/tc3, task id a29c6113-634b-400a-8ae9-76bd999ddac7
Waiting for task f51d8c13-515c-4b1d-8669-f45c867e2701 to finish
All the details on the subtasks can be found in the Reports tab under Control Tasks:
As you see it’s really easy to satisfy these kind of use cases, and especially with the right API documentation and examples within arm’s reach.
I’ve packaged everything into a plugin which can be checked out at this git repository .
Looking for more Tips and Tricks on XL Deploy? Check outour docs siteand start learning how to optimize your XL Deploy experience.