com.aliyun.ChangeOrderManager Maven / Gradle / Ivy
package com.aliyun;
import com.aliyun.bean.ChangeOrderTraceState;
import com.aliyun.bean.DefaultConfigBean;
import com.aliyun.enums.Constants;
import com.aliyun.enums.Status;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.edas.model.v20170801.GetChangeOrderInfoRequest;
import com.aliyuncs.edas.model.v20170801.GetChangeOrderInfoResponse;
import com.aliyuncs.edas.model.v20170801.GetChangeOrderInfoResponse.*;
import com.aliyuncs.edas.model.v20170801.GetChangeOrderInfoResponse.ChangeOrderInfo.PipelineInfo;
import com.aliyuncs.edas.model.v20170801.GetChangeOrderInfoResponse.ChangeOrderInfo.PipelineInfo.StageInfoDTO;
import com.aliyuncs.edas.model.v20170801.GetChangeOrderInfoResponse.ChangeOrderInfo.PipelineInfo.StageInfoDTO.StageResultDTO;
import com.aliyuncs.exceptions.ClientException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import java.util.List;
import static com.aliyun.enums.Constants.INTEGER_FIELD_NOT_INITIALIZED;
public class ChangeOrderManager {
private Log logger;
private ChangeOrderTraceState curTraceState;
private DefaultConfigBean config;
public ChangeOrderManager(DefaultConfigBean config, Log logger) {
this.config = config;
this.logger = logger;
this.curTraceState = new ChangeOrderTraceState();
}
public void trace(DefaultAcsClient defaultAcsClient, String changeOrderId) throws MojoExecutionException {
logger.info("Begin to trace change order: " + changeOrderId);
for (;;) {
boolean finish = trace0(defaultAcsClient, changeOrderId);
if (finish) {
break;
}
sleep(1000);
}
}
private boolean trace0(DefaultAcsClient defaultAcsClient, String changeOrderId) throws MojoExecutionException {
GetChangeOrderInfoRequest request = new GetChangeOrderInfoRequest();
request.setChangeOrderId(changeOrderId);
String routeKey = CommonUtils.getPopApiRouteKey();
if (routeKey != null) {
request.putHeaderParameter(Constants.POP_API_ROUTE_KEY, routeKey);
}
ChangeOrderTraceState beforeState = curTraceState.clone();
try {
GetChangeOrderInfoResponse response = defaultAcsClient.getAcsResponse(request);
if (response.getCode() == 200) {
boolean finish = handleSuccessResponse(response);
if (finish) {
return true;
}
} else {
String msg = String.format("Failed to get change order info, code:%d, message:%s",
response.getCode(), response.getMessage());
logger.error(msg);
}
} catch (ClientException ex) {
String msg = "Failed to get change order info: " + ex.getMessage();
logger.error(msg);
}
if (beforeState.equals(curTraceState)) {
logger.info("Waiting...");
}
return false;
}
private boolean handleSuccessResponse(GetChangeOrderInfoResponse response) throws MojoExecutionException {
ChangeOrderInfo orderInfo = response.getChangeOrderInfo();
List pipelineInfos = orderInfo.getPipelineInfoList();
while (curTraceState.getPipelineCounter() < pipelineInfos.size()) {
int beforePipelineCounter = curTraceState.getPipelineCounter();
showPipeline(pipelineInfos.get(curTraceState.getPipelineCounter()));
if (beforePipelineCounter == curTraceState.getPipelineCounter()) {
break;
}
}
if (isEndStatus(orderInfo.getStatus())) {
Status status = Status.getByVal(orderInfo.getStatus());
switch (status) {
case SUCCESS:
logger.info("Deploy application successfully!");
return true;
case EXCEPTION:
throw new MojoExecutionException("Deploy failed due to exception");
case FAIL:
throw new MojoExecutionException("Deploy failed");
case ABORT:
throw new MojoExecutionException("Deploy failed due to abort");
default:
throw new MojoExecutionException("Deploy failed for unknown reason.");
}
}
return false;
}
private void showPipeline(PipelineInfo pipelineInfo) throws MojoExecutionException {
if (!curTraceState.isHadPrintPipelineInfo()) {
String pipelineId = pipelineInfo.getPipelineId();
String pipelineName = pipelineInfo.getPipelineName();
logger.info(String.format("PipelineName:%s, PipelineId:%s", pipelineName, pipelineId));
curTraceState.setHadPrintPipelineInfo(true);
}
List stageInfoDTOS = pipelineInfo.getStageList();
if (curTraceState.getStageCounter() < stageInfoDTOS.size()) {
while (curTraceState.getStageCounter() < stageInfoDTOS.size()) {
int beforeStageCounter = curTraceState.getStageCounter();
showStage(stageInfoDTOS.get(curTraceState.getStageCounter()));
if (beforeStageCounter == curTraceState.getStageCounter()) {
break;
}
}
}
if (isSuccessStatus(pipelineInfo.getPipelineStatus())) {
curTraceState.setPipelineCounter(curTraceState.getPipelineCounter() + 1);
curTraceState.resetPipelineState();
}
}
private void showStage(StageInfoDTO stageInfo) throws MojoExecutionException {
if (!curTraceState.isHadPrintStageInfo()) {
String stageId = stageInfo.getStageId();
String stageName = stageInfo.getStageName();
logger.info(String.format("StageName:%s, StageId:%s", stageName, stageId));
curTraceState.setHadPrintStageInfo(true);
}
StageResultDTO stageResult = stageInfo.getStageResultDTO();
showStageResult(stageResult);
if (isSuccessStatus(stageInfo.getStatus())) {
curTraceState.setStageCounter(curTraceState.getStageCounter() + 1);
curTraceState.resetStageState();
}
}
private void showStageResult(StageResultDTO stageResult) throws MojoExecutionException {
StageResultDTO.ServiceStage serviceStage = stageResult.getServiceStage();
List instances = stageResult.getInstanceDTOList();
if (serviceStage != null && serviceStage.getStageId() != null) {
if (!curTraceState.isHadPrintServiceStageInfo()) {
String stageName = serviceStage.getStageName();
String stageId = serviceStage.getStageId();
logger.info(String.format("ServiceStageName:%s, ServiceStageId:%s", stageName, stageId));
curTraceState.setHadPrintServiceStageInfo(true);
curTraceState.getTimeoutManager().setServiceStageStartTime(System.currentTimeMillis());
}
if (isSuccessStatus(serviceStage.getStatus())) {
curTraceState.resetServiceStage();
}
checkBeginEndTimeout(
curTraceState.getTimeoutManager().getServiceStageStartTime(),
System.currentTimeMillis(),
config.getApp().getServiceStageTimeout() * 1000,
"Time out for waiting serviceStage: " + serviceStage.getStageName());
}
if (instances != null && instances.size() > 0) {
while (curTraceState.getInstanceCounter() < instances.size()) {
int beforeInstanceCounter = curTraceState.getInstanceCounter();
showInstance(instances.get(curTraceState.getInstanceCounter()));
if (beforeInstanceCounter == curTraceState.getInstanceCounter()) {
break;
}
}
}
}
private void checkBeginEndTimeout(long begin, long end, long timeout, String msg) throws MojoExecutionException {
if (begin != INTEGER_FIELD_NOT_INITIALIZED
&& end != INTEGER_FIELD_NOT_INITIALIZED
&& end - begin > timeout) {
throw new MojoExecutionException(msg);
}
}
private void showInstance(StageResultDTO.InstanceDTO instance) throws MojoExecutionException {
if (!curTraceState.isHadPrintInstanceInfo()) {
String instanceName = instance.getInstanceName();
String instanceIp = instance.getInstanceIp();
logger.info(String.format("InstanceName:%s, InstanceIp:%s", instanceName, instanceIp));
curTraceState.setHadPrintInstanceInfo(true);
}
List instanceStages = instance.getInstanceStageDTOList();
if (curTraceState.getInstanceStageCounter() < instanceStages.size()) {
while (curTraceState.getInstanceStageCounter() < instanceStages.size()) {
int beforeInstanceStageCounter = curTraceState.getInstanceStageCounter();
showInstanceStage(instanceStages.get(curTraceState.getInstanceStageCounter()));
if (beforeInstanceStageCounter == curTraceState.getInstanceStageCounter()) {
break;
}
}
}
if (isSuccessStatus(instance.getStatus())) {
curTraceState.setInstanceCounter(curTraceState.getInstanceCounter() + 1);
curTraceState.resetInstanceState();
}
}
private void showInstanceStage(StageResultDTO.InstanceDTO.InstanceStageDTO instanceStage)
throws MojoExecutionException {
if (!curTraceState.isHadPrintInstanceStageInfo()) {
String stageName = instanceStage.getStageName();
String stageId = instanceStage.getStageId();
logger.info(String.format("InstanceStageName:%s, InstanceStageId:%s", stageName, stageId));
curTraceState.setHadPrintInstanceStageInfo(true);
curTraceState.getTimeoutManager().setInstanceStageStartTime(System.currentTimeMillis());
}
if (isSuccessStatus(instanceStage.getStatus())) {
curTraceState.setInstanceStageCounter(curTraceState.getInstanceStageCounter() + 1);
curTraceState.resetInstanceStageState();
}
checkBeginEndTimeout(
curTraceState.getTimeoutManager().getInstanceStageStartTime(),
System.currentTimeMillis(),
config.getApp().getInstanceStageTimeout() * 1000,
"Time out for waiting instanceStage: " + instanceStage.getStageName());
}
private boolean isEndStatus(Integer s) {
Status status = Status.getByVal(s);
switch (status) {
case FAIL:
case ABORT:
case SUCCESS:
case EXCEPTION:
return true;
default:
return false;
}
}
private boolean isSuccessStatus(Integer s) {
return s != null && s == Status.SUCCESS.getVal();
}
private void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (Exception ex) {
//ignore
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy