Version Differences for Miscellaneous Scripts

(More alphabetization)
(Even more alphabetization)
Line 486:
  System.out.println("Deleted " + totalDeletes + " empty directories in " + secs + " seconds (" + deletionRate + " deletes per second).");</pre>    System.out.println("Deleted " + totalDeletes + " empty directories in " + secs + " seconds (" + deletionRate + " deletes per second).");</pre> 
       
- = Template for adding users to realms =   + = Consolidate Dependency Reports And Publish on Parent Project =  
    + * This is a script that will take logs published as reports for dependency projects and consolidate them so that they can be viewed in one location on the reports tab of the blanket project. A more efficient way is listed secondly.  
       
- Script Notes:   + ==== AHPSCRIPTS-7 ====  
- * Treat this mostly as a snippet for doing something more interesting with.   + <pre>import com.urbancode.anthill3.runtime.scripting.helpers.BuildLifeLookup;  
    + import com.urbancode.anthill3.runtime.paths.PublishPathHelper;  
    + import com.urbancode.anthill3.domain.buildlife.BuildLife;  
    + import com.urbancode.anthill3.domain.workflow.WorkflowCase;  
    + import com.urbancode.devilfish.services.var.VarService;  
    + import com.urbancode.anthill3.domain.jobtrace.JobTrace;  
    + import com.urbancode.commons.fileutils.FileUtils;  
    + import com.urbancode.codestation2.domain.buildlife.CodestationCompatableBuildLife;  
    + import java.util.*;  
       
- ==== AHPSCRIPTS-1 ====   + static final private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ReportScript");  
       
- <pre>import com.urbancode.anthill3.domain.project.*;   + final String REPORT_NAME = "LogReport";  
- import com.urbancode.anthill3.domain.authentication.*;      
- import com.urbancode.anthill3.domain.security.*;      
- import com.urbancode.anthill3.domain.singleton.security.*;      
       
- // Lookup Your roles   + VarService vs = VarService.getInstance();  
- buildRole = RoleFactory.getInstance().restoreForName("Build Master");      
- userRole = RoleFactory.getInstance().restoreForName("User");      
       
- // Look-up the LDAP realm here   + void centrallyLocateLogsForBuildLife(BuildLife buildLife, String reportPath) {  
- // This is a lookup by name, so insert the name of your LDAP realm.      
- realm = AuthenticationRealmFactory.getInstance().restore("LDAP");      
       
- // Do this for each user:   + File reportDir = new File(reportPath + File.separator + buildLife.getProfile().getProject().getName().replace(" ", "_"));  
- User user1 = new User(true, realm);   + reportDir.mkdirs();  
    + log.warn("Report Path Dir" + reportDir.getAbsolutePath());  
       
- user1.setName("JaneDoe");   + WorkflowCase workflowCase = buildLife.getOriginatingWorkflow();  
- user1.setPassword("Bogus");   + JobTrace[] jobTraces = workflowCase.getJobTraceArray();  
- user1.addRole(userRole);      
- user1.addRole(buildRole);      
- user1.store();</pre>      
       
- = Properties Contained Within Variables =   + for (int i = 0; i < jobTraces.length; i++) {  
    + String dependencyPath = PublishPathHelper.getInstance().getPublishPath(jobTraces[i], REPORT_NAME);  
    + log.warn("Dependency Path " + dependencyPath);  
    + dependencyPath = vs.resolve(dependencyPath);  
    + File dependencyReportDir = new File(dependencyPath);  
    + if (dependencyReportDir != null &&  
    + dependencyReportDir.exists() &&  
    + dependencyReportDir.isDirectory()) {  
    + log.warn("Copying reports from " + dependencyReportDir.getAbsolutePath() + " to " + reportDir.getAbsolutePath());  
    + FileUtils.copyFiles(dependencyReportDir, reportDir);  
    + }  
    + }  
    + }  
       
- Script Notes:   + Set buildDependencyBuildLivesSet(BuildLife buildLife) {  
- * 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.   + Set result = new HashSet();  
- * 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 ====   + CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();  
    + for (int i = 0; i < dependencyBuildLives.length; i++) {  
    + CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];  
    + if (dependencyBuildLife instanceof BuildLife) {  
    + log.warn("DependencyBuildLife " + dependencyBuildLife.getName());  
    + result.add(dependencyBuildLife);  
    + result.addAll(buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife));  
    + }  
    + }  
       
- <pre>import com.urbancode.anthill3.domain.agent.Agent;   + return result;  
- 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;   + BuildLife buildLife = BuildLifeLookup.getCurrent();  
       
- public PropertyBasedAgentVarEqualsCriteria(String propertyname, String varName) {   + String reportPath = PublishPathHelper.getInstance().getPublishPath(JobTraceLookup.getCurrent(), REPORT_NAME);  
- super(varName);   + reportPath = vs.resolve(reportPath);  
- this.propertyname = propertyname;   + log.warn("Report Path " + reportPath);  
- }      
       
- public Agent[] filter(Agent[] agents) {   + Set dependencyBuildLifeSet = buildDependencyBuildLivesSet(buildLife);  
- String value = PropertyLookup.get(propertyname);   + Iterator itr = dependencyBuildLifeSet.iterator();  
- 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 {   + while (itr.hasNext()) {  
    + BuildLife buildLife = (BuildLife) itr.next();  
    + centrallyLocateLogsForBuildLife(buildLife, reportPath);  
    + }</pre>  
       
- String propertyname;   + ''More Efficient Way'':  
- String varName;      
       
- public PropertyBasedAgentVarContainsCriteria(String propertyname, String varName) {   + * more efficient way of getting the entire dependency graph. This will not process the same build life more than once.  
- this.propertyname = propertyname;      
- this.varName = varName;      
- }      
       
- public Agent[] filter(Agent[] agents) {   + <pre>Set buildDependencyBuildLivesSet(BuildLife buildLife) {  
- ArrayList resultList = new ArrayList();   + Set result = new HashSet();  
-   + buildDependencyBuildLivesSet(buildLife, result, new HashSet());  
- String value = PropertyLookup.get(propertyname);   + return result;  
- 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);   + buildDependencyBuildLivesSet(BuildLife buildLife, Set depSet, Set traversedSet) {  
- if (varValue != null && varValue.contains(value)) {   + if (!travesedSet.contains(buildLife)) {  
- resultList.add(agent);   + travesedSet.add(buildLife):  
- }   + CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();  
    + for (int i = 0; i < dependencyBuildLives.length; i++) {  
    + CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];  
    + if (dependencyBuildLife instanceof BuildLife) {  
    + log.warn("DependencyBuildLife " + dependencyBuildLife.getName());  
    + depSet.add(dependencyBuildLife);  
    + buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife, depSet, traversedSet));  
    + }  
    + }  
    + }  
    + }</pre>  
    +  
    + = Consolidate Reports of Dependency Projects & Publish Them in Parent Project Report =  
       
    + Script Notes:  
    + * This is a script that will take logs published as reports for dependency projects and consolidate them so that they can be viewed in one location on the reports tab of the blanket project.  
    + * Here is a more efficient way of getting the entire dependency graph. This will not process the same build life more than once.  
    + :* <pre>Set buildDependencyBuildLivesSet(BuildLife buildLife) {  
    + Set result = new HashSet();  
    + buildDependencyBuildLivesSet(buildLife, result, new HashSet());  
    + return result;  
    + }  
       
    + buildDependencyBuildLivesSet(BuildLife buildLife, Set depSet, Set traversedSet) {  
    + if (!travesedSet.contains(buildLife)) {  
    + travesedSet.add(buildLife):  
    + CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();  
    + for (int i = 0; i < dependencyBuildLives.length; i++) {  
    + CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];  
    + if (dependencyBuildLife instanceof BuildLife) {  
    + log.warn("DependencyBuildLife " + dependencyBuildLife.getName());  
    + depSet.add(dependencyBuildLife);  
    + buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife, depSet, traversedSet));  
   
- catch (Exception e) {      
- throw new RuntimeException(e);      
- }      
   
-      
- Agent[] result = new Agent[resultList.size()];      
- resultList.toArray(result);      
- return result;      
   
- }   + } </pre>  
       
    + ==== AHPSCRIPTS-7 ====  
       
- /**** Start Script *****/   + <pre>import com.urbancode.anthill3.runtime.scripting.helpers.BuildLifeLookup;  
- return Where.all(   + import com.urbancode.anthill3.runtime.paths.PublishPathHelper;  
- new PropertyBasedAgentVarContainsCriteria("Track", "Track"),   + import com.urbancode.anthill3.domain.buildlife.BuildLife;  
- new PropertyBasedAgentVarEqualsCriteria("TechType", "TechType")   + import com.urbancode.anthill3.domain.workflow.WorkflowCase;  
- );</pre>   + import com.urbancode.devilfish.services.var.VarService;  
    + import com.urbancode.anthill3.domain.jobtrace.JobTrace;  
    + import com.urbancode.commons.fileutils.FileUtils;  
    + import com.urbancode.codestation2.domain.buildlife.CodestationCompatableBuildLife;  
    + import java.util.*;  
       
    + static final private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ReportScript");  
       
    + final String REPORT_NAME = "LogReport";  
       
- = Dynamic Dependency Selection =   + VarService vs = VarService.getInstance();  
       
