Version Differences for Miscellaneous Scripts

(AHPSCRIPTS-132)
(Initial Alphabetization)
Line 1:
  This page lists miscellaneous scripts that are not directly defined by the other categories listed in the scripting site. These scripts were taken straight from the [https://bugs.urbancode.com/secure/IssueNavigator.jspa?reset=true&&pid=10110&sorter/field=issuekey&sorter/order=ASC public JIRA site]. Please note that some scripts may be ''snippets'' and '''probably WILL need''' modification to work properly for your situation. Treat these as templates that you can modify from.    This page lists miscellaneous scripts that are not directly defined by the other categories listed in the scripting site. These scripts were taken straight from the [https://bugs.urbancode.com/secure/IssueNavigator.jspa?reset=true&&pid=10110&sorter/field=issuekey&sorter/order=ASC public JIRA site]. Please note that some scripts may be ''snippets'' and '''probably WILL need''' modification to work properly for your situation. Treat these as templates that you can modify from. 
       
- = Template for adding users to realms =   + = Add a Debugger to Log System Events =  
    + * '''Please note this event listener stays in the System until the next restart. Running the script more than once will create duplicate listeners and logging.'''  
       
- Script Notes:   + ==== AHPSCRIPTS-121 ====  
- * Treat this mostly as a snippet for doing something more interesting with.   + <pre>import com.urbancode.anthill3.domain.buildrequest.BuildRequestEvent;  
    + import com.urbancode.anthill3.domain.security.UserFactory;  
    + import com.urbancode.anthill3.domain.workflow.WorkflowEvent;  
    + import com.urbancode.anthill3.persistence.UnitOfWork;  
    + import com.urbancode.anthill3.services.event.EventListener;  
    + import com.urbancode.anthill3.services.event.EventService;  
    + import com.urbancode.anthill3.services.event.criteria.Criteria;  
    + import java.util.EventObject;  
    + import org.apache.log4j.Logger;  
       
- ==== AHPSCRIPTS-1 ====   + public class LoggingEventListener implements EventListener {  
       
- <pre>import com.urbancode.anthill3.domain.project.*;   + Logger log = Logger.getLogger("LoggingEventListener");  
- import com.urbancode.anthill3.domain.authentication.*;      
- import com.urbancode.anthill3.domain.security.*;      
- import com.urbancode.anthill3.domain.singleton.security.*;      
       
- // Lookup Your roles   + public void handleEvent(EventObject event) {  
- buildRole = RoleFactory.getInstance().restoreForName("Build Master");   + if (event instanceof BuildRequestEvent) {  
- userRole = RoleFactory.getInstance().restoreForName("User");   + UnitOfWork uow = UnitOfWork.create(UserFactory.getSystemUser());  
    + try {  
    + requestEvent = (BuildRequestEvent) event;  
    + request = requestEvent.getSource();  
    + log.warn("Received " + event.getClass().getSimpleName() + " for request " + request.getId() + " with status " + request.getStatus());  
    + }  
    + finally {  
    + uow.close();  
    + }  
    + }  
    + else if (event instanceof WorkflowEvent) {  
    + UnitOfWork uow = UnitOfWork.create(UserFactory.getSystemUser());  
    + try {  
    + workflowEvent = (WorkflowEvent) event;  
    + workflow = workflowEvent.getSource();  
    + log.warn("Received " + event.getClass().getSimpleName() + " for workflow " + workflow.getId() + " with status " + workflow.getStatus());  
    + }  
    + finally {  
    + uow.close();  
    + }  
    + }  
    + }  
       
- // Look-up the LDAP realm here   + public Class getEventClass() {  
- // This is a lookup by name, so insert the name of your LDAP realm.   + // get all events  
- realm = AuthenticationRealmFactory.getInstance().restore("LDAP");   + return EventObject.class;  
    + }  
       
- // Do this for each user:   + public Criteria[] getCriteria() {  
- User user1 = new User(true, realm);   + return null;  
    + }  
    + }  
       
- user1.setName("JaneDoe");   + EventService.getInstance().registerEventListener(new LoggingEventListener());</pre>  
- user1.setPassword("Bogus");   +  
- user1.addRole(userRole);   + = Add Random Test Results to a BuildLife =  
- user1.addRole(buildRole);      
- user1.store();</pre>      
       
