starcluster.commands.completers

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

# Source Code for Module starcluster.commands.completers

      1  #!/usr/bin/env python
      2
      3  from starcluster import config
      4  from starcluster import optcomplete
      5  from starcluster.logger import log
      6
      7  from base import CmdBase
      8
      9
     10 -class Completer(CmdBase):
     11      """
     12      Base class for all completer classes
     13      """
     14      _cfg = None
     15
     16 -    def _prep_for_completion(self):
     17          self._cfg = config.StarClusterConfig().load()
     18          self._ec2 = self._cfg.get_easy_ec2()
     19
     20      @property
     21 -    def completer(self):
     22          self._prep_for_completion()
     23          return self._completer()
     24
     25
     26 -class ClusterCompleter(Completer):
     27      """
     28      Returns a list of all cluster names as completion options
     29      """
     30 -    def _completer(self):
     31          try:
     32              cm = self.cm
     33              clusters = cm.get_cluster_security_groups()
     34              completion_list = [cm.get_tag_from_sg(sg.name) \
     35                                 for sg in clusters]
     36              return optcomplete.ListCompleter(completion_list)
     37          except Exception, e:
     38              log.error('something went wrong fix me: %s' % e)
     39
     40
     41 -class NodeCompleter(Completer):
     42      """
     43      Returns a list of all node names as completion options
     44      """
     45 -    def _completer(self):
     46          try:
     47              cm = self.cm
     48              clusters = cm.get_cluster_security_groups()
     49              compl_list = [cm.get_tag_from_sg(sg.name) for sg in clusters]
     50              max_num_nodes = 0
     51              for scluster in clusters:
     52                  num_instances = len(scluster.instances())
     53                  if num_instances > max_num_nodes:
     54                      max_num_nodes = num_instances
     55              compl_list.extend(['master'])
     56              compl_list.extend([str(i) for i in range(0, num_instances)])
     57              compl_list.extend(["node%03d" % i \
     58                                 for i in range(1, num_instances)])
     59              return optcomplete.ListCompleter(compl_list)
     60          except Exception, e:
     61              print e
     62              log.error('something went wrong fix me: %s' % e)
     63
     64
     65 -class ImageCompleter(Completer):
     66      """
     67      Returns a list of all registered image ids as completion options
     68      """
     69 -    def _completer(self):
     70          try:
     71              rimages = self.ec2.registered_images
     72              completion_list = [i.id for i in rimages]
     73              return optcomplete.ListCompleter(completion_list)
     74          except Exception, e:
     75              log.error('something went wrong fix me: %s' % e)
     76
     77
     78 -class InstanceCompleter(Completer):
     79      """
     80      Returns a list of all instance ids as completion options
     81      """
     82      show_dns_names = False
     83
     84 -    def _completer(self):
     85          try:
     86              instances = self.ec2.get_all_instances()
     87              completion_list = [i.id for i in instances]
     88              if self.show_dns_names:
     89                  completion_list.extend([i.dns_name for i in instances])
     90              return optcomplete.ListCompleter(completion_list)
     91          except Exception, e:
     92              log.error('something went wrong fix me: %s' % e)
     93
     94
     95 -class VolumeCompleter(Completer):
     96      """
     97      Returns a list of all volume ids as completion options
     98      """
     99 -    def _completer(self):
    100          try:
    101              completion_list = [v.id for v in self.ec2.get_volumes()]
    102              return optcomplete.ListCompleter(completion_list)
    103          except Exception, e:
    104              log.error('something went wrong fix me: %s' % e)
    105