- Script Notes:   + void centrallyLocateLogsForBuildLife(BuildLife buildLife, String reportPath) {  
- * This script is used to select only a subset of the dependencies. So if my project is dependent on the componets S1, S2, S3. This script empowers you to select only S1 and S3. It does this by looking for builds that have been stamped with some value passed into this workflow.      
- * The relationships to S1, S2, and S3 can't be defined in the workflow running this script or else they'll be locked in at build time, so we use a separate "template" workflow that defines the base relationships. This script inspects that workflow to determine which build tracks produce candidates for dependency selection.      
- * you will need to change the replace(String, String) to replace(Char, Char) if you are not running java 1.5, oops sorry.      
       
- ==== AHPSCRIPTS-4 ====   + File reportDir = new File(reportPath + File.separator + buildLife.getProfile().getProject().getName().replace(" ", "_"));  
    + reportDir.mkdirs();  
    + log.warn("Report Path Dir" + reportDir.getAbsolutePath());  
       
- <pre>import com.urbancode.codestation2.domain.project.*;   + WorkflowCase workflowCase = buildLife.getOriginatingWorkflow();  
- import com.urbancode.anthill3.domain.buildlife.*;   + JobTrace[] jobTraces = workflowCase.getJobTraceArray();  
- import com.urbancode.anthill3.runtime.scripting.helpers.*;      
- import com.urbancode.anthill3.dashboard.*;      
       
    + for (int i = 0; i < jobTraces.length; i++) {  
    + String dependencyPath = PublishPathHelper.getInstance().getPublishPath(jobTraces[i], REPORT_NAME);  
    + log.warn("Dependency Path " + dependencyPath);  
    + dependencyPath = vs.resolve(dependencyPath);  
    + File dependencyReportDir = new File(dependencyPath);  
    + if (dependencyReportDir != null &&  
    + dependencyReportDir.exists() &&  
    + dependencyReportDir.isDirectory()) {  
    + log.warn("Copying reports from " + dependencyReportDir.getAbsolutePath() + " to " + reportDir.getAbsolutePath());  
    + FileUtils.copyFiles(dependencyReportDir, reportDir);  
    + }  
    + }  
    + }  
       
