Adding update application command in cli
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/0ee04f91 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/0ee04f91 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/0ee04f91 Branch: refs/heads/master Commit: 0ee04f913ab2f728e78c4d1b3b7d48b012cee60a Parents: 7dc0c9a Author: Pubudu Gunatilaka <[email protected]> Authored: Thu May 21 00:42:56 2015 +0530 Committer: Lahiru Sandaruwan <[email protected]> Committed: Thu May 21 00:47:03 2015 +0530 ---------------------------------------------------------------------- .../services/impl/AutoscalerServiceImpl.java | 25 +- .../stratos/autoscaler/util/AutoscalerUtil.java | 31 +- .../stratos/cli/RestCommandLineService.java | 13 + .../apache/stratos/cli/StratosApplication.java | 3 + .../cli/commands/UpdateApplicationCommand.java | 125 ++++ .../src/main/resources/AutoscalerService.wsdl | 634 +++++++++---------- 6 files changed, 493 insertions(+), 338 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/0ee04f91/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/services/impl/AutoscalerServiceImpl.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/services/impl/AutoscalerServiceImpl.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/services/impl/AutoscalerServiceImpl.java index 1d97697..8587279 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/services/impl/AutoscalerServiceImpl.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/services/impl/AutoscalerServiceImpl.java @@ -209,25 +209,31 @@ public class AutoscalerServiceImpl implements AutoscalerService { ApplicationParser applicationParser = new DefaultApplicationParser(); Application application = applicationParser.parse(applicationContext); - //Need to update the application - AutoscalerUtil.getInstance().updateApplicationsTopology(application); + ApplicationContext existingApplicationContext = AutoscalerContext.getInstance(). + getApplicationContext(applicationId); + + if (existingApplicationContext.getStatus().equals(ApplicationContext.STATUS_DEPLOYED)) { + + //Need to update the application + AutoscalerUtil.getInstance().updateApplicationsTopology(application); + + //Update the clusterMonitors + AutoscalerUtil.getInstance().updateClusterMonitor(application); + + } - //Update the clusterMonitors - AutoscalerUtil.getInstance().updateClusterMonitor(application); + applicationContext.setStatus(existingApplicationContext.getStatus()); List<ApplicationClusterContext> applicationClusterContexts = applicationParser.getApplicationClusterContexts(); ApplicationClusterContext[] applicationClusterContextsArray = applicationClusterContexts.toArray( new ApplicationClusterContext[applicationClusterContexts.size()]); applicationContext.getComponents().setApplicationClusterContexts(applicationClusterContextsArray); - ApplicationContext existingApplicationContext = AutoscalerContext.getInstance(). - getApplicationContext(applicationId); - applicationContext.setStatus(existingApplicationContext.getStatus()); //updating the applicationContext AutoscalerContext.getInstance().updateApplicationContext(applicationContext); if (log.isInfoEnabled()) { - log.info(String.format("Application added successfully: [application-id] %s", + log.info(String.format("Application updated successfully: [application-id] %s", applicationId)); } return true; @@ -1218,8 +1224,7 @@ public class AutoscalerServiceImpl implements AutoscalerService { } if (removableDeploymentPolicy(deploymentPolicyID)) { PolicyManager.getInstance().removeDeploymentPolicy(deploymentPolicyID); - } - else { + } else { throw new UnremovablePolicyException("This deployment policy cannot be removed, since it is used in an " + "application."); } http://git-wip-us.apache.org/repos/asf/stratos/blob/0ee04f91/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java index 40d3438..9a10070 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java @@ -773,19 +773,28 @@ public class AutoscalerUtil { for (Group existingGroup : existingGroups) { Group newGroup = application.getGroupRecursively(existingGroup.getUniqueIdentifier()); if (newGroup != null) { - //Finding the GroupMonitor based on ApplicationMonitor - GroupMonitor groupMonitor = (GroupMonitor) AutoscalerContext.getInstance(). - getAppMonitor(application.getUniqueIdentifier()). - findGroupMonitorWithId(existingGroup.getUniqueIdentifier()); - //Updating the GroupMonitor - for (NetworkPartitionContext networkPartitionContext : groupMonitor. - getNetworkPartitionContextsMap().values()) { - ((ParentLevelNetworkPartitionContext) networkPartitionContext). - setMinInstanceCount(newGroup.getGroupMinInstances()); - ((ParentLevelNetworkPartitionContext) networkPartitionContext). - setMaxInstanceCount(newGroup.getGroupMaxInstances()); + + ApplicationMonitor applicationMonitor = AutoscalerContext.getInstance(). + getAppMonitor(application.getUniqueIdentifier()); + + if (applicationMonitor != null) { + //Finding the GroupMonitor based on ApplicationMonitor + GroupMonitor groupMonitor = (GroupMonitor) applicationMonitor. + findGroupMonitorWithId(existingGroup.getUniqueIdentifier()); + + if (groupMonitor != null) { + //Updating the GroupMonitor + for (NetworkPartitionContext networkPartitionContext : groupMonitor. + getNetworkPartitionContextsMap().values()) { + ((ParentLevelNetworkPartitionContext) networkPartitionContext). + setMinInstanceCount(newGroup.getGroupMinInstances()); + ((ParentLevelNetworkPartitionContext) networkPartitionContext). + setMaxInstanceCount(newGroup.getGroupMaxInstances()); + } + } } + try { ApplicationHolder.acquireWriteLock(); //update the min and max of Group instances http://git-wip-us.apache.org/repos/asf/stratos/blob/0ee04f91/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java index 8dd1b42..27174bf 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java @@ -138,11 +138,13 @@ public class RestCommandLineService { private static final String ENDPOINT_APPLICATION_SIGNUP = API_CONTEXT + "/applications/{applicationId}/signup"; private static final String ENDPOINT_UPDATE_DEPLOYMENT_POLICY = API_CONTEXT + "/deploymentPolicies"; + private static final String ENDPOINT_UPDATE_APPLICATION = API_CONTEXT + "/applications"; private static final String ENDPOINT_UPDATE_APPLICATION_POLICY = API_CONTEXT + "/applicationPolicies"; private static final String ENDPOINT_UPDATE_AUTOSCALING_POLICY = API_CONTEXT + "/autoscalingPolicies"; private static final String ENDPOINT_UPDATE_USER = API_CONTEXT + "/users"; private static final String ENDPOINT_UPDATE_TENANT = API_CONTEXT + "/tenants"; + private static class SingletonHolder { private final static RestCommandLineService INSTANCE = new RestCommandLineService(); } @@ -1575,6 +1577,16 @@ public class RestCommandLineService { } /** + * Update application + * + * @param entityBody application definition + * @throws CommandException + */ + public void updateApplication(String entityBody) throws CommandException { + restClient.updateEntity(ENDPOINT_UPDATE_APPLICATION, entityBody, "application"); + } + + /** * Delete autoscaling policy * * @param autoscalingPolicyId autoscaling policy id @@ -1942,4 +1954,5 @@ public class RestCommandLineService { restClient.updateEntity(ENDPOINT_UPDATE_APPLICATION_POLICY, applicationPolicy, "application policy"); } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/0ee04f91/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java index 54a5d8b..2fed93e 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java @@ -288,6 +288,9 @@ public class StratosApplication extends CommandLineApplication<StratosCommandCon command = new UpdateApplicationPolicyCommand(); commands.put(command.getName(), command); + command = new UpdateApplicationCommand(); + commands.put(command.getName(), command); + if (logger.isDebugEnabled()) { logger.debug("Created {} commands for the application. {}", commands.size(), commands.keySet()); } http://git-wip-us.apache.org/repos/asf/stratos/blob/0ee04f91/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UpdateApplicationCommand.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UpdateApplicationCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UpdateApplicationCommand.java new file mode 100644 index 0000000..6e80f77 --- /dev/null +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UpdateApplicationCommand.java @@ -0,0 +1,125 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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 org.apache.stratos.cli.commands; + +import org.apache.commons.cli.*; +import org.apache.stratos.cli.Command; +import org.apache.stratos.cli.RestCommandLineService; +import org.apache.stratos.cli.StratosCommandContext; +import org.apache.stratos.cli.exception.CommandException; +import org.apache.stratos.cli.utils.CliConstants; +import org.apache.stratos.cli.utils.CliUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +import static org.apache.stratos.cli.utils.CliUtils.mergeOptionArrays; + +/** + * Update application command. + */ +public class UpdateApplicationCommand implements Command<StratosCommandContext> { + + private static final Logger log = LoggerFactory.getLogger(UpdateApplicationCommand.class); + + private final Options options; + + public UpdateApplicationCommand() { + options = constructOptions(); + } + + private Options constructOptions() { + final Options options = new Options(); + + Option resourcePath = new Option(CliConstants.RESOURCE_PATH, CliConstants.RESOURCE_PATH_LONG_OPTION, true, + "Application resource path"); + resourcePath.setArgName("resource path"); + options.addOption(resourcePath); + + return options; + } + + public String getName() { + return "update-application"; + } + + public String getDescription() { + return "Update an application using cartridge groups/cartridges."; + } + + public String getArgumentSyntax() { + return null; + } + + public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts) throws CommandException { + if (log.isDebugEnabled()) { + log.debug("Executing {} command...", getName()); + } + + if (args != null && args.length > 0) { + String resourcePath = null; + String resourceFileContent = null; + + final CommandLineParser parser = new GnuParser(); + CommandLine commandLine; + + try { + commandLine = parser.parse(options, args); + //merge newly discovered options with previously discovered ones. + Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions()); + + if (log.isDebugEnabled()) { + log.debug("Updating application..."); + } + + if (opts.hasOption(CliConstants.RESOURCE_PATH)) { + if (log.isTraceEnabled()) { + log.trace("Resource path option is passed"); + } + resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue(); + resourceFileContent = CliUtils.readResource(resourcePath); + } + + if (resourcePath == null) { + context.getStratosApplication().printUsage(getName()); + return CliConstants.COMMAND_FAILED; + } + + RestCommandLineService.getInstance().updateApplication(resourceFileContent); + return CliConstants.COMMAND_SUCCESSFULL; + + } catch (ParseException e) { + log.error("Error parsing arguments", e); + System.out.println(e.getMessage()); + return CliConstants.COMMAND_FAILED; + } catch (IOException e) { + System.out.println("Invalid resource path"); + return CliConstants.COMMAND_FAILED; + } + } else { + context.getStratosApplication().printUsage(getName()); + return CliConstants.COMMAND_FAILED; + } + } + + public Options getOptions() { + return options; + } +} \ No newline at end of file
