private CIJobStatus deleteCI(CIJob job, List<String> builds) throws PhrescoException { S_LOGGER.debug("Entering Method CIManagerImpl.deleteCI(CIJob job)"); S_LOGGER.debug("Job name " + job.getName()); cli = getCLI(job); String deleteType = null; List<String> argList = new ArrayList<String>(); S_LOGGER.debug("job name " + job.getName()); S_LOGGER.debug("Builds " + builds); if (CollectionUtils.isEmpty(builds)) { // delete job S_LOGGER.debug("Job deletion started"); S_LOGGER.debug("Command " + FrameworkConstants.CI_JOB_DELETE_COMMAND); deleteType = DELETE_TYPE_JOB; argList.add(FrameworkConstants.CI_JOB_DELETE_COMMAND); argList.add(job.getName()); } else { // delete Build S_LOGGER.debug("Build deletion started"); deleteType = DELETE_TYPE_BUILD; argList.add(FrameworkConstants.CI_BUILD_DELETE_COMMAND); argList.add(job.getName()); StringBuilder result = new StringBuilder(); for (String string : builds) { result.append(string); result.append(","); } String buildNos = result.substring(0, result.length() - 1); argList.add(buildNos); S_LOGGER.debug("Command " + FrameworkConstants.CI_BUILD_DELETE_COMMAND); S_LOGGER.debug("Build numbers " + buildNos); } try { int status = cli.execute(argList); String message = deleteType + " deletion started in jenkins"; if (status == FrameworkConstants.JOB_STATUS_NOTOK) { deleteType = deleteType.substring(0, 1).toLowerCase() + deleteType.substring(1); message = "Error while deleting " + deleteType + " in jenkins"; } S_LOGGER.debug("Delete CI Status " + status); S_LOGGER.debug("Delete CI Message " + message); return new CIJobStatus(status, message); } finally { if (cli != null) { try { cli.close(); } catch (IOException e) { if (debugEnabled) { S_LOGGER.error( "Entered into catch block of CIManagerImpl.deleteCI(CIJob job) " + e.getLocalizedMessage()); } } catch (InterruptedException e) { if (debugEnabled) { S_LOGGER.error( "Entered into catch block of CIManagerImpl.deleteCI(CIJob job) " + e.getLocalizedMessage()); } } } } }
private CIJobStatus buildJob(CIJob job) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.buildJob(CIJob job)"); } cli = getCLI(job); List<String> argList = new ArrayList<String>(); argList.add(FrameworkConstants.CI_BUILD_JOB_COMMAND); argList.add(job.getName()); try { int status = cli.execute(argList); String message = FrameworkConstants.CI_BUILD_STARTED; if (status == FrameworkConstants.JOB_STATUS_NOTOK) { message = FrameworkConstants.CI_BUILD_STARTING_ERROR; } return new CIJobStatus(status, message); } finally { if (cli != null) { try { cli.close(); } catch (IOException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } catch (InterruptedException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } } } }
public void updateJob(ApplicationInfo appInfo, CIJob job) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug( "Entering Method ProjectAdministratorImpl.updateJob(Project project, CIJob job)"); } FileWriter writer = null; try { CIJobStatus jobStatus = configureJob(job, FrameworkConstants.CI_UPDATE_JOB_COMMAND); if (jobStatus.getCode() == -1) { throw new PhrescoException(jobStatus.getMessage()); } if (debugEnabled) { S_LOGGER.debug("getCustomModules() ProjectInfo = " + appInfo); } updateJsonJob(appInfo, job); } catch (ClientHandlerException ex) { if (debugEnabled) { S_LOGGER.error(ex.getLocalizedMessage()); } throw new PhrescoException(ex); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } } } }
private void setSvnCredential(CIJob job) throws JDOMException, IOException { S_LOGGER.debug("Entering Method CIManagerImpl.setSvnCredential"); try { String jenkinsTemplateDir = Utility.getJenkinsTemplateDir(); String credentialFilePath = jenkinsTemplateDir + job.getRepoType() + HYPHEN + CREDENTIAL_XML; if (debugEnabled) { S_LOGGER.debug("credentialFilePath ... " + credentialFilePath); } File credentialFile = new File(credentialFilePath); SvnProcessor processor = new SvnProcessor(credentialFile); // DataInputStream in = new DataInputStream(new FileInputStream(credentialFile)); // while (in.available() != 0) { // System.out.println(in.readLine()); // } // in.close(); processor.changeNodeValue("credentials/entry//userName", job.getUserName()); processor.changeNodeValue("credentials/entry//password", job.getPassword()); processor.writeStream(new File(Utility.getJenkinsHome() + File.separator + job.getName())); // jenkins home location String jenkinsJobHome = System.getenv(JENKINS_HOME); StringBuilder builder = new StringBuilder(jenkinsJobHome); builder.append(File.separator); processor.writeStream(new File(builder.toString() + CI_CREDENTIAL_XML)); } catch (Exception e) { S_LOGGER.error( "Entered into the catch block of CIManagerImpl.setSvnCredential " + e.getLocalizedMessage()); } }
public int getTotalBuilds(ApplicationInfo appInfo) throws PhrescoException { try { CIJob ciJob = getJob(appInfo); return getTotalBuilds(ciJob); } catch (ClientHandlerException ex) { S_LOGGER.error(ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
// When already existing adapted project is created , need to move to new adapted project private boolean deleteCIJobFile(ApplicationInfo appInfo) throws PhrescoException { S_LOGGER.debug("Entering Method ProjectAdministratorImpl.deleteCI()"); try { File ciJobInfo = new File(getCIJobPath(appInfo)); return ciJobInfo.delete(); } catch (ClientHandlerException ex) { S_LOGGER.error( "Entered into catch block of ProjectAdministratorImpl.deleteCI()" + ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
String getLocalIPAddr() { if (localAddr == null) { try { IPAddr localHost = IPAddr.getLocalHost(); localAddr = localHost.getHostAddress(); } catch (UnknownHostException e) { // shouldn't happen log.error("LockssServlet: getLocalHost: " + e.toString()); return "???"; } } return localAddr; }
public CIJobStatus buildJobs(ApplicationInfo appInfo, List<String> jobsName) throws PhrescoException { try { CIJobStatus jobStatus = null; for (String jobName : jobsName) { CIJob ciJob = getJob(appInfo, jobName); jobStatus = buildJob(ciJob); } return jobStatus; } catch (ClientHandlerException ex) { S_LOGGER.error(ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
private void setMailCredential(CIJob job) { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.setMailCredential"); } try { String jenkinsTemplateDir = Utility.getJenkinsTemplateDir(); String mailFilePath = jenkinsTemplateDir + MAIL + HYPHEN + CREDENTIAL_XML; if (debugEnabled) { S_LOGGER.debug("configFilePath ... " + mailFilePath); } File mailFile = new File(mailFilePath); SvnProcessor processor = new SvnProcessor(mailFile); // DataInputStream in = new DataInputStream(new FileInputStream(mailFile)); // while (in.available() != 0) { // System.out.println(in.readLine()); // } // in.close(); // Mail have to go with jenkins running email address InetAddress ownIP = InetAddress.getLocalHost(); processor.changeNodeValue( CI_HUDSONURL, HTTP_PROTOCOL + PROTOCOL_POSTFIX + ownIP.getHostAddress() + COLON + job.getJenkinsPort() + FORWARD_SLASH + CI + FORWARD_SLASH); processor.changeNodeValue("smtpAuthUsername", job.getSenderEmailId()); processor.changeNodeValue("smtpAuthPassword", job.getSenderEmailPassword()); processor.changeNodeValue("adminAddress", job.getSenderEmailId()); // jenkins home location String jenkinsJobHome = System.getenv(JENKINS_HOME); StringBuilder builder = new StringBuilder(jenkinsJobHome); builder.append(File.separator); processor.writeStream(new File(builder.toString() + CI_MAILER_XML)); } catch (Exception e) { S_LOGGER.error( "Entered into the catch block of CIManagerImpl.setMailCredential " + e.getLocalizedMessage()); } }
private static synchronized String getJavascript() { if (jstext == null) { InputStream istr = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); istr = loader.getResourceAsStream(JAVASCRIPT_RESOURCE); jstext = StringUtil.fromInputStream(istr); istr.close(); } catch (Exception e) { log.error("Can't load javascript", e); } finally { IOUtil.safeClose(istr); } } return jstext; }
public void addRioxxVersionSection(List upload, Item item) throws WingException { String version = item.getMetadata("rioxxterms.version"); if (StringUtils.isNotBlank(version) && !"NA".equals(version)) { try { DCInputsReader a = new DCInputsReader(); java.util.List<String> pairs = a.getPairs("rioxxterms_version"); int humanReadable = pairs.indexOf(version) - 1; version = pairs.get(humanReadable); } catch (DCInputsReaderException e) { log.error(e.getMessage(), e); } upload .addItem("upload-rioxx-version-warning", "upload-rioxx-version-warning") .addContent(T_rioxx_version.parameterize(version)); } }
public CIJobStatus deleteJobs(ApplicationInfo appInfo, List<String> jobNames) throws PhrescoException { S_LOGGER.debug("Entering Method ProjectAdministratorImpl.deleteCI()"); try { CIJobStatus deleteCI = null; for (String jobName : jobNames) { S_LOGGER.debug(" Deleteable job name " + jobName); CIJob ciJob = getJob(appInfo, jobName); // job and build numbers deleteCI = deleteCI(ciJob, null); S_LOGGER.debug("write back json data after job deletion successfull"); deleteJsonJobs(appInfo, Arrays.asList(ciJob)); } return deleteCI; } catch (ClientHandlerException ex) { S_LOGGER.error( "Entered into catch block of ProjectAdministratorImpl.deleteCI()" + ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
public CIJobStatus deleteBuilds(ApplicationInfo appInfo, Map<String, List<String>> builds) throws PhrescoException { S_LOGGER.debug("Entering Method ProjectAdministratorImpl.deleteCI()"); try { CIJobStatus deleteCI = null; Iterator iterator = builds.keySet().iterator(); while (iterator.hasNext()) { String jobName = iterator.next().toString(); List<String> deleteBuilds = builds.get(jobName); S_LOGGER.debug("jobName " + jobName + " builds " + deleteBuilds); CIJob ciJob = getJob(appInfo, jobName); // job and build numbers deleteCI = deleteCI(ciJob, deleteBuilds); } return deleteCI; } catch (ClientHandlerException ex) { S_LOGGER.error( "Entered into catch block of ProjectAdministratorImpl.deleteCI()" + ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
private int getTotalBuilds(CIJob job) throws PhrescoException { try { S_LOGGER.debug("Entering Method CIManagerImpl.getTotalBuilds(CIJob job)"); S_LOGGER.debug("getCIBuilds() JobName = " + job.getName()); JsonArray jsonArray = getBuildsArray(job); Gson gson = new Gson(); CIBuild ciBuild = null; if (jsonArray.size() > 0) { ciBuild = gson.fromJson(jsonArray.get(0), CIBuild.class); String buildUrl = ciBuild.getUrl(); String jenkinsUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); // display the jenkins running url in ci buildUrl = buildUrl.replaceAll("localhost:" + job.getJenkinsPort(), jenkinsUrl); // list String response = getJsonResponse(buildUrl + API_JSON); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(response); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement resultJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT); JsonArray asJsonArray = jsonObject.getAsJsonArray(FrameworkConstants.CI_JOB_BUILD_ARTIFACTS); // when build result is not known if (jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT).toString().equals(STRING_NULL)) { // it indicates the job is in progress and not yet completed return -1; // when build is success and build zip relative path is unknown } else if (resultJson.getAsString().equals(CI_SUCCESS_FLAG) && asJsonArray.size() < 1) { return -1; } else { return jsonArray.size(); } } else { return -1; // When the project is build first time, } } catch (ClientHandlerException ex) { S_LOGGER.error(ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
// return true iff error protected boolean checkParam(boolean ok, String msg) throws IOException { if (ok) return false; log.error(myServletDescr().getPath() + ": " + msg); paramError(); return true; }
/** Common request handling. */ public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resetState(); boolean success = false; HttpSession session = req.getSession(false); try { this.req = req; this.resp = resp; if (log.isDebug()) { logParams(); } resp.setContentType("text/html"); if (!mayPageBeCached()) { resp.setHeader("pragma", "no-cache"); resp.setHeader("Cache-control", "no-cache"); } reqURL = new URL(UrlUtil.getRequestURL(req)); clientAddr = getLocalIPAddr(); // check that current user has permission to run this servlet if (!isServletAllowed(myServletDescr())) { displayWarningInLieuOfPage("You are not authorized to use " + myServletDescr().heading); return; } // check whether servlet is disabled String reason = ServletUtil.servletDisabledReason(myServletDescr().getServletName()); if (reason != null) { displayWarningInLieuOfPage("This function is disabled. " + reason); return; } if (session != null) { session.setAttribute(SESSION_KEY_RUNNING_SERVLET, getHeading()); String reqHost = req.getRemoteHost(); String forw = req.getHeader(HttpFields.__XForwardedFor); if (!StringUtil.isNullString(forw)) { reqHost += " (proxies for " + forw + ")"; } session.setAttribute(SESSION_KEY_REQUEST_HOST, reqHost); } lockssHandleRequest(); success = (errMsg == null); } catch (ServletException e) { log.error("Servlet threw", e); throw e; } catch (IOException e) { log.error("Servlet threw", e); throw e; } catch (RuntimeException e) { log.error("Servlet threw", e); throw e; } finally { if (session != null) { session.setAttribute(SESSION_KEY_RUNNING_SERVLET, null); session.setAttribute(LockssFormAuthenticator.__J_AUTH_ACTIVITY, TimeBase.nowMs()); } if ("please".equalsIgnoreCase(req.getHeader("X-Lockss-Result"))) { log.debug3("X-Lockss-Result: " + (success ? "Ok" : "Fail")); resp.setHeader("X-Lockss-Result", success ? "Ok" : "Fail"); } resetMyLocals(); resetLocals(); } }
private CIJobStatus configureJob(CIJob job, String jobType) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.createJob(CIJob job)"); } try { cli = getCLI(job); List<String> argList = new ArrayList<String>(); argList.add(jobType); argList.add(job.getName()); String jenkinsTemplateDir = Utility.getJenkinsTemplateDir(); String configFilePath = jenkinsTemplateDir + job.getRepoType() + HYPHEN + CONFIG_XML; if (debugEnabled) { S_LOGGER.debug("configFilePath ... " + configFilePath); } File configFile = new File(configFilePath); ConfigProcessor processor = new ConfigProcessor(configFile); customizeNodes(processor, job); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (debugEnabled) { S_LOGGER.debug("argList " + argList.toString()); } int result = cli.execute(argList, processor.getConfigAsStream(), System.out, baos); String message = "Job created successfully"; if (result == -1) { byte[] byteArray = baos.toByteArray(); message = new String(byteArray); } if (debugEnabled) { S_LOGGER.debug("message " + message); } // when svn is selected credential value has to set if (SVN.equals(job.getRepoType())) { setSvnCredential(job); } setMailCredential(job); return new CIJobStatus(result, message); } catch (IOException e) { throw new PhrescoException(e); } catch (JDOMException e) { throw new PhrescoException(e); } finally { if (cli != null) { try { cli.close(); } catch (IOException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } catch (InterruptedException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } } } }