Monday, April 25, 2011

sdcmlib: a python library to automate software deployment

KwateeSDCM's REST API lets you automate the management of your environments. This is very useful to automate the integration testing of distributed applications.

In this post we provide a simple Python library that makes this automation event simpler as well as some very simple examples.

You will need to fetch and install KwateeSDCM, the Httplib2 python client library and then download sdcmlib.

sdcmlib

The sdcmlib module is a simple python wrapper to KwateeSDCM's REST API

class sdcmlib.SDMSession(uri, userName, password)
The class that represents an SDCM REST session.

The uri is the URI of SDCM's REST base address and can begin with either http or https.

The userName and password must be a valid sdcm login
SDCMSession Objects

SDCMSession.get_servers()
Retrieves the list of servers in the following XML format:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<servers>
   <count>count</count>
   <server>
      <name>serverName</name>
      <address>serverIpAddress</address>
   </server>
   ...
   <server>...</server>
</servers>

In case of error, this method returns None. The cause and description can be retrieved from SDCMSession.get_last_status() and SDCMSession.get_last_reason().

SDCMSession.update_archive(packageName, versionName, archivePath)
Updates the archive of sdcm's repository package packageName[versionName] which must already exist in KwateeSDCM's repository. The archive will be updated with the local file (local to the KwateeSDCM server, i.e. it will not be uploaded via http) archivePath.

SDCMSession.deploy(environmentName, [deploymentName='SKETCH'])
Initiates the deployment (installation) of the environment deploymentName@environmentName which must already be defined in KwateeSDCM.

The method returns True if successfully initiated after which SDCMSession.get_environment_status() or alternatively SDCMSession.wait_for_completion() should be called. Otherwise the error cause and description  can be retrieved from SDCMSession.get_last_status() and SDCMSession.get_last_reason().

SDCMSession.start(environmentName, [deploymentName='SKETCH'])
Initiates the start of the environment deploymentName@environmentName which must already be defined in KwateeSDCM.
The method returns True if successfully initiated after which SDCMSession.get_environment_status() or alternatively SDCMSession.wait_for_completion() should be called. Otherwise the error cause and description  can be retrieved from SDCMSession.get_last_status() and SDCMSession.get_last_reason().

SDCMSession.stop(environmentName, [deploymentName='SKETCH'])
Initiates the stop of the environment deploymentName@environmentName which must already be defined in KwateeSDCM.

The method returns True if successfully initiated after which SDCMSession.get_environment_status() or alternatively SDCMSession.wait_for_completion() should be called. Otherwise the error cause and description  can be retrieved from SDCMSession.get_last_status() and SDCMSession.get_last_reason().

SDCMSession.cancel()
Cancels the last operation.

SDCMSession.get_environment_status()
Returns the tuple (inProgress, status) for the last opereration. InProgress is False then status is None and the operation has not yet completed. If InProgress is True then status will return details of the operation for each package in the environment as described below; SDCMSession.get_last_status() returns the value 200 then the global operation succeeded, and failed otherwise.

Details:
packageName: OK | FAILED
...

SDCMSession.wait_for_completion([timeout=60])
Utility method that will loop on SDCMSession.get_environment_status() until the last operation completes or the timeout, in seconds, expires in which case it will cancel the operation by calling SDCMSession.cancel().
The method returns the tuple (success, status). If success is False then status may be None if some internal error occured (see SDCMSession.get_last_status() and SDCMSession.get_last_reason()). Otherwise, status returns the details of the operation on each package as above.

SDCMSession.get_last_reason()
Returns the string description associated to the error code (see SDCMSession.get_last_status()) of the last REST http operation that was performed.

SDCMSession.get_last_status()
Returns the numeric http error code of the last REST http operation that was performed.


Examples
This example prints the list of servers
import sdcmlib
session = sdcmlib.SDCMSession('http://sdcm.kwatee.local/KwateeSDCM', 'sdcm', 'password')
servers = session.get_servers()
print str(session.get_last_status())+' '+session.get_last_reason()+'\n'
print servers

This example updates a sdcm package with a file that already exists on the KwateeSDCM server
import sdcmlib
session = sdcmlib.SDCMSession('http://sdcm.kwatee.local/KwateeSDCM', 'sdcm', 'password')
result = session.update_archive('myApp', 'v1.0.1', '/home/kwatee/uploads/myarchive.tar.gz')

This example deploys an environment and waits for the completion of the operation
import sdcmlib
session = sdcmlib.SDCMSession('http://sdcm.kwatee.local/KwateeSDCM', 'sdcm', 'password')
if session.deploy('test_environment', 'SKETCH'):
    result, data = session.wait_for_completion(120)
    if not result:
        print str(session.get_last_status()+' '+session.get_last_reason()+'\n'    print data
else:
    print str(session.get_last_status()+' '+session.get_last_reason()+'\n'

This example starts an environment and waits for the completion of the operation
import sdcmlib
session = sdcmlib.SDCMSession('http://sdcm.kwatee.local/KwateeSDCM', 'sdcm', 'password')
session.start('test_environment', 'SKETCH')
result, data = session.wait_for_completion(120)

This example stops an environment and waits for the completion of the operation
import sdcmlib
session = sdcmlib.SDCMSession('http://sdcm.kwatee.local/KwateeSDCM', 'sdcm', 'password')
session.stop('test_environment', 'SKETCH')
result, data = session.wait_for_completion(120)

This example fetches the status of an ongoing operation and cancels it if not completed
import sdcmlib
session = sdcmlib.SDCMSession('http://sdcm.kwatee.local/KwateeSDCM', 'sdcm', 'password')
completed, data = session.get_status()
if not completed:
    session.cancel()

No comments:

Post a Comment