Search in sources :

Example 1 with JDKTestProjectManager

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

the class GetterAPI method getJDKVersionHandler.

private QueryHandler getJDKVersionHandler() {
    final JDKProjectManager jdkProjectManager = settings.getConfigManager().jdkProjectManager;
    final JDKTestProjectManager jdkTestProjectManager = settings.getConfigManager().jdkTestProjectManager;
    final JDKVersionManager jdkVersionManager = settings.getConfigManager().jdkVersionManager;
    return new QueryHandler() {

        @Override
        public Result<String, String> handle(Map<String, List<String>> paramsMap) throws StorageException, ManagementException {
            Optional<String> productOpt = extractParamValue(paramsMap, PRODUCT);
            if (productOpt.isPresent()) {
                final String product = productOpt.get();
                final List<JDKVersion> jdkVersions = jdkVersionManager.readAll();
                final Optional<JDKVersion> optionalJDKVersion = jdkVersions.stream().filter(jdkVersion -> jdkVersion.getPackageNames().contains(product)).findFirst();
                if (optionalJDKVersion.isPresent()) {
                    final JDKVersion jdkVersion = optionalJDKVersion.get();
                    return Result.ok(jdkVersion.getId());
                }
            }
            final Optional<String> projectOpt = extractParamValue(paramsMap, PROJECT);
            if (projectOpt.isPresent()) {
                final String projectName = projectOpt.get();
                final Project project;
                if (jdkProjectManager.contains(projectName)) {
                    project = jdkProjectManager.read(projectName);
                } else if (jdkTestProjectManager.contains(projectName)) {
                    project = jdkTestProjectManager.read(projectName);
                } else {
                    return Result.err("Project " + projectName + " doesn't exist");
                }
                return Result.ok(project.getProduct().getJdk());
            }
            return Result.err("Wrong/missing parameters");
        }

        @Override
        public String about() {
            return "/jdkVersion?[" + String.join("|", PRODUCT + "=<packageName>", PROJECT + "=<projectName>") + "]";
        }
    };
}
Also used : JenkinsCliWrapper(org.fakekoji.jobmanager.JenkinsCliWrapper) Platform(org.fakekoji.model.Platform) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) Task(org.fakekoji.model.Task) TaskVariantManager(org.fakekoji.jobmanager.manager.TaskVariantManager) Job(org.fakekoji.jobmanager.model.Job) HashMap(java.util.HashMap) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) PlatformManager(org.fakekoji.jobmanager.manager.PlatformManager) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) RestUtils.extractParamValue(org.fakekoji.api.http.rest.RestUtils.extractParamValue) StorageException(org.fakekoji.storage.StorageException) Path(java.nio.file.Path) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) Project(org.fakekoji.jobmanager.model.Project) ManagementException(org.fakekoji.jobmanager.ManagementException) FileVisitor(java.nio.file.FileVisitor) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TaskManager(org.fakekoji.jobmanager.manager.TaskManager) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) EndpointGroup(io.javalin.apibuilder.EndpointGroup) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Collectors(java.util.stream.Collectors) TaskVariantValue(org.fakekoji.model.TaskVariantValue) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) OToolParser(org.fakekoji.core.utils.OToolParser) List(java.util.List) Stream(java.util.stream.Stream) OToolBuild(org.fakekoji.model.OToolBuild) Result(org.fakekoji.functional.Result) TaskVariant(org.fakekoji.model.TaskVariant) Optional(java.util.Optional) ApiBuilder.get(io.javalin.apibuilder.ApiBuilder.get) Pattern(java.util.regex.Pattern) ApiBuilder.path(io.javalin.apibuilder.ApiBuilder.path) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) JDKVersion(org.fakekoji.model.JDKVersion) Comparator(java.util.Comparator) Collections(java.util.Collections) OToolArchive(org.fakekoji.model.OToolArchive) AccessibleSettings(org.fakekoji.core.AccessibleSettings) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) Project(org.fakekoji.jobmanager.model.Project) JDKProject(org.fakekoji.jobmanager.model.JDKProject) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) JDKVersion(org.fakekoji.model.JDKVersion) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with JDKTestProjectManager

use of org.fakekoji.jobmanager.project.JDKTestProjectManager 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 3 with JDKTestProjectManager

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

the class GetterAPI method getProjectsHandler.

