cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1624
Views
1
Helpful
2
Comments
Orf Gelbrich
Cisco Employee
Cisco Employee

Here are 4 different UCSD roll back examples:

SSH with Rollback

     The custom task is called the following:

          Custom_SSH_Command_Task_v1

     In the code this line of code is needed (note the word custom in front of the task name):

          var undoHandler = "custom_Custom_SSH_Command_Task_v1";

  The custom task is using the following input variables:

       host

       port

       login

       password

       delaySeconds

       checkExitCode

       commands

       undoCommands

  In the code the variables to keep track off/register are:

     undoConfig.host = input.host;

     undoConfig.port = input.port;

     undoConfig.login = input.login;

     undoConfig.password = input.password;

     undoConfig.delaySeconds = input.delaySeconds;

     undoConfig.checkExitCode = "no";

     undoConfig.commands = input.undoCommands; // Denote the trickery here

     undoConfig.undoCommands = "";

The ssh code:

importPackage(com.cloupia.lib.util.ssh);

importPackage(java.lang);

input.host

input.port

input.login

input.password

input.delaySeconds

input.commands

input.checkExitCode

// output.LAST_EXIT_CODE

// output.EXIT_CODES (comma seperated)

// output.STD_OUT

// output.STD_ERR

function registerUndoTask()

{

     if (input.undoCommands === null || input.undoCommands.length() === 0)

     {

              return;

     }

     // register undo task

     var undoHandler = "custom_Custom_SSH_Command_Task_v1";

     var undoContext = ctxt.createInnerTaskContext(undoHandler);

     var undoConfig = undoContext.getConfigObject();

     undoConfig.host = input.host;

     undoConfig.port = input.port;

     undoConfig.login = input.login;

     undoConfig.password = input.password;

     undoConfig.delaySeconds = input.delaySeconds;

     undoConfig.checkExitCode = "no";

     undoConfig.commands = input.undoCommands;

     undoConfig.undoCommands = "";

 

     ctxt.getChangeTracker().undoableResourceModified("SSH-Commands-Undo",

                "Commands on Host "+input.host,

                 "SSH Commands on Host: "+input.host,

                "SSH Commands on Host: "+input.host,

                undoHandler,

                undoConfig);

}

if ( (input.commands === null) || (input.commands.trim().length() === 0) )

{

        ctxt.setFailed("No commands to execute");

        ctxt.exit();

}

function openSSH()

{

       // public SSHClient(String host, int port, String userName, String password)

        var ssh = new SSHClient(input.host, Integer.parseInt(input.port), input.login, input.password);

       ssh.connect();

       return ssh;

}

var delay = Integer.parseInt(input.delaySeconds);

var lines = input.commands.split("\n");

var stdout = new StringBuffer();

var stderr = new StringBuffer();

var exitCodes = "";

var exitCode = 0;

var check = false;

if (input.checkExitCode !== null & input.checkExitCode.equalsIgnoreCase("yes"))

{

      check = true;

}

var ssh;

try

{

logger.addDebug("Connecting to host: "+input.host);

ssh = openSSH();

logger.addDebug("Connected to host: "+input.host);

for (var i=0;i<lines.length; i++)

{

       if (delay > 0)

       {

             Thread.sleep(1000 * delay);

       }

       var line = lines[i].trim();

       //logger.addDebug("Executing command: "+line);

       var out = ssh.executeCommand(line);  

       logger.addDebug("Command completed with exit Code: "+out.getExitCode());

       exitCode = out.getExitCode();

       stdout.append(out.getStdOut());

       stderr.append(out.getStdErr());

       exitCodes = exitCodes + "," + exitCode;

       logger.addDebug(out.getStdOut());

       var errMsg = out.getStdErr();

       if (errMsg.length() > 0)

       {

               logger.addWarning(errMsg);

       }

       if (check === true)

       {

               if (exitCode !== 0)

               {

                    ctxt.setFailed("Command failed with exitCode "+exitCode);

                    ctxt.exit();

               }

       }

}

} finally {

       if (ssh !== null)

       {

            try {

                ssh.disconnect();

            } catch(ee3){}

            registerUndoTask();

        }

}

output.EXIT_CODE = exitCode;

output.EXIT_CODES = exitCodes;

//output.STD_OUT = stdout.toString();

output.STD_OUT = parseInt(stdout.toString());

output.STD_ERR = stderr.toString();

ctxt.updateInput("ReturnCode1", output.STD_OUT);

Delete System Policy / Delete Primary Deploy Policy Task / Roll back from a different task initiated

Here is the task that is going to be called when roll back is initiated from a different task.

The Delete System Policy Task:

importPackage(com.cloupia.model.cIM);

importPackage(java.util);

importPackage(java.lang);

importPackage(java.io);

importPackage(com.cloupia.lib.util);

importPackage(com.cloupia.model.cIM);

importPackage(com.cloupia.service.cIM.inframgr);

importPackage(org.apache.commons.httpclient);

importPackage(org.apache.commons.httpclient.cookie);

importPackage(org.apache.commons.httpclient.methods);

importPackage(org.apache.commons.httpclient.auth);

importPackage(com.cloupia.model.cEvent.notify);

importPackage(com.cloupia.lib.util.mail);

importPackage(com.cloupia.fw.objstore);

importPackage(com.cloupia.lib.util.managedreports);

logger.addInfo("------------------- Delete Primary Deploy Policy---------------------------------");

var sysPolicy = InfraPersistenceUtil.getPrivateCloudSystemProfile(vdcTempPrimaryDeployPolicyNameClone);

