mirror of
https://gitee.com/BDWare/ypk-deploy-tool
synced 2026-01-29 08:09:27 +00:00
add simple-ypk-packer
This commit is contained in:
74
src/main/java/org/bdware/ypkdeploy/DeployConfGenerator.java
Normal file
74
src/main/java/org/bdware/ypkdeploy/DeployConfGenerator.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
57
src/main/java/org/bdware/ypkdeploy/Entry.java
Normal file
57
src/main/java/org/bdware/ypkdeploy/Entry.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package org.bdware.ypkdeploy;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Entry {
|
||||
static class DeployEntry {
|
||||
List<GenerateTask> generateTasks;
|
||||
List<String> 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<String> deployTasks) {
|
||||
if (deployTasks == null) return;
|
||||
for (String deployTask : deployTasks) {
|
||||
HTTPTool.deploy(deployTask);
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateJson(List<GenerateTask> 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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> 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<String>() {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")) {
|
||||
|
||||
30
src/test/java/EntryTest.java
Normal file
30
src/test/java/EntryTest.java
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user