starcluster.commands.addnode

Package starcluster :: Package commands :: Module addnode |
[hide private]
[frames] | no frames]

# Source Code for Module starcluster.commands.addnode

      1  #!/usr/bin/env python
      2
      3  from completers import ClusterCompleter
      4
      5
      6 -class CmdAddNode(ClusterCompleter):
      7      """
      8      addnode [options] <cluster_tag>
      9
     10      Add a node to a running cluster
     11
     12      Examples:
     13
     14          $ starcluster addnode mycluster
     15
     16      This will launch a new node and add it to mycluster. The node's alias will
     17      be autogenerated based on the existing node aliases in the cluster.
     18
     19      If you want to provide your own alias for the node use the -a option:
     20
     21          $ starcluster addnode -a mynode mycluster
     22
     23      This will add a new node called 'mynode' to mycluster.
     24
     25      You can also add multiple nodes using the -n option:
     26
     27          $ starcluster addnode -n 3 mycluster
     28
     29      The above example will add three new nodes to mycluster with autogenerated
     30      aliases. If you'd rather provide your own aliases:
     31
     32          $ starcluster addnode -a mynode1,mynode2,mynode3 mycluster
     33
     34      This will add three new nodes to mycluster named mynode1, mynode2, and
     35      mynode3.
     36
     37      If you've previously attempted to add a node and it failed due to a plugin
     38      error or other bug or if you used the 'removenode' command with the '-k'
     39      option and wish to re-add the node to the cluster without launching a new
     40      instance you can use the '-x' option:
     41
     42          $ starcluster addnode -x -a mynode1 mycluster
     43
     44      NOTE: The -x option requires the -a option
     45
     46      This will add 'mynode1' to mycluster using the existing instance. If no
     47      instance exists with the alias specified by the '-a' option an error is
     48      reported. You can also do this for multiple nodes:
     49
     50          $ starcluster addnode -x -a mynode1,mynode2,mynode3 mycluster
     51      """
     52      names = ['addnode', 'an']
     53
     54      tag = None
     55
     56 -    def addopts(self, parser):
     57          parser.add_option("-a", "--alias", dest="alias",
     58                            action="append", type="string", default=[],
     59                            help=("alias to give to the new node " + \
     60                                  "(e.g. node007, mynode, etc)"))
     61          parser.add_option("-n", "--num-nodes", dest="num_nodes",
     62                            action="store", type="int", default=1,
     63                            help=("number of new nodes to launch"))
     64          parser.add_option("-x", "--no-create", dest="no_create",
     65                            action="store_true", default=False,
     66                            help="do not launch new EC2 instances when " + \
     67                            "adding nodes (use existing instances instead)")
     68
     69 -    def _get_duplicate(self, lst):
     70          d = {}
     71          for item in lst:
     72              if item in d:
     73                  return item
     74              else:
     75                  d[item] = 0
     76
     77 -    def execute(self, args):
     78          if len(args) != 1:
     79              self.parser.error("please specify a cluster <cluster_tag>")
     80          tag = self.tag = args[0]
     81          aliases = []
     82          for alias in self.opts.alias:
     83              aliases.extend(alias.split(','))
     84          if 'master' in aliases:
     85              self.parser.error("'master' is a reserved alias")
     86          num_nodes = self.opts.num_nodes
     87          if num_nodes == 1 and aliases:
     88              num_nodes = len(aliases)
     89          if num_nodes > 1 and aliases and len(aliases) != num_nodes:
     90              self.parser.error("you must specify the same number of aliases "
     91                                "(-a) as nodes (-n)")
     92          dupe = self._get_duplicate(aliases)
     93          if dupe:
     94              self.parser.error("cannot have duplicate aliases (duplicate: %s)" %
     95                                dupe)
     96          if not self.opts.alias and self.opts.no_create:
     97              self.parser.error("you must specify one or more node aliases via "
     98                                "the -a option when using -x")
     99          self.cm.add_nodes(tag, num_nodes, aliases=aliases,
    100                            no_create=self.opts.no_create)
    101