- private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger   + Set buildDependencyBuildLivesSet(BuildLife buildLife) {  
- ("ScriptEval");   + Set result = new HashSet();  
       
- String TEMPLATE_WORKFLOW_NAME="All Dependencies (Most Recent)";   + CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();  
    + for (int i = 0; i < dependencyBuildLives.length; i++) {  
- project = ProjectLookup.getCurrent();   + CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];  
- buildLife = BuildLifeLookup.getCurrent();   + if (dependencyBuildLife instanceof BuildLife) {  
- workflowArray = project.getOriginatingWorkflowArray();   + log.warn("DependencyBuildLife " + dependencyBuildLife.getName());  
- stamp = PropertyLookup.getValue("stamp");   + result.add(dependencyBuildLife);  
    + result.addAll(buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife));  
- templateWorkflow = null;      
- for (int i = 0; i < workflowArray.length; i++) {      
- if (workflowArray[i].getName().equals(TEMPLATE_WORKFLOW_NAME)) {      
- templateWorkflow = workflowArray[i];      
- break;      
- }      
   
    + }  
       
- if (templateWorkflow == null) {   + return result;  
- throw new Exception("Could not find expected workflow on this project: " + TEMPLATE_WORKFLOW_NAME);      
   
       
- dependencyArray = templateWorkflow.getBuildProfile().getDependencyArray();      
- log.error("Dependencies found in template: " + dependencyArray.length);      
- for (int i = 0; i < dependencyArray.length; i++) {      
- dep = dependencyArray[i].getDependency();      
- if (dep instanceof AnthillProject) {      
- buildProfile = dep.getBuildProfile();      
- workflow = buildProfile.getWorkflow();      
- depProject = buildProfile.getProject();      
- buildLifeSummaries = DashboardFactory.getInstance().getBuildLifeSummariesForProjectWithStamp(depProject, workflow, stamp, null, null);      
- log.info("Found " + buildLifeSummaries.length + " builds for stamp " + stamp + " on project " + depProject.getName() + " " + workflow.getName());      
- // We found a build life matching the right stamp, let's add it as a dependency      
- if (buildLifeSummaries.length > 0) {      
- // take the most recent build matching that stamp.      
- depBuildLife = BuildLifeFactory.getInstance().restore(buildLifeSummaries[0].getBuildLifeId());      
- buildLife.addDependencyBuildLife(depBuildLife);      
- }      
- }      
- }</pre>      
       
    + BuildLife buildLife = BuildLifeLookup.getCurrent();  
       
    + String reportPath = PublishPathHelper.getInstance().getPublishPath(JobTraceLookup.getCurrent(), REPORT_NAME);  
    + reportPath = vs.resolve(reportPath);  
    + log.warn("Report Path " + reportPath);  
       
    + Set dependencyBuildLifeSet = buildDependencyBuildLivesSet(buildLife);  
    + Iterator itr = dependencyBuildLifeSet.iterator();  
       
    + while (itr.hasNext()) {  
    + BuildLife buildLife = (BuildLife) itr.next();  
    + centrallyLocateLogsForBuildLife(buildLife, reportPath);  
    + } </pre>  
       
       
       
    + = Dashboard Not Displaying Latest BuildLife For Workflow Fix =  
    + *In older versions there was a bug where some build lives did not get entries in the BUILD_WORKFLOW_SUMMARY table that feeds the latest activity on the dashboard.  
    + **This can be run from Evaluate Script step. Enter the build life ids in the array for which ones to fix.  
    + ==== AHPSCRIPTS-136 ====  
    + <pre>import com.urbancode.anthill3.domain.buildlife.BuildLife;  
    + import com.urbancode.anthill3.domain.buildlife.BuildLifeFactory;  
    + import com.urbancode.anthill3.domain.workflow.WorkflowCase;  
    + import com.urbancode.anthill3.domain.workflow.WorkflowEndEvent;  
    + import com.urbancode.anthill3.services.event.EventService;  
       
    + long[] buildLifeIds = new long[] {  
    + 400L, 468L, 469L, 506L, 512L, 537L, 576L, 585L, 596L, 642L, 649L,  
    + 658L, 780L, 786L, 980L, 1025L, 1046L, 1051L, 1057L, 1086L, 1090L,  
    + 1094L, 1095L, 1110L, 1168L, 1174L, 1228L, 1366L, 1367L, 1378L,  
    + 1379L, 1383L, 1400L, 1426L, 1609L, 1712L, 1719L, 1749L, 1758L,  
    + 1770L, 1889L, 1917L, 1920L, 1931L, 1968L, 2033L, 2119L  
    + };  
    + for (long buildLifeId : buildLifeIds) {  
    + BuildLife buildLife = BuildLifeFactory.getInstance().restore(buildLifeId);  
    + WorkflowCase workflowCase = buildLife.getOriginatingWorkflow();  
    + WorkflowEndEvent workflowEndEvent = new WorkflowEndEvent(workflowCase);  
    + EventService.getInstance().sendEvent(workflowEndEvent);  
    + }</pre>  
    +  
  = Double Dynamic Dependency Selection =    = Double Dynamic Dependency Selection = 
       
Line 714:
       
  When the workflows to select are a well known set, you might want to use the strategy identified [http://wiki.urbancode.com/AnthillPro/Miscellaneous_Scripts#AHPSCRIPTS-4 by AHPSCRIPTS-4]    When the workflows to select are a well known set, you might want to use the strategy identified [http://wiki.urbancode.com/AnthillPro/Miscellaneous_Scripts#AHPSCRIPTS-4 by AHPSCRIPTS-4] 
- = Consolidate Reports of Dependency Projects & Publish Them in Parent Project Report =      
       
    + = Dynamic Dependency Selection =  
       
  Script Notes:    Script Notes: 
- * This is a script that will take logs published as reports for dependency projects and consolidate them so that they can be viewed in one location on the reports tab of the blanket project.   + * This script is used to select only a subset of the dependencies. So if my project is dependent on the componets S1, S2, S3. This script empowers you to select only S1 and S3. It does this by looking for builds that have been stamped with some value passed into this workflow.  
- * Here is a more efficient way of getting the entire dependency graph. This will not process the same build life more than once.   + * The relationships to S1, S2, and S3 can't be defined in the workflow running this script or else they'll be locked in at build time, so we use a separate "template" workflow that defines the base relationships. This script inspects that workflow to determine which build tracks produce candidates for dependency selection.  
- :* <pre>Set buildDependencyBuildLivesSet(BuildLife buildLife) {   + * you will need to change the replace(String, String) to replace(Char, Char) if you are not running java 1.5, oops sorry.  
- Set result = new HashSet();      
- buildDependencyBuildLivesSet(buildLife, result, new HashSet());   + ==== AHPSCRIPTS-4 ====  
- return result;      
    + <pre>import com.urbancode.codestation2.domain.project.*;  
    + import com.urbancode.anthill3.domain.buildlife.*;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.*;  
    + import com.urbancode.anthill3.dashboard.*;  
       
       
    + private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger  
    + ("ScriptEval");  
       
    + String TEMPLATE_WORKFLOW_NAME="All Dependencies (Most Recent)";  
       
    + project = ProjectLookup.getCurrent();  
    + buildLife = BuildLifeLookup.getCurrent();  
    + workflowArray = project.getOriginatingWorkflowArray();  
    + stamp = PropertyLookup.getValue("stamp");  
       
    + templateWorkflow = null;  
    + for (int i = 0; i < workflowArray.length; i++) {  
    + if (workflowArray[i].getName().equals(TEMPLATE_WORKFLOW_NAME)) {  
    + templateWorkflow = workflowArray[i];  
    + break;  
    + }  
   
       
- buildDependencyBuildLivesSet(BuildLife buildLife, Set depSet, Set traversedSet) {   + if (templateWorkflow == null) {  
- if (!travesedSet.contains(buildLife)) {   + throw new Exception("Could not find expected workflow on this project: " + TEMPLATE_WORKFLOW_NAME);  
- travesedSet.add(buildLife):   + }  
- CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();      
- for (int i = 0; i < dependencyBuildLives.length; i++) {      
- CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];      
- if (dependencyBuildLife instanceof BuildLife) {      
- log.warn("DependencyBuildLife " + dependencyBuildLife.getName());      
- depSet.add(dependencyBuildLife);      
- buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife, depSet, traversedSet));      
- }      
- }      
- }      
- } </pre>      
       
- ==== AHPSCRIPTS-7 ====   + dependencyArray = templateWorkflow.getBuildProfile().getDependencyArray();  
    + log.error("Dependencies found in template: " + dependencyArray.length);  
    + for (int i = 0; i < dependencyArray.length; i++) {  
    + dep = dependencyArray[i].getDependency();  
    + if (dep instanceof AnthillProject) {  
    + buildProfile = dep.getBuildProfile();  
    + workflow = buildProfile.getWorkflow();  
    + depProject = buildProfile.getProject();  
    + buildLifeSummaries = DashboardFactory.getInstance().getBuildLifeSummariesForProjectWithStamp(depProject, workflow, stamp, null, null);  
    + log.info("Found " + buildLifeSummaries.length + " builds for stamp " + stamp + " on project " + depProject.getName() + " " + workflow.getName());  
    + // We found a build life matching the right stamp, let's add it as a dependency  
    + if (buildLifeSummaries.length > 0) {  
    + // take the most recent build matching that stamp.  
    + depBuildLife = BuildLifeFactory.getInstance().restore(buildLifeSummaries[0].getBuildLifeId());  
    + buildLife.addDependencyBuildLife(depBuildLife);  
    + }  
    + }  
    + }</pre>  
       
- <pre>import com.urbancode.anthill3.runtime.scripting.helpers.BuildLifeLookup;   + = Dynamically Resolve Artifact Set Based Upon Current Environment =  
- import com.urbancode.anthill3.runtime.paths.PublishPathHelper;   + ==== AHPSCRIPTS-41 ====  
- import com.urbancode.anthill3.domain.buildlife.BuildLife;   + <pre>import com.urbancode.anthill3.domain.artifacts.*;  
- import com.urbancode.anthill3.domain.workflow.WorkflowCase;   + import com.urbancode.anthill3.runtime.scripting.helpers.*;  
- import com.urbancode.devilfish.services.var.VarService;   + import com.urbancode.anthill3.step.artifacts.*;  
- import com.urbancode.anthill3.domain.jobtrace.JobTrace;   + import com.urbancode.anthill3.services.jobs.StepExecutor;  
- import com.urbancode.commons.fileutils.FileUtils;      
- import com.urbancode.codestation2.domain.buildlife.CodestationCompatableBuildLife;      
- import java.util.*;      
       
- static final private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ReportScript");   + log = org.apache.log4j.Logger.getLogger("Scripted_Artifact_Resolve");  
       
- final String REPORT_NAME = "LogReport";   + try {  
    + String setName = EnvironmentLookup.getCurrent().getName();  
       
- VarService vs = VarService.getInstance();   + ArtifactModule module = new ArtifactModule();  
    + module.setArtifactSet(ProjectLookup.getCurrent().getLifeCycleModel().getArtifactSetGroup().getArtifactSet(setName));  
    + module.setTargetDirectory("artifacts");  
       
- void centrallyLocateLogsForBuildLife(BuildLife buildLife, String reportPath) {   + ResolveMyArtifactsStep step = new ResolveMyArtifactsStep(module);  
    + step.setExecutor(StepExecutor.getCurrent());  
    + step.setJob(StepExecutor.getCurrent().getJob());  
    + step.setAgent(StepExecutor.getCurrent().getJobTrace().getAgent());  
    + step.perform();  
    + }  
    + catch (Exception e) {  
    + log.error(e.getMessage(), e);  
    + throw e;  
    + }</pre>  
       
- File reportDir = new File(reportPath + File.separator + buildLife.getProfile().getProject().getName().replace(" ", "_"));   + = Update Environment Properties for All Projects =  
- reportDir.mkdirs();   + Here's a modification that runs form within AnthillPro. The environment is selected to be the current running environment and the other values are read out of properties (the environment could come from a property as well).  
- log.warn("Report Path Dir" + reportDir.getAbsolutePath());   + ==== AHPSCRIPTS-14 ====  
    + <pre>import com.urbancode.anthill3.main.client.AnthillClient;  
    + import com.urbancode.anthill3.persistence.UnitOfWork;  
    + import com.urbancode.anthill3.domain.project.*;  
    + import com.urbancode.anthill3.domain.project.envprops.*;  
    + import com.urbancode.anthill3.domain.servergroup.*;  
    + import java.util.*;  
       
- WorkflowCase workflowCase = buildLife.getOriginatingWorkflow();      
- JobTrace[] jobTraces = workflowCase.getJobTraceArray();      
       
- for (int i = 0; i < jobTraces.length; i++) {   + variableName = PropertyLookup.get("variableName");  
- String dependencyPath = PublishPathHelper.getInstance().getPublishPath(jobTraces[i], REPORT_NAME);   + newVariableValue = PropertyLookup.get("variableValue");  
- log.warn("Dependency Path " + dependencyPath);   + boolean isSecure = Boolean.parseBoolean(PropertyLookup.get("isSecure"));  
- dependencyPath = vs.resolve(dependencyPath);   + environment = EnvironmentLookup.getCurrent();  
- File dependencyReportDir = new File(dependencyPath);      
- if (dependencyReportDir != null &&      
- dependencyReportDir.exists() &&   + // Project  
- dependencyReportDir.isDirectory()) {   + Project[] projects = ProjectFactory.getInstance().restoreAll();  
- log.warn("Copying reports from " + dependencyReportDir.getAbsolutePath() + " to " + reportDir.getAbsolutePath());      
- FileUtils.copyFiles(dependencyReportDir, reportDir);   + // Loop through projects. If it has environment props for this, find the  
    + // right one and change it as appropriate.  
    + for (int i = 0; i < projects.length; i++) {  
    +  
    + ProjectEnvironmentProperty[] envPropArray = environment.getEnvironmentPropertiesForProject(projects[i]);  
    + boolean foundVariable = false;  
    + for (int j = 0; j < envPropArray.length; j++) {  
    + if (envPropArray[j].getName().equals(variableName)) {  
    + foundVariable = true;  
    + envPropArray[j].setValue(newVariableValue);  
    + envPropArray[j].store();  
    + }  
    + }  
    +  
    + if (!foundVariable) {  
    + ProjectEnvironmentProperty newProp = new ProjectEnvironmentProperty(true);  
    +  
    + newProp.setProject(projects[i]);  
    + newProp.setServerGroup(environment);  
    + newProp.setName(variableName);  
    + newProp.setValue(newVariableValue);  
    + newProp.setSetAsEnvProp(false);  
    + newProp.setSecureValue(isSecure);  
    +  
    + newProp.store();  
    + }  
    +  
   
- }   + </pre>  
- }      
       
- Set buildDependencyBuildLivesSet(BuildLife buildLife) {   + = Template for adding users to realms =  
- Set result = new HashSet();      
       
- CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();   + Script Notes:  
- for (int i = 0; i < dependencyBuildLives.length; i++) {   + * Treat this mostly as a snippet for doing something more interesting with.  
- CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];      
- if (dependencyBuildLife instanceof BuildLife) {   + ==== AHPSCRIPTS-1 ====  
- log.warn("DependencyBuildLife " + dependencyBuildLife.getName());      
- result.add(dependencyBuildLife);   + <pre>import com.urbancode.anthill3.domain.project.*;  
- result.addAll(buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife));   + 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);  
    + }  
   
- }      
       
- return result;   + 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;  
    + }  
   
       
Line 800:
- BuildLife buildLife = BuildLifeLookup.getCurrent();   + /**** Start Script *****/  
    + return Where.all(  
    + new PropertyBasedAgentVarContainsCriteria("Track", "Track"),  
    + new PropertyBasedAgentVarEqualsCriteria("TechType", "TechType")  
    + );</pre>  
       
- String reportPath = PublishPathHelper.getInstance().getPublishPath(JobTraceLookup.getCurrent(), REPORT_NAME);      
- reportPath = vs.resolve(reportPath);      
- log.warn("Report Path " + reportPath);      
       
- Set dependencyBuildLifeSet = buildDependencyBuildLivesSet(buildLife);      
- Iterator itr = dependencyBuildLifeSet.iterator();      
       
- while (itr.hasNext()) {      
- BuildLife buildLife = (BuildLife) itr.next();      
- centrallyLocateLogsForBuildLife(buildLife, reportPath);      
- } </pre>      
       
       
       
       
  = Export Project Configuration to XML =    = Export Project Configuration to XML = 
       
Line 912:
  workflow.getRequest().setProperty("resolve.status", status); </pre>    workflow.getRequest().setProperty("resolve.status", status); </pre> 
       
- = Dynamically Resolve Artifact Set Based Upon Current Environment =      
- ==== AHPSCRIPTS-41 ====      
- <pre>import com.urbancode.anthill3.domain.artifacts.*;      
- import com.urbancode.anthill3.runtime.scripting.helpers.*;      
- import com.urbancode.anthill3.step.artifacts.*;      
- import com.urbancode.anthill3.services.jobs.StepExecutor;      
       
- log = org.apache.log4j.Logger.getLogger("Scripted_Artifact_Resolve");      
       
- try {      
- String setName = EnvironmentLookup.getCurrent().getName();      
       
- ArtifactModule module = new ArtifactModule();      
- module.setArtifactSet(ProjectLookup.getCurrent().getLifeCycleModel().getArtifactSetGroup().getArtifactSet(setName));      
- module.setTargetDirectory("artifacts");      
       
- ResolveMyArtifactsStep step = new ResolveMyArtifactsStep(module);      
- step.setExecutor(StepExecutor.getCurrent());      
- step.setJob(StepExecutor.getCurrent().getJob());      
- step.setAgent(StepExecutor.getCurrent().getJobTrace().getAgent());      
- step.perform();      
- }      
- catch (Exception e) {      
- log.error(e.getMessage(), e);      
- throw e;      
- }</pre>      
       
- = Update Environment Properties for All Projects =      
- Here's a modification that runs form within AnthillPro. The environment is selected to be the current running environment and the other values are read out of properties (the environment could come from a property as well).      
- ==== AHPSCRIPTS-14 ====      
- <pre>import com.urbancode.anthill3.main.client.AnthillClient;      
- import com.urbancode.anthill3.persistence.UnitOfWork;      
- import com.urbancode.anthill3.domain.project.*;      
- import com.urbancode.anthill3.domain.project.envprops.*;      
- import com.urbancode.anthill3.domain.servergroup.*;      
- import java.util.*;      
       
       
- variableName = PropertyLookup.get("variableName");      
- newVariableValue = PropertyLookup.get("variableValue");      
- boolean isSecure = Boolean.parseBoolean(PropertyLookup.get("isSecure"));      
- environment = EnvironmentLookup.getCurrent();      
       
       
- // Project      
- Project[] projects = ProjectFactory.getInstance().restoreAll();      
       
- // Loop through projects. If it has environment props for this, find the      
- // right one and change it as appropriate.      
- for (int i = 0; i < projects.length; i++) {      
-      
- ProjectEnvironmentProperty[] envPropArray = environment.getEnvironmentPropertiesForProject(projects[i]);      
- boolean foundVariable = false;      
- for (int j = 0; j < envPropArray.length; j++) {      
- if (envPropArray[j].getName().equals(variableName)) {      
- foundVariable = true;      
- envPropArray[j].setValue(newVariableValue);      
- envPropArray[j].store();      
- }      
- }      
-      
- if (!foundVariable) {      
- ProjectEnvironmentProperty newProp = new ProjectEnvironmentProperty(true);      
-      
- newProp.setProject(projects[i]);      
- newProp.setServerGroup(environment);      
- newProp.setName(variableName);      
- newProp.setValue(newVariableValue);      
- newProp.setSetAsEnvProp(false);      
- newProp.setSecureValue(isSecure);      
-      
- newProp.store();      
- }      
-      
- }      
- </pre>      
  = Invoke Build of Another Project and Wait for Results =    = Invoke Build of Another Project and Wait for Results = 
  This script needs to launch a build of another project that is not a dependency but treat it like it is.    This script needs to launch a build of another project that is not a dependency but treat it like it is. 
Line 1885:
  }</pre>    }</pre> 
       
