You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2015/02/20 17:30:07 UTC

[8/9] stratos git commit: CLI commands added to describea kubernetes cluter and to get the master of a kubernetes cluster.

CLI commands added to describea  kubernetes cluter and to get the master of a kubernetes cluster.


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

Branch: refs/heads/4.1.0-beta-deployment-policy-fix
Commit: 73d58e60c01158f575351c60416f305d6bc9284e
Parents: c76f024
Author: Dinithi <di...@wso2.com>
Authored: Fri Feb 20 14:15:12 2015 +0530
Committer: Dinithi <di...@wso2.com>
Committed: Fri Feb 20 14:15:12 2015 +0530

----------------------------------------------------------------------
 .../stratos/cli/RestCommandLineService.java     | 63 +++++++++++++++--
 .../apache/stratos/cli/StratosApplication.java  |  6 ++
 .../DescribeKubernetesClusterCommand.java       | 71 +++++++++++++++++++
 .../DescribeKubernetesMasterCommand.java        | 72 ++++++++++++++++++++
 4 files changed, 207 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/73d58e60/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 d2f8f52..609620c 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
@@ -44,6 +44,7 @@ import org.apache.stratos.common.beans.cartridge.CartridgeBean;
 import org.apache.stratos.common.beans.cartridge.IaasProviderBean;
 import org.apache.stratos.common.beans.kubernetes.KubernetesClusterBean;
 import org.apache.stratos.common.beans.kubernetes.KubernetesHostBean;
+import org.apache.stratos.common.beans.kubernetes.KubernetesMasterBean;
 import org.apache.stratos.common.beans.partition.NetworkPartitionBean;
 import org.apache.stratos.common.beans.policy.autoscale.AutoscalePolicyBean;
 import org.apache.stratos.common.beans.policy.deployment.DeploymentPolicyBean;
