@Before
  public void setup() throws Exception {
    configHelper = new GoConfigFileHelper();
    configHelper.usingCruiseConfigDao(goConfigDao);
    configHelper.onSetUp();
    testRepo = new SvnTestRepo("testSvnRepo");
    dbHelper.onSetUp();
    repository = new SvnCommand(null, testRepo.projectRepositoryUrl());
    mingleConfig = configHelper.addPipeline("mingle", STAGE_NAME, repository, "unit", "functional");

    goConfig = configHelper.addPipeline("go", STAGE_NAME, repository, "unit");
    StageConfig ftStageConfig = StageConfigMother.custom("ft", "twist");
    ftStageConfig
        .jobConfigByConfigName(new CaseInsensitiveString("twist"))
        .addVariable("JOB_LVL", "job value");
    ftStageConfig.setVariables(env("STAGE_LVL", "stage value"));
    configHelper.addStageToPipeline("go", ftStageConfig);
    configHelper.addEnvironmentVariableToPipeline("go", env("PIPELINE_LVL", "pipeline value"));
    configHelper.addEnvironments("uat");
    EnvironmentConfig uatEnv =
        configHelper.currentConfig().getEnvironments().named(new CaseInsensitiveString("uat"));
    uatEnv.addPipeline(new CaseInsensitiveString("go"));
    uatEnv.addEnvironmentVariable("ENV_LVL", "env value");

    evolveConfig = configHelper.addPipeline("evolve", STAGE_NAME, repository, "unit");
    goCache.clear();
  }
Example #2
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);
 }
Example #3
0
  public boolean hasOperatePermissionForStage(
      String pipelineName, String stageName, String username) {
    if (!goConfigService.isSecurityEnabled()) {
      return true;
    }
    if (!goConfigService.hasStageConfigNamed(pipelineName, stageName)) {
      return false;
    }
    StageConfig stage = goConfigService.stageConfigNamed(pipelineName, stageName);
    CaseInsensitiveString userName = new CaseInsensitiveString(username);

    // TODO - #2517 - stage not exist
    if (stage.hasOperatePermissionDefined()) {
      CruiseConfig cruiseConfig = goConfigService.getCurrentConfig();
      String groupName =
          goConfigService.findGroupNameByPipeline(new CaseInsensitiveString(pipelineName));
      PipelineConfigs group = goConfigService.getCurrentConfig().findGroup(groupName);
      if (isUserAdmin(new Username(userName)) || isUserAdminOfGroup(userName, group)) {
        return true;
      }
      return goConfigService.readAclBy(pipelineName, stageName).isGranted(userName);
    }

    return hasOperatePermissionForPipeline(new CaseInsensitiveString(username), pipelineName);
  }
Example #4
0
 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);
 }
Example #5
0
 private void updateApproval(String pipelineName, String ftStage, Approval manualApproval) {
   CruiseConfig cruiseConfig = loadForEdit();
   PipelineConfig pipelineConfig =
       cruiseConfig.pipelineConfigByName(new CaseInsensitiveString(pipelineName));
   StageConfig config = pipelineConfig.findBy(new CaseInsensitiveString(ftStage));
   config.updateApproval(manualApproval);
   writeConfigFile(cruiseConfig);
 }
Example #6
0
 public void addEnvironmentVariableToStage(
     String pipelineName, String stageName, EnvironmentVariablesConfig envVars) {
   CruiseConfig cruiseConfig = loadForEdit();
   PipelineConfig pipelineConfig =
       cruiseConfig.pipelineConfigByName(new CaseInsensitiveString(pipelineName));
   StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(stageName));
   stageConfig.setVariables(envVars);
   writeConfigFile(cruiseConfig);
 }
Example #7
0
 public void removeJob(String pipelineName, String stageName, String jobName) {
   CruiseConfig cruiseConfig = loadForEdit();
   PipelineConfig pipelineConfig =
       cruiseConfig.pipelineConfigByName(new CaseInsensitiveString(pipelineName));
   StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(stageName));
   JobConfig job = stageConfig.getJobs().getJob(new CaseInsensitiveString(jobName));
   stageConfig.getJobs().remove(job);
   writeConfigFile(cruiseConfig);
 }
Example #8
0
 public void setOperatePermissionForStage(String pipelineName, String stageName, String username) {
   CruiseConfig cruiseConfig = loadForEdit();
   StageConfig stageConfig =
       cruiseConfig
           .pipelineConfigByName(new CaseInsensitiveString(pipelineName))
           .findBy(new CaseInsensitiveString(stageName));
   stageConfig.updateApproval(
       new Approval(new AuthConfig(new AdminUser(new CaseInsensitiveString(username)))));
   writeConfigFile(cruiseConfig);
 }
Example #9
0
 public void addAuthorizedUserForStage(String pipelineName, String stageName, String... users) {
   configureStageAsManualApproval(pipelineName, stageName);
   CruiseConfig cruiseConfig = loadForEdit();
   StageConfig stageConfig =
       cruiseConfig.stageConfigByName(
           new CaseInsensitiveString(pipelineName), new CaseInsensitiveString(stageName));
   Approval approval = stageConfig.getApproval();
   for (String user : users) {
     approval.getAuthConfig().add(new AdminUser(new CaseInsensitiveString(user)));
   }
   writeConfigFile(cruiseConfig);
 }
Example #10
0
 private StageConfig pushJobIntoStage(
     String pipelineName, String stageName, JobConfig jobConfig, boolean clearExistingJobs) {
   CruiseConfig cruiseConfig = loadForEdit();
   PipelineConfig pipelineConfig =
       cruiseConfig.pipelineConfigByName(new CaseInsensitiveString(pipelineName));
   StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(stageName));
   if (clearExistingJobs) {
     stageConfig.allBuildPlans().clear();
   }
   stageConfig.allBuildPlans().add(jobConfig);
   writeConfigFile(cruiseConfig);
   return stageConfig;
 }
Example #11
0
  public PipelineConfig changeStagenameForToPipeline(
      String pipelineName, String oldStageName, String newStageName) {
    CruiseConfig config = loadForEdit();
    PipelineConfig pipelineConfig =
        config.pipelineConfigByName(new CaseInsensitiveString(pipelineName));

    StageConfig stage = pipelineConfig.getStage(new CaseInsensitiveString(oldStageName));
    int index = pipelineConfig.indexOf(stage);

    stage =
        StageConfigMother.custom(
            newStageName,
            stage.isFetchMaterials(),
            stage.isCleanWorkingDir(),
            stage.getJobs(),
            stage.getApproval());
    pipelineConfig.set(index, stage);

    writeConfigFile(config);
    return pipelineConfig;
  }
Example #12
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);
 }
Example #13
0
 public boolean hasOperatePermissionForFirstStage(String pipelineName, String userName) {
   StageConfig stage =
       goConfigService.findFirstStageOfPipeline(new CaseInsensitiveString(pipelineName));
   return hasOperatePermissionForStage(
       pipelineName, CaseInsensitiveString.str(stage.name()), userName);
 }