You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by vg...@apache.org on 2011/10/12 08:48:15 UTC

svn commit: r1182227 - in /incubator/ambari/trunk: ./ client/src/main/java/org/apache/ambari/client/ controller/src/main/java/org/apache/ambari/controller/ controller/src/main/java/org/apache/ambari/controller/rest/resources/ src/site/apt/

Author: vgogate
Date: Wed Oct 12 06:48:13 2011
New Revision: 1182227

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

Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterDelete.java
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterUpdate.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.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=1182227&r1=1182226&r2=1182227&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Wed Oct 12 06:48:13 2011
@@ -2,6 +2,8 @@ Ambari Change log
 
 Release 0.1.0 - unreleased
 
+  AMBARI-63. Implement cluster update, rename and delete CLI commands (vgogte)
+
   AMBARI-62. Adds the install/uninstall checks in the heartbeat handler (ddas)
 
   AMBARI-60. Added permission check for RUN_ACTION, and WRITE_FILE_ACTION. (Eric Yang)

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterDelete.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterDelete.java?rev=1182227&r1=1182226&r2=1182227&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterDelete.java (original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterDelete.java Wed Oct 12 06:48:13 2011
@@ -17,6 +17,140 @@
  */
 package org.apache.ambari.client;
 
-public class ClusterDelete {
+import java.net.URI;
+import java.net.URL;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriBuilder;
+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.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+
+public class ClusterDelete extends Command {
+
+    String[] args = null;
+    Options options = null;
+    
+    String urlPath = "/clusters";
+    URL resourceURL = null;
+    CommandLine line;
+    
+    
+    public ClusterDelete (String [] args) throws Exception {  
+        /*
+         * Build options for cluster update
+         */
+        this.args = args;
+        addOptions();
+        this.resourceURL = new URL (""+this.baseURLString+this.urlPath);
+    }
+    
+    public void printUsage () {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp( "ambari cluster delete", this.options);
+    }
+    
+    public void addOptions () {
+             
+        Option wait = new Option( "wait", "Optionally wait for cluster to reach desired state" );
+        Option help = new Option( "help", "Help" );
+        
+        OptionBuilder.withArgName("cluster_name");
+        OptionBuilder.isRequired();
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription( "Name of the cluster to be deleted");
+        Option name = OptionBuilder.create( "name" );
+        
+        this.options = new Options();
+        options.addOption( wait );   
+        options.addOption( name );
+        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());
+          
+        /*
+         * Update cluster
+         */
+        ClientResponse response = service.path("clusters/"+line.getOptionValue("name")).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).delete(ClientResponse.class);
+        if (response.getStatus() != 204) { 
+            System.err.println("Cluster delete command failed. Reason [Code: <"+response.getStatus()+">, Message: <"+response.getHeaders().getFirst("ErrorMessage")+">]");
+            System.exit(-1);
+        }
+        
+        /*
+         * If no wait, then print the message and return
+         */
+        if (!line.hasOption("wait")) {
+           System.out.println("Cluster: ["+line.getOptionValue("name")+"] deleted.\n");
+           return; 
+        }
+        
+        /*
+         * If wait option is specified then wait for cluster to be deleted 
+         */
+        
+        for (;;) {
+            System.out.println("Waiting for cluster ["+line.getOptionValue("name")+"] to be deleted.\n");
+            response = service.path("clusters/"+line.getOptionValue("name")+"/state").accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+            if (response.getStatus() != 200 && response.getStatus() != 404) { 
+                System.err.println("Failed to get the cluster state. Reason [Code: <"+response.getStatus()+">, Message: <"+response.getHeaders().getFirst("ErrorMessage")+">]");
+                System.exit(-1);
+            }
+            
+            /*
+             * If cluster does not exists, then break
+             */
+            if (response.getStatus() == 404) {
+                break;
+            }
+            Thread.sleep(60 * 60000);
+        }         
+        System.out.println("Cluster: ["+line.getOptionValue("name")+"] deleted.\n");
+    }
 }

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterUpdate.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterUpdate.java?rev=1182227&r1=1182226&r2=1182227&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterUpdate.java (original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/ClusterUpdate.java Wed Oct 12 06:48:13 2011
@@ -17,6 +17,261 @@
  */
 package org.apache.ambari.client;
 
-public class ClusterUpdate {
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+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.ClusterState;
+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.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+
+public class ClusterUpdate extends Command {
+
+    String[] args = null;
+    Options options = null;
+    
+    String urlPath = "/clusters";
+    URL resourceURL = null;
+    CommandLine line;
+    String dry_run = "false";
+    
+    Properties roleToNodeExpressions = null;
+    List<RoleToNodes> roleToNodeList = null;
+    
+    public ClusterUpdate (String [] args) throws Exception {  
+        /*
+         * Build options for cluster update
+         */
+        this.args = args;
+        addOptions();
+        this.resourceURL = new URL (""+this.baseURLString+this.urlPath);
+    }
+    
+    public void printUsage () {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp( "ambari cluster update", this.options);
+    }
+    
+    public void addOptions () {
+             
+        Option wait = new Option( "wait", "Optionally wait for cluster to reach desired state" );
+        Option dry_run = new Option( "dry_run", "Dry run" );
+        Option help = new Option( "help", "Help" );
+        
+        OptionBuilder.withArgName("cluster_name");
+        OptionBuilder.isRequired();
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription( "Name of the cluster to be updated");
+        Option name = OptionBuilder.create( "name" );
+        
+        OptionBuilder.withArgName("blueprint_name");
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription( "Name of the cluster blueprint");
+        Option blueprint = OptionBuilder.create( "blueprint" );
+        
+        OptionBuilder.withArgName( "\"node_exp1; node_exp2; ...\"" );
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription(  "List of node range expressions separated by semicolon (;) and contained in double quotes (\"\")" );
+        Option nodes = OptionBuilder.create( "nodes" );
+        
+        OptionBuilder.withArgName( "blueprint_revision" );
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription(  "Blueprint revision" );
+        Option revision = OptionBuilder.create( "revision" );
+        
+        OptionBuilder.withArgName( "description" );
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription(  "Description associated with cluster" );
+        Option desc = OptionBuilder.create( "desc" );
+        
+        OptionBuilder.withArgName( "goalstate" );
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription(  "Desired goal state of the cluster" );
+        Option goalstate = OptionBuilder.create( "goalstate" );
+        
+        OptionBuilder.withArgName( "\"component-1; component-2; ...\"" );
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription(  "List of components to be active in the cluster. Components are seperated by semicolon \";\"" );
+        Option services = OptionBuilder.create( "services" );
+        
+        OptionBuilder.withArgName( "rolename=\"node_exp1; node_exp2; ... \"" );
+        OptionBuilder.hasArgs(2);
+        OptionBuilder.withValueSeparator();
+        OptionBuilder.withDescription( "Node range expressions for a given rolename separated by semicolon (;) and contained in double quotes (\"\")" );
+        Option role = OptionBuilder.create( "role" );
+
+        this.options = new Options();
+        options.addOption( wait );   
+        options.addOption(dry_run);
+        options.addOption( name );
+        options.addOption( blueprint );   
+        options.addOption(revision);
+        options.addOption( desc );
+        options.addOption( role );
+        options.addOption( goalstate );
+        options.addOption( nodes );
+        options.addOption( services );
+        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);
+            }
+            
+            if (line.hasOption("dry_run")) {
+                dry_run = "true";
+            }
+            
+        }
+        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 static 
+    List<RoleToNodes> getRoleToNodesList (Properties roleToNodeExpressions) {
+        if (roleToNodeExpressions == null) { return null; };
+        
+        List<RoleToNodes> roleToNodesMap = new ArrayList<RoleToNodes>();
+        for (String roleName : roleToNodeExpressions.stringPropertyNames()) {
+            RoleToNodes e = new RoleToNodes();
+            e.setRoleName(roleName);
+            e.setNodes(roleToNodeExpressions.getProperty(roleName));
+            roleToNodesMap.add(e);
+        }
+        return roleToNodesMap;
+    }
+    
+    private List<String> splitServices(String services) {
+      if (services == null) { return null; }
+      String[] arr = services.split(",");
+      List<String> result = new ArrayList<String>(arr.length);
+      for (String x: arr) {
+          result.add(x.trim());
+      }
+      return result;
+    }
+
+    public void printClusterDefinition(ClusterDefinition def) throws Exception {
+        JAXBContext jc = JAXBContext.newInstance(org.apache.ambari.common.rest.entities.ClusterDefinition.class);
+        Marshaller m = jc.createMarshaller();
+        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+        m.marshal(def, System.out);
+    }
+    
+    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());
+        
+        // Create Cluster Definition 
+        ClusterDefinition clsDef = new ClusterDefinition();
+        clsDef.setName(line.getOptionValue("name"));
+        clsDef.setBlueprintName(line.getOptionValue("blueprint"));
+        clsDef.setNodes(line.getOptionValue("nodes"));
+        
+        clsDef.setGoalState(line.getOptionValue("goalstate"));
+        clsDef.setBlueprintRevision(line.getOptionValue("revision"));
+        clsDef.setActiveServices(splitServices(line.getOptionValue("services")));
+        clsDef.setDescription(line.getOptionValue("desc"));
+        clsDef.setRoleToNodesMap(getRoleToNodesList(line.getOptionProperties("role")));
+        
+        /*
+         * Update cluster
+         */
+        ClientResponse response = service.path("clusters/"+clsDef.getName()).queryParam("dry_run", dry_run).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).put(ClientResponse.class, clsDef);
+        if (response.getStatus() != 200) { 
+            System.err.println("Cluster update command failed. Reason [Code: <"+response.getStatus()+">, Message: <"+response.getHeaders().getFirst("ErrorMessage")+">]");
+            System.exit(-1);
+        }
+        
+        /* 
+         * Retrieve the cluster definition from the response
+         */
+        ClusterDefinition def = response.getEntity(ClusterDefinition.class);
+        
+        /*
+         * If dry_run print the clsuter defn and return
+         */
+        if (line.hasOption("dry_run")) {
+            System.out.println("Cluster: ["+def.getName()+"] updated. Mode: dry_run.\n");
+            printClusterDefinition(def);
+            return;
+        }
+        
+        /*
+         * If no wait, then print the cluster definition and return
+         * TODO: 
+         */
+        if (!line.hasOption("wait") || !line.hasOption("goalstate")) {
+           System.out.println("Cluster: ["+def.getName()+"] updated.\n");
+           printClusterDefinition(def);
+           return; 
+        }
+        
+        /*
+         * If wait option is specified then wait for cluster state to reach the desired state 
+         */
+        ClusterState clusterState;
+        for (;;) {
+            response = service.path("clusters/"+def.getName()+"/state").accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+            if (response.getStatus() != 200) { 
+                System.err.println("Failed to get the cluster state. Reason [Code: <"+response.getStatus()+">, Message: <"+response.getHeaders().getFirst("ErrorMessage")+">]");
+                System.exit(-1);
+            }
+            
+            clusterState = response.getEntity(ClusterState.class);
+            if (clusterState.getState().equals(def.getGoalState())) {
+                break;
+            }
+            System.out.println("Waiting for cluster ["+def.getName()+"] to get to desired goalstate of ["+def.getGoalState()+"]");
+            Thread.sleep(15 * 60000);
+        }  
+        
+        System.out.println("Cluster: ["+def.getName()+"] updated. Cluster state: ["+clusterState.getState()+"]\n");
+        printClusterDefinition(def);
+    }
 }

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java?rev=1182227&r1=1182226&r2=1182227&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java Wed Oct 12 06:48:13 2011
@@ -163,10 +163,15 @@ public class Clusters {
          * 
          */
         if (!this.operational_clusters.containsKey(clusterName)) {
-            String msg = "Cluster ["+clusterName+"] does not exits";
+            String msg = "Cluster ["+clusterName+"] does not exist";
             throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.NOT_FOUND)).get());
         }
         