@@ -114,8 +115,8 @@ public class RestCommandLineService {
     private static final String ENDPOINT_GET_CARTRIDGE = API_CONTEXT + "/cartridges/{cartridgeType}";
     private static final String ENDPOINT_GET_CARTRIDGE_OF_TENANT = API_CONTEXT + "/subscriptions/{id}/cartridges";
     private static final String ENDPOINT_GET_KUBERNETES_GROUP = API_CONTEXT + "/kubernetesCluster/{kubernetesClusterId}";
-    private static final String ENDPOINT_GET_KUBERNETES_MASTER = API_CONTEXT + "/kubernetesCluster/{kubernetesClusterId}/master";
-    private static final String ENDPOINT_GET_KUBERNETES_HOST = API_CONTEXT + "/kubernetesCluster/{kubernetesClusterId}/hosts";
+    private static final String ENDPOINT_GET_KUBERNETES_MASTER = API_CONTEXT + "/kubernetesClusters/{kubernetesClusterId}/master";
+    private static final String ENDPOINT_GET_KUBERNETES_HOST_CLUSTER = API_CONTEXT + "/kubernetesClusters/{kubernetesClusterId}";
     private static final String ENDPOINT_GET_NETWORK_PARTITION = API_CONTEXT + "/networkPartitions/{networkPartitionId}";
 
     private static final String ENDPOINT_UPDATE_KUBERNETES_MASTER = API_CONTEXT + "/kubernetesClusters/{kubernetesClusterId}/master";
@@ -866,7 +867,7 @@ public class RestCommandLineService {
      */
     public void listKubernetesClusters() {
         try {
-            Type listType = new TypeToken<ArrayList<KubernetesHostBean>>() {
+            Type listType = new TypeToken<ArrayList<KubernetesClusterBean>>() {
             }.getType();
             List<KubernetesClusterBean> list = (List<KubernetesClusterBean>) restClient.
                     listEntity(ENDPOINT_LIST_KUBERNETES_CLUSTERS, listType, "kubernetes cluster");
@@ -917,8 +918,8 @@ public class RestCommandLineService {
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
 
-            if (responseCode.equals(CliConstants.RESPONSE_OK)) {
-                System.out.println("You have succesfully deployed host to Kubernetes cluster: " + clusterId);
+            if (responseCode.equals(CliConstants.RESPONSE_OK) || responseCode.equals(CliConstants.RESPONSE_CREATED)) {
+                System.out.println("You have successfully deployed host to Kubernetes cluster: " + clusterId);
                 return;
             } else {
                 String resultString = CliUtils.getHttpResponseString(response);
@@ -970,6 +971,58 @@ public class RestCommandLineService {
     }
 
     /**
+     * Get the master of a Kubernetes Cluster
+     * @param clusterId
+     * @throws CommandException
+     */
+    public void getKubernetesMaster(final String clusterId) throws CommandException {
+        try {
+            Type listType = new TypeToken<KubernetesMasterBean>() {
+            }.getType();
+            KubernetesMasterBean master = (KubernetesMasterBean) restClient
+                    .getEntity(ENDPOINT_GET_KUBERNETES_MASTER, KubernetesMasterBean.class, "{kubernetesClusterId}", clusterId,
+                            "network partition");
+
+            if (master == null) {
+                System.out.println("Kubernetes master not found in: " + clusterId);
+                return;
+            }
+
+            System.out.println("Cluster: " + clusterId);
+            System.out.println(getGson().toJson(master));
+        } catch (Exception e) {
+            String message = "Could not get the master of " + clusterId;
+            printError(message, e);
+        }
+    }
+
+    /**
+     * Describe a Kubernetes cluster
+     * @param clusterId
+     * @throws CommandException
+     */
+    public void describeKubernetesCluster(final String clusterId) throws CommandException {
+        try {
+            Type listType = new TypeToken<KubernetesClusterBean>() {
+            }.getType();
+            KubernetesClusterBean cluster = (KubernetesClusterBean) restClient
+                    .getEntity(ENDPOINT_GET_KUBERNETES_HOST_CLUSTER, KubernetesClusterBean.class, "{kubernetesClusterId}", clusterId,
+                            "kubernetes cluster");
+
+            if (cluster == null) {
+                System.out.println("Kubernetes cluster not found: " + clusterId);
+                return;
+            }
+
+            System.out.println("Kubernetes cluster: " + clusterId);
+            System.out.println(getGson().toJson(cluster));
+        } catch (Exception e) {
+            String message = "Could not describe kubernetes cluster: " + clusterId;
+            printError(message, e);
+        }
+    }
+
+    /**
      * Add Domain mappings
      * @throws CommandException
      */

http://git-wip-us.apache.org/repos/asf/stratos/blob/73d58e60/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 f5b45f9..3c3914e 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
@@ -240,6 +240,12 @@ public class StratosApplication extends CommandLineApplication<StratosCommandCon
         command = new DescribeDeploymentPolicyCommand();
         commands.put(command.getName(), command);
 
+        command = new DescribeKubernetesMasterCommand();
+        commands.put(command.getName(), command);
+
+        command = new DescribeKubernetesClusterCommand();
+        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/73d58e60/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesClusterCommand.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesClusterCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesClusterCommand.java
new file mode 100644
index 0000000..8ac4923
--- /dev/null
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesClusterCommand.java
@@ -0,0 +1,71 @@
+/**
+ *  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.Options;
+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;
+
+public class DescribeKubernetesClusterCommand implements Command<StratosCommandContext> {
+    private static final Logger logger = LoggerFactory.getLogger(DescribeKubernetesClusterCommand.class);
+
+    @Override
+    public String getName() {
+        return "describe-kubernetes-cluster";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Describing a kubernetes cluster";
+    }
+
+    @Override
+    public String getArgumentSyntax() {
+        return "[cluster-id]";
+    }
+
+    @Override
+    public Options getOptions() {
+        return null;
+    }
+
+    @Override
+    public int execute(StratosCommandContext context, String[] args) throws CommandException {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Executing {} command...", getName());
+        }
+        if (args != null && args.length == 1) {
+            String id = args[0];
+            if (logger.isDebugEnabled()) {
+                logger.debug("Getting Kubernetes cluster info {}", id);
+            }
+            RestCommandLineService.getInstance().describeKubernetesCluster(id);
+            return CliConstants.COMMAND_SUCCESSFULL;
+        } else {
+            context.getStratosApplication().printUsage(getName());
+            return CliConstants.COMMAND_FAILED;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/73d58e60/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesMasterCommand.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesMasterCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesMasterCommand.java
new file mode 100644
index 0000000..9b423ea
--- /dev/null
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/DescribeKubernetesMasterCommand.java
@@ -0,0 +1,72 @@
+/**
+ *  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.Options;
+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;
+
+public class DescribeKubernetesMasterCommand implements Command<StratosCommandContext> {
+
+    private static final Logger logger = LoggerFactory.getLogger(DescribeKubernetesMasterCommand.class);
+
+    @Override
+    public String getName() {
+        return "describe-kubernetes-master";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Describing the Kubernetes master";
+    }
+
+    @Override
+    public String getArgumentSyntax() {
+        return "[cluster-id]";
+    }
+
+    @Override
+    public Options getOptions() {
+        return null;
+    }
+
+    @Override
+    public int execute(StratosCommandContext context, String[] args) throws CommandException {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Executing {} command...", getName());
+        }
+        if (args != null && args.length == 1) {
+            String id = args[0];
+            if (logger.isDebugEnabled()) {
+                logger.debug("Getting Kubernetes master info {}", id);
+            }
+            RestCommandLineService.getInstance().getKubernetesMaster(id);
+            return CliConstants.COMMAND_SUCCESSFULL;
+        } else {
+            context.getStratosApplication().printUsage(getName());
+            return CliConstants.COMMAND_FAILED;
+        }
+    }
+}