Search in sources :

Example 1 with BuildJob

use of org.fakekoji.jobmanager.model.BuildJob in project jenkins-scm-koji-plugin by judovana.

the class DataGenerator method getJDKProjectJobs.

public static Set<Job> getJDKProjectJobs() {
    final Set<BuildProvider> buildProviders = DataGenerator.getBuildProviders();
    final JDKVersion jdkVersion = DataGenerator.getJDKVersion8();
    final Platform rhel7x64 = DataGenerator.getRHEL7x64();
    final Task tckTask = DataGenerator.getTCK();
    final Task buildTask = DataGenerator.getBuildTask();
    final File scriptsRoot = folderHolder.scriptsRoot;
    final File repositoriesRoot = folderHolder.reposRoot;
    final Product jdk8 = DataGenerator.getJDK8Product();
    final TaskVariant jreSdk = getJreSdk();
    final TaskVariantValue jreSdkDefault = jreSdk.getVariants().get(jreSdk.getDefaultValue());
    final TaskVariant agent = getAgent();
    final TaskVariantValue agentDefault = agent.getVariants().get(agent.getDefaultValue());
    final TaskVariant crypto = getCrypto();
    final TaskVariantValue cryptoDefault = crypto.getVariants().get(crypto.getDefaultValue());
    final TaskVariant jfr = getJfr();
    final TaskVariantValue jfrDefault = jfr.getVariants().get(jfr.getDefaultValue());
    final PullJob pullJob = new PullJob(PROJECT_NAME, PROJECT_URL, jdk8, jdkVersion, repositoriesRoot, scriptsRoot, Collections.emptyList());
    final BuildJob buildJobHotspotRelease = new BuildJob(BEAKER, PROJECT_NAME, jdk8, jdkVersion, buildProviders, buildTask, rhel7x64, Collections.unmodifiableMap(new HashMap<TaskVariant, TaskVariantValue>() {

        {
            put(DataGenerator.getJvmVariant(), DataGenerator.getHotspotVariant());
            put(DataGenerator.getDebugModeVariant(), DataGenerator.getReleaseVariant());
            put(jreSdk, jreSdkDefault);
        }
    }), scriptsRoot, Collections.emptyList());
    final BuildJob buildJobZeroRelease = new BuildJob(BEAKER, PROJECT_NAME, jdk8, jdkVersion, buildProviders, buildTask, rhel7x64, Collections.unmodifiableMap(new HashMap<TaskVariant, TaskVariantValue>() {

        {
            put(DataGenerator.getJvmVariant(), DataGenerator.getZeroVariant());
            put(DataGenerator.getDebugModeVariant(), DataGenerator.getReleaseVariant());
            put(jreSdk, jreSdkDefault);
        }
    }), scriptsRoot, Collections.emptyList());
    final TestJob testJobHotspotRelease = new TestJob(VAGRANT, buildJobHotspotRelease, tckTask, rhel7x64, Collections.unmodifiableMap(new HashMap<TaskVariant, TaskVariantValue>() {

        {
            put(DataGenerator.getGarbageCollectorCategory(), DataGenerator.getShenandoahVariant());
            put(DataGenerator.getDisplayProtocolCategory(), DataGenerator.getXServerVariant());
            put(agent, agentDefault);
            put(crypto, cryptoDefault);
            put(jfr, jfrDefault);
        }
    }));
    final TestJob testJobZeroHotspot = new TestJob(BEAKER, buildJobZeroRelease, tckTask, rhel7x64, Collections.unmodifiableMap(new HashMap<TaskVariant, TaskVariantValue>() {

        {
            put(DataGenerator.getGarbageCollectorCategory(), DataGenerator.getShenandoahVariant());
            put(DataGenerator.getDisplayProtocolCategory(), DataGenerator.getXServerVariant());
            put(agent, agentDefault);
            put(crypto, cryptoDefault);
            put(jfr, jfrDefault);
        }
    }));
    return new HashSet<>(Arrays.asList(pullJob, buildJobHotspotRelease, buildJobZeroRelease, testJobHotspotRelease, testJobZeroHotspot));
}
Also used : Task(org.fakekoji.model.Task) BuildProvider(org.fakekoji.model.BuildProvider) Platform(org.fakekoji.model.Platform) TestJob(org.fakekoji.jobmanager.model.TestJob) PullJob(org.fakekoji.jobmanager.model.PullJob) HashMap(java.util.HashMap) JDKVersion(org.fakekoji.model.JDKVersion) Product(org.fakekoji.jobmanager.model.Product) TaskVariantValue(org.fakekoji.model.TaskVariantValue) TaskVariant(org.fakekoji.model.TaskVariant) BuildJob(org.fakekoji.jobmanager.model.BuildJob) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with BuildJob

use of org.fakekoji.jobmanager.model.BuildJob in project jenkins-scm-koji-plugin by judovana.

the class ReverseJDKProjectParser method parseJDKProjectJobs.