+        if (new_name == null || new_name.equals("")) {
+            String msg = "New name of the cluster should be specified as query parameter, (?new_name=xxxx)";
+            throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
+        }
+        
         synchronized (operational_clusters) {
             /*
              * Check if cluster state is ATTAIC, If yes update the name
@@ -191,7 +196,7 @@ public class Clusters {
      * Add new Cluster to cluster list 
      * Validate the cluster definition
      * Lock the cluster list
-     *   -- Check if cluster with given name already exits?
+     *   -- Check if cluster with given name already exist?
      *   -- Set the cluster state and timestamps 
      *   -- Reserve the nodes. i.e. add the cluster and role referenes to Node
      *   -- Throw exception, if some nodes are already preallocated to other cluster.
@@ -209,10 +214,10 @@ public class Clusters {
         
         synchronized (operational_clusters) {
             /* 
-             * Check if cluster already exists
+             * Check if cluster already exist
              */
             if (operational_clusters.containsKey(cdef.getName())) {
-                String msg = "Cluster ["+cdef.getName()+"] already exists";
+                String msg = "Cluster ["+cdef.getName()+"] already exist";
                 throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.CONFLICT)).get());
             }
  
@@ -503,8 +508,8 @@ public class Clusters {
 
     /* 
      * Update cluster definition 
-     * TODO: Update to nodes or NodeToRoles, what if key service nodes are removed or replaced by
-     * other nodes? Does system allow this? 
+     * TODO: As nodes or role to node association changes, validate key services nodes are not removed
+     * TODO: Don't allow update of blueprint name/version if cluster is in ACTIVE/INACTIVE state
     */
     public ClusterDefinition updateCluster(String clusterName, ClusterDefinition c, boolean dry_run) throws Exception {
         
@@ -517,10 +522,10 @@ public class Clusters {
         }
         
         /*
-         * Check if cluster already exists.  
+         * Check if cluster already exist.  
          */
         if (!this.operational_clusters.containsKey(clusterName)) {
-            String msg = "Cluster ["+clusterName+"] does not exits";
+            String msg = "Cluster ["+clusterName+"] does not exist";
             throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.NOT_FOUND)).get());
         }
         
