标签:ber XML conf nal fileinput exist ase log test
package com.vip.webpagetest.utils; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import static com.jayway.restassured.path.json.JsonPath.with; public class JenkinsUtil { private static final Logger logger = LoggerFactory.getLogger(JenkinsUtil.class); static String jenkinsBaseUrl = ClassPathPropertiesUtils.getProperty("jenkins.server"); static String userName = ClassPathPropertiesUtils.getProperty("jenkins.userName"); static String apiToken = ClassPathPropertiesUtils.getProperty("jenkins.apiToken"); /** * 创建Jenkins Job * @param jobName * @throws Exception */ public static void creatJenkinsJob(String jobName) throws Exception{ if(isJenkinsJobExist(jobName)){ logger.info("已经存在job:"+jobName); }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/createItem?name="+jobName); post.setDoAuthentication(true); Resource resource = new ClassPathResource("config.xml"); InputStream fileInput = resource.getInputStream(); InputStreamRequestEntity requestEntity = new InputStreamRequestEntity(fileInput, "text/xml; charset=UTF-8"); post.setRequestEntity(requestEntity); int code = client.executeMethod(post); logger.info("成功创建job:"+jobName); } } /** * 查询是否存在名为jobName的job * @param jobName * @return * @throws Exception */ public static boolean isJenkinsJobExist(String jobName) throws Exception{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); GetMethod get = new GetMethod(jenkinsBaseUrl+"/api/json"); get.setDoAuthentication(true); client.executeMethod(get); String result = get.getResponseBodyAsString(); List<String> jobList = with(result).getList("jobs.name"); for(String job:jobList){ if(jobName.equals(job)){ return true; } } return false; } /** * 删除Jenkins Job * @param jobName * @throws Exception */ public static void deleteJenkinsJob(String jobName) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/doDelete"); post.setDoAuthentication(true); client.executeMethod(post); } } /** * 构建触发Jenkins Job * @param jobName * @throws Exception */ public static boolean buildJenkinsJob(String jobName) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); return false; }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/build"); post.setDoAuthentication(true); int code = client.executeMethod(post); if(code == 201){ logger.info("已开始构建job:"+jobName); return true; } } return false; } /** * 带参数的构建 * @param jobName * @param paramsJson--{"parameter":{"name":"xxx","value":"xxx"}} * @return * @throws Exception */ public static boolean buildJenkinsJobWithParameters(String jobName,String paramsJson) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); return false; }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/build"); RequestEntity entity = new StringRequestEntity(paramsJson,"application/json","UTF-8"); post.setRequestEntity(entity); post.setRequestHeader("Content-Type","application/json;charset=UTF-8"); client.executeMethod(post); return true; } } /** * 终止Jenkins Job构建 * @param jobName * @return * @throws Exception */ public static boolean stopJenkinsJob(String jobName) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); return false; }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/api/json"); post.setDoAuthentication(true); client.executeMethod(post); String result = post.getResponseBodyAsString(); int buildNumber = with(result).get("lastBuild.number"); PostMethod stopJenkinsRequest = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/"+buildNumber+"/stop"); client.executeMethod(stopJenkinsRequest); return true; } } public static void main(String[] args) throws Exception { creatJenkinsJob("yytest"); } }
一个java创建,删除,构建Jenkins等功能的JenkinsUtil工具类
标签:ber XML conf nal fileinput exist ase log test
原文地址:http://www.cnblogs.com/hfutyezi/p/7588882.html