(Almost Done Alphabetizing)
|
(Alphabetization Almost Complete)
|
Line 1966: | |||
br.setProperty("Stamp_Value", stamp); | br.setProperty("Stamp_Value", stamp); | ||
uow.commit();</pre> | uow.commit();</pre> | ||
+ | = Roll Back: If Step Fails During Deployment, Look Up Previously Deployed Build and Re-Deploy It = | ||
+ | ==== AHPSCRIPTS-122 ==== | ||
+ | <pre>import com.urbancode.anthill3.runtime.scripting.helpers; | ||
+ | import com.urbancode.anthill3.domain.servergroup.*; | ||
+ | import com.urbancode.anthill3.domain.buildrequest.*; | ||
+ | import com.urbancode.anthill3.domain.buildlife.*; | ||
+ | import com.urbancode.anthill3.services.build.*; | ||
+ | import com.urbancode.anthill3.domain.security.*; | ||
+ | //Get the useful information | ||
+ | currentEnv = EnvironmentLookup.getCurrent(); | ||
+ | currentWorkflow = WorkflowLookup.getCurrent(); | ||
+ | envShortName = currentEnv.getShortName(); | ||
+ | status = StatusLookup.getStatusByName(envShortName); | ||
+ | currentBuildLife = BuildLifeLookup.getCurrent(); | ||
+ | currentBuildRequest = BuildRequestLookup.getCurrent(); | ||
+ | requester = currentBuildRequest.getRequester(); | ||
+ | currentUser = currentBuildRequest.getUser(); | ||
+ | currentBuildProfile = currentBuildLife.getProfile(); | ||
+ | //Determine the last deployed build for a particular environment | ||
+ | previousDeployed = BuildLifeFactory.getInstance().restorePriorMostRecentForProfileAndStatus(currentBuildLife, currentBuildProfile, status); | ||
+ | // Create the new build request | ||
+ | BuildRequest newBuildRequest = new BuildRequest(previousDeployed, currentWorkflow, currentEnv, currentUser, RequestSourceEnum.EVENT, requester); | ||
+ | //store it | ||
+ | newBuildRequest.store(); | ||
+ | //Run the workflow | ||
+ | BuildService.getInstance().runWorkflow(newBuildRequest);</pre> | ||
+ | = Run Another Non-Originating Workflow On Same Build Life = | ||
+ | *Used as an evaluate script step | ||
+ | ==== AHPSCRIPTS-33 ==== | ||
+ | <pre>import com.urbancode.anthill3.runtime.scripting.helpers.*; | ||
+ | import com.urbancode.anthill3.services.build.*; | ||
+ | import com.urbancode.anthill3.AnthillRuntimeException.*; | ||
+ | import com.urbancode.anthill3.domain.buildrequest.*; | ||
+ | import com.urbancode.anthill3.domain.buildlife.*; | ||
+ | import com.urbancode.anthill3.domain.workflow.*; | ||
+ | import com.urbancode.anthill3.domain.servergroup.*; | ||
+ | import com.urbancode.anthill3.domain.security.*; | ||
+ | import com.urbancode.anthill3.domain.persistent.*; | ||
+ | import com.urbancode.anthill3.domain.project.*; | ||
+ | String wf = PropertyLookup.getValue("workflow_prop"); | ||
+ | Project prj = ProjectLookup.getCurrent(); | ||
+ | BuildLife bl = BuildLifeLookup.getCurrent(); | ||
+ | Workflow wflow = WorkflowLookup.getForProjectAndName(prj, wf); | ||
+ | ServerGroup sg = ServerGroupLookup.getCurrent(); | ||
+ | User usr = BuildRequestLookup.getCurrent().getUser(); | ||
+ | RequestSourceEnum rse = BuildRequestLookup.getCurrent().getRequestSource(); | ||
+ | Persistent req = BuildRequestLookup.getCurrent().getRequester(); | ||
+ | BuildRequest br = new BuildRequest(bl, wflow, sg, usr, rse, req); | ||
+ | BuildService.getInstance().runWorkflow(br);</pre> | ||
+ | = Run Originating Workflow and Wait Until Workflow is Finished = | ||
+ | ==== AHPSCRIPTS-132 ==== | ||
+ | <pre>import com.urbancode.anthill3.domain.buildrequest.BuildRequest; | ||
+ | import com.urbancode.anthill3.domain.project.Project; | ||
+ | import com.urbancode.anthill3.domain.project.ProjectFactory; | ||
+ | import com.urbancode.anthill3.domain.security.UserFactory; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowCase; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowEndEvent; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowEvent; | ||
+ | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
+ | import com.urbancode.anthill3.services.build.BuildService; | ||
+ | import com.urbancode.anthill3.services.event.EventListener; | ||
+ | import com.urbancode.anthill3.services.event.EventService; | ||
+ | import com.urbancode.anthill3.services.event.criteria.Criteria; | ||
+ | import com.urbancode.anthill3.services.event.criteria.FieldEqualsCriteria; | ||
+ | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
+ | import com.urbancode.anthill3.domain.buildrequest.*; | ||
+ | import com.urbancode.anthill3.domain.project.*; | ||
+ | import com.urbancode.anthill3.domain.security.*; | ||
+ | import com.urbancode.anthill3.services.build.*; | ||
+ | import java.util.EventObject; | ||
+ | class WorkflowEndEventListener implements EventListener { | ||
+ | |||
+ | private BuildRequest request; | ||
+ | private boolean success = false; | ||
+ | |||
+ | WorkflowEndEventListener(BuildRequest request) { | ||
+ | this.request = request; | ||
+ | } | ||
+ | |||
+ | public boolean isSuccess(){ | ||
+ | return success; | ||
+ | } | ||
+ | |||
+ | synchronized public void handleEvent(EventObject event) { | ||
+ | if (!(event instanceof WorkflowEvent)) { | ||
+ | throw new IllegalArgumentException( "The parameter event must be an instance of a WorkflowEvent!" ); | ||
+ | } | ||
+ | UnitOfWork uow = null; | ||
+ | try { | ||
+ | uow = UnitOfWork.create(UserFactory.getSystemUser()); | ||
+ | WorkflowCase wcase = ((WorkflowEvent) event).getCase(); | ||
+ | success = (wcase.getStatus() != null && wcase.getStatus().isSuccess()); | ||
+ | } | ||
+ | catch (Exception e) { | ||
+ | throw new RuntimeException(e.getMessage(), e); | ||
+ | } | ||
+ | finally { | ||
+ | if (uow != null) { | ||
+ | uow.close(); | ||
+ | } | ||
+ | } | ||
+ | this.notify(); | ||
+ | } | ||
+ | public Class getEventClass() { | ||
+ | return WorkflowEndEvent.class; | ||
+ | } | ||
+ | public Criteria[] getCriteria() { | ||
+ | return new Criteria[]{ | ||
+ | new FieldEqualsCriteria("request", request) | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | buildAndWait(String projectName, String workflowName) | ||
+ | throws Exception { | ||
+ | project = ProjectFactory.getInstance().restoreForName(projectName); | ||
+ | wflow = project.getWorkflow(workflowName); | ||
+ | buildService = BuildService.getInstance(); | ||
+ | user = UnitOfWork.getCurrent().getUser(); | ||
+ | request = new BuildRequest(wflow.getBuildProfile(), user, RequestSourceEnum.REPOSITORY, user ); | ||
+ | request.setForcedFlag(true); | ||
+ | request.store(); | ||
+ | UnitOfWork.getCurrent().commit(); | ||
+ | listener = new WorkflowEndEventListener(request); | ||
+ | EventService.getInstance().registerEventListener(listener); | ||
+ | try { | ||
+ | BuildService.getInstance().runBuild(request); | ||
+ | synchronized (listener) { | ||
+ | listener.wait(); | ||
+ | } | ||
+ | if (!listener.isSuccess()) { | ||
+ | throw new Exception(projectName + " - " + workflowName + " failed."); | ||
+ | } | ||
+ | } | ||
+ | finally { | ||
+ | EventService.getInstance().removeEventListener(listener); | ||
+ | } | ||
+ | } | ||
+ | buildAndWait("project", "workflow");</pre> | ||
+ | = Script That Optionally Cleans Working Directory = | ||
+ | * Sometimes we want to clean the workflow directory, other times we don't. The determination is property drive. | ||
+ | ==== AHPSCRIPTS-34 ==== | ||
+ | <pre>import com.urbancode.anthill3.command.workdir.PathBuilder; | ||
+ | import com.urbancode.anthill3.domain.source.*; | ||
+ | import com.urbancode.anthill3.domain.workdir.*; | ||
+ | import com.urbancode.command.path.Path; | ||
+ | import com.urbancode.devilfish.services.file.*; | ||
+ | org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("OptionallyCleanWorkingDirectory"); | ||
+ | SourceConfig source = SourceConfigLookup.getCurrent(); | ||
+ | if (source instanceof WithWorkDirScript) { | ||
+ | WorkDirScript workDirConfig = ((WithWorkDirScript) source).getWorkDirScript(); | ||
+ | Path workDirPath = PathBuilder.buildPath(workDirConfig); | ||
+ | WorkDirPath.setPath(workDirPath); | ||
+ | log.warn("Setting working directory to " + workDirPath.getPathStr()); | ||
+ | } | ||
+ | else { | ||
+ | log.warn("NOT setting working directory. Source Configuration does not support it."); | ||
+ | } | ||
+ | if (Boolean.valueOf(PropertyLookup.getValue("shouldCleanup")).booleanValue()) { | ||
+ | log.warn("Workflow property configured to clean the working directory."); | ||
+ | FileServiceClient service = new FileServiceClient(JobTraceLookup.getCurrent().getAgent().getEndpoint()); | ||
+ | File workDir = new File(WorkDirPath.get().getPathStr()); | ||
+ | FileInfo info = service.getFileInfo(workDir); | ||
+ | if (info.exists()) { | ||
+ | log.warn("Directory exists. Deleting directory \'"+info.getPath()+"\'."); | ||
+ | results = service.deleteFile(workDir); | ||
+ | log.warn("Cleanup of " + info.getPath() + " complete!"); | ||
+ | log.warn("Successfully removed " + results.filesDeleted + " files/directories."); | ||
+ | log.warn("Failed to remove " + results.getFailedDeletionCount() + " files/directories."); | ||
+ | if (results.hasFailedDeletions()) { | ||
+ | throw new Exception("Failed to clean the working directory"); | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | log.warn("Directory does not exist. No action needed."); | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | log.warn("Workflow property configured to NOT clean the working directory."); | ||
+ | }</pre> | ||
+ | = Script to Update Workflow Property with Names of All Agents in Environment = | ||
+ | * You can use the script with an Evaluate Script Step. You need to change the projectName, workflowName, envName and propertyName values to match your requirements. | ||
+ | ==== AHPSCRIPTS-93 ==== | ||
+ | <pre>import com.urbancode.anthill3.domain.agent.Agent; | ||
+ | import com.urbancode.anthill3.domain.agent.AgentFactory; | ||
+ | import com.urbancode.anthill3.domain.project.Project; | ||
+ | import com.urbancode.anthill3.domain.project.ProjectFactory; | ||
+ | import com.urbancode.anthill3.domain.servergroup.ServerGroup; | ||
+ | import com.urbancode.anthill3.domain.servergroup.ServerGroupFactory; | ||
+ | import com.urbancode.anthill3.domain.workflow.Workflow; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowFactory; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowProperty; | ||
+ | import com.urbancode.anthill3.domain.workflow.WorkflowPropertyTypeEnum; | ||
+ | import com.urbancode.devilfish.client.ServiceEndpoint; | ||
+ | import java.io.PrintStream; | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | try { | ||
+ | String projectName = "some project"; | ||
+ | String workflowName = "some workflow that has a multi select property defined"; | ||
+ | String envName = "some environment"; | ||
+ | String propertyName = "the property to update that's defined in the workflow above"; | ||
+ | List valueList = new ArrayList(); | ||
+ | // get all agents that belong to an environment, are configured and not ingnored | ||
+ | ServerGroup envirnoment = ServerGroupFactory.getInstance().restoreForName(envName); | ||
+ | if (envirnoment != null) { | ||
+ | for (ServiceEndpoint endpoint : envirnoment.getServerArray()) { | ||
+ | Agent agent = AgentFactory.getInstance().restoreByEndpoint(endpoint); | ||
+ | if (agent != null && agent.isConfigured() && !agent.isIgnored()) { | ||
+ | valueList.add(agent.getName()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | // now locate the workflow property that you want to update | ||
+ | Project project = ProjectFactory.getInstance().restoreForName(projectName); | ||
+ | if (project != null) { | ||
+ | Workflow workflow = WorkflowFactory.getInstance().restoreForProjectAndWorkflowName(project, workflowName); | ||
+ | if (workflow != null) { | ||
+ | WorkflowProperty property = workflow.getProperty(propertyName); | ||
+ | // here you can change/remove the check for the property type if you want | ||
+ | if (property != null && property.getType().equals(WorkflowPropertyTypeEnum.MULTI_SELECT)) { | ||
+ | property.setAllowedValues((String[])valueList.toArray(new String[valueList.size()])); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | catch (Exception e) { | ||
+ | e.printStackTrace(commandOutput); | ||
+ | }</pre> | ||
+ | = Script Which Can be Used to Trigger All Delayed Builds to Run Immediately = | ||
+ | ==== AHPSCRIPTS-107 ==== | ||
+ | <pre>import com.urbancode.anthill3.domain.buildrequest.*; | ||
+ | import com.urbancode.anthill3.domain.schedule.*; | ||
+ | import com.urbancode.anthill3.services.build.*; | ||
+ | buildRequestsArray=BuildRequestFactory.getInstance().restoreAllBuildLifeRequestsByStatus(BuildRequestStatusEnum.DELAYED_BUILD); | ||
+ | for (int i=0;i<buildRequestsArray.length;i++) | ||
+ | { | ||
+ | buildRequest=buildRequestsArray[i]; | ||
+ | buildRequestName=buildRequest.getName(); | ||
+ | commandOutput.println("buildRequestName="+buildRequestName); | ||
+ | buildRequestId=buildRequest.getId(); | ||
+ | commandOutput.println("buildRequestId="+buildRequestId); | ||
+ | buildRequestDelayUntil=buildRequest.getDelayUntilDate(); | ||
+ | commandOutput.println("buildRequestDelayUntil="+buildRequestDelayUntil); | ||
+ | // set the workflow to run on the next scheduled time | ||
+ | buildRequest.setDelayUntilDate(new Date()); | ||
+ | buildRequest.resetState(); | ||
+ | // store and schedule | ||
+ | buildRequest.store(); | ||
+ | BuildService.getInstance().runWorkflow(buildRequest); | ||
+ | }</pre> | ||
+ | = Set Output of Previous Step's Command As Property = | ||
+ | * This sets the property ''output'' on the request | ||
+ | ==== AHPSCRIPTS-95 ==== | ||
+ | <pre>job = JobTraceLookup.getCurrent(); | ||
+ | step = job.getStepTraceForSequence(job.getStepTraceArray().length - 2); | ||
+ | cmd = step.getCommandTraceForSequence(step.getCommandTraceArray().length - 1); | ||
+ | output = LogHelper.getOutput(cmd); | ||
+ | output = output.substring(output.indexOf("command output:") + 15); | ||
+ | output = output.substring(0, output.indexOf("command exit code:")); | ||
+ | PropertyLookup.set("output", output.trim());</pre> | ||
+ | = Set Property Based on Results of RESOLVE DEPENDENCY ARTIFACTS Step = | ||
+ | Use this script if you need a property to define whether the resolve step was successful or had conflicts. | ||
+ | ==== AHPSCRIPTS-38 ==== | ||
+ | <pre>import com.urbancode.anthill3.services.jobs.*; | ||
+ | status = null; | ||
+ | workflow = WorkflowLookup.getCurrentCase(); | ||
+ | jobs = workflow.getJobTraceArray(); | ||
+ | for (int j=0; j<jobs.length; j++) { | ||
+ | steps = jobs[j].getStepTraceArray(); | ||
+ | for (int s=0; s<steps.length; s++) { | ||
+ | if ("Get Dependency Artifacts".equals(steps[s].getName())) { | ||
+ | if (JobStatusEnum.SUCCESS_WARN.equals(steps[s].getStatus())) { | ||
+ | status = steps[s].getStatus().getName(); | ||
+ | } | ||
+ | else if (JobStatusEnum.SUCCESS.equals(steps[s].getStatus()) && status == null) { | ||
+ | status = steps[s].getStatus().getName(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | // set the property | ||
+ | workflow.getRequest().setProperty("resolve.status", status); </pre> |