incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgog...@apache.org
Subject svn commit: r1183222 - in /incubator/ambari/trunk: ./ client/src/main/java/org/apache/ambari/client/ client/src/main/java/org/apache/ambari/common/rest/entities/ controller/src/main/java/org/apache/ambari/controller/rest/resources/ src/site/apt/
Date Fri, 14 Oct 2011 06:50:15 GMT
Author: vgogate
Date: Fri Oct 14 06:50:14 2011
New Revision: 1183222

URL: http://svn.apache.org/viewvc?rev=1183222&view=rev
Log:
AMBARI-73

Added:
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterNodes.java
Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/AmbariClient.java
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/Command.java
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/NodeState.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/ClusterResource.java
    incubator/ambari/trunk/src/site/apt/cli.apt

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1183222&r1=1183221&r2=1183222&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Fri Oct 14 06:50:14 2011
@@ -2,6 +2,8 @@ Ambari Change log
 
 Release 0.1.0 - unreleased
 
+  AMBARI-73. Implement cluster nodes CLI. (vgogate)
+
   AMBARI-72. Adding (dummy) blueprints before (dummy) cluster definitions and
              fixing null pointer exception when parent blueprint is set to null
              for top level blueprint  (vgogate)

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/AmbariClient.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/AmbariClient.java?rev=1183222&r1=1183221&r2=1183222&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/AmbariClient.java
(original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/AmbariClient.java
Fri Oct 14 06:50:14 2011
@@ -39,6 +39,7 @@ public class AmbariClient {
         clusterCommands.put("list", "ClusterList");
         clusterCommands.put("get", "ClusterGet");
         clusterCommands.put("blueprint", "ClusterBlueprint");
+        clusterCommands.put("nodes", "ClusterNodes");
         
         
         HashMap<String, String> blueprintCommands = new HashMap<String, String>();

Added: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterNodes.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterNodes.java?rev=1183222&view=auto
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterNodes.java
(added)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterNodes.java
Fri Oct 14 06:50:14 2011
@@ -0,0 +1,174 @@
+/*
+ * 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.ambari.client;
+
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriBuilder;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+
+import org.apache.ambari.common.rest.entities.ClusterDefinition;
+import org.apache.ambari.common.rest.entities.ClusterInformation;
+import org.apache.ambari.common.rest.entities.ClusterState;
+import org.apache.ambari.common.rest.entities.Node;
+import org.apache.ambari.common.rest.entities.RoleToNodes;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+
+public class ClusterNodes extends Command {
+
+    String[] args = null;
+    Options options = null;
+    
+    String urlPath = "/clusters";
+    URL resourceURL = null;
+    CommandLine line;
+    
+    public ClusterNodes (String [] args) throws Exception {  
+        /*
+         * Build options for cluster create
+         */
+        this.args = args;
+        addOptions();
+        this.resourceURL = new URL (""+this.baseURLString+this.urlPath);
+    }
+    
+    public void printUsage () {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp( "ambari cluster create", this.options);
+    }
+    
+    public void addOptions () {
+             
+        Option help = new Option( "help", "Help" );
+        
+        OptionBuilder.withArgName("cluster_name");
+        OptionBuilder.isRequired();
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription( "Name of the cluster to be created");
+        Option name = OptionBuilder.create( "name" );
+        
+        OptionBuilder.withArgName("role_name");
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription( "Role name to get list of nodes associated with specified
role");
+        Option role = OptionBuilder.create( "role");
+        
+        OptionBuilder.withArgName( "[true/false]" );
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription(  "Node state alive as true or false" );
+        Option alive = OptionBuilder.create( "alive" );
+  
+        this.options = new Options();
+
+        options.addOption( name );
+        options.addOption( role );   
+        options.addOption( alive );
+        options.addOption(help);
+    }
+    
+    public void parseCommandLine() {
+     
+        // create the parser
+        CommandLineParser parser = new GnuParser();
+        try {
+            // parse the command line arguments
+            line = parser.parse(this.options, this.args );
+            
+            if (line.hasOption("help")) {
+                printUsage();
+                System.exit(0);
+            }
+        }
+        catch( ParseException exp ) {
+            // oops, something went wrong
+            System.err.println( "Command parsing failed. Reason: <" + exp.getMessage()+">\n"
);
+            printUsage();
+            System.exit(-1);
+        } 
+    }
+    
+    private static URI getBaseURI() {
+        return UriBuilder.fromUri(
+                "http://localhost:4080/rest/").build();
+    }
+    
+    public void run() throws Exception {
+        /* 
+         * Parse the command line to get the command line arguments
+         */
+        parseCommandLine();
+        
+        ClientConfig config = new DefaultClientConfig();
+        Client client = Client.create(config);
+        WebResource service = client.resource(getBaseURI());
+        String clusterName = line.getOptionValue("name");
+        String role = ""; 
+        String alive = "";
+        if (line.getOptionValue("alive") != null) { alive = line.getOptionValue("alive");
}
+        if (line.getOptionValue("role") != null) { role = line.getOptionValue("role"); }
+        
+        
+        /*
+         * Get Cluster node list
+         */
+        ClientResponse response = service.path("clusters/"+clusterName+"/nodes")
+                      .queryParam("alive", alive)
+                      .queryParam("role", role)
+                      .accept(MediaType.APPLICATION_JSON)
+                      .type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+        if (response.getStatus() == 204) {
+            System.out.println ("No nodes are associated.");
+            System.exit(0);
+        }
+        if (response.getStatus() != 200) { 
+            System.err.println("Cluster nodes command failed. Reason [Code: <"+response.getStatus()+">,
Message: <"+response.getHeaders().getFirst("ErrorMessage")+">]");
+            System.exit(-1);
+        }
+        
+        /* 
+         * Retrieve the node list from response
+         */
+        List<Node> nodes = response.getEntity(new GenericType<List<Node>>(){});
+        
+        System.out.println("List of associated nodes: \n");
+        for (Node node : nodes ) {
+            printNodeInformation(node);
+            System.out.println("\n");
+        }
+    }
+}

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/Command.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/Command.java?rev=1183222&r1=1183221&r2=1183222&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/Command.java (original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/Command.java Fri
Oct 14 06:50:14 2011
@@ -27,6 +27,7 @@ import javax.xml.bind.Unmarshaller;
 import org.apache.ambari.common.rest.entities.Blueprint;
 import org.apache.ambari.common.rest.entities.ClusterDefinition;
 import org.apache.ambari.common.rest.entities.ClusterInformation;
+import org.apache.ambari.common.rest.entities.Node;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
@@ -123,6 +124,13 @@ public abstract class Command {
         m.marshal(clsInfo, System.out);
     }
     
+    public void printNodeInformation(Node node) throws Exception {
+        JAXBContext jc = JAXBContext.newInstance(org.apache.ambari.common.rest.entities.Node.class);
+        Marshaller m = jc.createMarshaller();
+        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+        m.marshal(node, System.out);
+    }
+    
     public void printClusterBlueprint(Blueprint blueprint, String file_path) throws Exception
{
         JAXBContext jc = JAXBContext.newInstance(org.apache.ambari.common.rest.entities.Blueprint.class);
         Marshaller m = jc.createMarshaller();
@@ -130,7 +138,7 @@ public abstract class Command {
         if (file_path == null) {
             m.marshal(blueprint, System.out);
         } else {
-            m.marshal(blueprint, new File("file_path"));
+            m.marshal(blueprint, new File(file_path));
         }
     }
     

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/NodeState.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/NodeState.java?rev=1183222&r1=1183221&r2=1183222&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/NodeState.java
(original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/common/rest/entities/NodeState.java
Fri Oct 14 06:50:14 2011
@@ -33,17 +33,17 @@ import javax.xml.datatype.XMLGregorianCa
 
 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "NodeState", propOrder = {
-    "lastHeartbeatTime",
-    "clusterName",
+    "clusterID",
     "allocatedToCluster",
+    "lastHeartbeatTime",
     "agentInstalled",
     "nodeRoleNames",
     "nodeServers"
 })
-@XmlRootElement(name = "NodeState")
+@XmlRootElement
 public class NodeState {
 
-    @XmlElement(name = "lastHeartbeatTime", required = true)
+    @XmlElement
     @XmlSchemaType(name = "dateTime")
     protected XMLGregorianCalendar lastHeartbeatTime;
         
@@ -51,22 +51,22 @@ public class NodeState {
      * Associating the cluster name would reserve the node for a given cluster
      * 
      */
-    @XmlElement(name = "ClusterID", required = true)
+    @XmlElement
     protected String clusterID;
 
-    @XmlElement(name = "AgentInstalled", required = true)
+    @XmlElement
     protected Boolean agentInstalled = true;
 
-    @XmlElement(name = "AllocatedToCluster", required = true)
+    @XmlElement
     protected Boolean allocatedToCluster = false;
         
     /*
      * null indicates no roles associated with this node.
      */
-    @XmlElement(name = "NodeRoleNames", required = true)
+    @XmlElement
     protected List<String> nodeRoleNames = null;
         
-    @XmlElement(name = "NodeServers", required = true)
+    @XmlElement
     protected List<NodeServer> nodeServers = new ArrayList<NodeServer>();
 
     /**

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/ClusterResource.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/ClusterResource.java?rev=1183222&r1=1183221&r2=1183222&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/ClusterResource.java
(original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/ClusterResource.java
Fri Oct 14 06:50:14 2011
@@ -221,7 +221,7 @@ public class ClusterResource {
     /** 
      * Get the blueprint associated with cluster
      *  
-     *  @response.representation.200.doc .  
+     *  @response.representation.200.doc 
      *  
      *  @param  clusterName Name of the cluster; Each cluster is identified w/ 
      *                      unique name

Modified: incubator/ambari/trunk/src/site/apt/cli.apt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/src/site/apt/cli.apt?rev=1183222&r1=1183221&r2=1183222&view=diff
==============================================================================
--- incubator/ambari/trunk/src/site/apt/cli.apt (original)
+++ incubator/ambari/trunk/src/site/apt/cli.apt Fri Oct 14 06:50:14 2011
@@ -261,7 +261,7 @@ Cluster Commands
         * Optionally specify the node state alive as true or false. Alive nodes are ones

         regularly heart beating with Ambari controller. If this option is not specified then
all nodes are returned.
 
-      * <<--role>> [ROLE_NAME] <<[REPEATABLE]>>
+      * <<--role>> [ROLE_NAME] 
 
         * Optional role name to list the nodes associated with specified role.
 



Mime
View raw message