Package com.cloudbees.sdk.commands.bg

Source Code of com.cloudbees.sdk.commands.bg.ApplicationBlueGreenStop

/*
* 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.ApplicationStatusResponse;
import com.cloudbees.api.BeesClient;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;
import com.cloudbees.sdk.utils.Helper;
import org.kohsuke.args4j.Option;

import java.io.IOException;

@BeesCommand(group="Application", description = "Stop the non-active Blue-Green application")
@CLICommand("app:bg:stop")
public class ApplicationBlueGreenStop extends ApplicationBlueGreenBase {

    @Option(name = "-f", usage = "Force stop without prompting", aliases = "--force")
    private Boolean force;


    public ApplicationBlueGreenStop() {
    }

    @Override
    public int main() throws Exception {
        BeesClient client = getBeesClient(BeesClient.class);

        String appId = getSecondaryApplicationId(client, getName());

        if (force == null || !force.booleanValue()) {
            if (!Helper.promptMatches(String.format("Are you sure you want to stop the application [%s]: (y/n) ", appId), "[yY].*")) {
                return 0;
            }
        }

        ApplicationStatusResponse res = client.applicationStop(appId);
        if (res.getStatus().equalsIgnoreCase("stopped"))
            System.out.println("application stopped - " + appId);
        else
            System.out.println("application could not be stopped, current status: " + res.getStatus());

        return 0;
    }

    protected String getSecondaryApplicationId(BeesClient client, String name) throws IOException {
        try {
            String passiveAppId;
            BlueGreenSettings blueGreenSettings = BlueGreenSettings.getInstance(client, getAccount(), name);

            // 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, stop the other app
            if (application1.containAliases(blueGreenSettings.getActiveAliases())) {
                passiveAppId = blueGreenSettings.getApplication2();
            // If the app1 does not have the alias, stop it
            } else {
                passiveAppId = blueGreenSettings.getApplication1();
            }
            System.out.println("Blue-Green secondary application: " + passiveAppId);

            return passiveAppId;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalArgumentException("Application not configured for Blue-Green deployment.", e);
        }
    }

}
TOP

Related Classes of com.cloudbees.sdk.commands.bg.ApplicationBlueGreenStop

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.