Search in sources :

Example 1 with TargetProcess

use of com.creditease.uav.feature.upgrade.beans.TargetProcess in project uavstack by uavorg.

the class UAVOverrideFileAction method addAppVersionToProfile.

/**
 * Append upgrade target version to profile configuration file
 */
private void addAppVersionToProfile() {
    String version = UpgradeUtil.getVersionFromPackageName(this.getSoftwarePackage());
    if (!StringHelper.isEmpty(version)) {
        for (TargetProcess process : this.upgradeContext.getAffectedProcessList()) {
            Path profilePath = Paths.get(this.getConfigManager().getContext(IConfigurationManager.CONFIGPATH), process.getProfileName() + ".properties");
            if (log.isTraceEnable()) {
                log.info(this, "Start adding new app version " + version + " to " + profilePath);
            }
            try {
                List<String> lines = Files.readAllLines(profilePath, Charset.forName("UTF-8"));
                StringBuffer sbf = new StringBuffer();
                for (String line : lines) {
                    if (line.contains("meta.nodeappversion")) {
                        // ignore the old meta.nodeappversion
                        continue;
                    }
                    sbf.append(line).append(System.getProperty("line.separator"));
                    if (line.contains("meta.nodetype")) {
                        // append meta.nodeappversion after the line of meta.nodetype
                        sbf.append("meta.nodeappversion=" + version).append(System.getProperty("line.separator"));
                    }
                }
                Files.write(profilePath, sbf.toString().getBytes(Charset.forName("UTF-8")));
            } catch (IOException e) {
                if (log.isTraceEnable()) {
                    log.err(this, "Failed to app version info to profile " + e.getMessage());
                }
            }
        }
    } else {
        if (log.isTraceEnable()) {
            log.warn(this, "Did not get valid version from " + this.getSoftwarePackage());
        }
    }
}
Also used : Path(java.nio.file.Path) TargetProcess(com.creditease.uav.feature.upgrade.beans.TargetProcess) IOException(java.io.IOException)

Example 2 with TargetProcess

use of com.creditease.uav.feature.upgrade.beans.TargetProcess in project uavstack by uavorg.

the class UpgradeOperationRecordConsumer method handleUAVOperationRecord.

@SuppressWarnings("unchecked")
private void handleUAVOperationRecord(UpgradeOperationRecord oprRecord, EndAction endAction) {
    Map<String, Object> action = JSONHelper.toObject(String.valueOf(oprRecord.getAction()), Map.class);
    if (UpgradePhase.PROCESS_STOP.toString().equalsIgnoreCase(oprRecord.getPhase())) {
        // will start all the process which have been stopped
        this.upgradeContext.setRollback(true);
        List<TargetProcess> processList = UpgradeUtil.generateUAVProcessListFromJsonStrList((List<String>) action.get("processes"));
        StartUAVProcessAction startAction = new StartUAVProcessAction(this.feature, this.upgradeContext, this.engine);
        startAction.setProcessList(processList);
        engine.execute(startAction.getName(), new ActionContext());
    } else if (UpgradePhase.OVERRIDE_FILE.toString().equalsIgnoreCase(oprRecord.getPhase())) {
        // will roll back
        this.upgradeContext.setRollback(true);
        JSONObject object = (JSONObject) action.get("override");
        String backupZip = String.valueOf(object.get("backup"));
        OverrideFileAction overrideFileAction = new UAVOverrideFileAction(this.feature, this.upgradeContext, this.engine);
        overrideFileAction.setBackupZipPath(Paths.get(backupZip));
        List<UpgradeOperationRecord> recordList = getOperationRecordByPhase(UpgradePhase.PROCESS_STOP);
        Map<String, Object> actionMap = JSONHelper.toObject(String.valueOf(recordList.get(0).getAction()), Map.class);
        List<TargetProcess> processList = UpgradeUtil.generateUAVProcessListFromJsonStrList((List<String>) actionMap.get("processes"));
        StartUAVProcessAction startAction = new StartUAVProcessAction(this.feature, this.upgradeContext, this.engine);
        startAction.setProcessList(processList);
        engine.execute(overrideFileAction.getName(), new ActionContext());
    } else if (UpgradePhase.PROCESS_START.toString().equalsIgnoreCase(oprRecord.getPhase())) {
        if (Float.valueOf(String.valueOf(action.get("ratio"))) == 1.0f) {
            // all the processes have been started successfully, so just finished the end action.
            engine.execute(endAction.getName(), new ActionContext());
        } else {
            // no all the processes were started successfully, so continue to start processes
            List<TargetProcess> totalProcessList = UpgradeUtil.generateUAVProcessListFromJsonStrList((List<String>) action.get("processes"));
            List<TargetProcess> alreadyStartedProcessList = new ArrayList<TargetProcess>();
            List<UpgradeOperationRecord> recordList = getOperationRecordByPhase(UpgradePhase.PROCESS_START);
            for (UpgradeOperationRecord record : recordList) {
                Map<String, Object> actionMap = JSONHelper.toObject(String.valueOf(record.getAction()), Map.class);
                alreadyStartedProcessList.add(JSONHelper.toObject(String.valueOf(actionMap.get("start_process")), TargetProcess.class));
            }
            // find the processes to be started
            List<TargetProcess> toStartProcessList = new ArrayList<TargetProcess>();
            boolean find = false;
            for (TargetProcess process : totalProcessList) {
                String profile = process.getProfileName();
                for (TargetProcess startedProcess : alreadyStartedProcessList) {
                    if (profile.equals(startedProcess.getProfileName())) {
                        find = true;
                        break;
                    }
                }
                if (!find) {
                    toStartProcessList.add(process);
                }
                find = false;
            }
            StartUAVProcessAction startAction = new StartUAVProcessAction(this.feature, this.upgradeContext, this.engine);
            startAction.setProcessList(toStartProcessList);
            engine.execute(startAction.getName(), new ActionContext());
        }
    }
}
Also used : TargetProcess(com.creditease.uav.feature.upgrade.beans.TargetProcess) UAVOverrideFileAction(com.creditease.uav.feature.upgrade.action.uav.UAVOverrideFileAction) ArrayList(java.util.ArrayList) StartUAVProcessAction(com.creditease.uav.feature.upgrade.action.uav.StartUAVProcessAction) ActionContext(com.creditease.agent.spi.ActionContext) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) UAVOverrideFileAction(com.creditease.uav.feature.upgrade.action.uav.UAVOverrideFileAction) ThirdpartySoftwareOverrideFileAction(com.creditease.uav.feature.upgrade.action.thirdparty.ThirdpartySoftwareOverrideFileAction) OverrideFileAction(com.creditease.uav.feature.upgrade.action.OverrideFileAction) Map(java.util.Map)

