public StageConfig getStage(final CaseInsensitiveString stageName) { return findBy(stageName); }
private boolean alreadyContains(StageConfig stageConfig) { return findBy(stageConfig.name()) != null; }
@Override public boolean hasStageConfigNamed(final CaseInsensitiveString pipelineName, final CaseInsensitiveString stageName, boolean ignoreCase) { PipelineConfig pipelineConfig = getPipelineConfigByName(pipelineName); if (pipelineConfig == null) { return false; } return pipelineConfig.findBy(stageName) != null; }
@Override public boolean requiresApproval(final CaseInsensitiveString pipelineName, final CaseInsensitiveString stageName) { PipelineConfig pipelineConfig = getPipelineConfigByName(pipelineName); if (pipelineConfig == null) { return false; } final StageConfig stageConfig = pipelineConfig.findBy(stageName); return stageConfig != null && stageConfig.requiresApproval(); }
@Override public StageConfig stageConfigByName(final CaseInsensitiveString pipelineName, final CaseInsensitiveString stageName) { StageConfig stageConfig = pipelineConfigByName(pipelineName).findBy(stageName); StageNotFoundException.bombIfNull(stageConfig, pipelineName, stageName); return stageConfig; }
public Stage createStageInstance(PipelineConfig pipelineConfig, CaseInsensitiveString stageName, SchedulingContext context, String md5, Clock clock) { StageConfig stageConfig = pipelineConfig.findBy(stageName); if (stageConfig == null) { throw new StageNotFoundException(pipelineConfig.name(), stageName); } return createStageInstance(stageConfig, context, md5, clock); }
@Override public JobConfig findJob(String pipelineName, String stageName, String jobName) { return pipelineConfigByName(new CaseInsensitiveString(pipelineName)) .findBy(new CaseInsensitiveString(stageName)) .jobConfigByConfigName(new CaseInsensitiveString(jobName)); }
private List<JobPlan> getMismatchingJobPlansFromUpdatedPipeline(PipelineConfig pipelineConfig, List<JobPlan> allJobPlans) { List<JobPlan> jobsToRemove = new ArrayList<>(); for (JobPlan jobPlan : allJobPlans) { if (pipelineConfig.name().equals(new CaseInsensitiveString(jobPlan.getPipelineName()))) { StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(jobPlan.getStageName())); if (stageConfig != null) { JobConfig jobConfig = stageConfig.jobConfigByConfigName(new CaseInsensitiveString(jobPlan.getName())); if (jobConfig == null) { jobsToRemove.add(jobPlan); } } else { jobsToRemove.add(jobPlan); } } } return jobsToRemove; }
@Override protected void validateConcreteMaterial(ValidationContext validationContext) { CaseInsensitiveString upstreamPipelineName = this.getPipelineName(); CaseInsensitiveString upstreamStageName = this.getStageName(); PipelineConfig upstreamPipeline = validationContext.getPipelineConfigByName(upstreamPipelineName); PipelineConfig pipeline = validationContext.getPipeline(); if (upstreamPipeline==null) { errors.add(DependencyMaterialConfig.PIPELINE_STAGE_NAME, String.format("Pipeline with name '%s' does not exist, it is defined as a dependency for pipeline '%s' (%s)", upstreamPipelineName, pipeline.name(), pipeline.getOriginDisplayName())); } else if (upstreamPipeline.findBy(upstreamStageName) == null) { errors.add(DependencyMaterialConfig.PIPELINE_STAGE_NAME, String.format("Stage with name '%s' does not exist on pipeline '%s', it is being referred to from pipeline '%s' (%s)", upstreamStageName, upstreamPipelineName, pipeline.name(), pipeline.getOriginDisplayName())); } }
private void validateDependencies(ValidationContext validationContext, PipelineConfig currentPipeline) { DependencyMaterialConfig matchingMaterial = findMatchingDependencyMaterial(currentPipeline, pipelineName.getAncestorName()); PipelineConfig ancestor = validationContext.getPipelineConfigByName(pipelineName.getAncestorName()); if (matchingMaterial == null) { addError(PIPELINE_NAME, String.format("Pipeline \"%s\" tries to fetch artifact from pipeline " + "\"%s\" which is not an upstream pipeline", currentPipeline.name(), pipelineName)); return; } List<StageConfig> validStages = ancestor.validStagesForFetchArtifact(currentPipeline, validationContext.getStage().name()); if (!validStages.contains(ancestor.findBy(stage))) { addStageMayNotCompleteBeforeDownstreamError(currentPipeline, validationContext); } }
GoAcl readAclBy(String pipelineName, String stageName) { PipelineConfig pipelineConfig = pipelineConfigNamed(new CaseInsensitiveString(pipelineName)); StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(stageName)); AdminsConfig adminsConfig = stageConfig.getApproval().getAuthConfig(); List<CaseInsensitiveString> users = getAuthorizedUsers(adminsConfig); return new GoAcl(users); }
@Test public void shouldLoadPipelineFromXmlPartial() throws Exception { String pipelineXmlPartial = "<pipeline name=\"pipeline\">\n" + " <materials>\n" + " <hg url=\"/hgrepo\"/>\n" + " </materials>\n" + " <stage name=\"mingle\">\n" + " <jobs>\n" + " <job name=\"functional\">\n" + " <artifacts>\n" + " <log src=\"artifact1.xml\" dest=\"cruise-output\" />\n" + " </artifacts>\n" + " </job>\n" + " </jobs>\n" + " </stage>\n" + "</pipeline>\n"; PipelineConfig pipeline = xmlLoader.fromXmlPartial(pipelineXmlPartial, PipelineConfig.class); assertThat(pipeline.name(), is(new CaseInsensitiveString("pipeline"))); assertThat(pipeline.size(), is(1)); assertThat(pipeline.findBy(new CaseInsensitiveString("mingle")).jobConfigByInstanceName("functional", true), is(notNullValue())); }
public Stage scheduleStage(final Pipeline pipeline, final String stageName, final String username, final StageInstanceCreator creator, final ErrorConditionHandler errorHandler) { return (Stage) transactionTemplate.execute((TransactionCallback) status -> { String pipelineName = pipeline.getName(); PipelineConfig pipelineConfig = goConfigService.pipelineConfigNamed(new CaseInsensitiveString(pipelineName)); StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(stageName)); if (stageConfig == null) { throw new StageNotFoundException(pipelineName, stageName); } SchedulingContext context = schedulingContext(username, pipelineConfig, stageConfig); pipelineLockService.lockIfNeeded(pipeline); Stage instance = null; try { instance = creator.create(pipelineName, stageName, context); LOGGER.info("[Stage Schedule] Scheduling stage {} for pipeline {}", stageName, pipeline.getName()); } catch (CannotScheduleException e) { serverHealthService.update(stageSchedulingFailedState(pipelineName, e)); errorHandler.cantSchedule(e, pipelineName); } serverHealthService.update(stageSchedulingSuccessfulState(pipelineName, stageName)); stageService.save(pipeline, instance); return instance; }); }
@Test public void shouldFindByName() { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("pipeline"), null, completedStage(), buildingStage()); assertThat(pipelineConfig.findBy(new CaseInsensitiveString("completed stage")).name(), is(new CaseInsensitiveString("completed stage"))); }
@Test public void shouldLoadPartialConfigWithPipeline() throws Exception { String partialConfigWithPipeline = "<cruise schemaVersion='" + CONFIG_SCHEMA_VERSION + "'>\n" + "<pipelines group=\"first\">\n" + "<pipeline name=\"pipeline\">\n" + " <materials>\n" + " <hg url=\"/hgrepo\"/>\n" + " </materials>\n" + " <stage name=\"mingle\">\n" + " <jobs>\n" + " <job name=\"functional\">\n" + " <artifacts>\n" + " <log src=\"artifact1.xml\" dest=\"cruise-output\" />\n" + " </artifacts>\n" + " </job>\n" + " </jobs>\n" + " </stage>\n" + "</pipeline>\n" + "</pipelines>\n" + "</cruise>\n"; PartialConfig partialConfig = xmlLoader.fromXmlPartial(partialConfigWithPipeline, PartialConfig.class); assertThat(partialConfig.getGroups().size(), is(1)); PipelineConfig pipeline = partialConfig.getGroups().get(0).getPipelines().get(0); assertThat(pipeline.name(), is(new CaseInsensitiveString("pipeline"))); assertThat(pipeline.size(), is(1)); assertThat(pipeline.findBy(new CaseInsensitiveString("mingle")).jobConfigByInstanceName("functional", true), is(notNullValue())); }
CruiseConfig cruiseConfig = ConfigMigrator.load(xml); PipelineConfig pipelineConfig = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")); StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString("stage")); JobConfig build = stageConfig.jobConfigByInstanceName("functional", true);
private boolean validateExistenceAndOrigin(PipelineConfig currentPipeline, ValidationContext validationContext) { PipelineConfig srcPipeline = validationContext.getPipelineConfigByName(pipelineName.getAncestorName()); if (srcPipeline == null) { //"ProdDeploy :: deploy :: scp" tries|attempts to fetch artifact from pipeline "not-found" which does not exist. addError(PIPELINE, String.format("\"%s :: %s :: %s\" tries to fetch artifact from pipeline \"%s\" which does not exist." , currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName())); return true; } else { StageConfig srcStage = srcPipeline.findBy(stage); if (srcStage == null) { addError(STAGE, String.format("\"%s :: %s :: %s\" tries to fetch artifact from stage \"%s :: %s\" which does not exist." , currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName(), stage)); return true; } else { if (srcStage.jobConfigByInstanceName(CaseInsensitiveString.str(job), true) == null) { addError(JOB, String.format("\"%s :: %s :: %s\" tries to fetch artifact from job \"%s :: %s :: %s\" which does not exist.", currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName(), stage, job)); return true; } } if (validationContext.shouldCheckConfigRepo()) { if (!validationContext.getConfigRepos().isReferenceAllowed(currentPipeline.getOrigin(), srcPipeline.getOrigin())) { addError(ARTIFACT_ORIGIN, String.format("\"%s :: %s :: %s\" tries to fetch artifact from job \"%s :: %s :: %s\" which is defined in %s - reference is not allowed", currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName(), stage, job, displayNameFor(srcPipeline.getOrigin()))); return true; } } } return false; }
public JobConfigIdentifier translateToActualCase(JobConfigIdentifier identifier) { PipelineConfig pipelineConfig = getCurrentConfig().pipelineConfigByName(new CaseInsensitiveString(identifier.getPipelineName())); String translatedPipelineName = CaseInsensitiveString.str(pipelineConfig.name()); StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(identifier.getStageName())); if (stageConfig == null) { throw new StageNotFoundException(new CaseInsensitiveString(identifier.getPipelineName()), new CaseInsensitiveString(identifier.getStageName())); } String translatedStageName = CaseInsensitiveString.str(stageConfig.name()); JobConfig plan = stageConfig.jobConfigByInstanceName(identifier.getJobName(), true); if (plan == null) { throw new JobNotFoundException(identifier.getPipelineName(), identifier.getStageName(), identifier.getJobName()); } String translatedJobName = plan.translatedName(identifier.getJobName()); return new JobConfigIdentifier(translatedPipelineName, translatedStageName, translatedJobName); }
copy(from.getTimer(), to.getTimer()); for (StageConfig toStage : to.getStages()) { StageConfig fromStage = from.findBy(toStage.name()); copy(fromStage, toStage); copyCollectionErrors(fromStage.getVariables(), toStage.getVariables());