- = Properties Contained Within Variables =      
       
  Script Notes:    Script Notes: 
- * The idea here is that the user has identified a couple agent items (a Track and technology type) that span environments. This scirpt filters agents so that the technology type must always match the technology type specified on the workflow property. The tracks are similar but some agents may participate in mulitple tracks (prod is that same for all) so prod would have a track value of something like 1|2|3 to indicate that it supports tracks 1, 2 and 3. So we need to inspect it to see if any match.   + * This is a good script to use if you need some garbage results on the '''Tests tab'''.  
- * I changed the Track one to just see if the agent value contains the requested value. It should work fine until you get up to track 10, then you have to do 01, 02, etc.      
       
- ==== AHPSCRIPTS-2 ====   + <pre>import com.urbancode.anthill3.domain.test.*;  
    + import java.util.Random;  
       
- <pre>import com.urbancode.anthill3.domain.agent.Agent;   + Random timerand = new Random();  
- import com.urbancode.anthill3.runtime.scripting.helpers.* ;   + Random successrand = new Random();  
- import com.urbancode.anthill3.services.agent.AgentRemoteControl;   + Random totalrand = new Random();  
- import com.urbancode.devilfish.services.var.VarServiceClient;   + int timeint = timerand.nextInt(1048576) + 1;  
- import java.util.*;   + int successint = successrand.nextInt(32768) + 1;  
    + int totalint = totalrand.nextInt(32769) + 1;  
       
