Search in sources :

Example 1 with SyncVersionRunner

use of io.nuls.client.version.SyncVersionRunner in project nuls by nuls-io.

the class UpgradeThread method run.

@Override
public void run() {
    if (!upgrading) {
        return;
    }
    try {
        SyncVersionRunner syncor = SyncVersionRunner.getInstance();
        this.version = syncor.getNewestVersion();
        String versionFileHash = syncor.getVersionFileHash();
        if (!VersionUtils.higherThan(version, NulsConfig.VERSION)) {
            setFailedMessage("The version is wrong!");
            return;
        }
        process.setPercentage(1);
        VersionFile versionJson = getVersionJson(syncor, versionFileHash);
        if (null == versionJson) {
            return;
        }
        process.setPercentage(5);
        process.setStatus(VersionConstant.DOWNLOADING);
        String root = this.getClass().getClassLoader().getResource("").getPath();
        root = URLDecoder.decode(root, "UTF-8");
        String newDirPath = root + "/temp/" + version;
        File newDir = new File(newDirPath);
        if (!newDir.exists()) {
            newDir.mkdirs();
        }
        String urlRoot = syncor.getRootUrl() + "/" + version;
        boolean result = this.download(urlRoot + "/bin.zip", newDirPath + "/bin.zip", versionJson.getBinSig());
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(12);
        result = this.download(urlRoot + "/conf.zip", newDirPath + "/conf.zip", versionJson.getConfSig());
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(15);
        File libsDir = new File(newDirPath + "/libs");
        if (libsDir.exists()) {
            FileUtil.deleteFolder(libsDir);
        }
        libsDir.mkdirs();
        process.setPercentage(20);
        int count = 0;
        int size = versionJson.getJarSigList().size();
        for (JarSig jarSig : versionJson.getJarSigList()) {
            File file = new File(root + "/libs/" + jarSig.getFileName());
            if (file.exists() && this.verifySig(file, Hex.decode(jarSig.getSig()))) {
                FileUtil.copyFile(file, new File(newDirPath + "/libs/" + jarSig.getFileName()));
                result = true;
            } else {
                result = this.download(urlRoot + "/libs/" + jarSig.getFileName(), newDirPath + "/libs/" + jarSig.getFileName(), jarSig.getSig());
            }
            if (!result) {
                deleteTemp(root + "/temp/");
                return;
            }
            count++;
            process.setPercentage(20 + (count * 70) / (size));
        }
        process.setStatus(VersionConstant.INSTALLING);
        String oldDirPath = root + "/temp/old";
        result = this.copy(root + "/bin", oldDirPath + "/bin");
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(92);
        result = this.copy(root + "/conf", oldDirPath + "/conf");
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(95);
        result = this.copy(root + "/libs", oldDirPath + "/libs");
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(100);
        process.setStatus(VersionConstant.WAITING_RESTART);
    } catch (Exception e) {
        Log.error(e);
        setFailedMessage(e.getMessage());
        upgrading = false;
    }
}
Also used : VersionFile(io.nuls.client.rpc.resources.model.VersionFile) JarSig(io.nuls.client.rpc.resources.model.JarSig) SyncVersionRunner(io.nuls.client.version.SyncVersionRunner) VersionFile(io.nuls.client.rpc.resources.model.VersionFile)

Example 2 with SyncVersionRunner

use of io.nuls.client.version.SyncVersionRunner in project nuls by nuls-io.

the class ClientResource method startUpdate.

@POST
@Path("/upgrade/{version}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "升级")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = Boolean.class) })
public RpcClientResult startUpdate(@PathParam("version") String version) {
    AssertUtil.canNotEmpty(version);
    SyncVersionRunner syncor = SyncVersionRunner.getInstance();
    String newestVersion = syncor.getNewestVersion();
    if (!VersionUtils.higherThan(newestVersion, NulsConfig.VERSION)) {
        Result result = Result.getFailed(KernelErrorCode.NONEWVER);
        return result.toRpcClientResult();
    }
    if (!version.equals(newestVersion)) {
        Result result = Result.getFailed(KernelErrorCode.VERSION_NOT_NEWEST);
        return result.toRpcClientResult();
    }
    URL url = ClientResource.class.getClassLoader().getResource("libs");
    if (null == url) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND).toRpcClientResult();
    }
    UpgradeThread thread = UpgradeThread.getInstance();
    if (thread.isUpgrading()) {
        return Result.getFailed(KernelErrorCode.UPGRADING).toRpcClientResult();
    }
    boolean result = thread.start();
    if (result) {
        TaskManager.createAndRunThread((short) 1, "upgrade", thread);
        Map<String, Boolean> map = new HashMap<>();
        map.put("value", true);
        return Result.getSuccess().setData(map).toRpcClientResult();
    }
    return Result.getFailed(KernelErrorCode.FAILED).toRpcClientResult();
}
Also used : HashMap(java.util.HashMap) UpgradeThread(io.nuls.client.rpc.resources.thread.UpgradeThread) SyncVersionRunner(io.nuls.client.version.SyncVersionRunner) URL(java.net.URL) RpcClientResult(io.nuls.kernel.model.RpcClientResult) Result(io.nuls.kernel.model.Result) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with SyncVersionRunner

use of io.nuls.client.version.SyncVersionRunner in project nuls by nuls-io.

the class ClientResource method getVersion.

@GET
@Path("/version")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "查询系统版本信息")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = VersionDto.class) })
public RpcClientResult getVersion() {
    VersionDto rpcVersion = new VersionDto();
    rpcVersion.setMyVersion(NulsConfig.VERSION);
    SyncVersionRunner syncer = SyncVersionRunner.getInstance();
    rpcVersion.setNewestVersion(syncer.getNewestVersion());
    if (StringUtils.isBlank(rpcVersion.getNewestVersion())) {
        rpcVersion.setNewestVersion(NulsConfig.VERSION);
    }
    rpcVersion.setInfromation(syncer.getInformation());
    boolean upgradable = VersionUtils.higherThan(rpcVersion.getNewestVersion(), NulsConfig.VERSION);
    URL url = ClientResource.class.getClassLoader().getResource("libs");
    upgradable = upgradable && url != null;
    rpcVersion.setUpgradable(upgradable);
    rpcVersion.setNetworkVersion(NulsContext.MAIN_NET_VERSION);
    return Result.getSuccess().setData(rpcVersion).toRpcClientResult();
}
Also used : SyncVersionRunner(io.nuls.client.version.SyncVersionRunner) VersionDto(io.nuls.client.rpc.resources.dto.VersionDto) URL(java.net.URL) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

SyncVersionRunner (io.nuls.client.version.SyncVersionRunner)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 URL (java.net.URL)2 VersionDto (io.nuls.client.rpc.resources.dto.VersionDto)1 JarSig (io.nuls.client.rpc.resources.model.JarSig)1 VersionFile (io.nuls.client.rpc.resources.model.VersionFile)1 UpgradeThread (io.nuls.client.rpc.resources.thread.UpgradeThread)1 Result (io.nuls.kernel.model.Result)1 RpcClientResult (io.nuls.kernel.model.RpcClientResult)1 HashMap (java.util.HashMap)1