Ejemplo n.º 1
0
 @Override
 public int hashCode() {
   int result = (type != null ? type.hashCode() : 0);
   result = 31 * result + (pipelineName != null ? pipelineName.hashCode() : 0);
   result = 31 * result + (stageName != null ? stageName.hashCode() : 0);
   return result;
 }
Ejemplo n.º 2
0
 public List<MaterialRevision> addDependencyRevisionModification(
     List<MaterialRevision> materialRevisions,
     DependencyMaterial dependencyMaterial,
     Pipeline... upstreams) {
   String stageName = CaseInsensitiveString.str(dependencyMaterial.getStageName());
   String label = upstreams[0].getLabel();
   List<Modification> modifications = new ArrayList<Modification>();
   for (Pipeline upstream : upstreams) {
     modifications.add(
         new Modification(
             new Date(),
             DependencyMaterialRevision.create(
                     CaseInsensitiveString.str(dependencyMaterial.getPipelineName()),
                     upstream.getCounter(),
                     label,
                     stageName,
                     upstream.findStage(stageName).getCounter())
                 .getRevision(),
             label,
             upstream.getId()));
   }
   MaterialRevision depRev =
       addRevisionsWithModifications(
           dependencyMaterial, modifications.toArray(new Modification[0]));
   materialRevisions.add(depRev);
   return Arrays.asList(depRev);
 }
Ejemplo n.º 3
0
 public void toJson(Map json, Revision revision) {
   json.put("folder", getFolder() == null ? "" : getFolder());
   json.put("scmType", "Dependency");
   json.put("location", pipelineName + "/" + stageName);
   json.put("action", "Completed");
   if (!CaseInsensitiveString.isBlank(getName())) {
     json.put("materialName", CaseInsensitiveString.str(getName()));
   }
 }
Ejemplo n.º 4
0
 @Override
 public Map<String, Object> getAttributes(boolean addSecureFields) {
   Map<String, Object> materialMap = new HashMap<>();
   materialMap.put("type", "pipeline");
   Map<String, Object> configurationMap = new HashMap<>();
   configurationMap.put("pipeline-name", pipelineName.toString());
   configurationMap.put("stage-name", stageName.toString());
   materialMap.put("pipeline-configuration", configurationMap);
   return materialMap;
 }
Ejemplo n.º 5
0
  @Test
  public void shouldNotScheduleAnyNewPipelineWhenErrorHappens() throws Exception {
    String stageName = "invalidStageName";
    PipelineConfig invalidPipeline =
        configHelper.addPipelineWithInvalidMaterial("invalidPipeline", stageName);
    int beforeScheduling = pipelineDao.count(CaseInsensitiveString.str(invalidPipeline.name()));

    autoSchedulePipelines();

    int afterScheduling = pipelineDao.count(CaseInsensitiveString.str(invalidPipeline.name()));
    assertThat(beforeScheduling, is(afterScheduling));
  }
Ejemplo n.º 6
0
 private Pipeline passFirstStage(PipelineConfig pipelineConfig) {
   Stage completedMingleStage =
       stageDao.mostRecentWithBuilds(
           CaseInsensitiveString.str(pipelineConfig.name()),
           pipelineConfig.findBy(new CaseInsensitiveString("dev")));
   dbHelper.passStage(completedMingleStage);
   dbHelper.passStage(completedMingleStage);
   assertThat(completedMingleStage.getJobInstances().first().getState(), is(JobState.Completed));
   Pipeline pipeline =
       pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(pipelineConfig.name()));
   return dbHelper.passPipeline(pipeline);
 }
Ejemplo n.º 7
0
 public Collection<String> allRoleNames(CruiseConfig cruiseConfig) {
   List<String> roles = new ArrayList<String>();
   for (Role role : allRoles(cruiseConfig)) {
     roles.add(CaseInsensitiveString.str(role.getName()));
   }
   return roles;
 }
  @Test
  public void
      shouldCancelAScheduledJobInCaseThePipelineIsRemovedFromTheConfig_SpecificallyAPipelineRenameToADifferentCaseAndStageNameToADifferentName()
          throws Exception {
    Material hgMaterial = new HgMaterial("url", "folder");
    String[] hgRevs = new String[] {"h1"};
    u.checkinInOrder(hgMaterial, hgRevs);

    ScheduleTestUtil.AddedPipeline p1 =
        u.saveConfigWith("PIPELINE_WHICH_WILL_EVENTUALLY_CHANGE_CASE", u.m(hgMaterial));

    u.scheduleWith(p1, hgRevs);
    ScheduleTestUtil.AddedPipeline renamedPipeline =
        u.renamePipelineAndFirstStage(
            p1,
            "pipeline_which_will_eventually_change_case",
            "NEW_RANDOM_STAGE_NAME" + UUID.randomUUID());

    Pipeline p1_2 = u.scheduleWith(renamedPipeline, hgRevs);
    CruiseConfig cruiseConfig = configHelper.load();
    buildAssignmentService.onTimer(); // To Reload Job Plans
    buildAssignmentService.onConfigChange(cruiseConfig);

    Stages allStages = stageDao.findAllStagesFor(p1_2.getName(), p1_2.getCounter());
    assertThat(
        allStages.byName(CaseInsensitiveString.str(p1.config.first().name())).getState(),
        is(StageState.Cancelled));
  }
  @Test
  public void shouldScheduleIfAgentMatchingResources() throws Exception {
    JobConfig plan =
        evolveConfig
            .findBy(new CaseInsensitiveString(STAGE_NAME))
            .jobConfigByInstanceName("unit", true);
    plan.addResource("some-resource");

    scheduleHelper.schedule(evolveConfig, modifySomeFiles(evolveConfig), DEFAULT_APPROVED_BY);

    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("some-resource"));

    buildAssignmentService.onTimer();
    Work work = buildAssignmentService.assignWorkToAgent(agent(agentConfig));
    assertThat(work, is(not((Work) BuildAssignmentService.NO_WORK)));

    Pipeline pipeline =
        pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(evolveConfig.name()));
    JobInstance job = pipeline.findStage(STAGE_NAME).findJob("unit");

    JobPlan loadedPlan = jobInstanceDao.loadPlan(job.getId());
    assertThat(loadedPlan.getResources(), is((List<Resource>) plan.resources()));

    assertThat(job.getState(), is(JobState.Assigned));
    assertThat(job.getAgentUuid(), is(agentConfig.getUuid()));
  }
Ejemplo n.º 10
0
 public void createRerunInstances(
     JobInstance oldJob,
     JobInstances jobInstances,
     SchedulingContext context,
     StageConfig stageConfig,
     final Clock clock,
     InstanceFactory instanceFactory) {
   String jobName = oldJob.getName();
   JobConfig jobConfig = stageConfig.jobConfigByInstanceName(jobName, true);
   if (jobConfig == null) {
     throw new CannotRerunJobException(jobName, "Configuration for job doesn't exist.");
   }
   if (jobConfig.isRunMultipleInstanceType()) {
     String runType = "'run multiple instance'";
     throw new CannotRerunJobException(
         jobName, "Run configuration for job has been changed to " + runType + ".");
   }
   RunOnAllAgents.CounterBasedJobNameGenerator nameGenerator =
       new RunOnAllAgents.CounterBasedJobNameGenerator(
           CaseInsensitiveString.str(jobConfig.name()));
   JobInstances instances =
       instanceFactory.createJobInstance(
           stageConfig.name(), jobConfig, context, clock, nameGenerator);
   for (JobInstance instance : instances) {
     instance.setRerun(true);
   }
   jobInstances.addAll(instances);
 }
Ejemplo n.º 11
0
 @Test
 public void shouldNotScheduleActivePipeline() throws Exception {
   Pipeline pipeline = PipelineMother.building(mingleConfig);
   pipeline = dbHelper.savePipelineWithStagesAndMaterials(pipeline);
   Pipeline newPipeline = manualSchedule(CaseInsensitiveString.str(mingleConfig.name()));
   assertThat(newPipeline.getId(), is(pipeline.getId()));
 }
Ejemplo n.º 12
0
 public Boolean isUsedInFetchArtifact(PipelineConfig pipelineConfig) {
   List<FetchTask> fetchTasks = pipelineConfig.getFetchTasks();
   for (FetchTask fetchTask : fetchTasks) {
     if (pipelineName.equals(fetchTask.getDirectParentInAncestorPath())) return true;
   }
   return false;
 }
