You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/08/10 02:20:17 UTC

[incubator-servicecomb-java-chassis] 03/06: [SCB-793] support deploy serviceCenter: 1.reuse existing instance 2.deploy from environment "serviceCenterHome" 3.deploy from docker

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 1c5d87b21adcc899e569bc9dc9f1447b69ca8e32
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Aug 8 22:18:09 2018 +0800

    [SCB-793] support deploy serviceCenter:
    1.reuse existing instance
    2.deploy from environment "serviceCenterHome"
    3.deploy from docker
---
 .../org/apache/servicecomb/it/ConsumerMain.java    |  6 +-
 .../servicecomb/it/deploy/DeployDefinition.java    | 10 +++
 .../org/apache/servicecomb/it/deploy/Deploys.java  | 13 ++-
 .../apache/servicecomb/it/deploy/NormalDeploy.java |  7 +-
 .../servicecomb/it/deploy/ServiceCenterDeploy.java | 96 ++++++++++++++++++++++
 5 files changed, 127 insertions(+), 5 deletions(-)

diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
index 528f972..a52e787 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
@@ -39,13 +39,17 @@ public class ConsumerMain {
   public static boolean autoExit = true;
 
   public static void main(String[] args) throws Throwable {
+    deploys.init();
+    deploys.getServiceCenter().ensureReady();
+
     BeanUtils.init();
     ITUtils.waitBootFinished();
 
-    deploys.init();
     run();
 
     SCBEngine.getInstance().destroy();
+    deploys.getServiceCenter().stop();
+
     resultPrinter.print();
 
     if (autoExit) {
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/DeployDefinition.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/DeployDefinition.java
index f685e47..99becf4 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/DeployDefinition.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/DeployDefinition.java
@@ -25,6 +25,8 @@ public class DeployDefinition {
 
   protected String startCompleteLog;
 
+  protected String workDir;
+
   /**
    * <pre>
    * edge as the example:
@@ -67,6 +69,14 @@ public class DeployDefinition {
     this.startCompleteLog = startCompleteLog;
   }
 
+  public String getWorkDir() {
+    return workDir;
+  }
+
+  public void setWorkDir(String workDir) {
+    this.workDir = workDir;
+  }
+
   public String getCmd() {
     return cmd;
   }
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/Deploys.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/Deploys.java
index aae0dad..881ae6c 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/Deploys.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/Deploys.java
@@ -32,11 +32,17 @@ public class Deploys {
 
   private String pomVersion;
 
-  public MicroserviceDeploy edge;
+  private ServiceCenterDeploy serviceCenter;
 
-  public MicroserviceDeploy baseProducer;
+  private MicroserviceDeploy edge;
 
-  public MicroserviceDeploy zuul;
+  private MicroserviceDeploy baseProducer;
+
+  private MicroserviceDeploy zuul;
+
+  public ServiceCenterDeploy getServiceCenter() {
+    return serviceCenter;
+  }
 
   public MicroserviceDeploy getEdge() {
     return edge;
@@ -54,6 +60,7 @@ public class Deploys {
     initPomVersion();
     LOGGER.info("test version: {}", pomVersion);
 
+    serviceCenter = new ServiceCenterDeploy();
     initEdge();
     initBaseProducer();
 //    initZuul();
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/NormalDeploy.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/NormalDeploy.java
index 146ea5e..8db1828 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/NormalDeploy.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/NormalDeploy.java
@@ -18,6 +18,7 @@ package org.apache.servicecomb.it.deploy;
 
 
 import java.io.BufferedWriter;
+import java.io.File;
 import java.io.OutputStreamWriter;
 
 import org.apache.commons.io.IOUtils;
@@ -61,7 +62,11 @@ public class NormalDeploy {
   }
 
   protected ProcessBuilder createProcessBuilder(String[] cmds) {
-    return new ProcessBuilder(cmds).redirectErrorStream(true);
+    ProcessBuilder processBuilder = new ProcessBuilder(cmds).redirectErrorStream(true);
+    if (deployDefinition.getWorkDir() != null) {
+      processBuilder.directory(new File(deployDefinition.getWorkDir()));
+    }
+    return processBuilder;
   }
 
   public void waitStartComplete() {
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/ServiceCenterDeploy.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/ServiceCenterDeploy.java
new file mode 100644
index 0000000..9a38dbc
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/deploy/ServiceCenterDeploy.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.it.deploy;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.client.RestTemplate;
+
+import io.swagger.util.Json;
+
+public class ServiceCenterDeploy extends NormalDeploy {
+  private static final Logger LOGGER = LoggerFactory.getLogger(ServiceCenterDeploy.class);
+
+  public ServiceCenterDeploy() {
+    super(new DeployDefinition());
+
+    deployDefinition.setDeployName("serviceCenter");
+    deployDefinition.setDisplayName("serviceCenter");
+    if (SystemUtils.IS_OS_WINDOWS) {
+      deployDefinition.setCmd("service-center.exe");
+    } else {
+      deployDefinition.setCmd("service-center");
+    }
+    deployDefinition.setStartCompleteLog("server is ready");
+  }
+
+  public void ensureReady() throws Throwable {
+    // check if is running
+    // {"version":"1.0.0","buildTag":"20180608145515.1.0.0.b913a2d","runMode":"dev","apiVersion":"3.0.0"}
+    try {
+      String address = "http://localhost:30100/version";
+      ServiceCenterInfo serviceCenterInfo = new RestTemplate().getForObject(address, ServiceCenterInfo.class);
+      if (serviceCenterInfo != null && serviceCenterInfo.getVersion() != null) {
+        LOGGER.info("{} already started, {}.", deployDefinition.getDisplayName(), Json.pretty(serviceCenterInfo));
+        return;
+      }
+    } catch (Throwable e) {
+      LOGGER.info("failed to get ServiceCenter version, message={}", e.getMessage());
+    }
+
+    initServiceCenterCmd();
+    LOGGER.info("definition of {} is: {}", deployDefinition.getDeployName(), deployDefinition);
+
+    deploy();
+    waitStartComplete();
+  }
+
+  protected void initServiceCenterCmd() throws IOException {
+    // where is service center
+    // 1.find from env, for local dev environment
+    LOGGER.info("try to find serviceCenter by env {}.", "serviceCenterHome");
+    String dir = System.getenv("serviceCenterHome");
+    if (dir != null) {
+      LOGGER.info("serviceCenterHome={}.", dir);
+      File file = new File(dir, deployDefinition.getCmd());
+      if (file.exists()) {
+        FileUtils.cleanDirectory(new File(dir, "data"));
+        deployDefinition.setWorkDir(dir);
+        deployDefinition.setCmd(file.getAbsolutePath());
+        return;
+      }
+
+      LOGGER.info("{} is not exist.", file.getAbsolutePath());
+    }
+
+    // 2.docker, for CI environment
+    LOGGER.info("can not find serviceCenter by env {}, try run by docker.", "serviceCenterHome");
+    deployDefinition.setCmd("docker");
+    deployDefinition.setArgs(new String[] {
+        "run",
+        "-p",
+        "127.0.0.1:30100:30100",
+        "servicecomb/service-center"
+    });
+  }
+}