From 9b458f37319cf7cb7ba65acd08b3271b8fb6a07e Mon Sep 17 00:00:00 2001 From: liuruibin Date: Fri, 1 May 2020 10:00:04 +0800 Subject: [PATCH 01/45] fit2cloud -> metersphere --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6c51bae..c45cedf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ spring.application.name=node-controller -logging.file.path=/opt/fit2cloud/logs/${spring.application.name} +logging.file.path=/opt/metersphere/logs/${spring.application.name} server.port=8082 From 40e5212837e65401165ef1b878f80bcaabfdcd21 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 13 May 2020 11:07:22 +0800 Subject: [PATCH 02/45] get set --- .../metersphere/controller/request/DockerLoginRequest.java | 6 ++++-- .../java/io/metersphere/controller/request/TestRequest.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/metersphere/controller/request/DockerLoginRequest.java b/src/main/java/io/metersphere/controller/request/DockerLoginRequest.java index fa01205..9767401 100644 --- a/src/main/java/io/metersphere/controller/request/DockerLoginRequest.java +++ b/src/main/java/io/metersphere/controller/request/DockerLoginRequest.java @@ -1,8 +1,10 @@ package io.metersphere.controller.request; -import lombok.Data; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter public class DockerLoginRequest { private String username; private String password; diff --git a/src/main/java/io/metersphere/controller/request/TestRequest.java b/src/main/java/io/metersphere/controller/request/TestRequest.java index f790f43..3ec0d3f 100644 --- a/src/main/java/io/metersphere/controller/request/TestRequest.java +++ b/src/main/java/io/metersphere/controller/request/TestRequest.java @@ -1,11 +1,13 @@ package io.metersphere.controller.request; -import lombok.Data; +import lombok.Getter; +import lombok.Setter; import java.util.HashMap; import java.util.Map; -@Data +@Getter +@Setter public class TestRequest extends DockerLoginRequest { private int size; From 81a2574764bffda3dc3a6d3625833b193ecde735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=8C=AF?= Date: Thu, 4 Jun 2020 10:53:14 +0800 Subject: [PATCH 03/45] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Dockerfile b/Dockerfile index 644a0c6..19f885b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,8 @@ FROM registry.fit2cloud.com/fit2cloud2/fabric8-java-alpine-openjdk8-jre MAINTAINER FIT2CLOUD +ARG MS_VERSION=dev + RUN mkdir -p /opt/apps ADD target/node-controller-1.0.jar /opt/apps @@ -10,6 +12,8 @@ ENV JAVA_APP_JAR=/opt/apps/node-controller-1.0.jar ENV AB_OFF=true +ENV MS_VERSION=${MS_VERSION} + ENV JAVA_OPTIONS=-Dfile.encoding=utf-8 CMD ["/deployments/run-java.sh"] From 86507eca3c623401e014b8c2d4de8b2fc369f5ae Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 15 Jun 2020 14:40:44 +0800 Subject: [PATCH 04/45] =?UTF-8?q?=E5=81=9C=E6=AD=A2=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/metersphere/service/JmeterOperateService.java | 2 +- src/main/java/io/metersphere/util/DockerClientService.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/metersphere/service/JmeterOperateService.java b/src/main/java/io/metersphere/service/JmeterOperateService.java index 743a623..e9505fd 100644 --- a/src/main/java/io/metersphere/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/service/JmeterOperateService.java @@ -129,7 +129,7 @@ public void stopContainer(String testId) { .withNameFilter(Collections.singletonList(testId)) .exec(); // container stop - list.forEach(container -> DockerClientService.stopContainer(dockerClient, container.getId())); + list.forEach(container -> DockerClientService.removeContainer(dockerClient, container.getId())); } public List taskStatus(String testId) { diff --git a/src/main/java/io/metersphere/util/DockerClientService.java b/src/main/java/io/metersphere/util/DockerClientService.java index 7222232..c429534 100644 --- a/src/main/java/io/metersphere/util/DockerClientService.java +++ b/src/main/java/io/metersphere/util/DockerClientService.java @@ -83,7 +83,10 @@ public static void stopContainer(DockerClient client, String containerId) { * @param containerId */ public static void removeContainer(DockerClient client, String containerId) { - client.removeContainerCmd(containerId).exec(); + client.removeContainerCmd(containerId) + .withForce(true) + .withRemoveVolumes(true) + .exec(); } } From b11bf2dc298125f5f48dfda069083712289d1446 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Tue, 16 Jun 2020 17:20:49 +0800 Subject: [PATCH 05/45] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E5=89=8D=E6=A3=80=E6=9F=A5=E5=AE=B9=E5=99=A8=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/JmeterOperateService.java | 4 +++- .../metersphere/util/DockerClientService.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/metersphere/service/JmeterOperateService.java b/src/main/java/io/metersphere/service/JmeterOperateService.java index e9505fd..de1e3c5 100644 --- a/src/main/java/io/metersphere/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/service/JmeterOperateService.java @@ -77,7 +77,9 @@ public void onComplete() { try { FileUtils.forceDelete(new File(filePath)); LogUtil.info("Remove dir completed."); - DockerClientService.removeContainer(dockerClient, containerId); + if (DockerClientService.existContainer(dockerClient, containerId) > 0) { + DockerClientService.removeContainer(dockerClient, containerId); + } LogUtil.info("Remove container completed: " + containerId); } catch (IOException e) { LogUtil.error("Remove dir error: ", e); diff --git a/src/main/java/io/metersphere/util/DockerClientService.java b/src/main/java/io/metersphere/util/DockerClientService.java index c429534..79b0449 100644 --- a/src/main/java/io/metersphere/util/DockerClientService.java +++ b/src/main/java/io/metersphere/util/DockerClientService.java @@ -2,6 +2,7 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.CreateContainerResponse; +import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.Info; import com.github.dockerjava.core.DefaultDockerClientConfig; @@ -9,6 +10,9 @@ import io.metersphere.controller.request.DockerLoginRequest; import org.apache.commons.lang.StringUtils; +import java.util.Collections; +import java.util.List; + public class DockerClientService { /** @@ -89,4 +93,17 @@ public static void removeContainer(DockerClient client, String containerId) { .exec(); } + /** + * 容器是否存在 + * @param client + * @param containerId + */ + public static int existContainer(DockerClient client, String containerId) { + List list = client.listContainersCmd() + .withShowAll(true) + .withIdFilter(Collections.singleton(containerId)) + .exec(); + return list.size(); + } + } From 61a06bb02d6a012c763b7910919075d9b621062d Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Wed, 17 Jun 2020 11:01:50 +0800 Subject: [PATCH 06/45] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E5=89=8D=E6=A3=80=E6=9F=A5kafka?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/JmeterOperateService.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/io/metersphere/service/JmeterOperateService.java b/src/main/java/io/metersphere/service/JmeterOperateService.java index de1e3c5..8238282 100644 --- a/src/main/java/io/metersphere/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/service/JmeterOperateService.java @@ -17,6 +17,9 @@ import java.io.File; import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.TimeUnit; @@ -26,6 +29,9 @@ public class JmeterOperateService { public void startContainer(TestRequest testRequest) throws IOException { + String bootstrapServers = testRequest.getEnv().get("BOOTSTRAP_SERVERS"); + checkKafka(bootstrapServers); + LogUtil.info("Receive start container request, test id: {}", testRequest.getTestId()); DockerClient dockerClient = DockerClientService.connectDocker(testRequest); int size = testRequest.getSize(); @@ -90,6 +96,30 @@ public void onComplete() { }); } + private void checkKafka(String bootstrapServers) { + String[] servers = StringUtils.split(bootstrapServers, ","); + try { + for (String s : servers) { + String[] ipAndPort = s.split(":"); + //1,建立tcp + String ip = ipAndPort[0]; + int port = Integer.parseInt(ipAndPort[1]); + Socket soc = new Socket(); + soc.connect(new InetSocketAddress(ip, port), 1000); // 1s timeout + //2.输入内容 + String content = "1010"; + byte[] bs = content.getBytes(); + OutputStream os = soc.getOutputStream(); + os.write(bs); + //3.关闭 + soc.close(); + } + } catch (Exception e) { + LogUtil.error(e); + throw new RuntimeException("Failed to connect to Kafka"); + } + } + private String[] getEnvs(TestRequest testRequest) { Map env = testRequest.getEnv(); return env.keySet().stream().map(k -> k + "=" + env.get(k)).toArray(String[]::new); From f2e20db159710a8c6da3a76d1275fa5d4d315253 Mon Sep 17 00:00:00 2001 From: wangzhen-fit2cloud Date: Wed, 22 Jul 2020 19:41:37 +0800 Subject: [PATCH 07/45] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7=E4=B8=BA=201.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 19f885b..3f39b71 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,9 +6,9 @@ ARG MS_VERSION=dev RUN mkdir -p /opt/apps -ADD target/node-controller-1.0.jar /opt/apps +ADD target/node-controller-1.1.jar /opt/apps -ENV JAVA_APP_JAR=/opt/apps/node-controller-1.0.jar +ENV JAVA_APP_JAR=/opt/apps/node-controller-1.1.jar ENV AB_OFF=true diff --git a/pom.xml b/pom.xml index eaabcaf..a60ed5f 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ io.metersphere node-controller - 1.0 + 1.1 node-controller node-controller From 464747c5a073e7ebc4d41adc71ffe948ad01462e Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 11 Aug 2020 15:38:43 +0800 Subject: [PATCH 08/45] =?UTF-8?q?refactor:=20=E5=8E=BB=E6=8E=89=E4=B8=8D?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/util/DockerClientService.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/metersphere/util/DockerClientService.java b/src/main/java/io/metersphere/util/DockerClientService.java index 79b0449..2b25f78 100644 --- a/src/main/java/io/metersphere/util/DockerClientService.java +++ b/src/main/java/io/metersphere/util/DockerClientService.java @@ -4,7 +4,6 @@ import com.github.dockerjava.api.command.CreateContainerResponse; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.HostConfig; -import com.github.dockerjava.api.model.Info; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientBuilder; import io.metersphere.controller.request.DockerLoginRequest; @@ -21,11 +20,7 @@ public class DockerClientService { * @return */ public static DockerClient connectDocker() { - DockerClient dockerClient = DockerClientBuilder.getInstance().build(); - Info info = dockerClient.infoCmd().exec(); - LogUtil.info("docker的环境信息如下:================="); - LogUtil.info(info); - return dockerClient; + return DockerClientBuilder.getInstance().build(); } public static DockerClient connectDocker(DockerLoginRequest request) { @@ -37,11 +32,7 @@ public static DockerClient connectDocker(DockerLoginRequest request) { .withRegistryUsername(request.getUsername()) .withRegistryPassword(request.getPassword()) .build(); - DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); - Info info = dockerClient.infoCmd().exec(); - LogUtil.info("docker的环境信息如下:================="); - LogUtil.info(info); - return dockerClient; + return DockerClientBuilder.getInstance(config).build(); } /** @@ -95,6 +86,7 @@ public static void removeContainer(DockerClient client, String containerId) { /** * 容器是否存在 + * * @param client * @param containerId */ From 96bba101f670fc7f47fb604ceed0d78b43d1b2e4 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 21 Aug 2020 14:37:19 +0800 Subject: [PATCH 09/45] =?UTF-8?q?refactor:=20=E8=BE=93=E5=87=BA=20jmeter?= =?UTF-8?q?=20console=20=E7=9A=84=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/service/JmeterOperateService.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/io/metersphere/service/JmeterOperateService.java b/src/main/java/io/metersphere/service/JmeterOperateService.java index 8238282..05c08bb 100644 --- a/src/main/java/io/metersphere/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/service/JmeterOperateService.java @@ -94,6 +94,21 @@ public void onComplete() { } }); }); + + containerIdList.forEach(containerId -> { + dockerClient.logContainerCmd(containerId) + .withFollowStream(true) + .withStdOut(true) + .withStdErr(true) + .withTailAll() + .exec(new InvocationBuilder.AsyncResultCallback() { + @Override + public void onNext(Frame item) { + LogUtil.info(new String(item.getPayload()).trim()); + } + }); + + }); } private void checkKafka(String bootstrapServers) { From 644299df6efbc55f604e915b1fab7dd3534db11b Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 31 Aug 2020 12:22:01 +0800 Subject: [PATCH 10/45] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E8=B0=83=E6=95=B4=EF=BC=8C=E6=B7=BB=E5=8A=A0=20jmeter?= =?UTF-8?q?=20HEAP=20=E5=8F=82=E6=95=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/metersphere/Application.java | 13 ------------- .../java/io/metersphere/node/Application.java | 19 +++++++++++++++++++ .../node/config/JmeterProperties.java | 13 +++++++++++++ .../controller/DockerClientController.java | 2 +- .../controller/JmeterOperateController.java | 6 +++--- .../request/DockerLoginRequest.java | 2 +- .../controller/request/TestRequest.java | 2 +- .../service/JmeterOperateService.java | 14 ++++++++++---- .../{ => node}/util/DockerClientService.java | 4 ++-- .../metersphere/{ => node}/util/LogUtil.java | 2 +- src/main/resources/logback.xml | 4 ++-- 11 files changed, 53 insertions(+), 28 deletions(-) delete mode 100644 src/main/java/io/metersphere/Application.java create mode 100644 src/main/java/io/metersphere/node/Application.java create mode 100644 src/main/java/io/metersphere/node/config/JmeterProperties.java rename src/main/java/io/metersphere/{ => node}/controller/DockerClientController.java (86%) rename src/main/java/io/metersphere/{ => node}/controller/JmeterOperateController.java (93%) rename src/main/java/io/metersphere/{ => node}/controller/request/DockerLoginRequest.java (79%) rename src/main/java/io/metersphere/{ => node}/controller/request/TestRequest.java (89%) rename src/main/java/io/metersphere/{ => node}/service/JmeterOperateService.java (95%) rename src/main/java/io/metersphere/{ => node}/util/DockerClientService.java (96%) rename src/main/java/io/metersphere/{ => node}/util/LogUtil.java (99%) diff --git a/src/main/java/io/metersphere/Application.java b/src/main/java/io/metersphere/Application.java deleted file mode 100644 index 010e94c..0000000 --- a/src/main/java/io/metersphere/Application.java +++ /dev/null @@ -1,13 +0,0 @@ -package io.metersphere; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - -} diff --git a/src/main/java/io/metersphere/node/Application.java b/src/main/java/io/metersphere/node/Application.java new file mode 100644 index 0000000..702a592 --- /dev/null +++ b/src/main/java/io/metersphere/node/Application.java @@ -0,0 +1,19 @@ +package io.metersphere.node; + +import io.metersphere.node.config.JmeterProperties; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.PropertySource; + +@EnableConfigurationProperties({ + JmeterProperties.class +}) +@PropertySource(value = {"file:/opt/metersphere/conf/metersphere.properties"}, encoding = "UTF-8", ignoreResourceNotFound = true) +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/main/java/io/metersphere/node/config/JmeterProperties.java b/src/main/java/io/metersphere/node/config/JmeterProperties.java new file mode 100644 index 0000000..38d9e4e --- /dev/null +++ b/src/main/java/io/metersphere/node/config/JmeterProperties.java @@ -0,0 +1,13 @@ +package io.metersphere.node.config; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = JmeterProperties.JMETER_PREFIX) +@Setter +@Getter +public class JmeterProperties { + public static final String JMETER_PREFIX = "jmeter"; + private String heap = "-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m"; +} diff --git a/src/main/java/io/metersphere/controller/DockerClientController.java b/src/main/java/io/metersphere/node/controller/DockerClientController.java similarity index 86% rename from src/main/java/io/metersphere/controller/DockerClientController.java rename to src/main/java/io/metersphere/node/controller/DockerClientController.java index 516f498..7dea07c 100644 --- a/src/main/java/io/metersphere/controller/DockerClientController.java +++ b/src/main/java/io/metersphere/node/controller/DockerClientController.java @@ -1,4 +1,4 @@ -package io.metersphere.controller; +package io.metersphere.node.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/src/main/java/io/metersphere/controller/JmeterOperateController.java b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java similarity index 93% rename from src/main/java/io/metersphere/controller/JmeterOperateController.java rename to src/main/java/io/metersphere/node/controller/JmeterOperateController.java index 7c8d24c..5a89f47 100644 --- a/src/main/java/io/metersphere/controller/JmeterOperateController.java +++ b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java @@ -1,9 +1,9 @@ -package io.metersphere.controller; +package io.metersphere.node.controller; import com.github.dockerjava.api.model.Container; -import io.metersphere.controller.request.TestRequest; -import io.metersphere.service.JmeterOperateService; +import io.metersphere.node.controller.request.TestRequest; +import io.metersphere.node.service.JmeterOperateService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; diff --git a/src/main/java/io/metersphere/controller/request/DockerLoginRequest.java b/src/main/java/io/metersphere/node/controller/request/DockerLoginRequest.java similarity index 79% rename from src/main/java/io/metersphere/controller/request/DockerLoginRequest.java rename to src/main/java/io/metersphere/node/controller/request/DockerLoginRequest.java index 9767401..52c854b 100644 --- a/src/main/java/io/metersphere/controller/request/DockerLoginRequest.java +++ b/src/main/java/io/metersphere/node/controller/request/DockerLoginRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.controller.request; +package io.metersphere.node.controller.request; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/io/metersphere/controller/request/TestRequest.java b/src/main/java/io/metersphere/node/controller/request/TestRequest.java similarity index 89% rename from src/main/java/io/metersphere/controller/request/TestRequest.java rename to src/main/java/io/metersphere/node/controller/request/TestRequest.java index 3ec0d3f..c063c36 100644 --- a/src/main/java/io/metersphere/controller/request/TestRequest.java +++ b/src/main/java/io/metersphere/node/controller/request/TestRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.controller.request; +package io.metersphere.node.controller.request; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/io/metersphere/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java similarity index 95% rename from src/main/java/io/metersphere/service/JmeterOperateService.java rename to src/main/java/io/metersphere/node/service/JmeterOperateService.java index 05c08bb..bba3126 100644 --- a/src/main/java/io/metersphere/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -1,4 +1,4 @@ -package io.metersphere.service; +package io.metersphere.node.service; import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.WaitContainerResultCallback; @@ -7,14 +7,16 @@ import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.Image; import com.github.dockerjava.core.InvocationBuilder; -import io.metersphere.controller.request.TestRequest; -import io.metersphere.util.DockerClientService; -import io.metersphere.util.LogUtil; +import io.metersphere.node.config.JmeterProperties; +import io.metersphere.node.controller.request.TestRequest; +import io.metersphere.node.util.DockerClientService; +import io.metersphere.node.util.LogUtil; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.io.OutputStream; @@ -27,6 +29,8 @@ @Service public class JmeterOperateService { + @Resource + private JmeterProperties jmeterProperties; public void startContainer(TestRequest testRequest) throws IOException { String bootstrapServers = testRequest.getEnv().get("BOOTSTRAP_SERVERS"); @@ -137,6 +141,8 @@ private void checkKafka(String bootstrapServers) { private String[] getEnvs(TestRequest testRequest) { Map env = testRequest.getEnv(); + // HEAP + env.put("HEAP", jmeterProperties.getHeap()); return env.keySet().stream().map(k -> k + "=" + env.get(k)).toArray(String[]::new); } diff --git a/src/main/java/io/metersphere/util/DockerClientService.java b/src/main/java/io/metersphere/node/util/DockerClientService.java similarity index 96% rename from src/main/java/io/metersphere/util/DockerClientService.java rename to src/main/java/io/metersphere/node/util/DockerClientService.java index 2b25f78..ff7f083 100644 --- a/src/main/java/io/metersphere/util/DockerClientService.java +++ b/src/main/java/io/metersphere/node/util/DockerClientService.java @@ -1,4 +1,4 @@ -package io.metersphere.util; +package io.metersphere.node.util; import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.CreateContainerResponse; @@ -6,7 +6,7 @@ import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientBuilder; -import io.metersphere.controller.request.DockerLoginRequest; +import io.metersphere.node.controller.request.DockerLoginRequest; import org.apache.commons.lang.StringUtils; import java.util.Collections; diff --git a/src/main/java/io/metersphere/util/LogUtil.java b/src/main/java/io/metersphere/node/util/LogUtil.java similarity index 99% rename from src/main/java/io/metersphere/util/LogUtil.java rename to src/main/java/io/metersphere/node/util/LogUtil.java index c955137..df77159 100644 --- a/src/main/java/io/metersphere/util/LogUtil.java +++ b/src/main/java/io/metersphere/node/util/LogUtil.java @@ -1,4 +1,4 @@ -package io.metersphere.util; +package io.metersphere.node.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index d5eae80..455a999 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -153,8 +153,8 @@ - - + + \ No newline at end of file From 0610b68d249e92d082c3f20714eb576a1eba3115 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 1 Sep 2020 16:03:35 +0800 Subject: [PATCH 11/45] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E8=B0=83?= =?UTF-8?q?=E6=95=B4=EF=BC=8C=E5=90=AF=E5=8A=A8=E6=B5=8B=E8=AF=95=E6=97=B6?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=AE=B9=E5=99=A8=E6=98=AF=E5=90=A6=E5=AD=98?= =?UTF-8?q?=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../node/controller/request/TestRequest.java | 1 - .../node/service/JmeterOperateService.java | 111 ++++++++++-------- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/main/java/io/metersphere/node/controller/request/TestRequest.java b/src/main/java/io/metersphere/node/controller/request/TestRequest.java index c063c36..19f6ade 100644 --- a/src/main/java/io/metersphere/node/controller/request/TestRequest.java +++ b/src/main/java/io/metersphere/node/controller/request/TestRequest.java @@ -10,7 +10,6 @@ @Setter public class TestRequest extends DockerLoginRequest { - private int size; private String fileString; private String testId; private String image; diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index bba3126..ee8916b 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -23,7 +23,10 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -38,7 +41,6 @@ public void startContainer(TestRequest testRequest) throws IOException { LogUtil.info("Receive start container request, test id: {}", testRequest.getTestId()); DockerClient dockerClient = DockerClientService.connectDocker(testRequest); - int size = testRequest.getSize(); String testId = testRequest.getTestId(); String containerImage = testRequest.getImage(); @@ -59,60 +61,67 @@ public void startContainer(TestRequest testRequest) throws IOException { // 查找镜像 searchImage(dockerClient, testRequest.getImage()); + // 检查容器是否存在 + checkContainerExists(dockerClient, testId); + // 启动测试 + startContainer(testRequest, dockerClient, testId, containerImage, filePath); + } - ArrayList containerIdList = new ArrayList<>(); - for (int i = 0; i < size; i++) { - String containerName = testId + "-" + i; - // 创建 hostConfig - HostConfig hostConfig = HostConfig.newHostConfig(); - String[] envs = getEnvs(testRequest); - String containerId = DockerClientService.createContainers(dockerClient, containerName, containerImage, hostConfig, envs).getId(); - // 从主机复制文件到容器 - dockerClient.copyArchiveToContainerCmd(containerId) - .withHostResource(filePath) - .withDirChildrenOnly(true) - .withRemotePath("/test") - .exec(); - containerIdList.add(containerId); - } + private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage, String filePath) { + // 创建 hostConfig + HostConfig hostConfig = HostConfig.newHostConfig(); + String[] envs = getEnvs(testRequest); + String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId(); + // 从主机复制文件到容器 + dockerClient.copyArchiveToContainerCmd(containerId) + .withHostResource(filePath) + .withDirChildrenOnly(true) + .withRemotePath("/test") + .exec(); - containerIdList.forEach(containerId -> { - DockerClientService.startContainer(dockerClient, containerId); - LogUtil.info("Container create started containerId: " + containerId); - dockerClient.waitContainerCmd(containerId) - .exec(new WaitContainerResultCallback() { - @Override - public void onComplete() { - // 清理文件夹 - try { - FileUtils.forceDelete(new File(filePath)); - LogUtil.info("Remove dir completed."); - if (DockerClientService.existContainer(dockerClient, containerId) > 0) { - DockerClientService.removeContainer(dockerClient, containerId); - } - LogUtil.info("Remove container completed: " + containerId); - } catch (IOException e) { - LogUtil.error("Remove dir error: ", e); + DockerClientService.startContainer(dockerClient, containerId); + LogUtil.info("Container create started containerId: " + containerId); + dockerClient.waitContainerCmd(containerId) + .exec(new WaitContainerResultCallback() { + @Override + public void onComplete() { + // 清理文件夹 + try { + FileUtils.forceDelete(new File(filePath)); + LogUtil.info("Remove dir completed."); + if (DockerClientService.existContainer(dockerClient, containerId) > 0) { + DockerClientService.removeContainer(dockerClient, containerId); } - LogUtil.info("completed...."); + LogUtil.info("Remove container completed: " + containerId); + } catch (IOException e) { + LogUtil.error("Remove dir error: ", e); } - }); - }); - - containerIdList.forEach(containerId -> { - dockerClient.logContainerCmd(containerId) - .withFollowStream(true) - .withStdOut(true) - .withStdErr(true) - .withTailAll() - .exec(new InvocationBuilder.AsyncResultCallback() { - @Override - public void onNext(Frame item) { - LogUtil.info(new String(item.getPayload()).trim()); - } - }); + LogUtil.info("completed...."); + } + }); + + dockerClient.logContainerCmd(containerId) + .withFollowStream(true) + .withStdOut(true) + .withStdErr(true) + .withTailAll() + .exec(new InvocationBuilder.AsyncResultCallback() { + @Override + public void onNext(Frame item) { + LogUtil.info(new String(item.getPayload()).trim()); + } + }); + } - }); + private void checkContainerExists(DockerClient dockerClient, String testId) { + List list = dockerClient.listContainersCmd() + .withShowAll(true) + .withStatusFilter(Arrays.asList("created", "restarting", "running", "paused", "exited")) + .withNameFilter(Collections.singletonList(testId)) + .exec(); + if (!CollectionUtils.isEmpty(list)) { + list.forEach(container -> DockerClientService.removeContainer(dockerClient, container.getId())); + } } private void checkKafka(String bootstrapServers) { From fb4a23620c2a172abc0977020dceaa55fd731dec Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 2 Sep 2020 13:42:43 +0800 Subject: [PATCH 12/45] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0=E4=BE=9D?= =?UTF-8?q?=E8=B5=96jar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index a60ed5f..cd0bbe3 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,10 @@ + + org.springframework.boot + spring-boot-configuration-processor + org.springframework.boot spring-boot-starter-web From ccb0e9a8f82c71af27d9a2ca7b8712a9be0f13d5 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 14 Sep 2020 16:06:43 +0800 Subject: [PATCH 13/45] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=AE=B9=E5=99=A8=E7=9A=84=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +- .../controller/JmeterOperateController.java | 22 +----- .../RestControllerExceptionHandler.java | 18 +++++ .../node/controller/handler/ResultHolder.java | 73 +++++++++++++++++++ .../handler/ResultResponseBodyAdvice.java | 50 +++++++++++++ .../node/service/JmeterOperateService.java | 2 +- 6 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 src/main/java/io/metersphere/node/controller/handler/RestControllerExceptionHandler.java create mode 100644 src/main/java/io/metersphere/node/controller/handler/ResultHolder.java create mode 100644 src/main/java/io/metersphere/node/controller/handler/ResultResponseBodyAdvice.java diff --git a/pom.xml b/pom.xml index cd0bbe3..f746ac3 100644 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,10 @@ org.projectlombok lombok - + + org.apache.commons + commons-lang3 + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/io/metersphere/node/controller/JmeterOperateController.java b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java index 5a89f47..e8f16e1 100644 --- a/src/main/java/io/metersphere/node/controller/JmeterOperateController.java +++ b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java @@ -20,32 +20,18 @@ public class JmeterOperateController { * 初始化测试任务,根据需求启动若干个 JMeter Engine 容器 */ @PostMapping("/container/start") - public void startContainer(@RequestBody TestRequest testRequest) throws IOException { + public String startContainer(@RequestBody TestRequest testRequest) throws IOException { jmeterOperateService.startContainer(testRequest); - } - - // 上传测试相关文件,将请求传过来的脚本、测试数据等文件,拷贝到上述容器中 - @PostMapping("/upload/task") - public void uploadFile(String jmxString) { - // 挂载数据到启动的容器 - // FileUtil.saveFile(jmxString, "/User/liyuhao/test", "ceshi2.jmx"); - } - - // 启动测试任务,控制上述容器执行 jmeter 相关命令开始进行测试 - @PostMapping("/exec/task") - public void taskPerform() { - // 执行 jmx - // docker run -it -v /Users/liyuhao/test:/test justb4/jmeter:latest -n -t /test/ceshi3.jmx - // dockerClient.startContainerCmd("18894c7755b1").exec(); - // dockerClient.execCreateCmd("18894c7755b1").withAttachStdout(true).withCmd("jmeter", "-n", "-t", "/test/*.jmx"); + return "OK"; } /** * 停止指定测试任务,控制上述容器停止指定的 JMeter 测试 */ @GetMapping("container/stop/{testId}") - public void stopContainer(@PathVariable String testId) { + public String stopContainer(@PathVariable String testId) { jmeterOperateService.stopContainer(testId); + return "OK"; } /** diff --git a/src/main/java/io/metersphere/node/controller/handler/RestControllerExceptionHandler.java b/src/main/java/io/metersphere/node/controller/handler/RestControllerExceptionHandler.java new file mode 100644 index 0000000..de54f00 --- /dev/null +++ b/src/main/java/io/metersphere/node/controller/handler/RestControllerExceptionHandler.java @@ -0,0 +1,18 @@ +package io.metersphere.node.controller.handler; + + +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +@RestControllerAdvice +public class RestControllerExceptionHandler { + + @ExceptionHandler(Exception.class) + public ResultHolder msExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) { + return ResultHolder.error(e.getMessage()); + } +} diff --git a/src/main/java/io/metersphere/node/controller/handler/ResultHolder.java b/src/main/java/io/metersphere/node/controller/handler/ResultHolder.java new file mode 100644 index 0000000..e3c1d2a --- /dev/null +++ b/src/main/java/io/metersphere/node/controller/handler/ResultHolder.java @@ -0,0 +1,73 @@ +package io.metersphere.node.controller.handler; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +public class ResultHolder { + public ResultHolder() { + this.success = true; + } + + private ResultHolder(Object data) { + this.data = data; + this.success = true; + } + + private ResultHolder(boolean success, String msg) { + this.success = success; + this.message = msg; + } + + private ResultHolder(boolean success, String msg, Object data) { + this.success = success; + this.message = msg; + this.data = data; + } + + // 请求是否成功 + private boolean success = false; + // 描述信息 + private String message; + // 返回数据 + private Object data = ""; + + public boolean isSuccess() { + return this.success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public static ResultHolder success(Object obj) { + return new ResultHolder(obj); + } + + public static ResultHolder error(String message) { + return new ResultHolder(false, message, null); + } + + public static ResultHolder error(String message, Object object) { + return new ResultHolder(false, message, object); + } + + public String toString() { + return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); + } +} diff --git a/src/main/java/io/metersphere/node/controller/handler/ResultResponseBodyAdvice.java b/src/main/java/io/metersphere/node/controller/handler/ResultResponseBodyAdvice.java new file mode 100644 index 0000000..e784219 --- /dev/null +++ b/src/main/java/io/metersphere/node/controller/handler/ResultResponseBodyAdvice.java @@ -0,0 +1,50 @@ +package io.metersphere.node.controller.handler; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.metersphere.node.util.LogUtil; +import org.springframework.core.MethodParameter; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +/** + * 统一处理返回结果集 + */ +@RestControllerAdvice(value = {"io.metersphere.node"}) +public class ResultResponseBodyAdvice implements ResponseBodyAdvice { + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public boolean supports(MethodParameter methodParameter, Class> converterType) { + return MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType) || StringHttpMessageConverter.class.isAssignableFrom(converterType); + } + + @Override + public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class> converterType, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { + // 处理空值 + if (o == null && StringHttpMessageConverter.class.isAssignableFrom(converterType)) { + return null; + } + + if (!(o instanceof ResultHolder)) { + if (o instanceof String) { + ResultHolder success = ResultHolder.success(o); + try { + return objectMapper.writeValueAsString(success); + } catch (JsonProcessingException e) { + LogUtil.error(e); + return o; + } + } + return ResultHolder.success(o); + } + return o; + } + +} diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index ee8916b..48ef252 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -175,7 +175,7 @@ private void searchImage(DockerClient dockerClient, String imageName) { }).collect(Collectors.toList()); if (collect.size() == 0) { - throw new RuntimeException("Image Not Found."); + throw new RuntimeException("Image Not Found: " + imageName); } } From 6b614cdd2b9846d156f3d8aee67be997dec68925 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Sep 2020 10:32:24 +0800 Subject: [PATCH 14/45] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3f39b71..e779aa1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,9 +6,9 @@ ARG MS_VERSION=dev RUN mkdir -p /opt/apps -ADD target/node-controller-1.1.jar /opt/apps +ADD target/node-controller-1.3.jar /opt/apps -ENV JAVA_APP_JAR=/opt/apps/node-controller-1.1.jar +ENV JAVA_APP_JAR=/opt/apps/node-controller-1.3.jar ENV AB_OFF=true diff --git a/pom.xml b/pom.xml index a60ed5f..23bcc23 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ io.metersphere node-controller - 1.1 + 1.3 node-controller node-controller From 7f41d73e9f9ebbe9d1e9207b2159c5a8abc05eef Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 5 Nov 2020 16:01:24 +0800 Subject: [PATCH 15/45] =?UTF-8?q?feat:=20=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=94=AF=E6=8C=81=E4=B8=8A=E4=BC=A0jar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/node/controller/request/TestRequest.java | 1 + .../metersphere/node/service/JmeterOperateService.java | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/io/metersphere/node/controller/request/TestRequest.java b/src/main/java/io/metersphere/node/controller/request/TestRequest.java index 19f6ade..74c7518 100644 --- a/src/main/java/io/metersphere/node/controller/request/TestRequest.java +++ b/src/main/java/io/metersphere/node/controller/request/TestRequest.java @@ -15,4 +15,5 @@ public class TestRequest extends DockerLoginRequest { private String image; private Map testData = new HashMap<>(); private Map env = new HashMap<>(); + private Map testJars = new HashMap<>(); } diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 48ef252..124a0a2 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -59,6 +59,15 @@ public void startContainer(TestRequest testRequest) throws IOException { } } + // 保存 byte[] jar + Map jarFiles = testRequest.getTestJars(); + if (!CollectionUtils.isEmpty(jarFiles)) { + for (String k : jarFiles.keySet()) { + byte[] v = jarFiles.get(k); + FileUtils.writeByteArrayToFile(new File(filePath + File.separator + k), v); + } + } + // 查找镜像 searchImage(dockerClient, testRequest.getImage()); // 检查容器是否存在 From c9bf63ae90952eac5c9dd043278e14633d85aff0 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 11 Nov 2020 17:57:08 +0800 Subject: [PATCH 16/45] =?UTF-8?q?feat:=20=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BF=9D=E5=AD=98jtl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/JmeterOperateController.java | 17 +++++++++ .../node/controller/request/TestRequest.java | 1 + .../node/service/JmeterOperateService.java | 37 ++++++++++++++++--- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/metersphere/node/controller/JmeterOperateController.java b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java index e8f16e1..3ca6ed1 100644 --- a/src/main/java/io/metersphere/node/controller/JmeterOperateController.java +++ b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java @@ -4,6 +4,9 @@ import com.github.dockerjava.api.model.Container; import io.metersphere.node.controller.request.TestRequest; import io.metersphere.node.service.JmeterOperateService; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -34,6 +37,20 @@ public String stopContainer(@PathVariable String testId) { return "OK"; } + @GetMapping("download/jtl/{reportId}") + public ResponseEntity downloadJtl(@PathVariable String reportId) { + byte[] bytes = jmeterOperateService.downloadJtl(reportId); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + reportId + ".jtl" + "\"") + .body(bytes); + } + + @GetMapping("delete/jtl/{reportId}") + public boolean deleteJtl(@PathVariable String reportId) { + return jmeterOperateService.deleteJtl(reportId); + } + /** * 停止指定测试任务,控制上述容器停止指定的 JMeter 测试 */ diff --git a/src/main/java/io/metersphere/node/controller/request/TestRequest.java b/src/main/java/io/metersphere/node/controller/request/TestRequest.java index 74c7518..eb14292 100644 --- a/src/main/java/io/metersphere/node/controller/request/TestRequest.java +++ b/src/main/java/io/metersphere/node/controller/request/TestRequest.java @@ -12,6 +12,7 @@ public class TestRequest extends DockerLoginRequest { private String fileString; private String testId; + private String reportId; private String image; private Map testData = new HashMap<>(); private Map env = new HashMap<>(); diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 124a0a2..c4e7b3d 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -12,14 +12,13 @@ import io.metersphere.node.util.DockerClientService; import io.metersphere.node.util.LogUtil; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; @@ -34,6 +33,7 @@ public class JmeterOperateService { @Resource private JmeterProperties jmeterProperties; + private static final String rootPath = StringUtils.join(new String[]{"", "opt", "node-data"}, File.separator); public void startContainer(TestRequest testRequest) throws IOException { String bootstrapServers = testRequest.getEnv().get("BOOTSTRAP_SERVERS"); @@ -44,7 +44,7 @@ public void startContainer(TestRequest testRequest) throws IOException { String testId = testRequest.getTestId(); String containerImage = testRequest.getImage(); - String filePath = StringUtils.join(new String[]{"", "opt", "node-data", testId}, File.separator); + String filePath = StringUtils.join(new String[]{rootPath, testId}, File.separator); String fileName = testId + ".jmx"; @@ -96,13 +96,19 @@ private void startContainer(TestRequest testRequest, DockerClient dockerClient, public void onComplete() { // 清理文件夹 try { + String jtlFileName = testRequest.getReportId() + ".jtl"; + InputStream input = dockerClient + .copyArchiveFromContainerCmd(containerId, "/test/" + jtlFileName) + .exec(); + + IOUtils.copyLarge(input, new FileOutputStream(rootPath + File.separator + jtlFileName)); FileUtils.forceDelete(new File(filePath)); LogUtil.info("Remove dir completed."); if (DockerClientService.existContainer(dockerClient, containerId) > 0) { DockerClientService.removeContainer(dockerClient, containerId); } LogUtil.info("Remove container completed: " + containerId); - } catch (IOException e) { + } catch (Exception e) { LogUtil.error("Remove dir error: ", e); } LogUtil.info("completed...."); @@ -244,4 +250,25 @@ public void onNext(Frame item) { } return sb.toString(); } + + public byte[] downloadJtl(String reportId) { + try { + String jtlFileName = reportId + ".jtl"; + return IOUtils.toByteArray(new FileInputStream(rootPath + File.separator + jtlFileName)); + } catch (IOException e) { + LogUtil.error(e); + } + return new byte[0]; + } + + public boolean deleteJtl(String reportId) { + String jtlFileName = reportId + ".jtl"; + try { + FileUtils.forceDelete(new File(rootPath + File.separator + jtlFileName)); + return true; + } catch (IOException e) { + LogUtil.error(e); + } + return false; + } } From d725bae6f9cb8ebea1cfe761cea1d5a50bc8ac8c Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 12 Nov 2020 10:22:17 +0800 Subject: [PATCH 17/45] =?UTF-8?q?feat:=20=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BF=9D=E5=AD=98jtl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../node/service/JmeterOperateService.java | 39 +++++++++---------- .../node/util/DockerClientService.java | 5 ++- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 876c73e..2e0f4df 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ com.github.docker-java docker-java - 3.2.0 + 3.2.5 org.projectlombok diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index c4e7b3d..228c0ec 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -2,10 +2,7 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.WaitContainerResultCallback; -import com.github.dockerjava.api.model.Container; -import com.github.dockerjava.api.model.Frame; -import com.github.dockerjava.api.model.HostConfig; -import com.github.dockerjava.api.model.Image; +import com.github.dockerjava.api.model.*; import com.github.dockerjava.core.InvocationBuilder; import io.metersphere.node.config.JmeterProperties; import io.metersphere.node.controller.request.TestRequest; @@ -18,14 +15,14 @@ import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -77,16 +74,18 @@ public void startContainer(TestRequest testRequest) throws IOException { } private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage, String filePath) { + List volumes = new ArrayList<>(); + List binds = new ArrayList<>(); + final Volume volume = new Volume("/test"); + volumes.add(volume); + final Bind bind = new Bind(filePath, volume); + binds.add(bind); // 创建 hostConfig HostConfig hostConfig = HostConfig.newHostConfig(); + hostConfig.withBinds(binds); + String[] envs = getEnvs(testRequest); - String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId(); - // 从主机复制文件到容器 - dockerClient.copyArchiveToContainerCmd(containerId) - .withHostResource(filePath) - .withDirChildrenOnly(true) - .withRemotePath("/test") - .exec(); + String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, volumes, envs).getId(); DockerClientService.startContainer(dockerClient, containerId); LogUtil.info("Container create started containerId: " + containerId); @@ -97,11 +96,11 @@ public void onComplete() { // 清理文件夹 try { String jtlFileName = testRequest.getReportId() + ".jtl"; - InputStream input = dockerClient - .copyArchiveFromContainerCmd(containerId, "/test/" + jtlFileName) - .exec(); - IOUtils.copyLarge(input, new FileOutputStream(rootPath + File.separator + jtlFileName)); + File srcFile = new File(filePath + File.separator + jtlFileName); + File destDir = new File(rootPath + File.separator + jtlFileName); + + FileUtils.copyFile(srcFile, destDir); FileUtils.forceDelete(new File(filePath)); LogUtil.info("Remove dir completed."); if (DockerClientService.existContainer(dockerClient, containerId) > 0) { diff --git a/src/main/java/io/metersphere/node/util/DockerClientService.java b/src/main/java/io/metersphere/node/util/DockerClientService.java index ff7f083..c977795 100644 --- a/src/main/java/io/metersphere/node/util/DockerClientService.java +++ b/src/main/java/io/metersphere/node/util/DockerClientService.java @@ -4,6 +4,7 @@ import com.github.dockerjava.api.command.CreateContainerResponse; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.HostConfig; +import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientBuilder; import io.metersphere.node.controller.request.DockerLoginRequest; @@ -41,11 +42,13 @@ public static DockerClient connectDocker(DockerLoginRequest request) { * @param client * @return */ - public static CreateContainerResponse createContainers(DockerClient client, String containerName, String imageName, HostConfig hostConfig, String... env) { + public static CreateContainerResponse createContainers(DockerClient client, String containerName, String imageName, + HostConfig hostConfig, List volumes, String... env) { CreateContainerResponse container = client.createContainerCmd(imageName) .withName(containerName) .withHostConfig(hostConfig) .withEnv(env) + .withVolumes(volumes) .exec(); return container; } From a74754ad8aa0f926e1e18c310812f7456f4fa07b Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 12 Nov 2020 10:44:09 +0800 Subject: [PATCH 18/45] =?UTF-8?q?feat:=20=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BF=9D=E5=AD=98jtl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/node/service/JmeterOperateService.java | 2 +- .../java/io/metersphere/node/util/DockerClientService.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 228c0ec..f34cf89 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -85,7 +85,7 @@ private void startContainer(TestRequest testRequest, DockerClient dockerClient, hostConfig.withBinds(binds); String[] envs = getEnvs(testRequest); - String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, volumes, envs).getId(); + String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId(); DockerClientService.startContainer(dockerClient, containerId); LogUtil.info("Container create started containerId: " + containerId); diff --git a/src/main/java/io/metersphere/node/util/DockerClientService.java b/src/main/java/io/metersphere/node/util/DockerClientService.java index c977795..9d0766a 100644 --- a/src/main/java/io/metersphere/node/util/DockerClientService.java +++ b/src/main/java/io/metersphere/node/util/DockerClientService.java @@ -4,7 +4,6 @@ import com.github.dockerjava.api.command.CreateContainerResponse; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.HostConfig; -import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientBuilder; import io.metersphere.node.controller.request.DockerLoginRequest; @@ -43,12 +42,11 @@ public static DockerClient connectDocker(DockerLoginRequest request) { * @return */ public static CreateContainerResponse createContainers(DockerClient client, String containerName, String imageName, - HostConfig hostConfig, List volumes, String... env) { + HostConfig hostConfig, String... env) { CreateContainerResponse container = client.createContainerCmd(imageName) .withName(containerName) .withHostConfig(hostConfig) .withEnv(env) - .withVolumes(volumes) .exec(); return container; } From 77e04657f8a0c503a3585994a61ece13153859a2 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 12 Nov 2020 11:02:17 +0800 Subject: [PATCH 19/45] =?UTF-8?q?refactor:=20=E5=9B=9E=E9=80=80=E5=88=B0?= =?UTF-8?q?=E4=BB=A5=E5=89=8D=E7=9A=84=E5=A4=8D=E5=88=B6=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../node/service/JmeterOperateService.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index f34cf89..c4e7b3d 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -2,7 +2,10 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.WaitContainerResultCallback; -import com.github.dockerjava.api.model.*; +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.api.model.HostConfig; +import com.github.dockerjava.api.model.Image; import com.github.dockerjava.core.InvocationBuilder; import io.metersphere.node.config.JmeterProperties; import io.metersphere.node.controller.request.TestRequest; @@ -15,14 +18,14 @@ import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -74,18 +77,16 @@ public void startContainer(TestRequest testRequest) throws IOException { } private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage, String filePath) { - List volumes = new ArrayList<>(); - List binds = new ArrayList<>(); - final Volume volume = new Volume("/test"); - volumes.add(volume); - final Bind bind = new Bind(filePath, volume); - binds.add(bind); // 创建 hostConfig HostConfig hostConfig = HostConfig.newHostConfig(); - hostConfig.withBinds(binds); - String[] envs = getEnvs(testRequest); String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId(); + // 从主机复制文件到容器 + dockerClient.copyArchiveToContainerCmd(containerId) + .withHostResource(filePath) + .withDirChildrenOnly(true) + .withRemotePath("/test") + .exec(); DockerClientService.startContainer(dockerClient, containerId); LogUtil.info("Container create started containerId: " + containerId); @@ -96,11 +97,11 @@ public void onComplete() { // 清理文件夹 try { String jtlFileName = testRequest.getReportId() + ".jtl"; + InputStream input = dockerClient + .copyArchiveFromContainerCmd(containerId, "/test/" + jtlFileName) + .exec(); - File srcFile = new File(filePath + File.separator + jtlFileName); - File destDir = new File(rootPath + File.separator + jtlFileName); - - FileUtils.copyFile(srcFile, destDir); + IOUtils.copyLarge(input, new FileOutputStream(rootPath + File.separator + jtlFileName)); FileUtils.forceDelete(new File(filePath)); LogUtil.info("Remove dir completed."); if (DockerClientService.existContainer(dockerClient, containerId) > 0) { From 215dc6416c1cb4cce920a73b7d81e7ece40c5cc8 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 12 Nov 2020 12:23:44 +0800 Subject: [PATCH 20/45] =?UTF-8?q?refactor:=20=E9=87=87=E7=94=A8=E6=8C=82?= =?UTF-8?q?=E5=8D=B7=E7=9A=84=E6=96=B9=E5=BC=8F=E5=A4=8D=E5=88=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../node/service/JmeterOperateService.java | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index c4e7b3d..569eed5 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -2,10 +2,7 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.WaitContainerResultCallback; -import com.github.dockerjava.api.model.Container; -import com.github.dockerjava.api.model.Frame; -import com.github.dockerjava.api.model.HostConfig; -import com.github.dockerjava.api.model.Image; +import com.github.dockerjava.api.model.*; import com.github.dockerjava.core.InvocationBuilder; import io.metersphere.node.config.JmeterProperties; import io.metersphere.node.controller.request.TestRequest; @@ -18,7 +15,10 @@ import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; @@ -33,7 +33,15 @@ public class JmeterOperateService { @Resource private JmeterProperties jmeterProperties; - private static final String rootPath = StringUtils.join(new String[]{"", "opt", "node-data"}, File.separator); + private static String ROOT_PATH; + + static { + ROOT_PATH = System.getenv("JMETER_DATA_PATH"); + LogUtil.info("JMETER_DATA_PATH: " + ROOT_PATH); + if (StringUtils.isBlank(ROOT_PATH)) { + ROOT_PATH = "/opt/metersphere/data/jmeter/"; + } + } public void startContainer(TestRequest testRequest) throws IOException { String bootstrapServers = testRequest.getEnv().get("BOOTSTRAP_SERVERS"); @@ -44,7 +52,7 @@ public void startContainer(TestRequest testRequest) throws IOException { String testId = testRequest.getTestId(); String containerImage = testRequest.getImage(); - String filePath = StringUtils.join(new String[]{rootPath, testId}, File.separator); + String filePath = StringUtils.join(new String[]{ROOT_PATH, testId}, File.separator); String fileName = testId + ".jmx"; @@ -79,14 +87,10 @@ public void startContainer(TestRequest testRequest) throws IOException { private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage, String filePath) { // 创建 hostConfig HostConfig hostConfig = HostConfig.newHostConfig(); + hostConfig.withBinds(Bind.parse(filePath + ":/test")); + String[] envs = getEnvs(testRequest); String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId(); - // 从主机复制文件到容器 - dockerClient.copyArchiveToContainerCmd(containerId) - .withHostResource(filePath) - .withDirChildrenOnly(true) - .withRemotePath("/test") - .exec(); DockerClientService.startContainer(dockerClient, containerId); LogUtil.info("Container create started containerId: " + containerId); @@ -97,11 +101,11 @@ public void onComplete() { // 清理文件夹 try { String jtlFileName = testRequest.getReportId() + ".jtl"; - InputStream input = dockerClient - .copyArchiveFromContainerCmd(containerId, "/test/" + jtlFileName) - .exec(); - IOUtils.copyLarge(input, new FileOutputStream(rootPath + File.separator + jtlFileName)); + File srcFile = new File(filePath + File.separator + jtlFileName); + File destDir = new File(ROOT_PATH + File.separator + jtlFileName); + + FileUtils.copyFile(srcFile, destDir); FileUtils.forceDelete(new File(filePath)); LogUtil.info("Remove dir completed."); if (DockerClientService.existContainer(dockerClient, containerId) > 0) { @@ -254,7 +258,7 @@ public void onNext(Frame item) { public byte[] downloadJtl(String reportId) { try { String jtlFileName = reportId + ".jtl"; - return IOUtils.toByteArray(new FileInputStream(rootPath + File.separator + jtlFileName)); + return IOUtils.toByteArray(new FileInputStream(ROOT_PATH + File.separator + jtlFileName)); } catch (IOException e) { LogUtil.error(e); } @@ -264,7 +268,7 @@ public byte[] downloadJtl(String reportId) { public boolean deleteJtl(String reportId) { String jtlFileName = reportId + ".jtl"; try { - FileUtils.forceDelete(new File(rootPath + File.separator + jtlFileName)); + FileUtils.forceDelete(new File(ROOT_PATH + File.separator + jtlFileName)); return true; } catch (IOException e) { LogUtil.error(e); From 3149e9f767a6e343db8c93547cfc9352da8488ad Mon Sep 17 00:00:00 2001 From: BugKing Date: Wed, 25 Nov 2020 11:16:31 +0800 Subject: [PATCH 21/45] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index e779aa1..27f2f7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,9 +6,9 @@ ARG MS_VERSION=dev RUN mkdir -p /opt/apps -ADD target/node-controller-1.3.jar /opt/apps +ADD target/node-controller-1.5.jar /opt/apps -ENV JAVA_APP_JAR=/opt/apps/node-controller-1.3.jar +ENV JAVA_APP_JAR=/opt/apps/node-controller-1.5.jar ENV AB_OFF=true diff --git a/pom.xml b/pom.xml index 2e0f4df..1a08c4f 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ io.metersphere node-controller - 1.3 + 1.5 node-controller node-controller From b4deb946a3cf2e150268b117ffc400fa5b9ea12d Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 26 Nov 2020 11:20:50 +0800 Subject: [PATCH 22/45] =?UTF-8?q?refactor:=20=E6=89=A7=E8=A1=8C=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E5=90=8E=E6=B8=85=E7=90=86=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/node/service/JmeterOperateService.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 569eed5..18c85c2 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -100,12 +100,6 @@ private void startContainer(TestRequest testRequest, DockerClient dockerClient, public void onComplete() { // 清理文件夹 try { - String jtlFileName = testRequest.getReportId() + ".jtl"; - - File srcFile = new File(filePath + File.separator + jtlFileName); - File destDir = new File(ROOT_PATH + File.separator + jtlFileName); - - FileUtils.copyFile(srcFile, destDir); FileUtils.forceDelete(new File(filePath)); LogUtil.info("Remove dir completed."); if (DockerClientService.existContainer(dockerClient, containerId) > 0) { From c668ad0b7807f230cbc5a1e514334c37d244c3c2 Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 3 Dec 2020 19:24:36 +0800 Subject: [PATCH 23/45] =?UTF-8?q?build:=20=E5=A2=9E=E5=8A=A0=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-push.yml | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/build-push.yml diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml new file mode 100644 index 0000000..a292545 --- /dev/null +++ b/.github/workflows/build-push.yml @@ -0,0 +1,58 @@ +name: Build Docker Image and Push + +on: + push: + branches: + - main + - v1* + pull_request: + branches: + - main + - v1* + + workflow_dispatch: + +jobs: + build_push: + runs-on: ubuntu-latest + name: Build Docker Image and Push + steps: + - uses: actions/checkout@v2 + - name: Inject slug/short variables + uses: rlespinasse/github-slug-action@v3.x + - name: Cache node modules + uses: actions/cache@v2 + env: + cache-name: cache-node-modules + with: + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + - name: Cache local Maven repository + uses: actions/cache@v2 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + server-id: github # Value of the distributionManagement/repository/id field of the pom.xml + settings-path: ${{ github.workspace }} # location for the settings.xml file + + - name: Build with Maven + run: mvn -B package --file pom.xml + + - name: Push to Docker Hub + uses: docker/build-push-action@v1 + with: + username: metersphere + password: ${{ secrets.DOCKER_HUB_TOKEN }} + repository: metersphere/node-controller + tag_with_ref: true + build_args: MS_VERSION=${{ env.GITHUB_REF_SLUG }}-${{ env.GITHUB_SHA_SHORT }} From ee139124c156713ac0b45d6b360cb85b91c66f09 Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 3 Dec 2020 19:29:17 +0800 Subject: [PATCH 24/45] =?UTF-8?q?build:=20=E4=BF=AE=E6=94=B9=20Dockerfile?= =?UTF-8?q?=20=E5=9F=BA=E7=A1=80=E9=95=9C=E5=83=8F=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e779aa1..835c858 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM registry.fit2cloud.com/fit2cloud2/fabric8-java-alpine-openjdk8-jre +FROM metersphere/fabric8-java-alpine-openjdk8-jre MAINTAINER FIT2CLOUD From 7c8a97b3da406cecc712bf34da0cf4e73e73464b Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 3 Dec 2020 19:34:24 +0800 Subject: [PATCH 25/45] =?UTF-8?q?build:=20=E6=9B=B4=E6=96=B0=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-push.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml index a292545..57f0698 100644 --- a/.github/workflows/build-push.yml +++ b/.github/workflows/build-push.yml @@ -3,11 +3,11 @@ name: Build Docker Image and Push on: push: branches: - - main + - master - v1* pull_request: branches: - - main + - master - v1* workflow_dispatch: From f8849952f66458529248e4f5799363c5da7832d9 Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 3 Dec 2020 19:24:36 +0800 Subject: [PATCH 26/45] =?UTF-8?q?build:=20=E5=A2=9E=E5=8A=A0=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-push.yml | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/build-push.yml diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml new file mode 100644 index 0000000..a292545 --- /dev/null +++ b/.github/workflows/build-push.yml @@ -0,0 +1,58 @@ +name: Build Docker Image and Push + +on: + push: + branches: + - main + - v1* + pull_request: + branches: + - main + - v1* + + workflow_dispatch: + +jobs: + build_push: + runs-on: ubuntu-latest + name: Build Docker Image and Push + steps: + - uses: actions/checkout@v2 + - name: Inject slug/short variables + uses: rlespinasse/github-slug-action@v3.x + - name: Cache node modules + uses: actions/cache@v2 + env: + cache-name: cache-node-modules + with: + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + - name: Cache local Maven repository + uses: actions/cache@v2 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + server-id: github # Value of the distributionManagement/repository/id field of the pom.xml + settings-path: ${{ github.workspace }} # location for the settings.xml file + + - name: Build with Maven + run: mvn -B package --file pom.xml + + - name: Push to Docker Hub + uses: docker/build-push-action@v1 + with: + username: metersphere + password: ${{ secrets.DOCKER_HUB_TOKEN }} + repository: metersphere/node-controller + tag_with_ref: true + build_args: MS_VERSION=${{ env.GITHUB_REF_SLUG }}-${{ env.GITHUB_SHA_SHORT }} From 0cd2e6cc911fd25c09291a2cd0b9ae04814fcb7c Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 3 Dec 2020 19:29:17 +0800 Subject: [PATCH 27/45] =?UTF-8?q?build:=20=E4=BF=AE=E6=94=B9=20Dockerfile?= =?UTF-8?q?=20=E5=9F=BA=E7=A1=80=E9=95=9C=E5=83=8F=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 27f2f7d..b0e2a1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM registry.fit2cloud.com/fit2cloud2/fabric8-java-alpine-openjdk8-jre +FROM metersphere/fabric8-java-alpine-openjdk8-jre MAINTAINER FIT2CLOUD From d3e0453b9e0893bfa00e6b87f704a16ba4992f95 Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 10 Dec 2020 13:15:02 +0800 Subject: [PATCH 28/45] =?UTF-8?q?build:=20=E6=9B=B4=E6=96=B0=20docker=20?= =?UTF-8?q?=E9=95=9C=E5=83=8F=20tag=20=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-push.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml index 57f0698..2a07cb3 100644 --- a/.github/workflows/build-push.yml +++ b/.github/workflows/build-push.yml @@ -54,5 +54,5 @@ jobs: username: metersphere password: ${{ secrets.DOCKER_HUB_TOKEN }} repository: metersphere/node-controller - tag_with_ref: true + tags: ${{ env.GITHUB_REF_SLUG }} build_args: MS_VERSION=${{ env.GITHUB_REF_SLUG }}-${{ env.GITHUB_SHA_SHORT }} From 5b626e4b98fb96e728b5506990db059a24dc9721 Mon Sep 17 00:00:00 2001 From: BugKing Date: Thu, 17 Dec 2020 12:58:06 +0800 Subject: [PATCH 29/45] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index b0e2a1b..9fd33f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,9 +6,9 @@ ARG MS_VERSION=dev RUN mkdir -p /opt/apps -ADD target/node-controller-1.5.jar /opt/apps +ADD target/node-controller-1.6.jar /opt/apps -ENV JAVA_APP_JAR=/opt/apps/node-controller-1.5.jar +ENV JAVA_APP_JAR=/opt/apps/node-controller-1.6.jar ENV AB_OFF=true diff --git a/pom.xml b/pom.xml index 1a08c4f..5fd1ce9 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ io.metersphere node-controller - 1.5 + 1.6 node-controller node-controller From c6b0ffc51f95f8a08bcb366b7abf41593a7afda4 Mon Sep 17 00:00:00 2001 From: BugKing Date: Fri, 18 Dec 2020 13:47:30 +0800 Subject: [PATCH 30/45] =?UTF-8?q?build:=20=E5=A2=9E=E5=8A=A0=20Jenkinsfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..0daf2cb --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,28 @@ +pipeline { + agent { + node { + label 'master' + } + } + options { quietPeriod(2400) } + parameters { + string(name: 'IMAGE_NAME', defaultValue: 'ms-node-controller', description: '构建后的 Docker 镜像名称') + string(name: 'IMAGE_FREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') + } + stages { + stage('Build/Test') { + steps { + configFileProvider([configFile(fileId: 'metersphere-maven', targetLocation: 'settings.xml')]) { + sh "mvn clean package --settings ./settings.xml" + } + } + } + stage('Docker build & push') { + steps { + sh "docker build --build-arg MS_VERSION=\${TAG_NAME:-\$BRANCH_NAME}-b\${BUILD_NUMBER} -t ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ." + sh "docker tag ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" + sh "docker push ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" + } + } + } +} From 775a9d982f5afa79f662f4b015dd5c2dbff163b1 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 10:45:36 +0800 Subject: [PATCH 31/45] =?UTF-8?q?build:=20Jenkinsfile=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 0daf2cb..aafe65f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,5 +24,12 @@ pipeline { sh "docker push ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" } } + stage('Notification') { + steps { + withCredentials([string(credentialsId: 'wechat-bot-webhook', variable: 'WEBHOOK')]) { + qyWechatNotification failSend: true, mentionedId: '', mentionedMobile: '', webhookUrl: '${WEBHOOK}' + } + } + } } } From c41ee0a96c5d58c91188622101713a447d4f75b1 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 10:46:11 +0800 Subject: [PATCH 32/45] =?UTF-8?q?build:=20Jenkinsfile=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 0daf2cb..aafe65f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,5 +24,12 @@ pipeline { sh "docker push ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" } } + stage('Notification') { + steps { + withCredentials([string(credentialsId: 'wechat-bot-webhook', variable: 'WEBHOOK')]) { + qyWechatNotification failSend: true, mentionedId: '', mentionedMobile: '', webhookUrl: '${WEBHOOK}' + } + } + } } } From 8715a5cc6101f60aa149a86e71667e3ce53e7198 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 14:07:12 +0800 Subject: [PATCH 33/45] =?UTF-8?q?build:=20Jenkinsfile=20=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20post?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index aafe65f..f9e7194 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,11 +24,12 @@ pipeline { sh "docker push ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" } } - stage('Notification') { - steps { - withCredentials([string(credentialsId: 'wechat-bot-webhook', variable: 'WEBHOOK')]) { - qyWechatNotification failSend: true, mentionedId: '', mentionedMobile: '', webhookUrl: '${WEBHOOK}' - } + } + post('Notification') { + always { + sh "echo \$WEBHOOK\n" + withCredentials([string(credentialsId: 'wechat-bot-webhook', variable: 'WEBHOOK')]) { + qyWechatNotification failSend: true, mentionedId: '', mentionedMobile: '', webhookUrl: "$WEBHOOK" } } } From 666b840fce91add04ca6b028eed35e984f3844ee Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 21 Dec 2020 14:17:51 +0800 Subject: [PATCH 34/45] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=B7?= =?UTF-8?q?=E5=80=BC=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/JmeterOperateController.java | 20 +---- .../node/controller/request/TestRequest.java | 6 -- .../node/service/JmeterOperateService.java | 83 +++---------------- 3 files changed, 13 insertions(+), 96 deletions(-) diff --git a/src/main/java/io/metersphere/node/controller/JmeterOperateController.java b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java index 3ca6ed1..00359a0 100644 --- a/src/main/java/io/metersphere/node/controller/JmeterOperateController.java +++ b/src/main/java/io/metersphere/node/controller/JmeterOperateController.java @@ -4,13 +4,9 @@ import com.github.dockerjava.api.model.Container; import io.metersphere.node.controller.request.TestRequest; import io.metersphere.node.service.JmeterOperateService; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import java.io.IOException; import java.util.List; @RestController @@ -23,7 +19,7 @@ public class JmeterOperateController { * 初始化测试任务,根据需求启动若干个 JMeter Engine 容器 */ @PostMapping("/container/start") - public String startContainer(@RequestBody TestRequest testRequest) throws IOException { + public String startContainer(@RequestBody TestRequest testRequest) { jmeterOperateService.startContainer(testRequest); return "OK"; } @@ -37,20 +33,6 @@ public String stopContainer(@PathVariable String testId) { return "OK"; } - @GetMapping("download/jtl/{reportId}") - public ResponseEntity downloadJtl(@PathVariable String reportId) { - byte[] bytes = jmeterOperateService.downloadJtl(reportId); - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + reportId + ".jtl" + "\"") - .body(bytes); - } - - @GetMapping("delete/jtl/{reportId}") - public boolean deleteJtl(@PathVariable String reportId) { - return jmeterOperateService.deleteJtl(reportId); - } - /** * 停止指定测试任务,控制上述容器停止指定的 JMeter 测试 */ diff --git a/src/main/java/io/metersphere/node/controller/request/TestRequest.java b/src/main/java/io/metersphere/node/controller/request/TestRequest.java index eb14292..ccb1729 100644 --- a/src/main/java/io/metersphere/node/controller/request/TestRequest.java +++ b/src/main/java/io/metersphere/node/controller/request/TestRequest.java @@ -9,12 +9,6 @@ @Getter @Setter public class TestRequest extends DockerLoginRequest { - - private String fileString; - private String testId; - private String reportId; private String image; - private Map testData = new HashMap<>(); private Map env = new HashMap<>(); - private Map testJars = new HashMap<>(); } diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 18c85c2..7565e56 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -2,26 +2,23 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.WaitContainerResultCallback; -import com.github.dockerjava.api.model.*; +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.api.model.HostConfig; +import com.github.dockerjava.api.model.Image; import com.github.dockerjava.core.InvocationBuilder; import io.metersphere.node.config.JmeterProperties; import io.metersphere.node.controller.request.TestRequest; import io.metersphere.node.util.DockerClientService; import io.metersphere.node.util.LogUtil; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -33,61 +30,28 @@ public class JmeterOperateService { @Resource private JmeterProperties jmeterProperties; - private static String ROOT_PATH; - static { - ROOT_PATH = System.getenv("JMETER_DATA_PATH"); - LogUtil.info("JMETER_DATA_PATH: " + ROOT_PATH); - if (StringUtils.isBlank(ROOT_PATH)) { - ROOT_PATH = "/opt/metersphere/data/jmeter/"; - } - } - - public void startContainer(TestRequest testRequest) throws IOException { - String bootstrapServers = testRequest.getEnv().get("BOOTSTRAP_SERVERS"); + public void startContainer(TestRequest testRequest) { + Map env = testRequest.getEnv(); + String bootstrapServers = env.get("BOOTSTRAP_SERVERS"); checkKafka(bootstrapServers); - LogUtil.info("Receive start container request, test id: {}", testRequest.getTestId()); + LogUtil.info("Receive start container request, test id: {}", env.get("TEST_ID")); DockerClient dockerClient = DockerClientService.connectDocker(testRequest); - String testId = testRequest.getTestId(); String containerImage = testRequest.getImage(); - String filePath = StringUtils.join(new String[]{ROOT_PATH, testId}, File.separator); - String fileName = testId + ".jmx"; - - - // 每个测试生成一个文件夹 - FileUtils.writeStringToFile(new File(filePath + File.separator + fileName), testRequest.getFileString(), StandardCharsets.UTF_8); - // 保存测试数据文件 - Map testData = testRequest.getTestData(); - if (!CollectionUtils.isEmpty(testData)) { - for (String k : testData.keySet()) { - String v = testData.get(k); - FileUtils.writeStringToFile(new File(filePath + File.separator + k), v, StandardCharsets.UTF_8); - } - } - - // 保存 byte[] jar - Map jarFiles = testRequest.getTestJars(); - if (!CollectionUtils.isEmpty(jarFiles)) { - for (String k : jarFiles.keySet()) { - byte[] v = jarFiles.get(k); - FileUtils.writeByteArrayToFile(new File(filePath + File.separator + k), v); - } - } // 查找镜像 searchImage(dockerClient, testRequest.getImage()); // 检查容器是否存在 - checkContainerExists(dockerClient, testId); + checkContainerExists(dockerClient, env.get("TEST_ID")); // 启动测试 - startContainer(testRequest, dockerClient, testId, containerImage, filePath); + startContainer(testRequest, dockerClient, env.get("TEST_ID"), containerImage); } - private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage, String filePath) { + private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage) { // 创建 hostConfig HostConfig hostConfig = HostConfig.newHostConfig(); - hostConfig.withBinds(Bind.parse(filePath + ":/test")); String[] envs = getEnvs(testRequest); String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId(); @@ -100,14 +64,12 @@ private void startContainer(TestRequest testRequest, DockerClient dockerClient, public void onComplete() { // 清理文件夹 try { - FileUtils.forceDelete(new File(filePath)); - LogUtil.info("Remove dir completed."); if (DockerClientService.existContainer(dockerClient, containerId) > 0) { DockerClientService.removeContainer(dockerClient, containerId); } LogUtil.info("Remove container completed: " + containerId); } catch (Exception e) { - LogUtil.error("Remove dir error: ", e); + LogUtil.error("Remove container error: ", e); } LogUtil.info("completed...."); } @@ -248,25 +210,4 @@ public void onNext(Frame item) { } return sb.toString(); } - - public byte[] downloadJtl(String reportId) { - try { - String jtlFileName = reportId + ".jtl"; - return IOUtils.toByteArray(new FileInputStream(ROOT_PATH + File.separator + jtlFileName)); - } catch (IOException e) { - LogUtil.error(e); - } - return new byte[0]; - } - - public boolean deleteJtl(String reportId) { - String jtlFileName = reportId + ".jtl"; - try { - FileUtils.forceDelete(new File(ROOT_PATH + File.separator + jtlFileName)); - return true; - } catch (IOException e) { - LogUtil.error(e); - } - return false; - } } From 43a2f41f2e6d1a09f2d4b867d008fab3fb15ea73 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 14:33:58 +0800 Subject: [PATCH 35/45] =?UTF-8?q?build:=20=E4=BF=AE=E6=94=B9=E9=9D=99?= =?UTF-8?q?=E9=BB=98=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index f9e7194..7f01aaf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,7 +4,7 @@ pipeline { label 'master' } } - options { quietPeriod(2400) } + options { quietPeriod(1200) } parameters { string(name: 'IMAGE_NAME', defaultValue: 'ms-node-controller', description: '构建后的 Docker 镜像名称') string(name: 'IMAGE_FREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') From 0c79f27002814e491245eab45d86dd4f75dea245 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 21 Dec 2020 15:25:53 +0800 Subject: [PATCH 36/45] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=B7?= =?UTF-8?q?=E5=80=BC=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/metersphere/node/service/JmeterOperateService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 7565e56..8b989f2 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -125,8 +125,6 @@ private void checkKafka(String bootstrapServers) { private String[] getEnvs(TestRequest testRequest) { Map env = testRequest.getEnv(); - // HEAP - env.put("HEAP", jmeterProperties.getHeap()); return env.keySet().stream().map(k -> k + "=" + env.get(k)).toArray(String[]::new); } From 5ec119acee3dcc4ef7b1166e6ba73a8f068d97fe Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 16:52:51 +0800 Subject: [PATCH 37/45] xx --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index b2f6ac8..1d31971 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,7 +1,7 @@ pipeline { agent { node { - label 'master' + label 'metersphere' } } options { quietPeriod(1200) } From 34cc77f80ba3040f59ed6dd3fad8f844766a5eef Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 16:53:52 +0800 Subject: [PATCH 38/45] =?UTF-8?q?build:=20=E4=BF=AE=E6=94=B9=20Jenkinsfile?= =?UTF-8?q?=20=E6=9E=84=E5=BB=BA=E8=8A=82=E7=82=B9=20label?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index b2f6ac8..1d31971 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,7 +1,7 @@ pipeline { agent { node { - label 'master' + label 'metersphere' } } options { quietPeriod(1200) } From 4fc618c21c7d2eaa7b9e2e58599921d3879ce526 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 16:55:18 +0800 Subject: [PATCH 39/45] =?UTF-8?q?build:=20=E4=BF=AE=E6=94=B9=E9=9D=99?= =?UTF-8?q?=E9=BB=98=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 1d31971..755b724 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,7 +4,7 @@ pipeline { label 'metersphere' } } - options { quietPeriod(1200) } + options { quietPeriod(60) } parameters { string(name: 'IMAGE_NAME', defaultValue: 'ms-node-controller', description: '构建后的 Docker 镜像名称') string(name: 'IMAGE_FREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') From 00c2cf250c091e5efb758d4d249941d8056913f7 Mon Sep 17 00:00:00 2001 From: BugKing Date: Mon, 21 Dec 2020 16:56:05 +0800 Subject: [PATCH 40/45] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=9D=99=E9=BB=98?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 755b724..b967dba 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,7 +4,7 @@ pipeline { label 'metersphere' } } - options { quietPeriod(60) } + options { quietPeriod(600) } parameters { string(name: 'IMAGE_NAME', defaultValue: 'ms-node-controller', description: '构建后的 Docker 镜像名称') string(name: 'IMAGE_FREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') From 246c8183a8d72a8ca9b3d4ea28945df273523df5 Mon Sep 17 00:00:00 2001 From: BugKing Date: Wed, 23 Dec 2020 09:59:48 +0800 Subject: [PATCH 41/45] =?UTF-8?q?build:=20=E9=95=9C=E5=83=8F=E5=86=85?= =?UTF-8?q?=E7=9A=84=E7=89=88=E6=9C=AC=E5=8F=B7=E4=BD=BF=E7=94=A8=20commit?= =?UTF-8?q?=20ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index b967dba..fa8deb4 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,7 +7,7 @@ pipeline { options { quietPeriod(600) } parameters { string(name: 'IMAGE_NAME', defaultValue: 'ms-node-controller', description: '构建后的 Docker 镜像名称') - string(name: 'IMAGE_FREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') + string(name: 'IMAGE_PREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') } stages { stage('Build/Test') { @@ -19,9 +19,9 @@ pipeline { } stage('Docker build & push') { steps { - sh "docker build --build-arg MS_VERSION=\${TAG_NAME:-\$BRANCH_NAME}-b\${BUILD_NUMBER} -t ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ." - sh "docker tag ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" - sh "docker push ${IMAGE_FREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" + sh "docker build --build-arg MS_VERSION=\${TAG_NAME:-\$BRANCH_NAME}-\${GIT_COMMIT:0:8} -t ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ." + sh "docker tag ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ${IMAGE_PREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" + sh "docker push ${IMAGE_PREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" } } stage('Notification') { From 740dc32c186d9816e64204a8bef1239ea2ab8cf6 Mon Sep 17 00:00:00 2001 From: BugKing Date: Wed, 6 Jan 2021 13:38:45 +0800 Subject: [PATCH 42/45] =?UTF-8?q?build:=20Jenkinsfile=20=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=20parameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index fa8deb4..3b07ce5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,9 +5,9 @@ pipeline { } } options { quietPeriod(600) } - parameters { - string(name: 'IMAGE_NAME', defaultValue: 'ms-node-controller', description: '构建后的 Docker 镜像名称') - string(name: 'IMAGE_PREFIX', defaultValue: 'registry.cn-qingdao.aliyuncs.com/metersphere', description: '构建后的 Docker 镜像带仓库名的前缀') + environment { + IMAGE_NAME = 'metersphere' + IMAGE_PREFIX = 'registry.cn-qingdao.aliyuncs.com/metersphere' } stages { stage('Build/Test') { From 9e9455447aaf7394c27f36cba83b4d35a2fbd0c9 Mon Sep 17 00:00:00 2001 From: BugKing Date: Wed, 6 Jan 2021 14:01:46 +0800 Subject: [PATCH 43/45] =?UTF-8?q?build:=20=E4=BF=AE=E6=94=B9=20Jenkinsfile?= =?UTF-8?q?=20=E9=95=9C=E5=83=8F=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 3b07ce5..1106dc0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,7 +6,7 @@ pipeline { } options { quietPeriod(600) } environment { - IMAGE_NAME = 'metersphere' + IMAGE_NAME = 'ms-node-controller' IMAGE_PREFIX = 'registry.cn-qingdao.aliyuncs.com/metersphere' } stages { From cda41a930718163c4cd2c5d197b57de0b4c7d7fa Mon Sep 17 00:00:00 2001 From: BugKing Date: Wed, 27 Jan 2021 15:05:58 +0800 Subject: [PATCH 44/45] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9fd33f5..8582b1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,9 +6,9 @@ ARG MS_VERSION=dev RUN mkdir -p /opt/apps -ADD target/node-controller-1.6.jar /opt/apps +ADD target/node-controller-1.7.jar /opt/apps -ENV JAVA_APP_JAR=/opt/apps/node-controller-1.6.jar +ENV JAVA_APP_JAR=/opt/apps/node-controller-1.7.jar ENV AB_OFF=true diff --git a/pom.xml b/pom.xml index 5fd1ce9..4032cd9 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ io.metersphere node-controller - 1.6 + 1.7 node-controller node-controller From 164591f05242a01cd686ee6565f9cf04d76855d2 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 22 Feb 2021 10:10:12 +0800 Subject: [PATCH 45/45] =?UTF-8?q?refactor:=20=E5=8E=BB=E6=8E=89=E4=B8=8D?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/metersphere/node/Application.java | 5 ----- .../metersphere/node/config/JmeterProperties.java | 13 ------------- .../node/service/JmeterOperateService.java | 12 +++++------- 3 files changed, 5 insertions(+), 25 deletions(-) delete mode 100644 src/main/java/io/metersphere/node/config/JmeterProperties.java diff --git a/src/main/java/io/metersphere/node/Application.java b/src/main/java/io/metersphere/node/Application.java index 702a592..35b186f 100644 --- a/src/main/java/io/metersphere/node/Application.java +++ b/src/main/java/io/metersphere/node/Application.java @@ -1,14 +1,9 @@ package io.metersphere.node; -import io.metersphere.node.config.JmeterProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.PropertySource; -@EnableConfigurationProperties({ - JmeterProperties.class -}) @PropertySource(value = {"file:/opt/metersphere/conf/metersphere.properties"}, encoding = "UTF-8", ignoreResourceNotFound = true) @SpringBootApplication public class Application { diff --git a/src/main/java/io/metersphere/node/config/JmeterProperties.java b/src/main/java/io/metersphere/node/config/JmeterProperties.java deleted file mode 100644 index 38d9e4e..0000000 --- a/src/main/java/io/metersphere/node/config/JmeterProperties.java +++ /dev/null @@ -1,13 +0,0 @@ -package io.metersphere.node.config; - -import lombok.Getter; -import lombok.Setter; -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties(prefix = JmeterProperties.JMETER_PREFIX) -@Setter -@Getter -public class JmeterProperties { - public static final String JMETER_PREFIX = "jmeter"; - private String heap = "-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m"; -} diff --git a/src/main/java/io/metersphere/node/service/JmeterOperateService.java b/src/main/java/io/metersphere/node/service/JmeterOperateService.java index 8b989f2..7a0c7c4 100644 --- a/src/main/java/io/metersphere/node/service/JmeterOperateService.java +++ b/src/main/java/io/metersphere/node/service/JmeterOperateService.java @@ -7,7 +7,6 @@ import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.Image; import com.github.dockerjava.core.InvocationBuilder; -import io.metersphere.node.config.JmeterProperties; import io.metersphere.node.controller.request.TestRequest; import io.metersphere.node.util.DockerClientService; import io.metersphere.node.util.LogUtil; @@ -15,7 +14,6 @@ import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import javax.annotation.Resource; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; @@ -28,15 +26,15 @@ @Service public class JmeterOperateService { - @Resource - private JmeterProperties jmeterProperties; public void startContainer(TestRequest testRequest) { Map env = testRequest.getEnv(); + String testId = env.get("TEST_ID"); + LogUtil.info("Receive start container request, test id: {}", testId); String bootstrapServers = env.get("BOOTSTRAP_SERVERS"); + // 检查kafka连通性 checkKafka(bootstrapServers); - LogUtil.info("Receive start container request, test id: {}", env.get("TEST_ID")); DockerClient dockerClient = DockerClientService.connectDocker(testRequest); String containerImage = testRequest.getImage(); @@ -44,9 +42,9 @@ public void startContainer(TestRequest testRequest) { // 查找镜像 searchImage(dockerClient, testRequest.getImage()); // 检查容器是否存在 - checkContainerExists(dockerClient, env.get("TEST_ID")); + checkContainerExists(dockerClient, testId); // 启动测试 - startContainer(testRequest, dockerClient, env.get("TEST_ID"), containerImage); + startContainer(testRequest, dockerClient, testId, containerImage); } private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage) {