Ejemplo n.º 13
0
 private void verifyMingleScheduledWithModifications() {
   Pipeline scheduledPipeline =
       pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(mingleConfig.name()));
   BuildCause buildCause = scheduledPipeline.getBuildCause();
   assertThat(buildCause.getMaterialRevisions().totalNumberOfModifications(), is(3));
   JobInstance instance = scheduledPipeline.getFirstStage().getJobInstances().first();
   assertThat(instance.getState(), is(JobState.Scheduled));
 }
Ejemplo n.º 14
0
  public PipelineSelections addPipelineToSelections(CaseInsensitiveString pipelineToAdd) {
    ArrayList<String> updatedListOfPipelines = new ArrayList<String>();
    updatedListOfPipelines.addAll(pipelines);
    updatedListOfPipelines.add(CaseInsensitiveString.str(pipelineToAdd));

    this.updateSelections(updatedListOfPipelines);
    return this;
  }
Ejemplo n.º 15
0
 public boolean hasViewPermissionForPipeline(Username username, String pipelineName) {
   String groupName =
       goConfigService.findGroupNameByPipeline(new CaseInsensitiveString(pipelineName));
   if (groupName == null) {
     return true;
   }
   return hasViewPermissionForGroup(CaseInsensitiveString.str(username.getUsername()), groupName);
 }
Ejemplo n.º 16
0
 public List<CaseInsensitiveString> viewablePipelinesFor(Username username) {
   List<CaseInsensitiveString> pipelines = new ArrayList<CaseInsensitiveString>();
   for (String group : goConfigService.allGroups()) {
     if (hasViewPermissionForGroup(CaseInsensitiveString.str(username.getUsername()), group)) {
       pipelines.addAll(goConfigService.pipelines(group));
     }
   }
   return pipelines;
 }
Ejemplo n.º 17
0
 private void assertPipelineScheduled(PipelineConfig config) {
   Stage evolveStage =
       stageDao.mostRecentWithBuilds(
           CaseInsensitiveString.str(config.name()),
           config.findBy(new CaseInsensitiveString("dev")));
   assertThat(evolveStage.getName(), is("dev"));
   assertThat(evolveStage.getJobInstances().size(), is(1));
   assertThat(evolveStage.getJobInstances().first().getState(), is(JobState.Scheduled));
 }
  @Test
  public void canPauseShouldBeTrueForAuthorizedAccess() throws Exception {
    configHelper.addSecurityWithAdminConfig();
    configHelper.setOperatePermissionForGroup(
        "defaultGroup", CaseInsensitiveString.str(Username.ANONYMOUS.getUsername()));

    JsonMap jsonMap = requestPipelineHistoryPage();
    assertThat(getItemInJson(jsonMap, "canPause"), is("true"));
  }
Ejemplo n.º 19
0
  @RequestMapping(value = "/**/pipelineHistory.json", method = RequestMethod.GET)
  public ModelAndView list(
      @RequestParam("pipelineName") String pipelineName,
      @RequestParam(value = "perPage", required = false) Integer perPageParam,
      @RequestParam(value = "start", required = false) Integer startParam,
      HttpServletResponse response,
      HttpServletRequest request)
      throws NamingException {
    PipelineConfig pipelineConfig =
        goConfigService.pipelineConfigNamed(new CaseInsensitiveString(pipelineName));
    String username = CaseInsensitiveString.str(UserHelper.getUserName().getUsername());

    Pagination pagination;
    try {
      pagination =
          Pagination.pageStartingAt(
              startParam, pipelineHistoryService.totalCount(pipelineName), perPageParam);
    } catch (Exception e) {
      Map<String, Object> json = new LinkedHashMap<>();
      addDeveloperErrorMessage(json, e);
      return jsonNotAcceptable(json).respond(response);
    }

    PipelinePauseInfo pauseInfo = pipelinePauseService.pipelinePauseInfo(pipelineName);
    boolean hasBuildCauseInBuffer =
        pipelineScheduleQueue.hasBuildCause(CaseInsensitiveString.str(pipelineConfig.name()));
    PipelineInstanceModels pipelineHistory =
        pipelineHistoryService.load(pipelineName, pagination, username, true);

    boolean hasForcedBuildCause = pipelineScheduleQueue.hasForcedBuildCause(pipelineName);

    PipelineHistoryJsonPresentationModel historyJsonPresenter =
        new PipelineHistoryJsonPresentationModel(
            pauseInfo,
            pipelineHistory,
            pipelineConfig,
            pagination,
            canForce(pipelineConfig, username),
            hasForcedBuildCause,
            hasBuildCauseInBuffer,
            canPause(pipelineConfig, username));
    return jsonFound(historyJsonPresenter.toJson()).respond(response);
  }
