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 testNodeOfflineCli() throws Exception { DumbSlave s = createSlave(); CLI cli = new CLI(getURL()); try { assertTrue(cli.execute("wait-node-offline", "xxx") != 0); assertTrue(cli.execute("wait-node-online", s.getNodeName()) == 0); s.toComputer().disconnect().get(); assertTrue(cli.execute("wait-node-offline", s.getNodeName()) == 0); } finally { cli.close(); } }
@Test public void testCliFailure() throws Exception { prepareSecurity(); FreeStyleProject srcProject = j.createFreeStyleProject(); srcProject.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", false))); srcProject.save(); WebClient wc = j.createWebClient(); wc.login("test1", "test1"); // GET config.xml of srcProject (userid is set to admin) String configXml = null; { CLI cli = new CLI(j.getURL()); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); int ret = cli.execute( Arrays.asList( "get-job", srcProject.getFullName(), "--username", "test1", "--password", "test1"), new NullInputStream(0), stdout, stderr); assertEquals(stderr.toString(), 0, ret); configXml = stdout.toString(); } // POST config.xml of srcProject (userid is set to admin) to a new project. // This should fail. FreeStyleProject destProject = j.createFreeStyleProject(); destProject.save(); String projectName = destProject.getFullName(); { CLI cli = new CLI(j.getURL()); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); int ret = cli.execute( Arrays.asList( "update-job", destProject.getFullName(), "--username", "test1", "--password", "test1"), new ByteArrayInputStream(configXml.getBytes()), stdout, stderr); assertNotEquals(0, ret); } { FreeStyleProject p = j.jenkins.getItemByFullName(projectName, FreeStyleProject.class); assertNotNull(p); AuthorizeProjectProperty prop = p.getProperty(AuthorizeProjectProperty.class); assertTrue(prop == null || prop.getStrategy() == null); } j.jenkins.reload(); { FreeStyleProject p = j.jenkins.getItemByFullName(projectName, FreeStyleProject.class); assertNotNull(p); AuthorizeProjectProperty prop = p.getProperty(AuthorizeProjectProperty.class); assertTrue(prop == null || prop.getStrategy() == null); } }
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()); } } } } }
public static int _main(String[] _args) throws Exception { List<String> args = Arrays.asList(_args); List<KeyPair> candidateKeys = new ArrayList<KeyPair>(); boolean sshAuthRequestedExplicitly = false; String url = System.getenv("JENKINS_URL"); if (url == null) url = System.getenv("HUDSON_URL"); while (!args.isEmpty()) { String head = args.get(0); if (head.equals("-s") && args.size() >= 2) { url = args.get(1); args = args.subList(2, args.size()); continue; } if (head.equals("-i") && args.size() >= 2) { File f = new File(args.get(1)); if (!f.exists()) { printUsage(Messages.CLI_NoSuchFileExists(f)); return -1; } try { candidateKeys.add(loadKey(f)); } catch (IOException e) { throw new Exception("Failed to load key: " + f, e); } catch (GeneralSecurityException e) { throw new Exception("Failed to load key: " + f, e); } args = args.subList(2, args.size()); sshAuthRequestedExplicitly = true; continue; } break; } if (url == null) { printUsage(Messages.CLI_NoURL()); return -1; } if (args.isEmpty()) args = Arrays.asList("help"); // default to help if (candidateKeys.isEmpty()) addDefaultPrivateKeyLocations(candidateKeys); CLI cli = new CLI(new URL(url)); try { if (!candidateKeys.isEmpty()) { try { // TODO: server verification cli.authenticate(candidateKeys); } catch (IllegalStateException e) { if (sshAuthRequestedExplicitly) { System.err.println("The server doesn't support public key authentication"); return -1; } } catch (UnsupportedOperationException e) { if (sshAuthRequestedExplicitly) { System.err.println("The server doesn't support public key authentication"); return -1; } } catch (GeneralSecurityException e) { if (sshAuthRequestedExplicitly) { System.err.println(e.getMessage()); LOGGER.log(FINE, e.getMessage(), e); return -1; } System.err.println( "Failed to authenticate with your SSH keys. Proceeding with anonymous access"); LOGGER.log( FINE, "Failed to authenticate with your SSH keys. Proceeding with anonymous access", e); } } // execute the command // Arrays.asList is not serializable --- see 6835580 args = new ArrayList<String>(args); return cli.execute(args, System.in, System.out, System.err); } finally { cli.close(); } }