- = Consolidate Dependency Reports And Publish on Parent Project =      
- * This is a script that will take logs published as reports for dependency projects and consolidate them so that they can be viewed in one location on the reports tab of the blanket project. A more efficient way is listed secondly.      
       
- ==== AHPSCRIPTS-7 ====      
- <pre>import com.urbancode.anthill3.runtime.scripting.helpers.BuildLifeLookup;      
- import com.urbancode.anthill3.runtime.paths.PublishPathHelper;      
- import com.urbancode.anthill3.domain.buildlife.BuildLife;      
- import com.urbancode.anthill3.domain.workflow.WorkflowCase;      
- import com.urbancode.devilfish.services.var.VarService;      
- import com.urbancode.anthill3.domain.jobtrace.JobTrace;      
- import com.urbancode.commons.fileutils.FileUtils;      
- import com.urbancode.codestation2.domain.buildlife.CodestationCompatableBuildLife;      
- import java.util.*;      
       
- static final private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ReportScript");      
       
- final String REPORT_NAME = "LogReport";      
       
- VarService vs = VarService.getInstance();      
       
- void centrallyLocateLogsForBuildLife(BuildLife buildLife, String reportPath) {      
       
- File reportDir = new File(reportPath + File.separator + buildLife.getProfile().getProject().getName().replace(" ", "_"));      
- reportDir.mkdirs();      
- log.warn("Report Path Dir" + reportDir.getAbsolutePath());      
       
- WorkflowCase workflowCase = buildLife.getOriginatingWorkflow();      
- JobTrace[] jobTraces = workflowCase.getJobTraceArray();      
       
- for (int i = 0; i < jobTraces.length; i++) {      
- String dependencyPath = PublishPathHelper.getInstance().getPublishPath(jobTraces[i], REPORT_NAME);      
- log.warn("Dependency Path " + dependencyPath);      
- dependencyPath = vs.resolve(dependencyPath);      
- File dependencyReportDir = new File(dependencyPath);      
- if (dependencyReportDir != null &&      
- dependencyReportDir.exists() &&      
- dependencyReportDir.isDirectory()) {      
- log.warn("Copying reports from " + dependencyReportDir.getAbsolutePath() + " to " + reportDir.getAbsolutePath());      
- FileUtils.copyFiles(dependencyReportDir, reportDir);      
- }      
- }      
- }      
       
- Set buildDependencyBuildLivesSet(BuildLife buildLife) {      
- Set result = new HashSet();      
       
- CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();      
- for (int i = 0; i < dependencyBuildLives.length; i++) {      
- CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];      
- if (dependencyBuildLife instanceof BuildLife) {      
- log.warn("DependencyBuildLife " + dependencyBuildLife.getName());      
- result.add(dependencyBuildLife);      
- result.addAll(buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife));      
- }      
- }      
       
- return result;      
- }      
       
       
- BuildLife buildLife = BuildLifeLookup.getCurrent();      
       
- String reportPath = PublishPathHelper.getInstance().getPublishPath(JobTraceLookup.getCurrent(), REPORT_NAME);      
- reportPath = vs.resolve(reportPath);      
- log.warn("Report Path " + reportPath);      
       
- Set dependencyBuildLifeSet = buildDependencyBuildLivesSet(buildLife);      
- Iterator itr = dependencyBuildLifeSet.iterator();      
       
- while (itr.hasNext()) {      
- BuildLife buildLife = (BuildLife) itr.next();      
- centrallyLocateLogsForBuildLife(buildLife, reportPath);      
- }</pre>      
       
- ''More Efficient Way'':      
       
- * more efficient way of getting the entire dependency graph. This will not process the same build life more than once.      
       