Ejemplo n.º 20
0
  public void addUserIfDoesNotExist(Username userName) {
    synchronized (enableUserMutex) {
      User user = new User(CaseInsensitiveString.str(userName.getUsername()));
      if (!(user.isAnonymous() || userExists(user))) {
        assertUnknownUsersAreAllowedToLogin();

        userDao.saveOrUpdate(user);
      }
    }
  }
Ejemplo n.º 21
0
 public List<PipelineConfigs> viewableGroupsFor(Username username) {
   ArrayList<PipelineConfigs> list = new ArrayList<PipelineConfigs>();
   for (PipelineConfigs pipelineConfigs : goConfigService.getCurrentConfig().getGroups()) {
     if (hasViewPermissionForGroup(
         CaseInsensitiveString.str(username.getUsername()), pipelineConfigs.getGroup())) {
       list.add(pipelineConfigs);
     }
   }
   return list;
 }
Ejemplo n.º 22
0
 public MaterialConfig findMaterialWithName(
     final CaseInsensitiveString pipelineName, final CaseInsensitiveString materialName) {
   MaterialConfigs materialConfigs = materialConfigsFor(pipelineName);
   for (MaterialConfig materialConfig : materialConfigs) {
     if (materialName.equals(materialConfig.getName())) {
       return materialConfig;
     }
   }
   LOGGER.error("material [" + materialName + "] not found in pipeline [" + pipelineName + "]");
   return null;
 }
Ejemplo n.º 23
0
  @Test
  public void shouldNotScheduleBuildIfNoModification() throws Exception {
    autoSchedulePipelines("mingle", "evolve");
    // Get the scheduled evolve stage and complete it.
    Stage evolveInstance =
        stageDao.mostRecentWithBuilds(
            CaseInsensitiveString.str(evolveConfig.name()),
            evolveConfig.findBy(new CaseInsensitiveString("dev")));
    dbHelper.passStage(evolveInstance);
    stageDao.stageStatusChanged(evolveInstance);

    autoSchedulePipelines();
    Stage mostRecent =
        stageDao.mostRecentWithBuilds(
            CaseInsensitiveString.str(evolveConfig.name()),
            evolveConfig.findBy(new CaseInsensitiveString("dev")));

    assertThat(mostRecent.getId(), is(evolveInstance.getId()));
    assertThat(mostRecent.getJobInstances().first().getState(), is(JobState.Completed));
  }
Ejemplo n.º 24
0
 private List<String> invertSelections(List<String> selectedPipelines) {
   List<String> unselectedPipelines = new ArrayList<>();
   List<PipelineConfig> pipelineConfigList = cruiseConfig().getAllPipelineConfigs();
   for (PipelineConfig pipelineConfig : pipelineConfigList) {
     String pipelineName = CaseInsensitiveString.str(pipelineConfig.name());
     if (!selectedPipelines.contains(pipelineName)) {
       unselectedPipelines.add(pipelineName);
     }
   }
   return unselectedPipelines;
 }
Ejemplo n.º 25
0
 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);
 }
Ejemplo n.º 26
0
  private void assertPipelinesScheduled() {
    Pipeline minglePipeline =
        pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(mingleConfig.name()));
    Stage mingleStage = minglePipeline.getFirstStage();
    assertThat(mingleStage.getName(), is(STAGE_NAME));
    assertThat(mingleStage.getJobInstances().size(), is(2));
    JobInstance mingleJob = mingleStage.getJobInstances().first();
    assertThat(mingleJob.getState(), is(JobState.Scheduled));

    assertPipelineScheduled(evolveConfig);
    assertPipelineScheduled(goConfig);
  }
  @Test
  public void shouldReScheduleToCorrectAgent() throws Exception {
    JobConfig plan =
        evolveConfig
            .findBy(new CaseInsensitiveString(STAGE_NAME))
            .jobConfigByInstanceName("unit", true);
    plan.addResource("some-resource");

    scheduleHelper.schedule(evolveConfig, modifySomeFiles(evolveConfig), DEFAULT_APPROVED_BY);

    buildAssignmentService.onTimer();

    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("some-resource"));
    Work work = buildAssignmentService.assignWorkToAgent(agent(agentConfig));
    assertThat(work, is(not((Work) BuildAssignmentService.NO_WORK)));

    Pipeline pipeline =
        pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(evolveConfig.name()));
    JobInstance job = pipeline.findStage(STAGE_NAME).findJob("unit");

    JobInstance runningJob = jobInstanceDao.buildByIdWithTransitions(job.getId());

    scheduleService.rescheduleJob(runningJob);

    pipeline = pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(evolveConfig.name()));
    JobInstance rescheduledJob = pipeline.findStage(STAGE_NAME).findJob("unit");

    assertThat(rescheduledJob.getId(), not(runningJob.getId()));

    buildAssignmentService.onTimer();
    Work noResourcesWork =
        buildAssignmentService.assignWorkToAgent(
            agent(AgentMother.localAgentWithResources("WITHOUT_RESOURCES")));
    assertThat(noResourcesWork, is((Work) BuildAssignmentService.NO_WORK));

    buildAssignmentService.onTimer();
    Work correctAgentWork = buildAssignmentService.assignWorkToAgent(agent(agentConfig));
    assertThat(correctAgentWork, is(not((Work) BuildAssignmentService.NO_WORK)));
  }
