Updated Branches:
  refs/heads/master 4886e11ab -> fd40aa9a1

STRATOS-277 - Provide operation for deployService


Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/fd40aa9a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/fd40aa9a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/fd40aa9a

Branch: refs/heads/master
Commit: fd40aa9a1fefd2b0c079b76852929fa194595a32
Parents: 4886e11
Author: Manula Thantriwatte <[email protected]>
Authored: Tue Feb 11 17:22:30 2014 +0530
Committer: Manula Thantriwatte <[email protected]>
Committed: Tue Feb 11 17:22:30 2014 +0530

----------------------------------------------------------------------
 .../stratos/cli/RestCommandLineService.java     |  27 ++++
 .../apache/stratos/cli/StratosApplication.java  |   3 +
 .../DeployServiceDeploymentCommand.java         | 139 +++++++++++++++++++
 .../apache/stratos/cli/utils/CliConstants.java  |   5 +
 4 files changed, 174 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fd40aa9a/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 6b74e8f..ee3c53e 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
@@ -80,6 +80,7 @@ public class RestCommandLineService {
     private final String listAutoscalePolicyRestEndPoint = 
"/stratos/admin/policy/autoscale";
     private final String describeDeploymentPolicyRestEndPoint = 
"/stratos/admin/policy/deployment/";
     private final String listDeploymentPolicyRestEndPoint = 
"/stratos/admin/policy/deployment";
+    private final String deployServiceEndPoint = 
"/stratos/admin/service/definition";
 
     private static class SingletonHolder {
                private final static RestCommandLineService INSTANCE = new 
RestCommandLineService();
@@ -941,6 +942,32 @@ public class RestCommandLineService {
         }
     }
 
+    // This method helps to deploy service
+    public void deployService (String deployService) throws CommandException{
+        DefaultHttpClient httpClient= new DefaultHttpClient();
+        try {
+            HttpResponse response = restClientService.doPost(httpClient, 
restClientService.getUrl() + deployServiceEndPoint,
+                    deployService, restClientService.getUsername(), 
restClientService.getPassword());
+
+            String responseCode = "" + 
response.getStatusLine().getStatusCode();
+            if (responseCode.equals("" + 
CliConstants.RESPONSE_AUTHORIZATION_FAIL)) {
+                System.out.println("Invalid operations. Authorization failed");
+                return;
+            } else if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
+                System.out.println("Error occured while deploying service");
+                return;
+            } else {
+                System.out.println("You have successfully deployed the 
service");
+                return;
+            }
+
+        } catch (Exception e) {
+            handleException("Exception in deploying autoscale police", e);
+        } finally {
+            httpClient.getConnectionManager().shutdown();
+        }
+    }
+
     // This method helps to deploy deployment polices
     public void deployDeploymentPolicy (String deploymentPolicy) throws 
CommandException{
         DefaultHttpClient httpClient = new DefaultHttpClient();

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fd40aa9a/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 68730c3..f47bea7 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
@@ -111,6 +111,9 @@ public class StratosApplication extends 
CommandLineApplication<StratosCommandCon
         command = new AutoscalingPolicyDeploymentCommand();
         commands.put(command.getName(), command);
 
+        command = new DeployServiceDeploymentCommand();
+        commands.put(command.getName(), command);
+
         command = new DeploymentPolicyDeploymentCommand();
         commands.put(command.getName(), command);
                

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fd40aa9a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DeployServiceDeploymentCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DeployServiceDeploymentCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DeployServiceDeploymentCommand.java
new file mode 100644
index 0000000..95d9647
--- /dev/null
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DeployServiceDeploymentCommand.java
@@ -0,0 +1,139 @@
+/**
+ *  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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+
+public class DeployServiceDeploymentCommand implements 
Command<StratosCommandContext> {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(DeployServiceDeploymentCommand.class);
+
+    private final Options options;
+
+    public DeployServiceDeploymentCommand(){
+        options = constructOptions();
+    }
+
+    private Options constructOptions() {
+        final Options options = new Options();
+
+        Option resourcePath = new Option(CliConstants.RESOURCE_PATH, 
CliConstants.RESOURCE_PATH_LONG_OPTION, true,
+                "Deploy Service resource path");
+        resourcePath.setArgName("resource path");
+        options.addOption(resourcePath);
+
+        return options;
+    }
+
+    public String getName() {
+        return CliConstants.DEPLOY_SERVICE_DEPLOYMENT;
+    }
+
+    public String getDescription() {
+        return "Add new deploy service";
+    }
+
+    public String getArgumentSyntax() {
+        return null;
+    }
+
+    public int execute(StratosCommandContext context, String[] args) throws 
CommandException {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Executing {} command...", getName());
+        }
+
+        if (args != null && args.length > 0) {
+            String resourcePath = null;
+            String deployServiceDeployment = null;
+
+            final CommandLineParser parser = new GnuParser();
+            CommandLine commandLine;
+
+            try {
+                commandLine = parser.parse(options, args);
+
+                if (logger.isDebugEnabled()) {
+                    logger.debug("Deploy Service Deployment");
+                }
+
+                if (commandLine.hasOption(CliConstants.RESOURCE_PATH)) {
+                    if (logger.isTraceEnabled()) {
+                        logger.trace("Resource path option is passed");
+                    }
+                    resourcePath = 
commandLine.getOptionValue(CliConstants.RESOURCE_PATH);
+                    deployServiceDeployment = readResource(resourcePath);
+                }
+
+                if (resourcePath == null) {
+                    System.out.println("usage: " + getName() + " [-p <resource 
path>]");
+                    return CliConstants.BAD_ARGS_CODE;
+                }
+
+                
RestCommandLineService.getInstance().deployService(deployServiceDeployment);
+                return CliConstants.SUCCESSFUL_CODE;
+
+            } catch (ParseException e) {
+                if (logger.isErrorEnabled()) {
+                    logger.error("Error parsing arguments", e);
+                }
+                System.out.println(e.getMessage());
+                return CliConstants.BAD_ARGS_CODE;
+            } catch (IOException e) {
+                //e.printStackTrace();
+                System.out.println("Invalid resource path");
+                return CliConstants.BAD_ARGS_CODE;
+            }
+        } else {
+            context.getStratosApplication().printUsage(getName());
+            return CliConstants.BAD_ARGS_CODE;
+        }
+    }
+
+    private String readResource(String fileName) throws IOException {
+        BufferedReader br = new BufferedReader(new FileReader(fileName));
+        try {
+            StringBuilder sb = new StringBuilder();
+            String line = br.readLine();
+
+            while (line != null) {
+                sb.append(line);
+                sb.append("\n");
+                line = br.readLine();
+            }
+            return sb.toString();
+        } finally {
+            br.close();
+        }
+    }
+
+    public Options getOptions() {
+        return options;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fd40aa9a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
index 9d8a8e2..72561fd 100644
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
@@ -115,6 +115,11 @@ public class CliConstants {
     public static final String AUTOSCALING_POLICY_DEPLOYMENT = 
"deploy-autoscaling-policy";
 
     /**
+     * Deploy, deployment service
+     */
+    public static final String DEPLOY_SERVICE_DEPLOYMENT = "deploy-service";
+
+    /**
      * Deployment policy deployment
      */
     public static final String DEPLOYMENT_POLICY_DEPLOYMENT = 
"deploy-deployment-policy";

Reply via email to