- <pre>Set buildDependencyBuildLivesSet(BuildLife buildLife) {      
- Set result = new HashSet();      
- buildDependencyBuildLivesSet(buildLife, result, new HashSet());      
- return result;      
- }      
       
- buildDependencyBuildLivesSet(BuildLife buildLife, Set depSet, Set traversedSet) {      
- if (!travesedSet.contains(buildLife)) {      
- travesedSet.add(buildLife):      
- CodestationCompatableBuildLife[] dependencyBuildLives = buildLife.getDependencyBuildLifeArray();      
- for (int i = 0; i < dependencyBuildLives.length; i++) {      
- CodestationCompatableBuildLife dependencyBuildLife = dependencyBuildLives[i];      
- if (dependencyBuildLife instanceof BuildLife) {      
- log.warn("DependencyBuildLife " + dependencyBuildLife.getName());      
- depSet.add(dependencyBuildLife);      
- buildDependencyBuildLivesSet((BuildLife) dependencyBuildLife, depSet, traversedSet));      
- }      
- }      
- }      
- }</pre>      
       
  = Script Which Can be Used to Trigger All Delayed Builds to Run Immediately =    = Script Which Can be Used to Trigger All Delayed Builds to Run Immediately = 
  ==== AHPSCRIPTS-107 ====    ==== AHPSCRIPTS-107 ==== 