Ejemplo n.º 28
0
  @Test
  public void shouldScheduleJobForAllAgentsWhenToBeRunOnAllAgents() throws Exception {
    configHelper.addAgent("localhost", "uuid1");
    configHelper.addAgent("localhost", "uuid2");
    configHelper.addAgent("localhost", "uuid3");
    configHelper.addAgentToEnvironment("dev", "uuid1");
    configHelper.setRunOnAllAgents(
        CaseInsensitiveString.str(evolveConfig.name()), STAGE_NAME, "unit", true);

    Material stubMaterial = new TestingMaterial();
    evolveConfig.setMaterialConfigs(new MaterialConfigs(stubMaterial.config()));
    MaterialRevisions revisions = new MaterialRevisions();
    revisions.addRevision(
        stubMaterial,
        ((TestingMaterial) stubMaterial)
            .modificationsSince(null, null, subprocessExecutionContext));
    BuildCause buildCause = BuildCause.createWithModifications(revisions, "");
    dbHelper.saveMaterials(buildCause.getMaterialRevisions());

    Pipeline pipeline =
        instanceFactory.createPipelineInstance(
            evolveConfig,
            buildCause,
            new DefaultSchedulingContext(
                DEFAULT_APPROVED_BY,
                environmentConfigService.agentsForPipeline(evolveConfig.name())),
            md5,
            new TimeProvider());
    pipelineService.save(pipeline);

    Stage instance =
        scheduleService.scheduleStage(
            pipeline,
            STAGE_NAME,
            "anyone",
            new ScheduleService.NewStageInstanceCreator(goConfigService),
            new ScheduleService.ExceptioningErrorHandler());
    JobInstances scheduledJobs = instance.getJobInstances();
    assertThat(
        scheduledJobs.toArray(),
        hasItemInArray(
            hasProperty(
                "name", is(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("unit", 1)))));
    assertThat(scheduledJobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid2"))));
    assertThat(
        scheduledJobs.toArray(),
        hasItemInArray(
            hasProperty(
                "name", is(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("unit", 2)))));
    assertThat(scheduledJobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid3"))));
    assertThat(scheduledJobs.size(), is(2));
  }
  private String setUpPipelineGroupsWithAdminPermissions() throws IOException {
    setCurrentUser("ram");
    assertThat(CaseInsensitiveString.str(UserHelper.getUserName().getUsername()), is("ram"));

    configHelper.addPipelineWithGroup("studios", "go", "stage", "build1", "build2");
    configHelper.setAdminPermissionForGroup("studios", "barrow");

    configHelper.addPipelineWithGroup("consulting", "bcg", "stage", "build1", "build2");
    configHelper.setAdminPermissionForGroup("consulting", "ram");
    configHelper.addTemplate("newTemplate", "stage");

    return goConfigDao.md5OfConfigFile();
  }
Ejemplo n.º 30
0
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    DependencyMaterial that = (DependencyMaterial) o;
    if (type != null ? !type.equals(that.type) : that.type != null) {
      return false;
    }
    if (pipelineName != null
        ? !pipelineName.equals(that.pipelineName)
        : that.pipelineName != null) {
      return false;
    }
    if (stageName != null ? !stageName.equals(that.stageName) : that.stageName != null) {
      return false;
    }

    return true;
  }