if(sysPolicy != null){

     var policyId = sysPolicy.getPolicyId();

     InfraPersistenceUtil.deletePrivateCloudSystemProfile(policyId);

      logger.addInfo("vdcTempPrimaryDeployPolicyNameClone Policy deleted successfully"+vdcTempPrimaryDeployPolicyNameClone);

}else{

   logger.addError("vdcTempPrimaryDeployPolicyNameClone Policy not found : "+vdcTempPrimaryDeployPolicyNameClone);

}

The Rollback is created  in a different task:

  The task that is registered is a custom task and its name is:

       Delete System Policy

  In the code this is needed:

       var undoHandler = "custom_Delete System Policy";

  The variable to keep track of in the Delete System Policy is:

       SystemPolicyName

  In the code this is needed:

       undoConfig.SystemPolicyName = syspoltmp;

function registerUndoTask()

{

     // register undo task

     var undoHandler = "custom_Delete System Policy";

     var undoContext = ctxt.createInnerTaskContext(undoHandler);

     var undoConfig = undoContext.getConfigObject();

     undoConfig.SystemPolicyName = syspoltmp;

     logger.addInfo("Registering syspoltmp var: "+syspoltmp);

       

     ctxt.getChangeTracker().undoableResourceModified("DeleteSystemPolicy-Commands-Undo",

                "DeleteSystemPolicy "+syspoltmp,

                 "DeleteSystemPolicy: "+syspoltmp,

                "DeleteSystemPolicy: "+syspoltmp,

                undoHandler,

                undoConfig);

}

Rollback VM Ware Inventory Execution

The interesting part about this task is that it is empty and only registers the roll back at execution time.

It registers a built in task in this case.

The out of the box task is called:

       Collect Inventory

In the code this is needed:

       var undoHandler = "Collect Inventory";

The input variable to the out of the box task is

       vmwareAccounts

In the code this is needed:

       undoConfig.vmwareAccounts = vmware_accounts;

Clone task to see exact spelling of variables as shown here:

Screen Shot 2017-01-26 at 8.20.12 AM.png

Screen Shot 2017-01-26 at 8.27.52 AM.png

The custom Task Code:

importPackage(java.util);

importPackage(java.lang);

var vmware_accounts = input.VMware_Account;

//-------Register Rollback------->>

function registerUndoTask(vmware_accounts) {

  var undoHandler = "Collect Inventory";

  var undoContext = ctxt.createInnerTaskContext(undoHandler);

  undoConfig = undoContext.getConfigObject();

 

  undoConfig.vmwareAccounts = vmware_accounts;

 

  ctxt.getChangeTracker().undoableResourceModified("Rollback Inventory", "rollback", "Rollback Inventory", "rollback", undoHandler,undoConfig);

}

registerUndoTask(vmware_accounts);



PowerShell Empty Task for Forward on Rollback Execution Occurs



importPackage(java.util);

importPackage(java.lang);

importPackage(com.cloupia.model.cIM);

importPackage(java.io);

importPackage(com.cloupia.lib.util);

importPackage(com.cloupia.model.cIM);

importPackage(com.cloupia.service.cIM.inframgr);

importPackage(org.apache.commons.httpclient);

importPackage(org.apache.commons.httpclient.cookie);

importPackage(org.apache.commons.httpclient.methods);

importPackage(org.apache.commons.httpclient.auth);

importPackage(com.cloupia.model.cEvent.notify);

importPackage(com.cloupia.lib.util.mail);

importPackage(com.cloupia.fw.objstore);

importPackage(com.cloupia.lib.util.managedreports);

function registerUndoTask()

{

     if (input.rollbackCommand === null || input.rollbackCommand.length() === 0)

     {

              return;

     }

     // register undo task

     var undoHandler = "Execute PowerShell Command";

     var undoContext = ctxt.createInnerTaskContext(undoHandler);

     var undoConfig = undoContext.getConfigObject();

     undoConfig.label = input.label;

     undoConfig.psAgent = input.psAgent;

     undoConfig.targetIPAddress = input.targetIPAddress;

     undoConfig.targetUserID = input.targetUserID;

     undoConfig.targetPassword = input.targetPassword;

     undoConfig.targetDomain = input.targetDomain;

     undoConfig.targetCommand =  input.rollbackCommand;

     undoConfig.rollbackCommand = "";

     undoConfig.outputFormat = input.outputFormat;

     undoConfig.depth = input.depth;

     undoConfig.maxWaitTimeMinutes =input.maxWaitTimeMinutes;

      ctxt.getChangeTracker().undoableResourceModified("PowerShell-Rollback",

                "Host 1: "+input.targetIPAddress,

                 "Host 2: "+input.targetIPAddress,

                 "Host 3: "+input.targetIPAddress,

                undoHandler,

                undoConfig);

}

registerUndoTask();


Screen Shot 2017-02-26 at 8.19.39 AM.png'

Screen Shot 2017-02-26 at 8.19.51 AM.png


Screen Shot 2017-02-26 at 8.20.10 AM.png


Screen Shot 2017-02-26 at 8.20.16 AM.png


Screen Shot 2017-02-26 at 8.20.55 AM.png

Comments
sandeepthumu
Level 1
Level 1

Hi Orf,

I would like to know if can rollback configurations done through multiple custom tasks at a time.

ex: I have a workflow-1 and it has multiple custom tasks ctask-1, ctask-2, .. (each task has a different configuration to push to APIC), so in this case can I have a single rollback where I can remove all the configurations made by each custom task.

Orf Gelbrich
Cisco Employee
Cisco Employee

Yes – you can – the task would do nothing on execute and only do something on rollback.

Only problem is then your workflow looks like this

Workflow

Task 1

Task 2

Task3

Task4 (empty only do something on rollback – roll back task 1,2,3)

So what happens when this workflow only does Task1 and Task2 the roll back would attempt to do roll back all 3 and error out..

Not good

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links