public Result<JDKProject, String> parseJDKProjectJobs(Set<Job> jobs) {
    final List<PullJob> pullJobs = jobs.stream().filter(j -> j instanceof PullJob).map(j -> (PullJob) j).collect(Collectors.toList());
    final Set<TaskJob> taskJobs = jobs.stream().filter(j -> j instanceof TaskJob).map(j -> (TaskJob) j).collect(Collectors.toSet());
    final Set<BuildJob> buildJobs = jobs.stream().filter(j -> j instanceof BuildJob).map(j -> (BuildJob) j).collect(Collectors.toSet());
    final Set<TestJob> testJobs = jobs.stream().filter(j -> j instanceof TestJob).map(j -> (TestJob) j).collect(Collectors.toSet());
    if (pullJobs.isEmpty()) {
        return Result.err("Error: pull job not found");
    }
    if (pullJobs.size() > 1) {
        return Result.err("Error: more than one pull job (" + pullJobs.size() + ")");
    }
    final PullJob pullJob = pullJobs.get(0);
    return validateJobs(taskJobs, getTaskJobValidator()).flatMap(projectRepresentingJob -> {
        final Set<PlatformConfig> buildPlatforms = new HashSet<>();
        for (final BuildJob buildJob : buildJobs) {
            final String platformProvider = buildJob.getPlatformProvider();
            final Platform buildPlatform = buildJob.getPlatform();
            final Map<String, String> buildVariants = variantsMapToStringMap(buildJob.getVariants());
            final Task buildTask = buildJob.getTask();
            final PlatformConfig buildPlatformConfig = findOrCreatePlatform(buildPlatforms, buildPlatform.getId(), platformProvider);
            final TaskConfig buildTaskConfig = findOrCreateTask(buildPlatformConfig.getTasks(), buildTask.getId());
            final VariantsConfig buildVariantsConfig;
            final Optional<VariantsConfig> bvOptional = findVariants(buildTaskConfig.getVariants(), buildVariants);
            if (bvOptional.isPresent()) {
                return Result.err("Duplicate build job: " + buildJob.getName());
            } else {
                buildVariantsConfig = new VariantsConfig(buildVariants, new HashSet<>());
                buildTaskConfig.getVariants().add(buildVariantsConfig);
            }
        }
        for (final TestJob testJob : testJobs) {
            final String platformProvider = testJob.getPlatformProvider();
            final String buildPlatformProvider = testJob.getBuildPlatformProvider();
            final Platform testPlatform = testJob.getPlatform();
            final Map<String, String> buildVariants = variantsMapToStringMap(testJob.getBuildVariants());
            final Task buildTask = testJob.getBuildTask();
            final Platform buildPlatform = testJob.getBuildPlatform();
            final Map<String, String> testVariants = variantsMapToStringMap(testJob.getVariants());
            final Task testTask = testJob.getTask();
            final PlatformConfig buildPlatformConfig;
            final TaskConfig buildTaskConfig;
            final VariantsConfig buildVariantsConfig;
            final Optional<PlatformConfig> buildPlatformConfigOptional = findPlatform(buildPlatforms, buildPlatform.getId(), buildPlatformProvider);
            if (buildPlatformConfigOptional.isPresent()) {
                buildPlatformConfig = buildPlatformConfigOptional.get();
            } else {
                return Result.err("Could not find build platform " + buildPlatform.getId() + " for job: " + testJob.getName());
            }
            final Optional<TaskConfig> buildTaskConfigOptional = findTask(buildPlatformConfig.getTasks(), buildTask.getId());
            if (buildTaskConfigOptional.isPresent()) {
                buildTaskConfig = buildTaskConfigOptional.get();
            } else {
                return Result.err("Could not find build task " + buildTask.getId() + " for job: " + testJob.getName());
            }
            final Optional<VariantsConfig> buildVariantsOptional = findVariants(buildTaskConfig.getVariants(), buildVariants);
            if (buildVariantsOptional.isPresent()) {
                buildVariantsConfig = buildVariantsOptional.get();
            } else {
                return Result.err("Could not find build variants for job: " + testJob.getName());
            }
            final PlatformConfig testPlatformConfig = findOrCreatePlatform(buildVariantsConfig.getPlatforms(), testPlatform.getId(), platformProvider);
            final TaskConfig testTaskConfig = findOrCreateTask(testPlatformConfig.getTasks(), testTask.getId());
            final Optional<VariantsConfig> testVariantsConfigOptional = findVariants(testTaskConfig.getVariants(), testVariants);
            if (testVariantsConfigOptional.isPresent()) {
                return Result.err("Duplicate test job: " + testJob.getName());
            } else {
                testTaskConfig.getVariants().add(new VariantsConfig(testVariants, new HashSet<>()));
            }
        }
        return Result.ok(new JDKProject(projectRepresentingJob.getProjectName(), projectRepresentingJob.getProduct(), pullJob.getRepoUrl(), projectRepresentingJob.getBuildProviders().stream().map(BuildProvider::getId).collect(Collectors.toSet()), new JobConfiguration(buildPlatforms), projectRepresentingJob.getProjectVariables()));
    });
}
Also used : Platform(org.fakekoji.model.Platform) Task(org.fakekoji.model.Task) Job(org.fakekoji.jobmanager.model.Job) TaskConfig(org.fakekoji.jobmanager.model.TaskConfig) PlatformConfig(org.fakekoji.jobmanager.model.PlatformConfig) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TaskJob(org.fakekoji.jobmanager.model.TaskJob) JobConfiguration(org.fakekoji.jobmanager.model.JobConfiguration) HashSet(java.util.HashSet) Map(java.util.Map) BuildJob(org.fakekoji.jobmanager.model.BuildJob) Project(org.fakekoji.jobmanager.model.Project) TestJobConfiguration(org.fakekoji.jobmanager.model.TestJobConfiguration) VariantsConfig(org.fakekoji.jobmanager.model.VariantsConfig) Predicate(java.util.function.Predicate) Set(java.util.Set) BuildPlatformConfig(org.fakekoji.jobmanager.model.BuildPlatformConfig) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Collectors(java.util.stream.Collectors) BuildProvider(org.fakekoji.model.BuildProvider) TaskVariantValue(org.fakekoji.model.TaskVariantValue) List(java.util.List) TestJob(org.fakekoji.jobmanager.model.TestJob) PullJob(org.fakekoji.jobmanager.model.PullJob) Result(org.fakekoji.functional.Result) TaskVariant(org.fakekoji.model.TaskVariant) Optional(java.util.Optional) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Task(org.fakekoji.model.Task) TestJob(org.fakekoji.jobmanager.model.TestJob) Platform(org.fakekoji.model.Platform) PullJob(org.fakekoji.jobmanager.model.PullJob) TaskConfig(org.fakekoji.jobmanager.model.TaskConfig) TaskJob(org.fakekoji.jobmanager.model.TaskJob) PlatformConfig(org.fakekoji.jobmanager.model.PlatformConfig) BuildPlatformConfig(org.fakekoji.jobmanager.model.BuildPlatformConfig) VariantsConfig(org.fakekoji.jobmanager.model.VariantsConfig) BuildJob(org.fakekoji.jobmanager.model.BuildJob) JobConfiguration(org.fakekoji.jobmanager.model.JobConfiguration) TestJobConfiguration(org.fakekoji.jobmanager.model.TestJobConfiguration) HashSet(java.util.HashSet)

Example 3 with BuildJob

use of org.fakekoji.jobmanager.model.BuildJob in project jenkins-scm-koji-plugin by judovana.

the class RedeployApi method addEndpoints.

@Override
public void addEndpoints() {
    get(LATEST, context -> {
        String countValue = context.queryParam(LATEST_count);
        String commentsValue = context.queryParam(PROCESSED_cmments);
        String jobsNamesValue = context.queryParam(PROCESSED_job);
        List<String> jobs = new RedeployApiWorkerBase.RedeployApiStringListing(context).process(jdkProjectManager, jdkTestProjectManager, parser);
        Set<String> nvrs = new HashSet<>(jobs.size());
        for (String job : jobs) {
            File processed = new File(new File(settings.getJenkinsJobsRoot(), job), Constants.PROCESSED_BUILDS_HISTORY);
            List<String> raw = new ArrayList<>(0);
            if (processed.exists()) {
                raw = Utils.readFileToLines(processed, null);
            }
            if (raw.size() > 0) {
                nvrs.add(raw.get(raw.size() - 1).replaceAll("\\s*#.*", ""));
            }
        }
        context.status(OToolService.OK).result(String.join("\n", nvrs) + "\n");
    });
    get(PROCESSED, context -> {
        String job = context.queryParam(PROCESSED_job);
        if (job != null) {
            try {
                File processed = new File(new File(settings.getJenkinsJobsRoot(), job), Constants.PROCESSED_BUILDS_HISTORY);
                List<String> raw = Utils.readFileToLines(processed, null);
                if (context.queryParam(PROCESSED_cmments) == null) {
                    for (int i = 0; i < raw.size(); i++) {
                        raw.set(i, raw.get(i).replaceAll("\\s*#.*", ""));
                    }
                }
                if (context.queryParam(PROCESSED_uniq) == null) {
                    raw = new ArrayList<>(new TreeSet<>(raw));
                }
                if (context.queryParam(PROCESSED_sort) != null) {
                    Collections.sort(raw);
                // Collections.reverse(raw);no!
                }
                context.status(OToolService.OK).result(String.join("\n", raw) + "\n");
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, e.getMessage(), e);
                context.status(400).result(e.getMessage());
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, e.getMessage(), e);
                context.status(500).result(e.getMessage());
            }
        } else {
            context.status(OToolService.BAD).result("job is mandatory\n");
        }
    });
    get(REDEPLOY_CHECKOUT, context -> {
        try {
            String job = context.queryParam(RERUN_JOB);
            String nvr = context.queryParam(REDEPLOY_NVR);
            String force = context.queryParam("force");
            StringBuilder sb = forceCheckoutOnJob(job, nvr, "true".equals(force));
            context.status(OToolService.OK).result(sb.toString());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(RELOAD, context -> {
        try {
            String job = context.queryParam(RERUN_JOB);
            if (job == null) {
                throw new RuntimeException(RERUN_JOB + " is required parameter for " + RELOAD);
            }
            JenkinsCliWrapper.ClientResponse cr = JenkinsCliWrapper.getCli().reloadJob(job);
            try {
                cr.throwIfNecessary();
            } catch (Exception ex) {
                LOGGER.log(Level.WARNING, ex.getMessage(), cr.toString());
                context.status(500).result(ex.getMessage() + ": " + cr.toString());
            }
            context.status(OToolService.OK).result(cr.toString());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(REPROVIDER, context -> {
        try {
            String OTOOL_PLATFORM_PROVIDER = JenkinsJobTemplateBuilder.OTOOL_BASH_VAR_PREFIX + JenkinsJobTemplateBuilder.PLATFORM_PROVIDER_VAR;
            List<String> jobs = new RedeployApiWorkerBase.RedeployApiStringListing(context).process(jdkProjectManager, jdkTestProjectManager, parser);
            String doAndHow = context.queryParam(REDEPLOY_DO);
            String nwProvider = context.queryParam(REPROVIDERARG);
            String skipSlaves = context.queryParam(SKIP_SLAVES);
            if (nwProvider == null || nwProvider.trim().isEmpty()) {
                throw new RuntimeException(REPROVIDERARG + " is mandatory\n");
            }
            Set<String> providers = GetterAPI.getProviders(settings.getConfigManager().platformManager);
            if (!providers.contains(nwProvider)) {
                throw new RuntimeException(nwProvider + ": is not valid provider. Use one of: " + String.join(",", providers) + "\n");
            }
            /*
                 * To be able to compute nodes, we need to load nearly everything
                 */
            List<Job> allJobs = new ArrayList<>();
            List<Project> allProjects = new ArrayList<>();
            allProjects.addAll(settings.getConfigManager().jdkProjectManager.readAll());
            allProjects.addAll(settings.getConfigManager().jdkTestProjectManager.readAll());
            for (Project project : allProjects) {
                allJobs.addAll(parser.parse(project));
            }
            StringBuilder sb = new StringBuilder();
            int totalFiles = 0;
            int totalReplacements = 0;
            int invalidNodes = 0;
            final String noneNode = "NoneNodeFound";
            for (String job : jobs) {
                Job foundJob = findJob(allJobs, job);
                sb.append(job).append("\n");
                File jobDir = new File(settings.getJenkinsJobsRoot(), job);
                File config = new File(jobDir, "config.xml");
                List<String> lines = Utils.readFileToLines(config, null);
                for (int i = 0; i < lines.size(); i++) {
                    String mainline = lines.get(i);
                    String[] xmlLines = mainline.split(JenkinsJobTemplateBuilder.XML_NEW_LINE);
                    for (String line : xmlLines) {
                        if (!"true".equals(skipSlaves)) {
                            if (line.contains("<assignedNode>")) {
                                List<OToolVariable> fakeList = new ArrayList<>();
                                JenkinsJobTemplateBuilder.VmWithNodes mWithNodes = null;
                                try {
                                    if (foundJob instanceof TestJob) {
                                        fakeList = Arrays.asList(new OToolVariable("OS_NAME", ((TestJob) foundJob).getPlatform().getOs()));
                                        mWithNodes = JenkinsJobTemplateBuilder.getVmWithNodes(((TestJob) foundJob).getTask(), ((TestJob) foundJob).getPlatform(), new ArrayList<OToolVariable>(), nwProvider);
                                    } else if (foundJob instanceof BuildJob) {
                                        fakeList = Arrays.asList(new OToolVariable("OS_NAME", ((TestJob) foundJob).getPlatform().getOs()));
                                        mWithNodes = JenkinsJobTemplateBuilder.getVmWithNodes(((BuildJob) foundJob).getTask(), ((BuildJob) foundJob).getPlatform(), new ArrayList<OToolVariable>(), nwProvider);
                                    }
                                } catch (Exception ex) {
                                    mWithNodes = new JenkinsJobTemplateBuilder.VmWithNodes("NoNodesForThisCombination", Arrays.asList(noneNode));
                                    invalidNodes++;
                                }
                                String nwVal = "<assignedNode>" + String.join("||", mWithNodes.nodes) + "</assignedNode>";
                                List<String> nvWalToExpand = Arrays.asList(nwVal);
                                String nwValExpanded = JenkinsJobTemplateBuilder.expand(nvWalToExpand, fakeList).get(0);
                                mainline = mainline.replace(line.trim(), nwValExpanded);
                                sb.append(" - " + line.trim() + " -> " + nwValExpanded + "\n");
                                totalReplacements++;
                            }
                        }
                        if (line.contains(OTOOL_PLATFORM_PROVIDER + "=")) {
                            String nwVal = "export " + OTOOL_PLATFORM_PROVIDER + "=" + nwProvider + " # hacked at " + new Date().toString();
                            mainline = mainline.replace(line.trim(), nwVal);
                            sb.append(" - " + line.trim() + " -> " + nwVal + "\n");
                            totalReplacements++;
                        }
                    }
                    lines.set(i, mainline);
                }
                if ("true".equals(doAndHow)) {
                    Utils.writeToFile(config, String.join("\n", lines));
                    sb.append(" - written\n");
                    try {
                        JenkinsCliWrapper.getCli().reloadJob(job).throwIfNecessary();
                        sb.append(" - reloaded\n");
                    } catch (Exception ex) {
                        sb.append(" - reload failed, reload on your own\n");
                    }
                }
                totalFiles++;
            }
            sb.append("true".equals(doAndHow) ? "Written " : "Would be written: " + totalFiles + " of " + jobs.size() + "\n");
            if ("true".equals(skipSlaves)) {
                sb.append("Replaced " + totalReplacements + " of " + jobs.size() + "\n");
            } else {
                sb.append("Replaced " + totalReplacements + " of " + (jobs.size() * 2) + "\n");
            }
            sb.append("Invlaid nodes: " + invalidNodes + "; search for " + noneNode + "\n");
            context.status(OToolService.OK).result(sb.toString() + "\n");
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(RESLAVES, context -> {
        try {
            List<String> jobs = new RedeployApiWorkerBase.RedeployApiStringListing(context).process(jdkProjectManager, jdkTestProjectManager, parser);
            String doAndHow = context.queryParam(REDEPLOY_DO);
            String nwSlaves = context.queryParam(RESLAVES);
            if (nwSlaves == null || nwSlaves.trim().isEmpty()) {
                throw new RuntimeException(RESLAVES + " is mandatory\n");
            }
            StringBuilder sb = new StringBuilder();
            int totalCountReplacements = 0;
            int totalCountFiles = 0;
            for (String job : jobs) {
                sb.append(job).append("\n");
                File jobDir = new File(settings.getJenkinsJobsRoot(), job);
                File config = new File(jobDir, "config.xml");
                List<String> lines = Utils.readFileToLines(config, null);
                for (int i = 0; i < lines.size(); i++) {
                    String line = lines.get(i);
                    if (line.contains("<assignedNode>")) {
                        String orig = line.trim();
                        String nw = "<assignedNode>" + nwSlaves + "</assignedNode>";
                        sb.append(" - " + orig + " -> " + nw + "\n");
                        lines.set(i, nw);
                        totalCountReplacements++;
                    }
                }
                if ("true".equals(doAndHow)) {
                    Utils.writeToFile(config, String.join("\n", lines));
                    sb.append(" - written\n");
                    try {
                        JenkinsCliWrapper.getCli().reloadJob(job).throwIfNecessary();
                        sb.append(" - reloaded\n");
                    } catch (Exception ex) {
                        sb.append(" - reload failed, reload on your own\n");
                    }
                }
                totalCountFiles++;
            }
            sb.append("true".equals(doAndHow) ? "Written " : "Would be written: " + totalCountFiles + " of " + jobs.size() + "\n");
            sb.append("Replaced " + totalCountReplacements + " of " + jobs.size() + "\n");
            context.status(OToolService.OK).result(sb.toString() + "\n");
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(REDEPLOY_CHECKOUT_ALL, context -> {
        try {
            List<String> jobs = new RedeployApiWorkerBase.RedeployApiStringListing(context).process(jdkProjectManager, jdkTestProjectManager, parser);
            String doAndHow = context.queryParam(REDEPLOY_DO);
            String nvr = context.queryParam(REDEPLOY_NVR);
            String force = context.queryParam("force");
            if ("true".equals(doAndHow)) {
                List<String> results = new ArrayList<>(jobs.size());
                for (String job : jobs) {
                    try {
                        forceCheckoutOnJob(job, nvr, "true".equals(force));
                        results.add("ok - checking out -  " + job);
                    } catch (Exception ex) {
                        LOGGER.log(Level.WARNING, ex.getMessage(), ex);
                        results.add("failed " + job + ex.toString());
                    }
                }
                context.status(OToolService.OK).result(String.join("\n", results) + "\n");
            } else {
                context.status(OToolService.OK).result(String.join("\n", jobs) + "\n");
            }
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(REDEPLOY_NOW, context -> {
        try {
            List<String> jobs = new RedeployApiWorkerBase.RedeployApiStringListing(context).process(jdkProjectManager, jdkTestProjectManager, parser);
            String doAndHow = context.queryParam(REDEPLOY_DO);
            if ("true".equals(doAndHow)) {
                List<String> results = new ArrayList<>(jobs.size());
                for (String job : jobs) {
                    try {
                        JenkinsCliWrapper.ClientResponse cr = JenkinsCliWrapper.getCli().scheduleBuild(job);
                        cr.throwIfNecessary();
                        results.add("ok - scheduling -  " + job);
                    } catch (Exception ex) {
                        LOGGER.log(Level.WARNING, ex.getMessage(), ex);
                        results.add("failed " + job + ex.toString());
                    }
                }
                context.status(OToolService.OK).result(String.join("\n", results) + "\n");
            } else {
                context.status(OToolService.OK).result(String.join("\n", jobs) + "\n");
            }
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(REDEPLOY_RUN, context -> {
        // hmm enable filtering?
        try {
            String job = context.queryParam(RERUN_JOB);
            if (job == null) {
                throw new RuntimeException(RERUN_JOB + " must be an existing job id, was " + job);
            }
            String id = context.queryParam(RERUN_BUILD);
            if (id == null) {
                throw new RuntimeException(RERUN_BUILD + " must be an existing build number, was " + id);
            }
            File currentt = new File(settings.getJenkinsJobsRoot().getAbsolutePath() + File.separator + job + File.separator + "build.xml");
            if (!currentt.getParentFile().exists()) {
                throw new RuntimeException(currentt.getParentFile() + " do not exists. bad job?");
            }
            File archived = new File(settings.getJenkinsJobsRoot().getAbsolutePath() + File.separator + job + File.separator + "builds" + File.separator + id + File.separator + "changelog.xml");
            if (!archived.exists()) {
                throw new RuntimeException(archived.getAbsolutePath() + " do not exists. failed at checkout? Bad id? bad job?");
            }
            StringBuilder sb = new StringBuilder();
            if (!currentt.exists()) {
                sb.append(currentt.getAbsolutePath() + " do not exists. bad job? Broken checkou?t No koji-scm job?\n");
            }
            Files.copy(archived.toPath(), currentt.toPath(), StandardCopyOption.REPLACE_EXISTING);
            sb.append("Copied " + archived.getAbsolutePath() + " as " + currentt.getAbsolutePath() + "\n");
            JenkinsCliWrapper.ClientResponse cr = JenkinsCliWrapper.getCli().scheduleBuild(job);
            cr.throwIfNecessary();
            sb.append("scheduled " + job + "\n");
            context.status(OToolService.OK).result(sb.toString());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getClass().getName() + ": " + e.getMessage());
        }
    });
    get(REDEPLOY_BUILD, context -> {
        WarHorse wh = new WarHorse(context);
        wh.workload(new NvrDirOperatorFactory(), BuildJob.class);
    });
    get(REDEPLOY_TEST, context -> {
        WarHorse wh = new WarHorse(context);
        wh.workload(new FakeNvrDirOperatorFactory(), TestJob.class);
    });
    get(ARCHES_EXPECTED, context -> {
        try {
            String nvr = context.queryParam(REDEPLOY_NVR);
            if (nvr != null) {
                OToolParser.LegacyNVR nvrParsed = new OToolParser.LegacyNVR(nvr);
                String set = context.queryParam(ARCHES_EXPECTED_SET);
                if (set == null) {
                    File f = new File(settings.getDbFileRoot().toPath().toFile().getAbsolutePath() + "/" + nvrParsed.getFullPath() + "/data");
                    while (f.getParentFile() != null) {
                        File ae = new File(f, FakeBuild.archesConfigFileName);
                        if (ae.exists()) {
                            break;
                        }
                        f = f.getParentFile();
                    }
                    if (f.getParentFile() == null) {
                        context.status(OToolService.OK).result("No " + FakeBuild.archesConfigFileName + " for " + nvr + " arches expected are old api only!\n");
                    } else {
                        File ae = new File(f, FakeBuild.archesConfigFileName);
                        // by luck, same comments schema is used in processed.txt and arches.expected
                        List<String> archesWithoutCommenrs = Utils.readProcessedTxt(ae);
                        if (ae.length() < 4 || archesWithoutCommenrs.isEmpty()) {
                            context.status(OToolService.BAD).result(ae.getAbsolutePath() + " is emmpty or very small. That will break a lot of stuff!\n");
                        } else {
                            context.status(OToolService.OK).result(String.join(" ", archesWithoutCommenrs.get(0)) + " (" + ae.getAbsolutePath() + ")\n");
                        }
                    }
                } else {
                    File mainPath = new File(settings.getDbFileRoot().toPath().toFile().getAbsolutePath() + "/" + nvrParsed.getFullPath());
                    if (!mainPath.exists()) {
                        context.status(OToolService.BAD).result(mainPath.getAbsolutePath() + " Do not exists. Invlid NVRos?\n");
                    } else {
                        File data = new File(mainPath, "data");
                        File mainFile = new File(data, FakeBuild.archesConfigFileName);
                        if (mainFile.exists()) {
                            List<String> archesWithoutCommenrs = Utils.readProcessedTxt(mainFile);
                            FakeBuild.generateDefaultArchesFile(mainFile, set);
                            if (archesWithoutCommenrs.isEmpty()) {
                                context.status(OToolService.OK).result("overwritten " + mainFile + " (was: empty, is`" + set + "`)\n");
                            } else {
                                context.status(OToolService.OK).result("overwritten " + mainFile + " (was: `" + archesWithoutCommenrs.get(0) + "`, is`" + set + "`)\n");
                            }
                        } else {
                            data.mkdirs();
                            FakeBuild.generateDefaultArchesFile(mainFile, set);
                            context.status(OToolService.OK).result("written " + mainFile + "  (is`" + set + "`)\n");
                        }
                    }
                }
            } else {
                ArchesExpectedWorker aw = new ArchesExpectedWorker();
                aw.prepare();
                List<Platform> platforms = platformManager.readAll();
                Set<String> kojiArches = new HashSet<>();
                for (Platform p : platforms) {
                    kojiArches.add(p.getKojiArch().orElse(p.getArchitecture()));
                }
                // filtr affected jobs + places in builds (s neb arches>
                context.status(OToolService.OK).result(aw.dirsWithArches.entrySet().stream().map((Function<Map.Entry<String, String[]>, String>) e -> String.join(" ", e.getValue()) + " (" + e.getKey() + ")").sorted().collect(Collectors.joining("\n")) + "\n" + "All used: " + aw.usedArches.stream().collect(Collectors.joining(" ")) + "\n" + "All possible: " + kojiArches.stream().collect(Collectors.joining(" ")) + "\n");
            // better to filter them up rather then here
            }
        } catch (StorageException | IOException e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(400).result(e.getMessage());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            context.status(500).result(e.getMessage());
        }
    });
}
Also used : MISC(org.fakekoji.api.http.rest.OToolService.MISC) Arrays(java.util.Arrays) Date(java.util.Date) TaskVariantManager(org.fakekoji.jobmanager.manager.TaskVariantManager) Utils(org.fakekoji.Utils) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) PlatformManager(org.fakekoji.jobmanager.manager.PlatformManager) Context(io.javalin.http.Context) Map(java.util.Map) StorageException(org.fakekoji.storage.StorageException) BuildJob(org.fakekoji.jobmanager.model.BuildJob) JavaServerConstants(org.fakekoji.xmlrpc.server.JavaServerConstants) Path(java.nio.file.Path) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) Project(org.fakekoji.jobmanager.model.Project) FileVisitor(java.nio.file.FileVisitor) VariantsConfig(org.fakekoji.jobmanager.model.VariantsConfig) Set(java.util.Set) BuildPlatformConfig(org.fakekoji.jobmanager.model.BuildPlatformConfig) Logger(java.util.logging.Logger) EndpointGroup(io.javalin.apibuilder.EndpointGroup) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Collectors(java.util.stream.Collectors) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) OToolBuild(org.fakekoji.model.OToolBuild) Result(org.fakekoji.functional.Result) ApiBuilder.get(io.javalin.apibuilder.ApiBuilder.get) NotNull(org.jetbrains.annotations.NotNull) JenkinsCliWrapper(org.fakekoji.jobmanager.JenkinsCliWrapper) Platform(org.fakekoji.model.Platform) Tuple(org.fakekoji.functional.Tuple) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) Job(org.fakekoji.jobmanager.model.Job) HashMap(java.util.HashMap) JDKProjectParser(org.fakekoji.jobmanager.project.JDKProjectParser) Function(java.util.function.Function) TreeSet(java.util.TreeSet) OToolVariable(org.fakekoji.model.OToolVariable) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) ConfigManager(org.fakekoji.jobmanager.ConfigManager) RedeployApiWorkerBase(org.fakekoji.api.http.rest.utils.RedeployApiWorkerBase) Constants(hudson.plugins.scm.koji.Constants) TestJobConfiguration(org.fakekoji.jobmanager.model.TestJobConfiguration) ManagementException(org.fakekoji.jobmanager.ManagementException) Files(java.nio.file.Files) JenkinsJobTemplateBuilder(org.fakekoji.jobmanager.JenkinsJobTemplateBuilder) FakeBuild(org.fakekoji.core.FakeBuild) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) OToolParser(org.fakekoji.core.utils.OToolParser) TestJob(org.fakekoji.jobmanager.model.TestJob) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) Comparator(java.util.Comparator) Collections(java.util.Collections) AccessibleSettings(org.fakekoji.core.AccessibleSettings) TestJob(org.fakekoji.jobmanager.model.TestJob) Platform(org.fakekoji.model.Platform) ArrayList(java.util.ArrayList) JenkinsJobTemplateBuilder(org.fakekoji.jobmanager.JenkinsJobTemplateBuilder) TreeSet(java.util.TreeSet) OToolVariable(org.fakekoji.model.OToolVariable) BuildJob(org.fakekoji.jobmanager.model.BuildJob) BuildJob(org.fakekoji.jobmanager.model.BuildJob) Job(org.fakekoji.jobmanager.model.Job) TestJob(org.fakekoji.jobmanager.model.TestJob) HashSet(java.util.HashSet) OToolParser(org.fakekoji.core.utils.OToolParser) IOException(java.io.IOException) StorageException(org.fakekoji.storage.StorageException) ManagementException(org.fakekoji.jobmanager.ManagementException) IOException(java.io.IOException) Date(java.util.Date) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) Project(org.fakekoji.jobmanager.model.Project) JDKProject(org.fakekoji.jobmanager.model.JDKProject) JenkinsCliWrapper(org.fakekoji.jobmanager.JenkinsCliWrapper) RedeployApiWorkerBase(org.fakekoji.api.http.rest.utils.RedeployApiWorkerBase) File(java.io.File) StorageException(org.fakekoji.storage.StorageException)

Example 4 with BuildJob

use of org.fakekoji.jobmanager.model.BuildJob in project jenkins-scm-koji-plugin by judovana.

the class MatrixGenerator method getJobConsumer.

private Consumer<TaskJob> getJobConsumer(final Map<String, Map<String, CellGroup>> map, final boolean inverted) {
    return job -> {
        final BuildSpec buildSpec;
        final TestSpec testSpec;
        final String projectName = job.getProjectName();
        final Product product = job.getProduct();
        final Task task = job.getTask();
        final Platform.Provider provider = job.getPlatform().getProviders().stream().filter(p -> p.getId().equals(job.getPlatformProvider())).findFirst().orElseGet(this::createNoneProvider);
        final List<String> variants = job.getVariants().entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).map(e -> e.getValue().getId()).collect(Collectors.toList());
        if (job instanceof BuildJob) {
            final BuildJob buildJob = (BuildJob) job;
            buildSpec = new BuildSpec(buildJob.getPlatform(), provider, projectName, product, variants, buildFilter);
            testSpec = new TestSpec(buildJob.getPlatform(), provider, task, testFilter);
        } else if (job instanceof TestJob) {
            final TestJob testJob = (TestJob) job;
            final List<String> buildVariants = testJob.getBuildVariants().entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).map(e -> e.getValue().getId()).collect(Collectors.toList());
            final Platform.Provider buildPlatformProvider = testJob.getBuildPlatform().getProviders().stream().filter(p -> p.getId().equals(testJob.getBuildPlatformProvider())).findFirst().orElse(provider);
            testSpec = new TestSpec(testJob.getPlatform(), provider, task, variants, testFilter);
            if (testJob.getProjectType() == Project.ProjectType.JDK_TEST_PROJECT) {
                final BuildJob testOnlyBuildJob = new BuildJob(EXTERNAL_PLATFORM_PROVIDER, projectName, product, testJob.getJdkVersion(), job.getBuildProviders(), cache.getTask("build").orElse(null), testJob.getBuildPlatform(), testJob.getBuildVariants(), null, null);
                buildSpec = new BuildSpec(testJob.getBuildPlatform(), createExternalProvider(), projectName, product, buildVariants, buildFilter);
                final TestSpec testBuildSpec = new TestSpec(testOnlyBuildJob.getPlatform(), createExternalProvider(), testOnlyBuildJob.getTask(), testFilter);
                addJob(map, buildSpec, testBuildSpec, parseExternalJob(testOnlyBuildJob), inverted);
            } else {
                buildSpec = new BuildSpec(testJob.getBuildPlatform(), buildPlatformProvider, projectName, product, buildVariants, buildFilter);
            }
        } else {
            return;
        }
        addJob(map, buildSpec, testSpec, parseJob(job), inverted);
    };
}
Also used : MultiUrlCell(org.fakekoji.core.utils.matrix.cell.MultiUrlCell) Arrays(java.util.Arrays) Platform(org.fakekoji.model.Platform) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Formatter(org.fakekoji.core.utils.matrix.formatter.Formatter) Product(org.fakekoji.jobmanager.model.Product) Task(org.fakekoji.model.Task) URISyntaxException(java.net.URISyntaxException) Job(org.fakekoji.jobmanager.model.Job) Cell(org.fakekoji.core.utils.matrix.cell.Cell) HashMap(java.util.HashMap) TaskJob(org.fakekoji.jobmanager.model.TaskJob) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) UpperCornerCell(org.fakekoji.core.utils.matrix.cell.UpperCornerCell) Map(java.util.Map) StorageException(org.fakekoji.storage.StorageException) BuildJob(org.fakekoji.jobmanager.model.BuildJob) JavaServerConstants(org.fakekoji.xmlrpc.server.JavaServerConstants) PrintStream(java.io.PrintStream) Project(org.fakekoji.jobmanager.model.Project) ManagementException(org.fakekoji.jobmanager.ManagementException) Collection(java.util.Collection) Set(java.util.Set) CellGroup(org.fakekoji.core.utils.matrix.cell.CellGroup) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) TaskVariantValue(org.fakekoji.model.TaskVariantValue) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) List(java.util.List) TitleCell(org.fakekoji.core.utils.matrix.cell.TitleCell) UrlCell(org.fakekoji.core.utils.matrix.cell.UrlCell) TestJob(org.fakekoji.jobmanager.model.TestJob) Stream(java.util.stream.Stream) PlainTextFormatter(org.fakekoji.core.utils.matrix.formatter.PlainTextFormatter) TaskVariant(org.fakekoji.model.TaskVariant) ConfigCache(org.fakekoji.jobmanager.ConfigCache) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) AccessibleSettings(org.fakekoji.core.AccessibleSettings) Task(org.fakekoji.model.Task) TestJob(org.fakekoji.jobmanager.model.TestJob) Platform(org.fakekoji.model.Platform) Product(org.fakekoji.jobmanager.model.Product) ArrayList(java.util.ArrayList) List(java.util.List) BuildJob(org.fakekoji.jobmanager.model.BuildJob) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with BuildJob

use of org.fakekoji.jobmanager.model.BuildJob in project jenkins-scm-koji-plugin by judovana.

the class JenkinsJobTemplateBuilderTest method buildBuildJobTemplateWithVmPlatform.

@Test
public void buildBuildJobTemplateWithVmPlatform() throws IOException {
    final Set<BuildProvider> buildProviders = DataGenerator.getBuildProviders();
    final Task buildTask = DataGenerator.getBuildTask();
    final Platform vmPlatform = DataGenerator.getF29x64();
    final BuildJob buildJob = new BuildJob(VAGRANT, PROJECT_NAME, jdk8Product, jdk8, buildProviders, buildTask, vmPlatform, buildVariants, scriptsRoot, null);
    final String expectedTemplate = "<?xml version=\"1.1\" encoding=\"UTF-8\" ?>\n" + "<project>\n" + "    <actions/>\n" + "    <description/>\n" + "    <keepDependencies>false</keepDependencies>\n" + "    <properties/>\n" + "    <scm class=\"hudson.plugins.scm.koji.KojiSCM\" plugin=\"jenkins-scm-koji-plugin@0.2-SNAPSHOT\">\n" + BUILD_PROVIDERS_TEMPLATE + "        <kojiXmlRpcApi class=\"hudson.plugins.scm.koji.FakeKojiXmlRpcApi\">\n" + "            <xmlRpcApiType>FAKE_KOJI</xmlRpcApiType>\n" + "            <projectName>" + PROJECT_NAME + "</projectName>\n" + "            <buildVariants>" + "buildPlatform=" + vmPlatform.getId() + " debugMode=" + buildVariants.get(debugMode).getId() + " jreSdk=" + buildVariants.get(jreSdk).getId() + " jvm=" + buildVariants.get(jvm).getId() + "</buildVariants>\n" + "            <buildPlatform>src</buildPlatform>\n" + "            <isBuilt>" + false + "</isBuilt>\n" + "        </kojiXmlRpcApi>\n" + "        <downloadDir>rpms</downloadDir>\n" + "        <cleanDownloadDir>true</cleanDownloadDir>\n" + "        <dirPerNvr>false</dirPerNvr>\n" + "        <maxPreviousBuilds>10</maxPreviousBuilds>\n" + "    </scm>\n" + "    <assignedNode>" + String.join("||", vmPlatform.getProviders().get(0).getVmNodes()) + "</assignedNode>\n" + "    <canRoam>false</canRoam>\n" + "    <disabled>false</disabled>\n" + "    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n" + "    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n" + "    <triggers>\n" + "        <hudson.triggers.SCMTrigger>\n" + "            <spec>" + SCP_POLL_SCHEDULE + "</spec>\n" + "            <ignorePostCommitHooks>false</ignorePostCommitHooks>\n" + "        </hudson.triggers.SCMTrigger>\n" + "    </triggers>\n" + "    <concurrentBuild>false</concurrentBuild>\n" + "    <builders>\n" + "        <hudson.tasks.Shell>\n" + "            <command>\n" + "#!/bin/bash&#13;\n" + "export OTOOL_ARCH=" + vmPlatform.getArchitecture() + XML_NEW_LINE + "export OTOOL_JDK_VERSION=" + jdk8.getVersion() + XML_NEW_LINE + "export OTOOL_JOB_NAME=" + buildJob.getName() + XML_NEW_LINE + "export OTOOL_JOB_NAME_SHORTENED=" + buildJob.getShortName() + XML_NEW_LINE + "export OTOOL_OJDK=" + jdk8.getId() + XML_NEW_LINE + "export OTOOL_OS=" + vmPlatform.getOs() + '.' + vmPlatform.getVersion() + XML_NEW_LINE + "export OTOOL_OS_NAME=" + vmPlatform.getOs() + XML_NEW_LINE + "export OTOOL_OS_VERSION=" + vmPlatform.getVersionNumber() + XML_NEW_LINE + "export OTOOL_PACKAGE_NAME=" + jdk8.getPackageNames().get(0) + XML_NEW_LINE + "export OTOOL_PLATFORM_PROVIDER=" + vmPlatform.getProviders().get(0).getId() + XML_NEW_LINE + "export OTOOL_PROJECT_NAME=" + PROJECT_NAME + XML_NEW_LINE + "export OTOOL_RELEASE_SUFFIX=" + RELEASE + '.' + HOTSPOT + '.' + SDK + '.' + vmPlatform.getId() + XML_NEW_LINE + "export OTOOL_VM_NAME_OR_LOCAL=" + vmPlatform.getVmName() + XML_NEW_LINE + "export OTOOL_debugMode=" + buildVariants.get(debugMode).getId() + XML_NEW_LINE + "export OTOOL_jreSdk=" + buildVariants.get(jreSdk).getId() + XML_NEW_LINE + "export OTOOL_jvm=" + buildVariants.get(jvm).getId() + XML_NEW_LINE + "\nbash " + Paths.get(scriptsRoot.getAbsolutePath(), O_TOOL, RUN_SCRIPT_NAME) + " '" + buildTask.getScript() + "'&#13;\n" + "</command>\n" + "        </hudson.tasks.Shell>\n" + "    </builders>\n" + "    <publishers>\n" + DataGenerator.BUILD_POST_BUILD_TASK + DataGenerator.getPostTasks(scriptsRoot.getAbsolutePath(), true, true, VAGRANT, buildJob.getShortName(), vmPlatform.getVmName()) + "    </publishers>\n" + "    <buildWrappers/>\n" + "</project>\n";
    final String actualTemplate = buildJob.generateTemplate();
    Assert.assertEquals(expectedTemplate, actualTemplate);
}
Also used : Task(org.fakekoji.model.Task) BuildProvider(org.fakekoji.model.BuildProvider) Platform(org.fakekoji.model.Platform) BuildJob(org.fakekoji.jobmanager.model.BuildJob) Test(org.junit.Test)

Aggregations

BuildJob (org.fakekoji.jobmanager.model.BuildJob)7 Platform (org.fakekoji.model.Platform)7 TestJob (org.fakekoji.jobmanager.model.TestJob)5 Task (org.fakekoji.model.Task)5 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Job (org.fakekoji.jobmanager.model.Job)4 BuildProvider (org.fakekoji.model.BuildProvider)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Test (org.junit.Test)3 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 Function (java.util.function.Function)2