@@ -531,6 +536,7 @@ public class Clusters {
         ClusterDefinition newcd = new ClusterDefinition ();
         
         synchronized (cls.getClusterDefinitionRevisionsList()) {
+            newcd.setName(clusterName);
             if (c.getBlueprintName() != null) {
                 newcd.setBlueprintName(c.getBlueprintName());
             } else {
@@ -701,7 +707,7 @@ public class Clusters {
      */
     public ClusterDefinition getClusterDefinition(String clusterName) throws Exception  {
         if (!this.operational_clusters.containsKey(clusterName)) {
-            String msg = "Cluster ["+clusterName+"] does not exits";
+            String msg = "Cluster ["+clusterName+"] does not exist";
             throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.NOT_FOUND)).get());
         }
         return this.operational_clusters.get(clusterName).getLatestClusterDefinition();
@@ -713,7 +719,7 @@ public class Clusters {
     */
     public ClusterState getClusterState(String clusterName) throws WebApplicationException {
         if (!this.operational_clusters.containsKey(clusterName)) {
-            String msg = "Cluster ["+clusterName+"] does not exits";
+            String msg = "Cluster ["+clusterName+"] does not exist";
             throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.NOT_FOUND)).get());
         }
         return this.operational_clusters.get(clusterName).getClusterState();

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=1182227&r1=1182226&r2=1182227&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 Wed Oct 12 06:48:13 2011
@@ -109,6 +109,7 @@ public class ClusterResource {
      */ 
     @PUT
     @Consumes({"application/json", "application/xml"})
+    @Path(value = "/rename")
     public void updateClusterDefinition(
            @PathParam("clusterName") String clusterName,
            @DefaultValue("") @QueryParam("new_name") String new_name) throws Exception {    

Modified: incubator/ambari/trunk/src/site/apt/cli.apt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/src/site/apt/cli.apt?rev=1182227&r1=1182226&r2=1182227&view=diff
==============================================================================
--- incubator/ambari/trunk/src/site/apt/cli.apt (original)
+++ incubator/ambari/trunk/src/site/apt/cli.apt Wed Oct 12 06:48:13 2011
@@ -134,6 +134,10 @@ Cluster Commands
 
       * <<--blueprint>> [ BLUEPRINT NAME ]   
 
+      * <<--blueprint_revision>> [ BLUEPRINT REVISION ] <<[REQUIRED]>>
+
+        * Revision of the blueprint. If not specified latest one is used.
+
       * <<--goalstate>> [ACTIVE/INACTIVE/ATTIC]
 
       * <<--services>> [component, component, component]