среда, 2 октября 2013 г.

AntHillPro: Create, manage and delete environments via BeanShell scripts

Creating new entity (project, environment etc)

Lets assume that we need to create new environment within script
First of all, all new stuff will be applied into database only after step ends. When you create new environment you can't manipulate it, you can only add Properties on it, but you can't add agents or you cant add it into Environment Group, you will get an Exception because environment still not exists in scope of anthill. Same exception will happen if you will try to manipulate newly created project or some other instance. So, remember, if you create something you need to do:
  1. <your_new_entity> = new <Something>;
  2. <you_new_entity>.store();
  3. Finish step.
On next step your new entity will be in database and available for manipulation in anthill, you can add it into server groups, projects etc.

Adding into Environment Group

It is quite simple:
  1. Get environment group: EnvironmentGroup envGroup = EnvironmentGroupFactory.getInstance().restore( envGroupID );
  2. envGroup.addServerGroup( env ) , where env should be instance of ServerGroup
I want to point that you do not need to call .store() method, it will store by itself after the end of the step.

Adding into project's workflows.

There is a one limitation: you can add environment only in non originating workflows.
It also simple:
  1. Get project: Project prj = ProjectFactory.getInstance().restore( projectID );
  2. Get non-originating workflow like Workflow wf = prj.getWorkflow( nonOrigWFID ) or get an array of workflows Workflow[] wfArray = prj.getNonOriginatingWorkflowArray() and iterate over it.
  3. Add environment into it: wf.addServerGroup( env ) , where env should be instance of ServerGroup
Also you do not need to call .store() method here.

Deleting environment

In first part we have created environment and added it into environment group and workflows. Now if you want to delete it from anthill you need to delete it from all workflows where this environment is used.
It's not quite obvious, but... :) Here will be 3 steps in total: 

First step:

  • Get environment: ServerGroup env = ServerGroupFactory.getInstance().restoreForName( "my-env-name" );
  • Get environment's group: EnvironmentGroup[] envGroupArray = EnvironmentGroupFactory.getInstance().restoreAllForEnvironment( env );
Get all non-originating workflows that use this environment. In docs I've found method in WorkflowFactory:
  • Workflow[] wfInEnvArray = WorkflowFactory.getInstance().restoreAllNonOriginatingWorkflowsForEnvGroupsInEnvironment( env, envGroupArray, "*", "*", "*" );
we got all required variables earlier. Then you just need to iterate over this list and use <workflow>.removeServerGroup( env ); 
On this point Job step should finish and all removals will be applied into database, without it we can't delete environment, you'll get en exception that this env is in use.

Second step:

Also before delete you need to remove this environment from the environment group.
We have environment group which this environment belongs to, so just remove: envGroup.removeServerGroup( env ); 

Third step:

Just get ServerGroup object for required environment and call .delete() method.

Комментариев нет:

Отправить комментарий