Line 2435:
  depXml.importDependencies(xmlString);</pre>    depXml.importDependencies(xmlString);</pre> 
       
- = Dashboard Not Displaying Latest BuildLife For Workflow Fix =      
- *In older versions there was a bug where some build lives did not get entries in the BUILD_WORKFLOW_SUMMARY table that feeds the latest activity on the dashboard.      
- **This can be run from Evaluate Script step. Enter the build life ids in the array for which ones to fix.      
- ==== AHPSCRIPTS-136 ====      
- <pre>import com.urbancode.anthill3.domain.buildlife.BuildLife;      
- import com.urbancode.anthill3.domain.buildlife.BuildLifeFactory;      
- import com.urbancode.anthill3.domain.workflow.WorkflowCase;      
- import com.urbancode.anthill3.domain.workflow.WorkflowEndEvent;      
- import com.urbancode.anthill3.services.event.EventService;      
       
- long[] buildLifeIds = new long[] {      
- 400L, 468L, 469L, 506L, 512L, 537L, 576L, 585L, 596L, 642L, 649L,      
- 658L, 780L, 786L, 980L, 1025L, 1046L, 1051L, 1057L, 1086L, 1090L,      
- 1094L, 1095L, 1110L, 1168L, 1174L, 1228L, 1366L, 1367L, 1378L,      
- 1379L, 1383L, 1400L, 1426L, 1609L, 1712L, 1719L, 1749L, 1758L,      
- 1770L, 1889L, 1917L, 1920L, 1931L, 1968L, 2033L, 2119L      
- };      
- for (long buildLifeId : buildLifeIds) {      
- BuildLife buildLife = BuildLifeFactory.getInstance().restore(buildLifeId);      
- WorkflowCase workflowCase = buildLife.getOriginatingWorkflow();      
- WorkflowEndEvent workflowEndEvent = new WorkflowEndEvent(workflowCase);      
- EventService.getInstance().sendEvent(workflowEndEvent);      
- }</pre>      
       
  = Run Another Non-Originating Workflow On Same Build Life =    = Run Another Non-Originating Workflow On Same Build Life =