Example 3 with TargetProcess

use of com.creditease.uav.feature.upgrade.beans.TargetProcess in project uavstack by uavorg.

the class StopUAVProcessAction method doAction.

@Override
public void doAction(ActionContext context) throws Exception {
    setUpgradePhase(UpgradePhase.PROCESS_STOP);
    List<TargetProcess> processList = getProcessList();
    int size = processList.size();
    float processIndex = 1;
    for (TargetProcess process : processList) {
        if (!UpgradeUtil.isUAVProcessAlive(process)) {
            if (log.isTraceEnable()) {
                log.info(this, process + " is not alive, no need to stop it");
            }
            continue;
        }
        stopProcess(process);
        if (this.upgradeContext.canWriteOperationRecord()) {
            this.writeProcessRecord(this.cName, process, processIndex / size, UpgradeUtil.getUAVProcessJsonStrList(processList));
        }
        processIndex++;
    }
    context.setSucessful(true);
}
Also used : TargetProcess(com.creditease.uav.feature.upgrade.beans.TargetProcess)

Example 4 with TargetProcess

use of com.creditease.uav.feature.upgrade.beans.TargetProcess in project uavstack by uavorg.

the class StartUAVProcessAction method doAction.

@Override
public void doAction(ActionContext context) throws Exception {
    setUpgradePhase(UpgradePhase.PROCESS_START);
    List<TargetProcess> processList = getProcessList();
    int size = processList.size();
    float processIndex = 1;
    for (TargetProcess process : processList) {
        if (UpgradeUtil.isUAVProcessAlive(process)) {
            if (log.isTraceEnable()) {
                log.info(this, process + " is already alive, no need to start");
            }
            continue;
        }
        startProcess(process);
        // Sleep 3 seconds to finish starting
        ThreadHelper.suspend(3000);
        if (!UpgradeUtil.isUAVProcessAlive(process)) {
            throw new UpgradeException("Failed to start profile: " + process.getProfileName());
        }
        if (this.upgradeContext.canWriteOperationRecord()) {
            this.writeProcessRecord(this.cName, process, processIndex / size, UpgradeUtil.getUAVProcessJsonStrList(processList));
        }
        if (log.isTraceEnable()) {
            log.info(this, "Profile: " + process.getProfileName() + " was restarted successfully");
        }
        processIndex++;
    }
    context.setSucessful(true);
}
Also used : UpgradeException(com.creditease.uav.feature.upgrade.exception.UpgradeException) TargetProcess(com.creditease.uav.feature.upgrade.beans.TargetProcess)

Aggregations

TargetProcess (com.creditease.uav.feature.upgrade.beans.TargetProcess)4 JSONObject (com.alibaba.fastjson.JSONObject)1 ActionContext (com.creditease.agent.spi.ActionContext)1 OverrideFileAction (com.creditease.uav.feature.upgrade.action.OverrideFileAction)1 ThirdpartySoftwareOverrideFileAction (com.creditease.uav.feature.upgrade.action.thirdparty.ThirdpartySoftwareOverrideFileAction)1 StartUAVProcessAction (com.creditease.uav.feature.upgrade.action.uav.StartUAVProcessAction)1 UAVOverrideFileAction (com.creditease.uav.feature.upgrade.action.uav.UAVOverrideFileAction)1 UpgradeException (com.creditease.uav.feature.upgrade.exception.UpgradeException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1