private QueryHandler getProjectsHandler() {
    final JDKProjectManager jdkProjectManager = settings.getConfigManager().jdkProjectManager;
    final JDKTestProjectManager jdkTestProjectManager = settings.getConfigManager().jdkTestProjectManager;
    final JDKVersionManager jdkVersionManager = settings.getConfigManager().jdkVersionManager;
    final TaskVariantManager taskVariantManager = settings.getConfigManager().taskVariantManager;
    return new QueryHandler() {

        @Override
        public Result<String, String> handle(Map<String, List<String>> queryParams) throws StorageException {
            final Optional<String> typeOpt = extractParamValue(queryParams, TYPE);
            final Optional<String> nvrOpt = extractParamValue(queryParams, NVR);
            final Optional<String> rulesOpt = extractParamValue(queryParams, ADDITIONAL_RULES);
            final List<String[]> rulesPairList;
            if (rulesOpt.isPresent()) {
                String[] rules = rulesOpt.get().split("[\\s,]+");
                if (rules.length == 0 || rules.length % 2 == 1) {
                    return Result.err("if " + ADDITIONAL_RULES + " is used, then it must be >0 and mus be even number of them. You have " + rules.length);
                }
                rulesPairList = new ArrayList<>(rules.length / 2);
                for (int i = 0; i < rules.length; i += 2) {
                    rulesPairList.add(new String[] { rules[i], rules[i + 1] });
                }
            } else {
                rulesPairList = Collections.emptyList();
            }
            final String asRegex = extractParamValue(queryParams, AS_REGEX_LIST).orElse(null);
            final boolean unsafe = Boolean.valueOf(extractParamValue(queryParams, UNSAFE).orElse("false"));
            final String prep;
            final String join;
            final String post;
            if ("regex".equals(asRegex)) {
                prep = ".*-";
                join = "-.*|.*-";
                post = "-.*";
            } else if ("list".equals(asRegex)) {
                prep = "";
                join = ",";
                post = "";
            } else {
                prep = "";
                join = "\n";
                post = "\n";
            }
            if (typeOpt.isPresent()) {
                final String type = typeOpt.get();
                final Stream<Project> projects;
                switch(Project.ProjectType.valueOf(type)) {
                    case JDK_PROJECT:
                        projects = jdkProjectManager.readAll().stream().map(project -> project);
                        break;
                    case JDK_TEST_PROJECT:
                        projects = jdkTestProjectManager.readAll().stream().map(project -> project);
                        break;
                    default:
                        return Result.err("Unknown project type");
                }
                return Result.ok(prep + projects.map(Project::getId).sorted(String::compareTo).collect(Collectors.joining(join)) + post);
            } else if (nvrOpt.isPresent()) {
                final OToolParser parser = new OToolParser(jdkProjectManager.readAll(), jdkVersionManager.readAll(), taskVariantManager.getBuildVariants());
                final String archive = nvrOpt.get();
                Result r;
                try {
                    r = parser.parseArchive(archive).map(oToolArchive -> prep + oToolArchive.getProjectName() + post);
                } catch (Exception ex) {
                    // ex.printStackTrace(); we do not care, falling back to
                    r = Result.err("Not and parse-able project");
                }
                if (r.isOk()) {
                    return r;
                } else {
                    if (unsafe) {
                        // by longest project, try stupid substring
                        List<JDKProject> projects = jdkProjectManager.readAll();
                        Collections.sort(projects, new Comparator<JDKProject>() {

                            @Override
                            public int compare(JDKProject o1, JDKProject o2) {
                                return o2.getId().length() - o1.getId().length();
                            }
                        });
                        for (JDKProject project : projects) {
                            if (nvrOpt.get().contains("-" + project.getId() + "-") || nvrOpt.get().contains("." + project.getId() + ".") || nvrOpt.get().contains("." + project.getId() + "-") || nvrOpt.get().contains("-" + project.getId() + ".") || nvrOpt.get().endsWith("-" + project.getId()) || nvrOpt.get().endsWith("." + project.getId())) {
                                // add also cehck against pkg? Likely not, the src snapshots may not be parseable
                                return Result.ok(prep + project.getId() + post);
                            }
                        }
                    }
                    // ok, it is not jdkProject, so it must be jdkTestProject
                    String nv = archive.substring(0, archive.lastIndexOf("-"));
                    String n = nv.substring(0, nv.lastIndexOf("-"));
                    String vr = archive.replaceFirst(n + "-", "");
                    Pattern tmpRuleMatcher = Pattern.compile(".*");
                    List<JDKTestProject> testProjects = jdkTestProjectManager.readAll();
                    for (String[] rule : rulesPairList) {
                        if (vr.matches(rule[0])) {
                            tmpRuleMatcher = Pattern.compile(rule[1]);
                            break;
                        }
                    }
                    final Pattern ruleMatcher = tmpRuleMatcher;
                    List<String> results = testProjects.stream().filter(testproject -> testproject.getProduct().getPackageName().equals(n)).filter(testproject -> ruleMatcher.matcher(testproject.getId()).matches()).map(testproject -> testproject.getId()).sorted().collect(Collectors.toList());
                    if (results.isEmpty()) {
                        if (unsafe) {
                            results = jdkTestProjectManager.readAll().stream().filter(testproject -> ruleMatcher.matcher(testproject.getId()).matches()).map(Project::getId).sorted(String::compareTo).collect(Collectors.toList());
                            if (results.isEmpty()) {
                                // without rules
                                results = jdkTestProjectManager.readAll().stream().map(Project::getId).sorted(String::compareTo).collect(Collectors.toList());
                            }
                            return Result.ok(prep + String.join(join, results) + post);
                        } else {
                            return Result.err("");
                        }
                    } else {
                        return Result.ok(prep + String.join(join, results) + post);
                    }
                }
            } else {
                final Stream<Project> projects = Stream.of(jdkProjectManager.readAll(), jdkTestProjectManager.readAll()).flatMap(List::stream);
                Optional<String> productOpt = extractParamValue(queryParams, PRODUCT);
                if (productOpt.isPresent()) {
                    final String product = productOpt.get();
                    return Result.ok(prep + projects.filter(project -> project.getProduct().getPackageName().equals(product)).map(Project::getId).collect(Collectors.joining(join)) + post);
                }
                return Result.ok(prep + projects.map(Project::getId).sorted(String::compareTo).collect(Collectors.joining(join)) + post);
            }
        }

        @Override
        public String about() {
            return "/projects?[ one of \n" + "\t" + TYPE + "=[" + Project.ProjectType.JDK_PROJECT + "|" + Project.ProjectType.JDK_TEST_PROJECT + "]\n" + "\t" + NVR + "=nvr with optional as=<regex|list> and usnafe=true (will try also longest substring for jdkProjects or return all testOnly on failure)\n" + "\t" + ADDITIONAL_RULES + "=space or comma sepparated list of pairs eg Pa1 Pb1,Pa2 Pb2,...PaN PBn \n" + "\t" + "each pair is if VR matches Pa then use Pb matching projects. Firs matched, first served \n" + "]";
        }
    };
}
Also used : JenkinsCliWrapper(org.fakekoji.jobmanager.JenkinsCliWrapper) Platform(org.fakekoji.model.Platform) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) Task(org.fakekoji.model.Task) TaskVariantManager(org.fakekoji.jobmanager.manager.TaskVariantManager) Job(org.fakekoji.jobmanager.model.Job) HashMap(java.util.HashMap) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) PlatformManager(org.fakekoji.jobmanager.manager.PlatformManager) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) RestUtils.extractParamValue(org.fakekoji.api.http.rest.RestUtils.extractParamValue) StorageException(org.fakekoji.storage.StorageException) Path(java.nio.file.Path) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) Project(org.fakekoji.jobmanager.model.Project) ManagementException(org.fakekoji.jobmanager.ManagementException) FileVisitor(java.nio.file.FileVisitor) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TaskManager(org.fakekoji.jobmanager.manager.TaskManager) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) EndpointGroup(io.javalin.apibuilder.EndpointGroup) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Collectors(java.util.stream.Collectors) TaskVariantValue(org.fakekoji.model.TaskVariantValue) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) OToolParser(org.fakekoji.core.utils.OToolParser) List(java.util.List) Stream(java.util.stream.Stream) OToolBuild(org.fakekoji.model.OToolBuild) Result(org.fakekoji.functional.Result) TaskVariant(org.fakekoji.model.TaskVariant) Optional(java.util.Optional) ApiBuilder.get(io.javalin.apibuilder.ApiBuilder.get) Pattern(java.util.regex.Pattern) ApiBuilder.path(io.javalin.apibuilder.ApiBuilder.path) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) JDKVersion(org.fakekoji.model.JDKVersion) Comparator(java.util.Comparator) Collections(java.util.Collections) OToolArchive(org.fakekoji.model.OToolArchive) AccessibleSettings(org.fakekoji.core.AccessibleSettings) FileVisitResult(java.nio.file.FileVisitResult) Result(org.fakekoji.functional.Result) Comparator(java.util.Comparator) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) TaskVariantManager(org.fakekoji.jobmanager.manager.TaskVariantManager) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Pattern(java.util.regex.Pattern) Optional(java.util.Optional) OToolParser(org.fakekoji.core.utils.OToolParser) StorageException(org.fakekoji.storage.StorageException) ManagementException(org.fakekoji.jobmanager.ManagementException) IOException(java.io.IOException) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) Project(org.fakekoji.jobmanager.model.Project) JDKProject(org.fakekoji.jobmanager.model.JDKProject) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with JDKTestProjectManager

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

