From 3dd7a24f657aecd661c9b6c412cc68af969275c3 Mon Sep 17 00:00:00 2001 From: Pubudu Gunatilaka Date: Wed, 20 May 2015 21:42:11 +0530 Subject: [PATCH 1/3] Adding update application in cli --- .../services/impl/AutoscalerServiceImpl.java | 25 +- .../autoscaler/util/AutoscalerUtil.java | 37 +- .../stratos/cli/RestCommandLineService.java | 13 + .../stratos/cli/StratosApplication.java | 3 + .../commands/UpdateApplicationCommand.java | 125 ++++ .../{ => wsdl}/AutoscalerService.wsdl | 606 +++++++++--------- 6 files changed, 492 insertions(+), 317 deletions(-) create mode 100644 components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UpdateApplicationCommand.java rename service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/{ => wsdl}/AutoscalerService.wsdl (94%) 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 6f5ff51495..e59ffcafb2 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 @@ -206,25 +206,31 @@ public boolean updateApplication(ApplicationContext applicationContext) 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 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; @@ -1215,8 +1221,7 @@ public boolean removeDeployementPolicy(String deploymentPolicyID) throws Deploym } 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."); } 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 40d3438322..8e530b7c7b 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,34 @@ public void updateApplicationsTopology(Application application) 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()); + } + } } + +// //Finding the GroupMonitor based on ApplicationMonitor +// GroupMonitor groupMonitor = (GroupMonitor) AutoscalerContext.getInstance(). +// getAppMonitor(application.getUniqueIdentifier()). +// findGroupMonitorWithId(existingGroup.getUniqueIdentifier()); + + try { ApplicationHolder.acquireWriteLock(); //update the min and max of Group instances 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 01bc45dd6f..8d780e8923 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(); } @@ -1592,6 +1594,16 @@ public void deleteApplication(String applicationId) throws CommandException { "application"); } + /** + * 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 * @@ -1960,4 +1972,5 @@ public void updateApplicationPolicy(String applicationPolicy) throws CommandExce restClient.updateEntity(ENDPOINT_UPDATE_APPLICATION_POLICY, applicationPolicy, "application policy"); } + } \ No newline at end of file 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 54a5d8b34a..2fed93e9aa 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 @@ private void createCommands() { 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()); } 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 0000000000..6e80f77841 --- /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 + *

+ * 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 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 { + + 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 diff --git a/service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/AutoscalerService.wsdl b/service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/wsdl/AutoscalerService.wsdl similarity index 94% rename from service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/AutoscalerService.wsdl rename to service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/wsdl/AutoscalerService.wsdl index 8e70bb773e..7691050eb1 100644 --- a/service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/AutoscalerService.wsdl +++ b/service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/wsdl/AutoscalerService.wsdl @@ -1,10 +1,10 @@ - + - + - + @@ -21,7 +21,7 @@ - + @@ -33,7 +33,7 @@ - + @@ -43,7 +43,7 @@ - + @@ -67,7 +67,7 @@ - + @@ -108,7 +108,7 @@ - + @@ -143,8 +143,8 @@ - - + + @@ -156,207 +156,196 @@ - - - - - + + + - + - + + + - + - + - + - + - - - + - + - + - + - + - + - + - - - - - - + - + + - + - + - + - + - + - + - - - - - - + - + - + - + - + - + - + - - - + - + - + - + - + - + - + - + - + - + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -377,13 +366,6 @@ - - - - - - - @@ -414,327 +396,345 @@ - + - + - + - - + - + - + - + - + - + - + - + - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + - + - + - + - + - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + - + - + + + + + + + + - + - + - + @@ -742,32 +742,32 @@ - - + + + + + + + + - - - - - - - + - + - + @@ -776,22 +776,22 @@ - + - + - + - + @@ -800,7 +800,7 @@ - + @@ -810,7 +810,7 @@ - + @@ -819,18 +819,26 @@ - + - + + + + + + + + + @@ -842,14 +850,6 @@ - - - - - - - - @@ -857,6 +857,9 @@ + + + @@ -864,17 +867,14 @@ - - - - - - + + + @@ -898,6 +898,12 @@ + + + + + + @@ -919,12 +925,6 @@ - - - - - - @@ -1018,18 +1018,18 @@ - - - - - - + + + + + + @@ -1117,18 +1117,18 @@ - - - - - - + + + + + + @@ -1140,6 +1140,10 @@ + + + + @@ -1151,10 +1155,6 @@ - - - - @@ -1215,14 +1215,14 @@ - - - - + + + + @@ -1236,6 +1236,7 @@ + @@ -1250,6 +1251,7 @@ + @@ -1283,15 +1285,15 @@ + + + + - - - - @@ -1319,15 +1321,6 @@ - - - - - - - - - @@ -1355,6 +1348,15 @@ + + + + + + + + + @@ -1499,8 +1501,8 @@ - - + + @@ -1508,8 +1510,8 @@ - - + + @@ -1549,6 +1551,9 @@ + + + @@ -1582,6 +1587,9 @@ + + + @@ -1661,26 +1669,26 @@ - - + + + + + - - + + - - - @@ -1709,15 +1717,6 @@ - - - - - - - - - @@ -1745,6 +1744,15 @@ + + + + + + + + + @@ -1889,8 +1897,8 @@ - - + + @@ -1898,8 +1906,8 @@ - - + + @@ -1939,6 +1947,9 @@ + + + @@ -1972,6 +1983,9 @@ + + + @@ -2051,26 +2065,26 @@ - - + + + + + - - + + - - - @@ -2093,8 +2107,8 @@ - - + + @@ -2102,8 +2116,8 @@ - - + + @@ -2111,8 +2125,8 @@ - - + + @@ -2228,8 +2242,8 @@ - - + + @@ -2237,8 +2251,8 @@ - - + + @@ -2354,8 +2368,8 @@ - - + + @@ -2363,8 +2377,8 @@ - - + + From b2f6572fde15d7c4b455447214d2c9e86fb4cc13 Mon Sep 17 00:00:00 2001 From: Pubudu Gunatilaka Date: Wed, 20 May 2015 23:38:01 +0530 Subject: [PATCH 2/3] Removed commented code --- .../org/apache/stratos/autoscaler/util/AutoscalerUtil.java | 6 ------ 1 file changed, 6 deletions(-) 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 8e530b7c7b..9a10070d80 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 @@ -795,12 +795,6 @@ public void updateApplicationsTopology(Application application) } -// //Finding the GroupMonitor based on ApplicationMonitor -// GroupMonitor groupMonitor = (GroupMonitor) AutoscalerContext.getInstance(). -// getAppMonitor(application.getUniqueIdentifier()). -// findGroupMonitorWithId(existingGroup.getUniqueIdentifier()); - - try { ApplicationHolder.acquireWriteLock(); //update the min and max of Group instances From 4e9e52070c11294d95aadb82f7fb87f45de89e81 Mon Sep 17 00:00:00 2001 From: Pubudu Gunatilaka Date: Wed, 20 May 2015 23:56:29 +0530 Subject: [PATCH 3/3] Fixed minor changes --- .../src/main/resources/{wsdl => }/AutoscalerService.wsdl | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/{wsdl => }/AutoscalerService.wsdl (100%) diff --git a/service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/wsdl/AutoscalerService.wsdl b/service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/AutoscalerService.wsdl similarity index 100% rename from service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/wsdl/AutoscalerService.wsdl rename to service-stubs/org.apache.stratos.autoscaler.service.stub/src/main/resources/AutoscalerService.wsdl