只含有回传结果给trustie的简版

This commit is contained in:
wangwei10061 2017-03-24 09:49:02 +08:00
parent 50d9f9b088
commit 1e4f8deeba
14 changed files with 85 additions and 229 deletions

View File

@ -48,10 +48,10 @@ public class JenkinsApi {
* @param pipeLine pipeline配置文件
* @param params 参数配置用来传projectid,学生id之类的
*/
public void createJob(String jobName, String pipeLine, Map<String, String> params) throws IOException {
public String createJob(String jobName, String pipeLine, Map<String, String> params) throws IOException {
//首先得对pipeline转义
String xml = makeJobXml(pipeLine, params);
jenkins.createJob(jobName, xml);
return jenkins.createJob(jobName, xml);
}

View File

@ -30,21 +30,8 @@ public class TrustieApi {
Map map = new HashMap();
map.put("taskId", params.get("taskId"));
map.put("status", params.get("status"));
String msg = "success";
if (params.getInteger("status")!=0) {
msg = params.getString("msg");
}
try {
// 目前编程类的单元测试用例要求返回测试输出
if (params.containsKey("output")) {
map.put("output", Base64Helper.encode(params.getString("output")));
}
map.put("msg", Base64Helper.encode(msg));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
map.put("msg", params.get("msg"));
map.put("outPut", params.get("outPut"));
logger.debug("url {}, taksId {}", appConfig.getTrustieUrl(), params.get("taskId"));

View File

@ -1,18 +0,0 @@
package cn.guange.app.jenkins.cases;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by weishao on 2017/3/8.
*/
public class HelloWorld {
public String checkInput(String arg) {
if ("H".equals(arg)) {
return "Hello World";
} else {
return "Goodbye";
}
}
}

View File

@ -1,18 +0,0 @@
package cn.guange.app.jenkins.cases;//package cn.guange.app.jenkins.cases;
//
//import org.junit.Test;
//
//import static org.junit.Assert.assertEquals;
//
///**
// * Created by weishao on 2017/3/8.
// */
//public class HelloWorldTest {
// private HelloWorld helloWorld = new HelloWorld();
//
// @Test
// public void testCheckInput() throws Exception {
// assertEquals("Hello World", helloWorld.checkInput("H"));
// assertEquals("Goodbye", helloWorld.checkInput("H222"));
// }
//}

View File

@ -47,10 +47,12 @@ public class JenkinsController {
pipeLine = Base64Helper.decode(pipeLine);
jenkinsApi.createJob(jobName, pipeLine, params);
String result = jenkinsApi.createJob(jobName, pipeLine, params);
logger.debug(result);
}catch (Exception e){
logger.error("createJob", e);
if (e.getMessage().startsWith("A job already exists")){
// 这里后续考虑是不是也替换成status
response.put("code", -2);
response.put("msg", jobName+"already exists");
return response;

View File

@ -7,15 +7,11 @@ import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.lang.reflect.Method;
/**
* Created by guange on 18/02/2017.
*/
@ -34,62 +30,42 @@ public class PipeLineController extends BaseController {
@ApiOperation(value = "调用请求", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@RequestMapping(path = "/call")
public JSONObject call(@RequestParam String taskId,
@RequestParam String className,
@RequestParam String methodName,
@RequestParam String commitType,
@RequestParam String jsonTypeArg) {
logger.debug("/pipeline/call taskId: " + taskId + " className: " + className + " methodName: " + methodName + " jsonTypeArg: " + jsonTypeArg);
logger.debug("/pipeline/call taskId: " + taskId + " commitType: " + commitType + " jsonTypeArg: " + jsonTypeArg);
this.taskId = taskId;
JSONObject result;
JSONObject callResult = new JSONObject();
try {
result = executeCmd(className, methodName, jsonTypeArg);
if(result == null) {
result = new JSONObject();
}
result.put("code", 0);
result.put("msg", "success");
commitResult(commitType, jsonTypeArg);
callResult.put("code", 0);
callResult.put("msg", "call success");
} catch (Exception e) {
e.printStackTrace();
this.response.setStatus(299); //自定义code
result = JSONObject.parseObject(e.getMessage());
result.put("code", -1);
callResult.put("code", -1);
callResult.put("msg", e.getMessage());
}
return result;
return callResult;
}
/**
* 执行pipeline命令
* @param className 处理类名
* @param methodName 处理方法名
* 回传结果到trustie
* @param commitType 回传trustie的类型
* @param jsonTypeArg 方法参数json格式
* @return 处理结果
* @throws Exception
*/
private JSONObject executeCmd(String className, String methodName, String jsonTypeArg) throws Exception {
JSONObject params;
// 将taskId也作为方法的参数
if (jsonTypeArg == null || "null".equals(jsonTypeArg) || "".equals(jsonTypeArg)) {
params = new JSONObject();
} else {
params = JSONObject.parseObject(jsonTypeArg);
}
private void commitResult(String commitType, String jsonTypeArg) throws Exception {
JSONObject params = JSONObject.parseObject(jsonTypeArg);
params.put("taskId", taskId);
// 反射调用对应的处理方法
ApplicationContext apx=new ClassPathXmlApplicationContext("/applicationContext.xml");
Object object = apx.getBean(className.substring(0, 1).toLowerCase() + className.substring(1));
Class clazz = Class.forName("cn.guange.app.jenkins.processes." + className);
Method method = clazz.getMethod(methodName, JSONObject.class);
Object result = null;
try {
result = method.invoke(object, params);
} catch (Exception e) {
throw new Exception(e.getCause().getMessage());
if ("commitFail".equals(commitType)) {
params.put("status", -1);
} else {
params.put("status", 0);
}
return result == null ? null : (JSONObject)result;
trustieApi.commitResultToTrustie(params);
}
}

View File

@ -1,60 +0,0 @@
package cn.guange.app.jenkins.processes;
import cn.guange.app.jenkins.utils.ExecuteShellCommand;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.List;
import java.util.Objects;
/**
* Created by weishao on 2017/3/12.
*/
@Service
public class HelloWorldJavaService {
public JSONObject compileAndRunJUnitCase(JSONObject params) throws Exception {
String folder = params.getString("folder");
File sourceFile = new File(folder, "HelloWorld.java");
if (!sourceFile.exists()){
JSONObject err = new JSONObject();
err.put("msg", "file not exists: HelloWorld.java");
throw new Exception(err.toJSONString());
}
// 运行单元测试
String unitOutput = ExecuteShellCommand.execute("cd " + folder + " && cp ~/cases/HelloWorldTest.java . " + "&& javac HelloWorld.java && " +
"javac -cp " + folder + ":/home/pdl/cases/lib/junit.jar HelloWorldTest.java && java -cp .:/home/pdl/cases/lib/junit.jar org.junit.runner.JUnitCore HelloWorldTest");
if (unitOutput.indexOf("OK") < 0) {
JSONObject err = new JSONObject();
err.put("msg", "unit case not pass, please check your answer");
err.put("output", unitOutput);
throw new Exception(err.toJSONString());
}
// 返回结果表示测试通过
JSONObject result = new JSONObject();
result.put("output", unitOutput);
return result;
}
public JSONObject existsFile(JSONObject params) throws Exception {
String folder = params.getString("folder");
File sourceFile = new File(folder, "HelloWorld.java");
if (!sourceFile.exists()){
JSONObject err = new JSONObject();
err.put("msg", "file not exists: HelloWorld.java");
throw new Exception(err.toJSONString());
}
return null;
}
}

View File

@ -1,44 +0,0 @@
package cn.guange.app.jenkins.processes;
import cn.guange.app.jenkins.api.TrustieApi;
import cn.guange.app.jenkins.utils.Base64Helper;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* Created by guange on 26/02/2017.
*/
@Service
public class ReportService {
private final static Logger logger = LoggerFactory.getLogger(ReportService.class);
@Autowired
TrustieApi trustieApi;
public JSONObject success(JSONObject params) {
// 如果有上一步的msg内容传过来msg不变
if (!params.containsKey("msg")) {
params.put("msg", "success");
}
params.put("status", 0);
trustieApi.commitResultToTrustie(params);
return params;
}
public JSONObject fail(JSONObject params) {
// 如果有上一步的msg内容传过来msg不变
if (!params.containsKey("msg")) {
params.put("msg", "failed");
}
params.put("status", -1);
trustieApi.commitResultToTrustie(params);
return params;
}
}

View File

@ -1,13 +1,15 @@
# trustieUrl=http://www.trustie.net/callback
# trustieUrl=http://ucloudtest.trustie.net/shixuns/training_task_status
trustieUrl=http://192.168.0.238:8888/projects/training_task_status
trustieUrl=http://192.168.0.238:8888/myshixuns/training_task_status
#jenkinsUrl
#jenkinsUrl=http://localhost:8080/
#jenkinsUrl=http://123.59.135.74:9999/jenkins
#jenkisUserName=guange
#jenkisPassWord=123456
jenkinsUrl=http://106.75.33.219:30001
jenkisUserName=root
jenkisPassWord=root
#jenkisUserName=admin
#jenkisPassWord=wang199637
jenkinsUrl=http://192.168.0.238:9999/jenkins
jenkisUserName=guange
jenkisPassWord=123456
#jenkinsUrl=http://106.75.33.219:30001
#jenkisUserName=root
#jenkisPassWord=root

View File

@ -2,6 +2,7 @@ package cn.guange.app.jenkins.api;
import cn.guange.app.jenkins.SpringTestBase;
import cn.guange.app.jenkins.settings.AppConfig;
import cn.guange.app.jenkins.utils.ReadFileUtil;
import com.offbytwo.jenkins.JenkinsServer;
import org.junit.After;
import org.junit.Assert;
@ -34,25 +35,19 @@ public class JenkinsApiTest extends SpringTestBase {
@Test
public void createJob() throws Exception {
String pipeLine = "node()\n" +
"{\n" +
" stage \"first\"\n" +
" def response = httpRequest \"http://localhost:8080/pipeline/call?taskId=${taskId}\"\n" +
" println('Status: '+response.status)\n" +
" println('Response: '+response.content)\n" +
"}";
String pipeLine = ReadFileUtil.readPipeline("src/test/resources/pipeline");
Map params = new HashMap();
params.put("project_id", "1");
params.put("student_id", "2");
params.put("process_id", "3");
params.put("gitUrl", "1");
params.put("step", "1");
params.put("taskId", "1");
jenkinsApi.createJob("tt", pipeLine,params);
jenkinsApi.createJob( "test", pipeLine, params);
}
@Test
public void buildJob() throws Exception {
jenkinsApi.buildJob("jenkins_param_test", "1", "1", "");
jenkinsApi.buildJob("jenkins_param_test", "1", "2", "");
}
@Test
@ -66,8 +61,8 @@ public class JenkinsApiTest extends SpringTestBase {
try {
JenkinsServer jenkinsServer = new JenkinsServer(new URI(appConfig.getJenkinsUrl()),
appConfig.getJenkisUserName(), appConfig.getJenkisPassWord());
if (jenkinsServer.getJob("tt") != null) {
jenkinsServer.deleteJob("tt");
if (jenkinsServer.getJob("test") != null) {
jenkinsServer.deleteJob("test");
}
} catch (Exception e) {
e.printStackTrace();

View File

@ -26,7 +26,7 @@ public class TrustieApiTest {
public void commitResultToTrustie() throws Exception {
JSONObject json = new JSONObject();
json.put("taskId", 1);
json.put("status", 2);
json.put("status", 0);
json.put("msg", "test msg");
json.put("outPut", "test outPut");
trustieApi.commitResultToTrustie(json);

View File

@ -0,0 +1,29 @@
package cn.guange.app.jenkins.utils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by weishao on 2017/3/17.
*/
public class ReadFileUtil {
public static String readPipeline(String pipelinePath) {
StringBuilder result = new StringBuilder("");
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(pipelinePath));
String temp;
while((temp = bufferedReader.readLine()) != null) {
result.append(temp);
result.append("\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
}

View File

@ -1,13 +1,15 @@
# trustieUrl=http://www.trustie.net/callback
# trustieUrl=http://ucloudtest.trustie.net/shixuns/training_task_status
trustieUrl=http://192.168.0.238:8888/projects/training_task_status
trustieUrl=http://192.168.0.238:8888/myshixuns/training_task_status
#jenkinsUrl
#jenkinsUrl=http://localhost:8080/
#jenkinsUrl=http://123.59.135.74:9999/jenkins
#jenkisUserName=guange
#jenkisPassWord=123456
jenkinsUrl=http://106.75.33.219:30001
jenkisUserName=root
jenkisPassWord=root
#jenkisUserName=admin
#jenkisPassWord=wang199637
jenkinsUrl=http://192.168.0.238:9999/jenkins
jenkisUserName=guange
jenkisPassWord=123456
#jenkinsUrl=http://106.75.33.219:30001
#jenkisUserName=root
#jenkisPassWord=root

View File

@ -0,0 +1,3 @@
node(){
}