package com.cloudbees.sdk.commands.bg;
import com.cloudbees.api.ApplicationInfo;
import com.cloudbees.api.BeesClient;
import com.cloudbees.api.ConfigurationParametersResponse;
import com.cloudbees.api.config.ConfigParameters;
import com.cloudbees.api.config.ParameterMap;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;
import com.cloudbees.sdk.commands.app.ApplicationDeploy;
import com.staxnet.appserver.config.AppConfig;
import java.io.IOException;
@BeesCommand(group="Application", description = "Deploy an application using the Blue-Green process")
@CLICommand("app:bg:deploy")
public class ApplicationBlueGreenDeploy extends ApplicationDeploy {
protected String initAppId(String appid, AppConfig appConfig) throws IOException {
String appId = super.initAppId(appid, appConfig);
// Overwrite appId if blue-green parameter is defined
try {
BeesClient client = getAppClient(appId);
ConfigurationParametersResponse res = client.configurationParameters(appId, "application");
if (res.getConfiguration() != null) {
ConfigParameters configParameters = ConfigParameters.parse(res.getConfiguration());
ParameterMap parameterMap = configParameters.getParameters();
String param = parameterMap.get("blue-green");
if (param != null) {
ApplicationAlias bgApp = new ApplicationAlias(getAccount(), param);
// Get the current alias if any
ApplicationInfo applicationInfo = client.applicationInfo(appId);
String aliases = applicationInfo.getSettings().get("aliases");
ApplicationAlias currentApp = new ApplicationAlias(getAccount(), appId, aliases);
// If the current app has the alias already, deploy to the other app
if (currentApp.containAliases(bgApp.getAliases())) {
appId = bgApp.getAppId();
System.out.println("Blue-Green deployment enabled, switching to application ID: " + appId);
// If the current app does not have the alias, deploy to it
} else {
System.out.println("Blue-Green deployment enabled, using application ID: " + appId);
}
} else {
throw new IllegalArgumentException("Application not configured for Blue-Green deployment. Use the \"blue-green=APP_ID[ALIASES]\" parameter");
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Application not configured for Blue-Green deployment. Use the \"blue-green=APP_ID[ALIASES]\" parameter", e);
}
return appId;
}
}