starcluster.commands.createvolume

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

# Source Code for Module starcluster.commands.createvolume

      1  #!/usr/bin/env python
      2  import os
      3
      4  from starcluster import node
      5  from starcluster import volume
      6  from starcluster import static
      7  from starcluster import exception
      8
      9  from base import CmdBase
     10
     11
     12 -class CmdCreateVolume(CmdBase):
     13      """
     14      createvolume [options] <volume_size> <volume_zone>
     15
     16      Create a new EBS volume for use with StarCluster
     17      """
     18
     19      names = ['createvolume', 'cv']
     20
     21 -    def addopts(self, parser):
     22          parser.add_option(
     23              "-b", "--bid", dest="spot_bid", action="store", type="float",
     24              default=None, help="Requests spot instances instead of flat "
     25              "rate instances. Uses SPOT_BID as max bid for the request.")
     26          parser.add_option(
     27              "-k", "--keypair", dest="keypair",
     28              action="store", type="string", default=None,
     29              help="The keypair to use when launching host instance " + \
     30              "(must be defined in the config)")
     31          parser.add_option(
     32              "-H", "--host-instance", dest="host_instance",
     33              action="store", type="string", default=None,
     34              help="Use specified instance as volume host rather than " + \
     35              "launching a new host")
     36          parser.add_option(
     37              "-d", "--detach-volume", dest="detach_vol",
     38              action="store_true", default=False,
     39              help="Detach new volume from host instance after creation")
     40          parser.add_option(
     41              "-s", "--shutdown-volume-host", dest="shutdown_instance",
     42              action="store_true", default=False,
     43              help="Shutdown host instance after creating new volume")
     44          parser.add_option(
     45              "-m", "--mkfs-cmd", dest="mkfs_cmd",
     46              action="store", type="string", default="mkfs.ext3",
     47              help="Specify alternate mkfs command to use when " + \
     48              "formatting volume (default: mkfs.ext3)")
     49          parser.add_option(
     50              "-i", "--image-id", dest="image_id",
     51              action="store", type="string", default=None,
     52              help="The AMI to use when launching volume host instance")
     53          parser.add_option(
     54              "-I", "--instance-type", dest="instance_type",
     55              action="store", type="choice", default="m1.small",
     56              choices=static.INSTANCE_TYPES.keys(),
     57              help="The instance type to use when launching volume " + \
     58              "host instance")
     59
     60 -    def cancel_command(self, signum, frame):
     61          raise exception.CancelledCreateVolume()
     62
     63 -    def _load_keypair(self, keypair=None):
     64          key_location = None
     65          if keypair:
     66              kp = self.ec2.get_keypair(keypair)
     67              key = self.cfg.get_key(kp.name)
     68              key_location = key.get('key_location', '')
     69          else:
     70              self.log.info("No keypair specified, picking one from config...")
     71              for kp in self.ec2.keypairs:
     72                  if kp.name in self.cfg.keys:
     73                      keypair = kp.name
     74                      kl = self.cfg.get_key(kp.name).get('key_location', '')
     75                      if os.path.exists(kl) and os.path.isfile(kl):
     76                          self.log.info('Using keypair: %s' % keypair)
     77                          key_location = kl
     78                          break
     79          if not keypair:
     80              raise exception.ConfigError(
     81                  "no keypairs in region %s defined in config" % \
     82                  self.ec2.region.name)
     83          if not key_location:
     84              raise exception.ConfigError(
     85                  "cannot determine key_location for keypair %s" % keypair)
     86          if not os.path.exists(key_location):
     87              raise exception.ValidationError(
     88                  "key_location '%s' does not exist." % key_location)
     89          elif not os.path.isfile(key_location):
     90              raise exception.ValidationError(
     91                  "key_location '%s' is not a file." % key_location)
     92          return (keypair, key_location)
     93
     94 -    def _get_size_arg(self, size):
     95          errmsg = "size argument must be an integer >= 1"
     96          try:
     97              size = int(size)
     98              if size <= 0:
     99                  self.parser.error(errmsg)
    100              return size
    101          except ValueError:
    102              self.parser.error(errmsg)
    103
    104 -    def execute(self, args):
    105          if len(args) != 2:
    106              self.parser.error(
    107                  "you must specify a size (in GB) and an availability zone")
    108          size, zone = args
    109          size = self._get_size_arg(size)
    110          zone = self.ec2.get_zone(zone).name
    111          key = self.opts.keypair
    112          host_instance = None
    113          if self.opts.host_instance:
    114              host_instance = self.ec2.get_instance(self.opts.host_instance)
    115              key = host_instance.key_name
    116          keypair, key_location = self._load_keypair(key)
    117          if host_instance:
    118              host_instance = node.Node(host_instance, key_location,
    119                                        alias="volumecreator_host")
    120          kwargs = self.specified_options_dict
    121          kwargs.update(dict(keypair=keypair, key_location=key_location,
    122                             host_instance=host_instance))
    123          vc = volume.VolumeCreator(self.ec2, **kwargs)
    124          if host_instance:
    125              vc._validate_host_instance(host_instance, zone)
    126          self.catch_ctrl_c()
    127          volid = vc.create(size, zone)
    128          if volid:
    129              self.log.info(
    130                  "Your new %sGB volume %s has been created successfully" % \
    131                  (size, volid))
    132          else:
    133              self.log.error("failed to create new volume")
    134