From 9eb1102a66fdddc85e39c47482d03c628606e948 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Thu, 26 May 2022 15:33:04 +0800 Subject: [PATCH] add simple-ypk-packer --- README.md | 9 +- build.gradle | 24 +++- docker/Dockerfile | 15 +++ docker/buildDocker.sh | 25 ++++ docker/docker-compose/deployentry.json | 14 ++ docker/docker-compose/docker-compose.yml | 9 ++ docker/docker-compose/dockerun.sh | 2 + docker/docker-compose/input/commonvar.json | 6 + .../input/example.json.template | 18 +++ docker/docker-compose/input/examplefiles.json | 46 +++++++ docker/javarun.sh | 2 + docker/prepareVersion.sh | 15 +++ .../bdware/ypkdeploy/DeployConfGenerator.java | 74 +++++++++++ src/main/java/org/bdware/ypkdeploy/Entry.java | 57 ++++++++ .../java/org/bdware/ypkdeploy/HTTPTool.java | 124 ++++++++++++------ .../ypkdeploy/SmartContractClientExt.java | 17 +++ src/test/java/EntryTest.java | 30 +++++ testinput/deploy/auditproxy.json.template | 18 +++ testinput/deploy/auditproxyfiles.json | 46 +++++++ testinput/deploy/commonvar.json | 25 ++++ testinput/deploy/controlproxy.json.template | 16 +++ testinput/deploy/controlproxyfiles.json | 38 ++++++ testinput/deploy/router.json.template | 19 +++ testinput/deploy/router/GlobalRouter-023.json | 19 +++ testinput/deploy/routerfiles.json | 76 +++++++++++ testinput/deployentry.json | 28 ++++ testinput/deployrouter.json | 7 + testinput/generateAllConf.json | 27 ++++ 28 files changed, 756 insertions(+), 50 deletions(-) create mode 100644 docker/Dockerfile create mode 100755 docker/buildDocker.sh create mode 100644 docker/docker-compose/deployentry.json create mode 100644 docker/docker-compose/docker-compose.yml create mode 100644 docker/docker-compose/dockerun.sh create mode 100644 docker/docker-compose/input/commonvar.json create mode 100644 docker/docker-compose/input/example.json.template create mode 100644 docker/docker-compose/input/examplefiles.json create mode 100644 docker/javarun.sh create mode 100755 docker/prepareVersion.sh create mode 100644 src/main/java/org/bdware/ypkdeploy/DeployConfGenerator.java create mode 100644 src/main/java/org/bdware/ypkdeploy/Entry.java create mode 100644 src/test/java/EntryTest.java create mode 100644 testinput/deploy/auditproxy.json.template create mode 100644 testinput/deploy/auditproxyfiles.json create mode 100644 testinput/deploy/commonvar.json create mode 100644 testinput/deploy/controlproxy.json.template create mode 100644 testinput/deploy/controlproxyfiles.json create mode 100644 testinput/deploy/router.json.template create mode 100644 testinput/deploy/router/GlobalRouter-023.json create mode 100644 testinput/deploy/routerfiles.json create mode 100644 testinput/deployentry.json create mode 100644 testinput/deployrouter.json create mode 100644 testinput/generateAllConf.json diff --git a/README.md b/README.md index 5a62970..dc42dc1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ mavenCentral() } dependencies { - classpath "org.bdware.bdcontract:ypk-deploy-tool:0.4.0" + classpath "org.bdware.bdcontract:ypk-deploy-tool:0.5.0" } } //.... @@ -20,9 +20,7 @@ task deploy(dependsOn: ["xxx"]) { 2) 配置`./xxx/deployconfig.json`文件。 参数说明: -`host`为运行了bdagent的服务端的ip - -`agentPort`为端口。 +`agentAddress`为运行了bdagent的服务端的ip:port `privateKey/publicKey`为有部署权限的一组SM2KeyPair @@ -31,8 +29,7 @@ task deploy(dependsOn: ["xxx"]) { `deployconfig.json`配置示例: ```json { - "host": "192.168.x.x", - "agentPort": 18000, + "agentAddress": "192.168.x.x:18000", "privateKey": "5895c18430dd...", "publicKey": "04d1924329f72ced14...", "ypkPath": "/path/to/todeploy.ypk", diff --git a/build.gradle b/build.gradle index 2db7d53..951c75e 100755 --- a/build.gradle +++ b/build.gradle @@ -4,10 +4,9 @@ plugins { id 'maven-publish' id 'signing' } -group 'org.bdware.bdcontract' -version '0.4.0' +group "org.bdware.bdcontract" +version "0.5.2" sourceCompatibility = 1.8 - repositories { mavenCentral() mavenLocal() @@ -24,6 +23,25 @@ dependencies { testImplementation 'junit:junit:4.13.2' } +task copyLibs(type: Copy, dependsOn: [':ypk-deploy-tool:jar']) { + from configurations.runtimeClasspath + into "./build/output/libs/" +} +task copyScript(type: Copy) { + from "./docker/javarun.sh" + from "./docker/docker-compose/deployentry.json" + into "./build/output/" +} +task copyJar(type: Copy, dependsOn: [":ypk-deploy-tool:copyLibs",":ypk-deploy-tool:copyScript"]) { + from "./build/libs/$project.name-${project.version}.jar" + into "./build/output" + rename { String fileName -> "${project.name}.jar" } + doFirst { + println "copyJar start" + } +} + + task classJar(type: Jar, dependsOn: classes) { classifier = "jar" } diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..83525c6 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,15 @@ +FROM openjdk:8 + +ARG GOPRIVATE=bdware.org/* +ARG GOPROXY=https://goproxy.cn + +LABEL maintainer="caihuaqian@internetapi.cn" +LABEL org.bdware.version="0.5.2" +LABEL org.bdware.version.isproduction="true" +LABEL org.bdware.release-date="2022-05-25" + +COPY ./output /ypk-deploy-tool +WORKDIR /ypk-deploy-tool +VOLUME /ypk-deploy-tool/input +ENTRYPOINT ["java"] +CMD ["-Dfile.encoding=UTF-8", "-cp", "./libs/*:ypk-deploy-tool.jar", "org.bdware.ypkdeploy.Entry"] diff --git a/docker/buildDocker.sh b/docker/buildDocker.sh new file mode 100755 index 0000000..a4cfe54 --- /dev/null +++ b/docker/buildDocker.sh @@ -0,0 +1,25 @@ +#!/bin/bash +if [ $# -gt 2 ]; then + echo "too many arguments,\nusage: \$version [-,push,save] \$saveDir" + exit 1 +fi +version=$(awk '/version "[0-9]/ { sub(/[^"]*"/,"") ; sub(/".*$/,""); print $0 } ' build.gradle) +cp ./docker/Dockerfile ./build/Dockerfile +cd ./build +DOCKER_CLI_EXPERIMENTAL=enabled + + +if [ "$1" == "push" ]; then + echo "push to docker hub" + docker buildx build --platform linux/arm64/v8,linux/amd64 -t bdware/ypk-deploy-tool:$version ./ --push +elif [ "$1" == "save" ]; then + echo "save to $2" + docker buildx build --platform linux/arm64/v8 -t bdware/ypk-deploy-tool:arm_$version ./ --load + docker save -o $2/ypk-deploy-tool-arm_$version.tar bdware/ypk-deploy-tool:arm_$version + docker buildx build --platform linux/amd64 -t bdware/ypk-deploy-tool:amd64_$version ./ --load + docker save -o $2/ypk-deploy-tool-amd64_$version.tar bdware/ypk-deploy-tool:amd64_$version +else + echo "create at local" + docker build -t bdware/ypk-deploy-tool:$version ./ + docker tag bdware/ypk-deploy-tool:$version bdware/ypk-deploy-tool:latest +fi \ No newline at end of file diff --git a/docker/docker-compose/deployentry.json b/docker/docker-compose/deployentry.json new file mode 100644 index 0000000..fd134dd --- /dev/null +++ b/docker/docker-compose/deployentry.json @@ -0,0 +1,14 @@ +{ + "generateTasks": [ + { + "isBatch": true, + "dir": "./input/deploy", + "template": "example.json.template", + "commvar": "commonvar.json", + "files": "examplefiles.json" + } + ], + "deployTasks": [ + "./input/deploy/controlproxy/" + ] +} \ No newline at end of file diff --git a/docker/docker-compose/docker-compose.yml b/docker/docker-compose/docker-compose.yml new file mode 100644 index 0000000..62bf6ed --- /dev/null +++ b/docker/docker-compose/docker-compose.yml @@ -0,0 +1,9 @@ +version: "3" + +services: + ypk-deploy-tool: + image: bdware/ypk-deploy-tool:0.5.2 + command: "-Dfile.encoding=UTF-8 -cp ./libs/*:ypk-deploy-tool.jar org.bdware.ypkdeploy.Entry ./deployentry.json" + volumes: + - ./input:/ypk-deploy-tool/input + - ./deployentry.json:/ypk-deploy-tool/deployentry.json \ No newline at end of file diff --git a/docker/docker-compose/dockerun.sh b/docker/docker-compose/dockerun.sh new file mode 100644 index 0000000..6c3656c --- /dev/null +++ b/docker/docker-compose/dockerun.sh @@ -0,0 +1,2 @@ +#! /bin/bash +docker run -v `pwd`/deployentry.json:/ypk-deploy-tool/deployentry.json bdware/ypk-deploy-tool:0.5.2 diff --git a/docker/docker-compose/input/commonvar.json b/docker/docker-compose/input/commonvar.json new file mode 100644 index 0000000..e5bcc60 --- /dev/null +++ b/docker/docker-compose/input/commonvar.json @@ -0,0 +1,6 @@ +{ + "_YPK_": "./input/ypk/Example-1.0.6.ypk", + "___山西___": "", + "_IP0_": "39.104.201.40", + "_IP1_": "39.104.208.148" +} \ No newline at end of file diff --git a/docker/docker-compose/input/example.json.template b/docker/docker-compose/input/example.json.template new file mode 100644 index 0000000..c793fe0 --- /dev/null +++ b/docker/docker-compose/input/example.json.template @@ -0,0 +1,18 @@ +{ + "agentAddress": "_HOST_:18010", + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "ypkPath": "_AUDIT_PROXY_YPK_", + "killBeforeStart": "AutoAudit", + "createParam": { + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "repoName": "AuditProxy", + "auditURI": "tcp://_HOST_:18051", + "grafanaUrl": "jdbc:postgresql://iodlog.demo.internetapi.cn:5432/iodlog?useServerPrepStmts=true", + "grafanaUserName": "iodlog", + "grafanaPassword": "iodlog1107", + "grafanaServicePort": 18055, + "aceiServicePort": 18053 + } +} \ No newline at end of file diff --git a/docker/docker-compose/input/examplefiles.json b/docker/docker-compose/input/examplefiles.json new file mode 100644 index 0000000..ddb14ba --- /dev/null +++ b/docker/docker-compose/input/examplefiles.json @@ -0,0 +1,46 @@ +[ + { + "fileName": "./auditproxy/AuditProxy_shanxi.json", + "_HOST_": "_IP0_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_ty.json", + "_HOST_": "_IP1_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_dt.json", + "_HOST_": "_IP2_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_ty_xd.json", + "_HOST_": "_IP3_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_ty_yz.json", + "_HOST_": "_IP4_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_dt_yg.json", + "_HOST_": "_IP5_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_dt_yz.json", + "_HOST_": "_IP6_" + }, + { + "fileName": "./auditproxy/AuditProxy_njust.json", + "_HOST_": "_IP7_" + }, + { + "fileName": "./auditproxy/AuditProxy_bit.json", + "_HOST_": "_IP8_" + }, + { + "fileName": "./auditproxy/AuditProxy_mpi.json", + "_HOST_": "_IP9_" + }, + { + "fileName": "./auditproxy/AuditProxy_weixing.json", + "_HOST_": "_IP12_" + } +] \ No newline at end of file diff --git a/docker/javarun.sh b/docker/javarun.sh new file mode 100644 index 0000000..71babbf --- /dev/null +++ b/docker/javarun.sh @@ -0,0 +1,2 @@ +#! /bin/bash +java -Dfile.encoding=UTF-8 -cp ./libs/*:ypk-deploy-tool.jar org.bdware.ypkdeploy.Entry ./deployentry.json \ No newline at end of file diff --git a/docker/prepareVersion.sh b/docker/prepareVersion.sh new file mode 100755 index 0000000..69f7f20 --- /dev/null +++ b/docker/prepareVersion.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +version=$(awk '/version "[0-9]/ { sub(/[^"]*"/,"") ; sub(/".*$/,""); print $0 } ' build.gradle) +echo version extract from build.gradle '-->' $version +if [ $# -gt 0 ]; then + time=$1 +else + time=$(date "+%Y-%m-%d") +fi +echo time is $time + +cd ./docker/ +awk ' {gsub(/version=\".*\"/,"version=\"'$version'\""); print $0; } ' Dockerfile >Dockerfile.2 +awk ' {gsub(/release-date=\".*\"/,"release-date=\"'$time'\""); print $0; } ' Dockerfile.2 >Dockerfile +rm Dockerfile.2 \ No newline at end of file diff --git a/src/main/java/org/bdware/ypkdeploy/DeployConfGenerator.java b/src/main/java/org/bdware/ypkdeploy/DeployConfGenerator.java new file mode 100644 index 0000000..0c3d0f9 --- /dev/null +++ b/src/main/java/org/bdware/ypkdeploy/DeployConfGenerator.java @@ -0,0 +1,74 @@ +package org.bdware.ypkdeploy; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class DeployConfGenerator { + final static String TAG = "[DeployConfGenerator] "; + + public static String getFileContent(String path) { + try { + File file = new File(path); + long fileLen = file.length(); + byte[] fileContent = new byte[(int) fileLen]; + FileInputStream in = new FileInputStream(file); + in.read(fileContent); + in.close(); + return new String(fileContent, StandardCharsets.UTF_8); + } catch (IOException e) { + } + return null; + } + + public static void generateDeployConf(String dir, String template, String commonvar, String fileName) { + String templateStr = getFileContent(dir + template); + try { + JsonObject commVar = JsonParser.parseReader(new FileReader(new File(dir, commonvar))).getAsJsonObject(); + String content = templateStr; + File output = new File(dir, fileName); + if (!output.getParentFile().exists()) + output.getParentFile().mkdirs(); + FileOutputStream fout = new FileOutputStream(output); + for (String key : commVar.keySet()) { + content = content.replaceAll(key, commVar.get(key).getAsString()); + } + fout.write(content.getBytes(StandardCharsets.UTF_8)); + fout.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void batchGenerateDeployConf(String dir, String template, String commonvar, String filesStr) { + String templateStr = getFileContent(new File(dir, template).getAbsolutePath()); + try { + JsonObject commVar = JsonParser.parseReader(new FileReader(new File(dir, commonvar))).getAsJsonObject(); + JsonArray files = JsonParser.parseReader(new FileReader(new File(dir, filesStr))).getAsJsonArray(); + for (JsonElement je : files) { + JsonObject file = je.getAsJsonObject(); + File output = new File(dir, file.get("fileName").getAsString()); + if (!output.getParentFile().exists()) + output.getParentFile().mkdirs(); + FileOutputStream fout = new FileOutputStream(output); + String content = templateStr; + for (String key : file.keySet()) { + if (!key.startsWith("_")) continue; + content = content.replaceAll(key, file.get(key).getAsString()); + } + for (String key : commVar.keySet()) { + if (file.has(key)) continue; + content = content.replaceAll(key, commVar.get(key).getAsString()); + } + fout.write(content.getBytes(StandardCharsets.UTF_8)); + fout.close(); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/bdware/ypkdeploy/Entry.java b/src/main/java/org/bdware/ypkdeploy/Entry.java new file mode 100644 index 0000000..90ced56 --- /dev/null +++ b/src/main/java/org/bdware/ypkdeploy/Entry.java @@ -0,0 +1,57 @@ +package org.bdware.ypkdeploy; + +import com.google.gson.Gson; + +import java.util.List; + +public class Entry { + static class DeployEntry { + List generateTasks; + List deployTasks; + } + + static class GenerateTask { + public String dir; + public String template; + public String commvar; + public String files; + public boolean isBatch; + } + + public static void goByStr(String str) { + DeployEntry entry = new Gson().fromJson(str, DeployEntry.class); + generateJson(entry.generateTasks); + deployYpk(entry.deployTasks); + } + + private static void deployYpk(List deployTasks) { + if (deployTasks == null) return; + for (String deployTask : deployTasks) { + HTTPTool.deploy(deployTask); + } + } + + private static void generateJson(List generateTaskList) { + if (generateTaskList == null) return; + for (GenerateTask task : generateTaskList) { + if (task.isBatch) { + DeployConfGenerator.batchGenerateDeployConf(task.dir, task.template, task.commvar, task.files); + } else + DeployConfGenerator.generateDeployConf(task.dir, task.template, task.commvar, task.files); + } + } + + public static void goByPath(String path) { + String conf = DeployConfGenerator.getFileContent(path); + goByStr(conf); + } + + public static void main(String[] args) { + System.out.println("Usage: set deployentry.json or "); + if (args.length > 0) + goByPath(args[0]); + else + goByPath("./deployentry.json"); + } + +} diff --git a/src/main/java/org/bdware/ypkdeploy/HTTPTool.java b/src/main/java/org/bdware/ypkdeploy/HTTPTool.java index f810ffe..24a6be9 100644 --- a/src/main/java/org/bdware/ypkdeploy/HTTPTool.java +++ b/src/main/java/org/bdware/ypkdeploy/HTTPTool.java @@ -1,8 +1,6 @@ package org.bdware.ypkdeploy; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.google.gson.*; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; @@ -11,6 +9,7 @@ import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.bdware.client.ResultCallback; +import org.bdware.client.ws.Action; import org.bouncycastle.crypto.CryptoException; import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; import org.zz.gmhelper.SM2KeyPair; @@ -18,51 +17,76 @@ import org.zz.gmhelper.SM2Util; import java.io.*; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class HTTPTool { final static String TAG = "[HTTPTool] "; static class DeployConfig { + String agentAddress; //common config - String host; - String ypkPath; + JsonElement createParam; + String killBeforeStart; String privateKey; String publicKey; - String killBeforeStart; - JsonElement createParam; - //config for http deploy - int agentPort; + String ypkPath; } - - public static void batchRun(String path, boolean restart) { + public static void deployWithYpk(String deployConfigPath, String ypkPath) { DeployConfig config = null; try { - config = new Gson().fromJson(new FileReader(path), DeployConfig.class); + config = new Gson().fromJson(new FileReader(deployConfigPath), DeployConfig.class); } catch (Exception e) { e.printStackTrace(); System.out.println(TAG + " parse config failed!"); } + config.ypkPath = ypkPath; deployUseHttp(config); + restart(config); + } + + public static void deploy(String path) { + DeployConfig config = null; + File f = new File(path); + try { + if (f.isDirectory()) { + File[] files = f.listFiles((f2) -> f2.getName().endsWith(".json")); + if (files != null) + for (File file : files) + deploy(file.getAbsolutePath()); + return; + } else { + config = new Gson().fromJson(new FileReader(path), DeployConfig.class); + deployUseHttp(config); + restart(config); + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println(TAG + " parse config failed!"); + } + + } + + private static void restart(DeployConfig config) { SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(config)); - SmartContractClientExt ext = new SmartContractClientExt(String.format("ws://%s:%d/SCIDE/SCExecutor", config.host, config.agentPort), keyPair); + SmartContractClientExt ext = new SmartContractClientExt(String.format("ws://%s/SCIDE/SCExecutor", config.agentAddress), keyPair); ext.waitForConnect(); ext.login(); for (; !ext.isLoggedIn; ) Thread.yield(); AtomicInteger counter = new AtomicInteger(0); - try { - - if (config.killBeforeStart != null) - ext.kill(config.killBeforeStart, new ResultCallback() { - @Override - public void onResult(JsonObject r) { - System.out.println(TAG + r); - counter.incrementAndGet(); - } - }); + if (config.killBeforeStart != null) ext.kill(config.killBeforeStart, new ResultCallback() { + @Override + public void onResult(JsonObject r) { + System.out.println(TAG + r); + counter.incrementAndGet(); + } + }); for (; counter.get() == 0; ) Thread.yield(); ; @@ -71,23 +95,49 @@ public class HTTPTool { e.printStackTrace(); } File f = new File(config.ypkPath); - ext.startContract(f.getName() - .replaceAll(".zip", "") - .replaceAll(".ypk", ""), config.createParam, new ResultCallback() { + List fileNames = new ArrayList<>(); + String contractName = f.getName().replaceAll(".zip", "").replaceAll(".ypk", ""); + ext.listProjects(true, new ResultCallback() { + @Override + public void onResult(JsonObject r) { + JsonArray array = JsonParser.parseString(r.get("data").getAsString()).getAsJsonArray(); + for (JsonElement je : array) { + String fileName = je.getAsString(); + if (fileName.startsWith(contractName)) fileNames.add(fileName); + } + counter.incrementAndGet(); + } + }); + for (; counter.get() == 1; ) + Thread.yield(); + Collections.sort(fileNames, new Comparator() { + @Override + public int compare(String o1, String o2) { + return getCount(o2, contractName) - getCount(o1, contractName); + } + }); + + System.out.println(TAG + "startContract:" + fileNames.get(0)); + ext.startContract(fileNames.get(0), config.createParam, new ResultCallback() { @Override public void onResult(JsonObject r) { System.out.println(TAG + r); counter.incrementAndGet(); } }); - for (; counter.get() == 1; ) + for (; counter.get() == 2; ) Thread.yield(); - ; } + public static int getCount(String dirName, String contractName) { + String sub = dirName.substring(contractName.length()); + if (sub.startsWith("_")) { + return Integer.valueOf(sub.substring(1)); + } else return -1; + } public static void deployUseHttp(DeployConfig config) { - String url = "http://%s:%d/Upload?%s&sign=%s"; + String url = "http://%s/Upload?%s&sign=%s"; File file = new File(config.ypkPath); String path = config.ypkPath; String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&order=%d&count=%d&pubKey=%s"; @@ -103,19 +153,15 @@ public class HTTPTool { CloseableHttpClient client = HttpClients.createDefault(); String arg = String.format(argWithoutSig, "./", file.getName(), order, count, pubKey); String sign = ByteUtils.toHexString(SM2Util.sign(keyPair.getPrivateKeyParameter(), arg.getBytes(StandardCharsets.UTF_8))); - String urlStr = String.format(url, config.host, config.agentPort, arg, sign); + String urlStr = String.format(url, config.agentAddress, arg, sign); HttpPost httpPost = new HttpPost(urlStr); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); if (len == trunc) - builder.addBinaryBody( - "file", buff, - ContentType.APPLICATION_OCTET_STREAM, file.getName()); + builder.addBinaryBody("file", buff, ContentType.APPLICATION_OCTET_STREAM, file.getName()); else { byte[] bu = new byte[len]; System.arraycopy(buff, 0, bu, 0, len); - builder.addBinaryBody( - "file", bu, - ContentType.APPLICATION_OCTET_STREAM, file.getName()); + builder.addBinaryBody("file", bu, ContentType.APPLICATION_OCTET_STREAM, file.getName()); } HttpEntity multipart = builder.build(); @@ -123,15 +169,11 @@ public class HTTPTool { CloseableHttpResponse response = client.execute(httpPost); ByteArrayOutputStream bo = new ByteArrayOutputStream(); response.getEntity().writeTo(bo); - System.out.println("[YpkDeployTool] " + order - + " " + count + " " + bo); + System.out.println(TAG + +order + " " + count + " " + bo); } } catch (IOException | CryptoException e) { e.printStackTrace(); } } - public static void main(String[] args) { - batchRun("./deployconfig.json", true); - } } diff --git a/src/main/java/org/bdware/ypkdeploy/SmartContractClientExt.java b/src/main/java/org/bdware/ypkdeploy/SmartContractClientExt.java index cde2092..4933dd0 100644 --- a/src/main/java/org/bdware/ypkdeploy/SmartContractClientExt.java +++ b/src/main/java/org/bdware/ypkdeploy/SmartContractClientExt.java @@ -263,6 +263,23 @@ public class SmartContractClientExt extends SmartContractClient { if (cb != null) cb.onResult(jo); } + public void listProjects(boolean isPrivate, ResultCallback resultCallback) { + JsonObject request = new JsonObject(); + request.addProperty("action", "listProjects"); + request.addProperty("isPrivate", isPrivate); + String requestID = System.currentTimeMillis() + ""; + request.addProperty("requestID", requestID); + cbs.put(requestID, resultCallback); + sendMsg(request.toString()); + } + + @Action + public void onListProjects(JsonObject jo) { + String responseID = jo.get("responseID").getAsString(); + ResultCallback cb = cbs.get(responseID); + if (cb != null) cb.onResult(jo); + } + @Action public void onKillContractProcess(JsonObject jo) { if (!jo.has("responseID")) { diff --git a/src/test/java/EntryTest.java b/src/test/java/EntryTest.java new file mode 100644 index 0000000..6cf605f --- /dev/null +++ b/src/test/java/EntryTest.java @@ -0,0 +1,30 @@ +import org.bdware.ypkdeploy.Entry; +import org.bdware.ypkdeploy.HTTPTool; +import org.junit.Test; + +public class EntryTest { + @Test + public void generateAllConf() { + Entry.main(new String[]{"./testinput/generateAllConf.json"}); + } + + @Test + public void deployrouter() { + Entry.main(new String[]{"./testinput/deployrouter.json"}); + } + + @Test + public void deployap() { + Entry.main(new String[]{"./testinput/deployrouter.json"}); + } + + @Test + public void deploycp() { + Entry.main(new String[]{"./testinput/deployrouter.json"}); + } + + @Test + public void deployBDTest() { + HTTPTool.deploy("./testinput/deploy/router/GlobalRouter-023.json"); + } +} diff --git a/testinput/deploy/auditproxy.json.template b/testinput/deploy/auditproxy.json.template new file mode 100644 index 0000000..c793fe0 --- /dev/null +++ b/testinput/deploy/auditproxy.json.template @@ -0,0 +1,18 @@ +{ + "agentAddress": "_HOST_:18010", + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "ypkPath": "_AUDIT_PROXY_YPK_", + "killBeforeStart": "AutoAudit", + "createParam": { + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "repoName": "AuditProxy", + "auditURI": "tcp://_HOST_:18051", + "grafanaUrl": "jdbc:postgresql://iodlog.demo.internetapi.cn:5432/iodlog?useServerPrepStmts=true", + "grafanaUserName": "iodlog", + "grafanaPassword": "iodlog1107", + "grafanaServicePort": 18055, + "aceiServicePort": 18053 + } +} \ No newline at end of file diff --git a/testinput/deploy/auditproxyfiles.json b/testinput/deploy/auditproxyfiles.json new file mode 100644 index 0000000..ddb14ba --- /dev/null +++ b/testinput/deploy/auditproxyfiles.json @@ -0,0 +1,46 @@ +[ + { + "fileName": "./auditproxy/AuditProxy_shanxi.json", + "_HOST_": "_IP0_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_ty.json", + "_HOST_": "_IP1_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_dt.json", + "_HOST_": "_IP2_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_ty_xd.json", + "_HOST_": "_IP3_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_ty_yz.json", + "_HOST_": "_IP4_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_dt_yg.json", + "_HOST_": "_IP5_" + }, + { + "fileName": "./auditproxy/AuditProxy_sx_dt_yz.json", + "_HOST_": "_IP6_" + }, + { + "fileName": "./auditproxy/AuditProxy_njust.json", + "_HOST_": "_IP7_" + }, + { + "fileName": "./auditproxy/AuditProxy_bit.json", + "_HOST_": "_IP8_" + }, + { + "fileName": "./auditproxy/AuditProxy_mpi.json", + "_HOST_": "_IP9_" + }, + { + "fileName": "./auditproxy/AuditProxy_weixing.json", + "_HOST_": "_IP12_" + } +] \ No newline at end of file diff --git a/testinput/deploy/commonvar.json b/testinput/deploy/commonvar.json new file mode 100644 index 0000000..3099638 --- /dev/null +++ b/testinput/deploy/commonvar.json @@ -0,0 +1,25 @@ +{ + "_CONTROL_PROXY_YPK_": "/Users/huaqiancai/BDWare/datanet/ControlProxy/backend/build/ControlProxy-1.2.0.ypk", + "_AUDIT_PROXY_YPK_": "./datanet-pack/ypk/AuditProxy-1.0.6.ypk", + "_RRouter_YPK_": "/Users/huaqiancai/BDWare/datanet/datanet-router-bunndle/datanet-router-backend/build/Router-1.6.0.ypk", + "_GRouter_YPK_": "/Users/huaqiancai/BDWare/datanet/datanet-router-bunndle/datanet-router-backend/build/GlobalRouter-1.6.0.ypk", + "_CTROUTER_": "Router", + "___山西___": "", + "_IP0_": "39.104.201.40", + "_IP1_": "39.104.208.148", + "_IP2_": "39.104.201.243", + "_IP3_": "39.104.208.223", + "_IP4_": "39.104.200.95", + "_IP5_": "39.104.202.247", + "_IP6_": "39.104.209.178", + "___工业___": "", + "_IP7_": "39.104.209.15", + "_IP8_": "39.104.207.76", + "___药监___": "", + "_IP9_": "39.104.204.44", + "_IP10_": "39.104.209.158", + "_IP11_": "39.104.208.114", + "___卫星___": "", + "_IP12_": "39.104.205.122", + "_IP13_": "39.104.200.8" +} \ No newline at end of file diff --git a/testinput/deploy/controlproxy.json.template b/testinput/deploy/controlproxy.json.template new file mode 100644 index 0000000..8fa9fc7 --- /dev/null +++ b/testinput/deploy/controlproxy.json.template @@ -0,0 +1,16 @@ +{ + "host": "_HOST_", + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "ypkPath": "_CONTROL_PROXY_YPK_", + "killBeforeStart": "ControlProxy", + "createParam": { + "privateKey": "4616ff0e2a4f982364914f9be30b51c6bc6ccb6602114a9ee8792f2ccf67465b", + "publicKey": "04f9b9b8f324908464f78a6235e2dd93e4dfdaf045e9b1b5cfd57374516cc61a79a86fc2b42d3321a5b49a0f25381a7bed61901b40b729f72354e716673d551e98", + "prefix": "_PREFIX_", + "router": "_CTROUTER_", + "routerURI": "tcp://_HOST_:18041", + "auditType": "OnlyHash" + }, + "agentPort": 18010 +} \ No newline at end of file diff --git a/testinput/deploy/controlproxyfiles.json b/testinput/deploy/controlproxyfiles.json new file mode 100644 index 0000000..6413212 --- /dev/null +++ b/testinput/deploy/controlproxyfiles.json @@ -0,0 +1,38 @@ +[ + { + "fileName": "./controlproxy/CP_shanxi.json", + "_HOST_": "_IP0_", + "_PREFIX_": "shanxi", + "_CTROUTER_": "GlobalRouter" + }, + { + "fileName": "./controlproxy/CP_sx_ty.json", + "_HOST_": "_IP1_", + "_PREFIX_": "shanxi.taiyuan" + }, + { + "fileName": "./controlproxy/CP_sx_dt.json", + "_HOST_": "_IP2_", + "_PREFIX_": "shanxi.datong" + }, + { + "fileName": "./controlproxy/CP_sx_ty_xd.json", + "_HOST_": "_IP3_", + "_PREFIX_": "shanxi.taiyuan.xiaodian" + }, + { + "fileName": "./controlproxy/CP_sx_ty_yj.json", + "_HOST_": "_IP4_", + "_PREFIX_": "shanxi.taiyuan.yingze" + }, + { + "fileName": "./controlproxy/CP_sx_dt_yg.json", + "_HOST_": "_IP5_", + "_PREFIX_": "shanxi.datong.yungang" + }, + { + "fileName": "./controlproxy/CP_sx_dt_yz.json", + "_HOST_": "_IP6_", + "_PREFIX_": "shanxi.datong.yunzhou" + } +] \ No newline at end of file diff --git a/testinput/deploy/router.json.template b/testinput/deploy/router.json.template new file mode 100644 index 0000000..55b77b1 --- /dev/null +++ b/testinput/deploy/router.json.template @@ -0,0 +1,19 @@ +{ + "agentAddress": "_HOST_:18010", + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "ypkPath": "_RRouter_YPK_", + "killBeforeStart": "_CTROUTER_", + "createParam": { + "auditProxyAddress": "tcp://_HOST_:18051", + "controlProxyAddress": "ws://_HOST_:18010", + "repositoryAddress": "tcp://_HOST_:18031", + "name": "_ROUTERNAME_", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "privateKey":"d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "doId": "_PREFIX_", + "routerAddress": "tcp://_HOST_:18041", + "upperRouterAddress": "tcp://_HOST2_:18041", + "scheme":"irp" + } +} \ No newline at end of file diff --git a/testinput/deploy/router/GlobalRouter-023.json b/testinput/deploy/router/GlobalRouter-023.json new file mode 100644 index 0000000..b7eb208 --- /dev/null +++ b/testinput/deploy/router/GlobalRouter-023.json @@ -0,0 +1,19 @@ +{ + "agentAddress": "023.node.internetapi.cn:21130", + "privateKey": "e85ce2f4d8882ff343d32ce42adde91d09e29c321452dd4ef9f07ebe76d1c6a5", + "publicKey": "04da01345770b7e09d4774bf6c0395399b18814aa0b7b158f64b634b8f3d628d9964af6523835225af11e467271f4969d67bf90b32eaa09f517c79b2d1f9b8a926", + "ypkPath": "/Users/huaqiancai/BDWare/datanet/datanet-router-bunndle/datanet-router-backend/build/GlobalRouter-1.6.0.ypk", + "killBeforeStart": "GlobalRouter", + "createParam": { + "auditProxyAddress": "tcp://39.104.204.44:18051", + "controlProxyAddress": "ws://39.104.209.15:21130", + "repositoryAddress": "tcp://39.104.204.44:18031", + "name": "数瑞测试网", + "publicKey": "04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27", + "privateKey": "d675782acf011dbc01a73c7967ccff9564486f7c3a9f5d5de151caffaa18936", + "doId": "bdtest", + "routerAddress": "tcp://39.104.209.15:21162", + "upperRouterAddress": "tcp://_HOST2_:18041", + "scheme": "irp" + } +} \ No newline at end of file diff --git a/testinput/deploy/routerfiles.json b/testinput/deploy/routerfiles.json new file mode 100644 index 0000000..fba2c04 --- /dev/null +++ b/testinput/deploy/routerfiles.json @@ -0,0 +1,76 @@ +[ + { + "fileName": "./router/GlobalRouter-sx.json", + "_HOST_": "_IP0_", + "_PREFIX_": "shanxi", + "_CTROUTER_": "GlobalRouter", + "_ROUTERNAME_": "山西能源数联网", + "_RRouter_YPK_": "_GRouter_YPK_" + }, + { + "fileName": "./router/Router_ty.json", + "_HOST_": "_IP1_", + "_HOST2_": "_IP0_", + "_PREFIX_": "shanxi.taiyuan", + "_ROUTERNAME_": "山西太原节点" + }, + { + "fileName": "./router/Router_dt.json", + "_HOST_": "_IP2_", + "_HOST2_": "_IP0_", + "_PREFIX_": "shanxi.datong", + "_ROUTERNAME_": "山西大同节点" + }, + { + "fileName": "./router/Router_ty_xd.json", + "_HOST_": "_IP3_", + "_HOST2_": "_IP1_", + "_PREFIX_": "shanxi.taiyuan.xiaodian", + "_ROUTERNAME_": "太原小店区节点" + }, + { + "fileName": "./router/Router_ty_yz.json", + "_HOST_": "_IP4_", + "_HOST2_": "_IP1_", + "_PREFIX_": "shanxi.taiyuan.yingze", + "_ROUTERNAME_": "太原迎泽区节点" + }, + { + "fileName": "./router/Router_dt_yg.json", + "_HOST_": "_IP5_", + "_HOST2_": "_IP2_", + "_PREFIX_": "shanxi.datong.yungang", + "_ROUTERNAME_": "大同云岗区节点" + }, + { + "fileName": "./router/Router_dt_yz.json", + "_HOST_": "_IP6_", + "_HOST2_": "_IP2_", + "_PREFIX_": "shanxi.datong.yunzhou", + "_ROUTERNAME_": "大同云州区节点" + }, + { + "fileName": "./router/GlobalRouter-njust.json", + "_HOST_": "_IP7_", + "_PREFIX_": "njust", + "_CTROUTER_": "GlobalRouter", + "_ROUTERNAME_": "5G工业数联网", + "_RRouter_YPK_": "_GRouter_YPK_" + }, + { + "fileName": "./router/GlobalRouter-mpi.json", + "_HOST_": "_IP9_", + "_PREFIX_": "mpi", + "_CTROUTER_": "GlobalRouter", + "_ROUTERNAME_": "药监数联网", + "_RRouter_YPK_": "_GRouter_YPK_" + }, + { + "fileName": "./router/GlobalRouter-wx.json", + "_HOST_": "_IP12_", + "_PREFIX_": "wx", + "_CTROUTER_": "GlobalRouter", + "_ROUTERNAME_": "卫星数联网", + "_RRouter_YPK_": "_GRouter_YPK_" + } +] \ No newline at end of file diff --git a/testinput/deployentry.json b/testinput/deployentry.json new file mode 100644 index 0000000..d624abd --- /dev/null +++ b/testinput/deployentry.json @@ -0,0 +1,28 @@ +{ + "generateTasks": [ + { + "isBatch": true, + "dir": "./testinput/deploy", + "template": "controlproxy.json.template", + "commvar": "commonvar.json", + "files": "controlproxyfiles.json" + }, + { + "isBatch": true, + "dir": "./testinput/deploy", + "template": "router.json.template", + "commvar": "commonvar.json", + "files": "routerfiles.json" + }, + { + "isBatch": true, + "dir": "./testinput/deploy", + "template": "auditproxy.json.template", + "commvar": "commonvar.json", + "files": "auditproxyfiles.json" + } + ], + "deployTasks": [ + "./testinput/deploy/controlproxy2/" + ] +} \ No newline at end of file diff --git a/testinput/deployrouter.json b/testinput/deployrouter.json new file mode 100644 index 0000000..1285d08 --- /dev/null +++ b/testinput/deployrouter.json @@ -0,0 +1,7 @@ +{ + "generateTasks": [ + ], + "deployTasks": [ + "./testinput/deploy/router/GlobalRouter-wx.json" + ] +} \ No newline at end of file diff --git a/testinput/generateAllConf.json b/testinput/generateAllConf.json new file mode 100644 index 0000000..c9b9a87 --- /dev/null +++ b/testinput/generateAllConf.json @@ -0,0 +1,27 @@ +{ + "generateTasks": [ + { + "isBatch": true, + "dir": "./testinput/deploy", + "template": "controlproxy.json.template", + "commvar": "commonvar.json", + "files": "controlproxyfiles.json" + }, + { + "isBatch": true, + "dir": "./testinput/deploy", + "template": "router.json.template", + "commvar": "commonvar.json", + "files": "routerfiles.json" + }, + { + "isBatch": true, + "dir": "./testinput/deploy", + "template": "auditproxy.json.template", + "commvar": "commonvar.json", + "files": "auditproxyfiles.json" + } + ], + "deployTasks": [ + ] +} \ No newline at end of file