/*
* Copyright 2010-2013, CloudBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cloudbees.sdk.commands.bg;
import com.cloudbees.api.ApplicationInfo;
import com.cloudbees.api.BeesClient;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;
import com.cloudbees.sdk.cli.CommandService;
import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@BeesCommand(group="Application", description = "Get Blue-Green application information")
@CLICommand("app:bg:info")
public class ApplicationBlueGreenInfo extends ApplicationBlueGreenBase {
@Inject
CommandService cs;
public ApplicationBlueGreenInfo() {
}
@Override
public int main() throws Exception {
BeesClient client = getBeesClient(BeesClient.class);
List<String> applicationIds = getApplicationIds(client, getName());
System.out.println();
System.out.println("PRIMARY APPLICATION");
System.out.println("===================");
displayApplicationInfo(applicationIds.get(0));
System.out.println();
System.out.println("SECONDARY APPLICATION");
System.out.println("=====================");
displayApplicationInfo(applicationIds.get(1));
return 0;
}
protected List<String> getApplicationIds(BeesClient client, String name) throws IOException {
try {
List<String> applicationIds = new ArrayList<String>();
BlueGreenSettings blueGreenSettings = BlueGreenSettings.getInstance(client, getAccount(), name);
System.out.println("Blue-Green settings for: " + getName());
System.out.println(" Primary Aliases: " + blueGreenSettings.getActiveAliasesString());
System.out.println(" Application 1 : " + blueGreenSettings.getApplication1());
System.out.println(" Application 2 : " + blueGreenSettings.getApplication2());
// Get the app1 alias
ApplicationInfo applicationInfo = client.applicationInfo(blueGreenSettings.getApplication1());
String aliases = applicationInfo.getSettings().get("aliases");
ApplicationAlias application1 = new ApplicationAlias(getAccount(), blueGreenSettings.getApplication1(), aliases);
// If the app1 has the alias already, it is the active one
if (application1.containAliases(blueGreenSettings.getActiveAliases())) {
applicationIds.add(blueGreenSettings.getApplication1());
applicationIds.add(blueGreenSettings.getApplication2());
// If the app1 does not have the alias, it is the passive one
} else {
applicationIds.add(blueGreenSettings.getApplication2());
applicationIds.add(blueGreenSettings.getApplication1());
}
return applicationIds;
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
throw new IllegalArgumentException("Application not configured for Blue-Green deployment.", e);
}
}
protected void displayApplicationInfo(String appId) throws Exception {
List<String> deployArgs = new ArrayList<String>();
deployArgs.add(0,"app:info");
deployArgs.add("-a");
deployArgs.add(appId);
cs.getCommand("app:info").run(deployArgs);
}
}