the class GetterAPI method getProductHandler.

private QueryHandler getProductHandler() {
    final JDKProjectManager jdkProjectManager = settings.getConfigManager().jdkProjectManager;
    final JDKTestProjectManager jdkTestProjectManager = settings.getConfigManager().jdkTestProjectManager;
    final JDKVersionManager jdkVersionManager = settings.getConfigManager().jdkVersionManager;
    final TaskVariantManager taskVariantManager = settings.getConfigManager().taskVariantManager;
    return new QueryHandler() {

        @Override
        public Result<String, String> handle(Map<String, List<String>> queryParams) throws StorageException {
            final Optional<String> archiveOpt = extractParamValue(queryParams, ARCHIVE);
            if (archiveOpt.isPresent()) {
                final String archive = archiveOpt.get();
                return new OToolParser(jdkProjectManager.readAll(), jdkVersionManager.readAll(), taskVariantManager.getBuildVariants()).parseArchive(archive).map(OToolBuild::getPackageName);
            }
            final Optional<String> projectOpt = extractParamValue(queryParams, PROJECT);
            if (projectOpt.isPresent()) {
                final String project = projectOpt.get();
                final List<JDKProject> jdkProjects = jdkProjectManager.readAll();
                final Optional<JDKProject> optionalJDKProject = jdkProjects.stream().filter(proj -> proj.getId().equals(project)).findFirst();
                if (optionalJDKProject.isPresent()) {
                    final JDKProject jdkProject = optionalJDKProject.get();
                    return Result.ok(jdkProject.getProduct().getPackageName());
                }
                final List<JDKTestProject> jdkTestProjects = jdkTestProjectManager.readAll();
                final Optional<JDKTestProject> optionalJDKTestProject = jdkTestProjects.stream().filter(proj -> proj.getId().equals(project)).findFirst();
                if (optionalJDKTestProject.isPresent()) {
                    final JDKTestProject jdkTestProject = optionalJDKTestProject.get();
                    return Result.ok(jdkTestProject.getProduct().getPackageName());
                }
                return Result.err(ERROR_PROJECT_NOT_FOUND);
            }
            final Optional<String> buildOpt = extractParamValue(queryParams, BUILD);
            if (buildOpt.isPresent()) {
                final String build = buildOpt.get();
                return new OToolParser(jdkProjectManager.readAll(), jdkVersionManager.readAll(), taskVariantManager.getBuildVariants()).parseBuild(build).map(OToolBuild::getPackageName);
            }
            return Result.err(ERROR_PARAMETERS_EXPECTED);
        }

        @Override
        public String about() {
            return "/product?[" + String.join("|", ARCHIVE + "=<NVRA>", BUILD + "=<NVR>", PROJECT + "=projectName") + "]";
        }
    };
}
Also used : TaskVariantManager(org.fakekoji.jobmanager.manager.TaskVariantManager) JenkinsCliWrapper(org.fakekoji.jobmanager.JenkinsCliWrapper) Platform(org.fakekoji.model.Platform) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) Task(org.fakekoji.model.Task) TaskVariantManager(org.fakekoji.jobmanager.manager.TaskVariantManager) Job(org.fakekoji.jobmanager.model.Job) HashMap(java.util.HashMap) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) PlatformManager(org.fakekoji.jobmanager.manager.PlatformManager) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) RestUtils.extractParamValue(org.fakekoji.api.http.rest.RestUtils.extractParamValue) StorageException(org.fakekoji.storage.StorageException) Path(java.nio.file.Path) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) Project(org.fakekoji.jobmanager.model.Project) ManagementException(org.fakekoji.jobmanager.ManagementException) FileVisitor(java.nio.file.FileVisitor) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TaskManager(org.fakekoji.jobmanager.manager.TaskManager) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) EndpointGroup(io.javalin.apibuilder.EndpointGroup) JDKProject(org.fakekoji.jobmanager.model.JDKProject) Collectors(java.util.stream.Collectors) TaskVariantValue(org.fakekoji.model.TaskVariantValue) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) OToolParser(org.fakekoji.core.utils.OToolParser) List(java.util.List) Stream(java.util.stream.Stream) OToolBuild(org.fakekoji.model.OToolBuild) Result(org.fakekoji.functional.Result) TaskVariant(org.fakekoji.model.TaskVariant) Optional(java.util.Optional) ApiBuilder.get(io.javalin.apibuilder.ApiBuilder.get) Pattern(java.util.regex.Pattern) ApiBuilder.path(io.javalin.apibuilder.ApiBuilder.path) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) JDKVersion(org.fakekoji.model.JDKVersion) Comparator(java.util.Comparator) Collections(java.util.Collections) OToolArchive(org.fakekoji.model.OToolArchive) AccessibleSettings(org.fakekoji.core.AccessibleSettings) JDKProject(org.fakekoji.jobmanager.model.JDKProject) OToolParser(org.fakekoji.core.utils.OToolParser) JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) JDKProjectManager(org.fakekoji.jobmanager.project.JDKProjectManager) JDKVersionManager(org.fakekoji.jobmanager.manager.JDKVersionManager) HashMap(java.util.HashMap) Map(java.util.Map) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject) OToolBuild(org.fakekoji.model.OToolBuild)

Example 5 with JDKTestProjectManager

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

the class GetterAPI method getAllJdkTestJobs.

public List<String> getAllJdkTestJobs(Optional<String> projectFilter) throws StorageException, ManagementException {
    final JDKTestProjectManager jdkTestProjectManager = settings.getConfigManager().jdkTestProjectManager;
    List<String> testProjects = new ArrayList<>();
    for (final JDKTestProject jdkTestProject : jdkTestProjectManager.readAll()) {
        if (checkProjectNames(jdkTestProject.getId(), projectFilter)) {
            Set<Job> testJobsSet = settings.getJdkProjectParser().parse(jdkTestProject);
            for (Job j : testJobsSet) {
                testProjects.add(j.getName());
            }
        }
    }
    Collections.sort(testProjects);
    return testProjects;
}
Also used : JDKTestProjectManager(org.fakekoji.jobmanager.project.JDKTestProjectManager) ArrayList(java.util.ArrayList) Job(org.fakekoji.jobmanager.model.Job) JDKTestProject(org.fakekoji.jobmanager.model.JDKTestProject)

Aggregations

JDKTestProject (org.fakekoji.jobmanager.model.JDKTestProject)6 Job (org.fakekoji.jobmanager.model.Job)6 JDKTestProjectManager (org.fakekoji.jobmanager.project.JDKTestProjectManager)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Collectors (java.util.stream.Collectors)5 JenkinsCliWrapper (org.fakekoji.jobmanager.JenkinsCliWrapper)5 JDKVersionManager (org.fakekoji.jobmanager.manager.JDKVersionManager)5 PlatformManager (org.fakekoji.jobmanager.manager.PlatformManager)5 TaskVariantManager (org.fakekoji.jobmanager.manager.TaskVariantManager)5 JDKProject (org.fakekoji.jobmanager.model.JDKProject)5 JDKProjectManager (org.fakekoji.jobmanager.project.JDKProjectManager)5 Platform (org.fakekoji.model.Platform)5 StorageException (org.fakekoji.storage.StorageException)5 ApiBuilder.get (io.javalin.apibuilder.ApiBuilder.get)4 EndpointGroup (io.javalin.apibuilder.EndpointGroup)4 FileVisitResult (java.nio.file.FileVisitResult)4 FileVisitor (java.nio.file.FileVisitor)4 Files (java.nio.file.Files)4 Path (java.nio.file.Path)4