mirror of
https://gitee.com/BDWare/ypk-deploy-tool
synced 2025-04-26 22:12:16 +00:00
fix http tool bugs
This commit is contained in:
parent
23f367f398
commit
ceb3709d7b
@ -8,7 +8,7 @@ plugins {
|
||||
apply from: '../spotless.gradle'
|
||||
|
||||
group "org.bdware.bdcontract"
|
||||
version "0.7.4"
|
||||
version "0.7.5"
|
||||
sourceCompatibility = 1.8
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@ -23,7 +23,7 @@ dependencies {
|
||||
implementation 'org.bouncycastle:bcprov-jdk15on:1.69'
|
||||
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
|
||||
implementation 'org.apache.httpcomponents:httpmime:4.5.13'
|
||||
implementation 'org.bdware.doip:bdosclient:0.0.7'
|
||||
implementation 'org.bdware.doip:bdosclient:0.0.8'
|
||||
// implementation 'org.bdware.doip:doip-audit-tool:1.2.4'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public class HTTPTool {
|
||||
String publicKey;
|
||||
String ypkPath;
|
||||
int remoteDebugPort;
|
||||
int chunkSize;
|
||||
}
|
||||
|
||||
public static void deployWithYpk(String deployConfigPath, String ypkPath) {
|
||||
@ -56,9 +57,8 @@ public class HTTPTool {
|
||||
try {
|
||||
if (f.isDirectory()) {
|
||||
File[] files = f.listFiles((f2) -> f2.getName().endsWith(".json"));
|
||||
if (files != null)
|
||||
for (File file : files)
|
||||
deploy(file.getAbsolutePath());
|
||||
if (files != null) for (File file : files)
|
||||
deploy(file.getAbsolutePath());
|
||||
return;
|
||||
} else {
|
||||
config = new Gson().fromJson(new FileReader(path), DeployConfig.class);
|
||||
@ -74,24 +74,23 @@ public class HTTPTool {
|
||||
|
||||
private static void restart(DeployConfig config) {
|
||||
SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(config));
|
||||
SmartContractClientExt ext = new SmartContractClientExt(
|
||||
String.format("ws://%s/SCIDE/SCExecutor", config.agentAddress), keyPair);
|
||||
SmartContractClientExt ext = new SmartContractClientExt(String.format("ws://%s/SCIDE/SCExecutor", config.agentAddress), keyPair);
|
||||
ext.waitForConnect();
|
||||
ext.login();
|
||||
for (; !ext.isLoggedIn;)
|
||||
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();
|
||||
}
|
||||
});
|
||||
for (; counter.get() == 0;)
|
||||
Thread.yield();;
|
||||
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();
|
||||
;
|
||||
Thread.sleep(200);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -102,17 +101,15 @@ public class HTTPTool {
|
||||
ext.listProjects(true, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
JsonArray array =
|
||||
JsonParser.parseString(r.get("data").getAsString()).getAsJsonArray();
|
||||
JsonArray array = JsonParser.parseString(r.get("data").getAsString()).getAsJsonArray();
|
||||
for (JsonElement je : array) {
|
||||
String fileName = je.getAsString();
|
||||
if (fileName.startsWith(contractName))
|
||||
fileNames.add(fileName);
|
||||
if (fileName.startsWith(contractName)) fileNames.add(fileName);
|
||||
}
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
for (; counter.get() == 1;)
|
||||
for (; counter.get() == 1; )
|
||||
Thread.yield();
|
||||
Collections.sort(fileNames, new Comparator<String>() {
|
||||
@Override
|
||||
@ -123,26 +120,25 @@ public class HTTPTool {
|
||||
|
||||
System.out.println(TAG + "startContract:" + fileNames.get(0));
|
||||
if (config.asDebug) {
|
||||
ext.startContractAsDebug(fileNames.get(0), config.createParam, config.remoteDebugPort,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
System.out.println(TAG + r);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
ext.startContractAsDebug(fileNames.get(0), config.createParam, config.remoteDebugPort, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
System.out.println(TAG + r);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ext.startContract(fileNames.get(0), config.createParam, config.remoteDebugPort,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
System.out.println(TAG + r);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
ext.startContract(fileNames.get(0), config.createParam, config.remoteDebugPort, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
System.out.println(TAG + r);
|
||||
System.out.println(TAG + "access: " + config.agentAddress);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (; counter.get() == 2;)
|
||||
for (; counter.get() == 2; )
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
@ -150,8 +146,7 @@ public class HTTPTool {
|
||||
String sub = dirName.substring(contractName.length());
|
||||
if (sub.startsWith("_")) {
|
||||
return Integer.valueOf(sub.substring(1));
|
||||
} else
|
||||
return -1;
|
||||
} else return -1;
|
||||
}
|
||||
|
||||
public static void deployUseHttp(DeployConfig config) {
|
||||
@ -160,41 +155,34 @@ public class HTTPTool {
|
||||
String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&order=%d&count=%d&pubKey=%s";
|
||||
SM2KeyPair keyPair;
|
||||
try {
|
||||
keyPair = SM2KeyPair
|
||||
.fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}",
|
||||
config.publicKey, config.privateKey));
|
||||
keyPair = SM2KeyPair.fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey));
|
||||
} catch (Exception e) {
|
||||
System.out.println("[HttpTool] parse key error:\n"
|
||||
+ String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}",
|
||||
config.publicKey, config.privateKey));
|
||||
System.out.println("[HttpTool] parse key error:\n" + String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey));
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
String pubKey = keyPair.getPublicKeyStr();
|
||||
int trunc = 300 * 1024;
|
||||
int trunc = 1000 * 1024;
|
||||
if (config.chunkSize > 10)
|
||||
trunc = config.chunkSize;
|
||||
byte[] buff = new byte[trunc];
|
||||
int order = 0;
|
||||
int count = (int) ((file.length() - 1) / (trunc)) + 1;
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
for (int len = 0; (len = fin.read(buff)) > 0; order++) {
|
||||
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 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.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();
|
||||
httpPost.setEntity(multipart);
|
||||
@ -202,10 +190,15 @@ public class HTTPTool {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
response.getEntity().writeTo(bo);
|
||||
System.out.println(TAG + +order + "/" + count + " " + bo);
|
||||
client.close();
|
||||
}
|
||||
} catch (IOException | CryptoException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ public class HttpUploadTest {
|
||||
return;
|
||||
}
|
||||
String pubKey = keyPair.getPublicKeyStr();
|
||||
int trunc = 300 * 1024;
|
||||
int trunc = 500 * 1024;
|
||||
byte[] buff = new byte[trunc];
|
||||
int order = 0;
|
||||
int count = (int) ((file.length() - 1) / (trunc)) + 1;
|
||||
@ -73,4 +73,9 @@ public class HttpUploadTest {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deployTest() {
|
||||
org.bdware.ypkdeploy.HTTPTool.deployWithYpk("/Users/huaqiancai/BDWare/sci-data-trace/backend/deployconf.json", "/Users/huaqiancai/BDWare/sci-data-trace/backend/build/sci-data-trace-2.1.5.ypk");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user