- public class PropertyBasedAgentVarEqualsCriteria extends VariableEqualsCriteria {   + Random srunrand = new Random();  
    + Random frunrand = new Random();  
    + int srun = srunrand.nextInt(254) + 1;  
    + int frun = frunrand.nextInt(254) + 1;  
       
- String propertyname;   + public newnums()  
    + {  
    + timeint = timerand.nextInt(1048576) + 1;  
    + successint = successrand.nextInt(32768) + 1;  
    + totalint = totalrand.nextInt(32769) + 1;  
    + }  
       
- public PropertyBasedAgentVarEqualsCriteria(String propertyname, String varName) {   + public newtimeint()  
- super(varName);   + {  
- this.propertyname = propertyname;   + timeint = timerand.nextInt(1048576) + 1;  
- }   + }  
       
- public Agent[] filter(Agent[] agents) {   + public roller()  
- String value = PropertyLookup.get(propertyname);   + {  
- if (value == null || value.length() == 0 || "null".equals(value)) {   + int rollnum=0;  
- value = BuildLifeLookup.getCurrent().getOriginatingWorkflow().getRequest().getProperty(propertyname);   + while(totalint < successint){  
- }   + newnums();  
- this.setValue(value);   + rollnum++;  
- return super.filter(agents);   + commandOutput.println("REROLL #"+rollnum+". . .TRYING FOR BETTER NUMBERS");  
   
   
Line 65:
- public class PropertyBasedAgentVarContainsCriteria extends Criteria {   + while(totalint < successint){roller();}  
       
- String propertyname;   + testReport = new TestReport();  
- String varName;   + testReport.setName("Unit Tests");  
    + testReport.setNumberOfSuites(1);  
    + testReport.setNumberOfTests(totalint);  
    + testReport.setNumberOfSuccesses(successint);  
    + testReport.setNumberOfFailures(totalint-successint);  
    + testReport.setJobTrace(JobTraceLookup.getCurrent());  
    + testReport.store();  
       
- public PropertyBasedAgentVarContainsCriteria(String propertyname, String varName) {   + while(totalint < successint){roller();}  
- this.propertyname = propertyname;      
- this.varName = varName;      
- }      
       
- public Agent[] filter(Agent[] agents) {   + testSuite = new TestSuite();  
- ArrayList resultList = new ArrayList();   + testSuite.setName("Test Suite");  
-   + testSuite.setTestReport(testReport);  
- String value = PropertyLookup.get(propertyname);   + testSuite.setNumberOfTests(totalint);  
- if (value == null || value.length() == 0 || "null".equals(value)) {   + testSuite.setNumberOfSuccesses(successint);  
- value = BuildLifeLookup.getCurrent().getOriginatingWorkflow().getRequest().getProperty(propertyname);   + testSuite.store();  
- }      
-      
- for (int i = 0; i < agents.length; i++) {      
- Agent agent = agents[i];      
-      
- AgentRemoteControl remote = new AgentRemoteControl(agent);      
- VarServiceClient client;      
- try {      
- client = remote.getVarServiceClient();      
       
- String varValue = client.getVarValue(varName);   + for (i=0; i<srun; i++){  
- if (varValue != null && varValue.contains(value)) {   + String stringpre = "Inserting test #";  
- resultList.add(agent);   + String stringpost = " into Anthill";  
- }   + String pass = Integer.toString(i+1);  
- }   + String stringer = stringpre + pass + stringpost;  
- catch (Exception e) {   +  
- throw new RuntimeException(e);   + while(totalint < successint){roller();}  
- }   + newtimeint();  
- }   + testCase = new TestCase();  
-   + testCase.setName("testMethod");  
- Agent[] result = new Agent[resultList.size()];   + testCase.setClassName("com.urbancode.Test");  
- resultList.toArray(result);   + testCase.setTestSuite(testSuite);  
- return result;   + testCase.setTime(timeint);  
- }   + testCase.setMessage(stringer);  
    + testCase.setResult("success");  
    + testCase.store();  
   
       
    + for (i=0; i<frun; i++){  
    + String stringpre = "Inserting test #";  
    + String stringpost = " into Anthill";  
    + String pass = Integer.toString(i+1);  
    + String stringer = stringpre + pass + stringpost;  
    +  
    + while(totalint < successint){roller();}  
    + newtimeint();  
    +  
    + testCase = new TestCase();  
    + testCase.setName("testMethod");  
    + testCase.setClassName("com.urbancode.Test");  
    + testCase.setTestSuite(testSuite);  
    + testCase.setTime(timeint);  
    + testCase.setMessage(stringer);  
    + testCase.setResult("failure");  
    + testCase.store();  
    + }</pre>  
       
- /**** Start Script *****/      
- return Where.all(      
- new PropertyBasedAgentVarContainsCriteria("Track", "Track"),      
- new PropertyBasedAgentVarEqualsCriteria("TechType", "TechType")      
- );</pre>      
       
  = Add unknown SCM users as AHP users =    = Add unknown SCM users as AHP users = 
       
Line 217:
  }</pre>    }</pre> 
       
    + = Template for adding users to realms =  
       
    + Script Notes:  
    + * Treat this mostly as a snippet for doing something more interesting with.  
       
    + ==== AHPSCRIPTS-1 ====  
       
    + <pre>import com.urbancode.anthill3.domain.project.*;  
    + import com.urbancode.anthill3.domain.authentication.*;  
    + import com.urbancode.anthill3.domain.security.*;  
    + import com.urbancode.anthill3.domain.singleton.security.*;  
       
    + // Lookup Your roles  
    + buildRole = RoleFactory.getInstance().restoreForName("Build Master");  
    + userRole = RoleFactory.getInstance().restoreForName("User");  
       
    + // Look-up the LDAP realm here  
    + // This is a lookup by name, so insert the name of your LDAP realm.  
    + realm = AuthenticationRealmFactory.getInstance().restore("LDAP");  
       
    + // Do this for each user:  
    + User user1 = new User(true, realm);  
       
    + user1.setName("JaneDoe");  
    + user1.setPassword("Bogus");  
    + user1.addRole(userRole);  
    + user1.addRole(buildRole);  
    + user1.store();</pre>  
       
    + = Properties Contained Within Variables =  
       
    + Script Notes:  
    + * The idea here is that the user has identified a couple agent items (a Track and technology type) that span environments. This scirpt filters agents so that the technology type must always match the technology type specified on the workflow property. The tracks are similar but some agents may participate in mulitple tracks (prod is that same for all) so prod would have a track value of something like 1|2|3 to indicate that it supports tracks 1, 2 and 3. So we need to inspect it to see if any match.  
    + * I changed the Track one to just see if the agent value contains the requested value. It should work fine until you get up to track 10, then you have to do 01, 02, etc.  
       
    + ==== AHPSCRIPTS-2 ====  
       
    + <pre>import com.urbancode.anthill3.domain.agent.Agent;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.* ;  
    + import com.urbancode.anthill3.services.agent.AgentRemoteControl;  
    + import com.urbancode.devilfish.services.var.VarServiceClient;  
    + import java.util.*;  
       
    + public class PropertyBasedAgentVarEqualsCriteria extends VariableEqualsCriteria {  
       
    + String propertyname;  
       
    + public PropertyBasedAgentVarEqualsCriteria(String propertyname, String varName) {  
    + super(varName);  
    + this.propertyname = propertyname;  
    + }  
       
    + public Agent[] filter(Agent[] agents) {  
    + String value = PropertyLookup.get(propertyname);  
    + if (value == null || value.length() == 0 || "null".equals(value)) {  
    + value = BuildLifeLookup.getCurrent().getOriginatingWorkflow().getRequest().getProperty(propertyname);  
    + }  
    + this.setValue(value);  
    + return super.filter(agents);  
    + }  
    + }  
       
    + public class PropertyBasedAgentVarContainsCriteria extends Criteria {  
       
    + String propertyname;  
    + String varName;  
       
    + public PropertyBasedAgentVarContainsCriteria(String propertyname, String varName) {  
    + this.propertyname = propertyname;  
    + this.varName = varName;  
    + }  
       
    + public Agent[] filter(Agent[] agents) {  
    + ArrayList resultList = new ArrayList();  
    +  
    + String value = PropertyLookup.get(propertyname);  
    + if (value == null || value.length() == 0 || "null".equals(value)) {  
    + value = BuildLifeLookup.getCurrent().getOriginatingWorkflow().getRequest().getProperty(propertyname);  
    + }  
    +  
    + for (int i = 0; i < agents.length; i++) {  
    + Agent agent = agents[i];  
    +  
    + AgentRemoteControl remote = new AgentRemoteControl(agent);  
    + VarServiceClient client;  
    + try {  
    + client = remote.getVarServiceClient();  
       
    + String varValue = client.getVarValue(varName);  
    + if (varValue != null && varValue.contains(value)) {  
    + resultList.add(agent);  
    + }  
    + }  
    + catch (Exception e) {  
    + throw new RuntimeException(e);  
    + }  
    + }  
    +  
    + Agent[] result = new Agent[resultList.size()];  
    + resultList.toArray(result);  
    + return result;  
    + }  
    + }  
       
       
    + /**** Start Script *****/  
    + return Where.all(  
    + new PropertyBasedAgentVarContainsCriteria("Track", "Track"),  
    + new PropertyBasedAgentVarEqualsCriteria("TechType", "TechType")  
    + );</pre>  
       
       
       
  = Dynamic Dependency Selection =    = Dynamic Dependency Selection = 
       
Line 502:
  out.close(); </pre>    out.close(); </pre> 
       
- = Add Random Test Results to a BuildLife =      
       
- Script Notes:      
- * This is a good script to use if you need some garbage results on the '''Tests tab'''.      
       
- <pre>import com.urbancode.anthill3.domain.test.*;      
- import java.util.Random;      
       
- Random timerand = new Random();      
- Random successrand = new Random();      
- Random totalrand = new Random();      
- int timeint = timerand.nextInt(1048576) + 1;      
- int successint = successrand.nextInt(32768) + 1;      
- int totalint = totalrand.nextInt(32769) + 1;      
       
- Random srunrand = new Random();      
- Random frunrand = new Random();      
- int srun = srunrand.nextInt(254) + 1;      
- int frun = frunrand.nextInt(254) + 1;      
       
- public newnums()      
- {      
- timeint = timerand.nextInt(1048576) + 1;      
- successint = successrand.nextInt(32768) + 1;      
- totalint = totalrand.nextInt(32769) + 1;      
- }      
       
- public newtimeint()      
- {      
- timeint = timerand.nextInt(1048576) + 1;      
- }      
       
- public roller()      
- {      
- int rollnum=0;      
- while(totalint < successint){      
- newnums();      
- rollnum++;      
- commandOutput.println("REROLL #"+rollnum+". . .TRYING FOR BETTER NUMBERS");      
- }      
- }      
       
- while(totalint < successint){roller();}      
       
- testReport = new TestReport();      
- testReport.setName("Unit Tests");      
- testReport.setNumberOfSuites(1);      
- testReport.setNumberOfTests(totalint);      
- testReport.setNumberOfSuccesses(successint);      
- testReport.setNumberOfFailures(totalint-successint);      
- testReport.setJobTrace(JobTraceLookup.getCurrent());      
- testReport.store();      
       
- while(totalint < successint){roller();}      
       
- testSuite = new TestSuite();      
- testSuite.setName("Test Suite");      
- testSuite.setTestReport(testReport);      
- testSuite.setNumberOfTests(totalint);      
- testSuite.setNumberOfSuccesses(successint);      
- testSuite.store();      
       
- for (i=0; i<srun; i++){      
- String stringpre = "Inserting test #";      
- String stringpost = " into Anthill";      
- String pass = Integer.toString(i+1);      
- String stringer = stringpre + pass + stringpost;      
-      
- while(totalint < successint){roller();}      
- newtimeint();      
- testCase = new TestCase();      
- testCase.setName("testMethod");      
- testCase.setClassName("com.urbancode.Test");      
- testCase.setTestSuite(testSuite);      
- testCase.setTime(timeint);      
- testCase.setMessage(stringer);      
- testCase.setResult("success");      
- testCase.store();      
- }      
       
- for (i=0; i<frun; i++){      
- String stringpre = "Inserting test #";      
- String stringpost = " into Anthill";      
- String pass = Integer.toString(i+1);      
- String stringer = stringpre + pass + stringpost;      
-      
- while(totalint < successint){roller();}      
- newtimeint();      
-      
- testCase = new TestCase();      
- testCase.setName("testMethod");      
- testCase.setClassName("com.urbancode.Test");      
- testCase.setTestSuite(testSuite);      
- testCase.setTime(timeint);      
- testCase.setMessage(stringer);      
- testCase.setResult("failure");      
- testCase.store();      
- }</pre>      
       
  = Change Owner of Project or Other Resource =    = Change Owner of Project or Other Resource = 
  This script must be run as the admin user. Just change the '''projectName''' and '''newOwnerName'''.    This script must be run as the admin user. Just change the '''projectName''' and '''newOwnerName'''. 
Line 2317:
       
       
- = Add a Debugger to Log System Events =      
- * '''Please note this event listener stays in the System until the next restart. Running the script more than once will create duplicate listeners and logging.'''      
       
- ==== AHPSCRIPTS-121 ====      
- <pre>import com.urbancode.anthill3.domain.buildrequest.BuildRequestEvent;      
- import com.urbancode.anthill3.domain.security.UserFactory;      
- import com.urbancode.anthill3.domain.workflow.WorkflowEvent;      
- import com.urbancode.anthill3.persistence.UnitOfWork;      
- import com.urbancode.anthill3.services.event.EventListener;      
- import com.urbancode.anthill3.services.event.EventService;      
- import com.urbancode.anthill3.services.event.criteria.Criteria;      
- import java.util.EventObject;      
- import org.apache.log4j.Logger;      
       
- public class LoggingEventListener implements EventListener {      
       
- Logger log = Logger.getLogger("LoggingEventListener");      
       
- public void handleEvent(EventObject event) {      
- if (event instanceof BuildRequestEvent) {      
- UnitOfWork uow = UnitOfWork.create(UserFactory.getSystemUser());      
- try {      
- requestEvent = (BuildRequestEvent) event;      
- request = requestEvent.getSource();      
- log.warn("Received " + event.getClass().getSimpleName() + " for request " + request.getId() + " with status " + request.getStatus());      
- }      
- finally {      
- uow.close();      
- }      
- }      
- else if (event instanceof WorkflowEvent) {      
- UnitOfWork uow = UnitOfWork.create(UserFactory.getSystemUser());      
- try {      
- workflowEvent = (WorkflowEvent) event;      
- workflow = workflowEvent.getSource();      
- log.warn("Received " + event.getClass().getSimpleName() + " for workflow " + workflow.getId() + " with status " + workflow.getStatus());      
- }      
- finally {      
- uow.close();      
- }      
- }      
- }      
       
- public Class getEventClass() {      
- // get all events      
- return EventObject.class;      
- }      
       
- public Criteria[] getCriteria() {      
- return null;      
- }      
- }      
       
- EventService.getInstance().registerEventListener(new LoggingEventListener());</pre>      
       
  = Clean up Massive Job Log Directories =    = Clean up Massive Job Log Directories =