starcluster.commands.createimage

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

# Source Code for Module starcluster.commands.createimage

      1  #!/usr/bin/env python
      2
      3  import sys
      4  import time
      5
      6  from starcluster import static
      7  from starcluster import exception
      8  from starcluster.logger import log
      9
     10  from completers import InstanceCompleter
     11
     12
     13 -class CmdCreateImage(InstanceCompleter):
     14      """
     15      createimage [options] <instance-id> <image_name> <bucket>
     16
     17      Create a new instance-store image (AMI) from a running EC2 instance
     18
     19      Example:
     20
     21          $ starcluster createimage i-999999 my-new-image mybucket
     22
     23      NOTE: It is recommended not to create a new StarCluster AMI from
     24      an instance launched by StarCluster. Rather, launch a single
     25      StarCluster instance using ElasticFox or the EC2 API tools, modify
     26      it to your liking, and then use this command to create a new AMI from
     27      the running instance.
     28      """
     29      names = ['createimage', 'ci']
     30      show_dns_names = True
     31
     32      bucket = None
     33      image_name = None
     34
     35 -    def addopts(self, parser):
     36          parser.add_option(
     37              "-c", "--confirm", dest="confirm",
     38              action="store_true", default=False,
     39              help="Do not warn about re-imaging StarCluster instances")
     40          parser.add_option(
     41              "-e", "--ebs", dest="ebs",
     42              action="store_true", default=False,
     43              help="Create an EBS image")
     44          parser.add_option(
     45              "-s", "--s3", dest="s3",
     46              action="store_true", default=False,
     47              help="Create an instance-store (S3-based) image")
     48          parser.add_option(
     49              "-r", "--remove-image-files", dest="remove_image_files",
     50              action="store_true", default=False,
     51              help="Remove generated image files on the " + \
     52              "instance after registering")
     53          parser.add_option(
     54              "-d", "--description", dest="description", action="store",
     55              type="string", default=time.strftime("%Y%m%d%H%M"),
     56              help="short description of this AMI")
     57          parser.add_option(
     58              "-k", "--kernel-id", dest="kernel_id", action="store",
     59              type="string", default=None,
     60              help="kernel id for the new AMI")
     61          parser.add_option(
     62              "-R", "--ramdisk-id", dest="ramdisk_id", action="store",
     63              type="string", default=None,
     64              help="ramdisk id for the new AMI")
     65
     66 -    def cancel_command(self, signum, frame):
     67          raise exception.CancelledCreateImage(self.bucket, self.image_name)
     68
     69 -    def execute(self, args):
     70          if len(args) != 3:
     71              self.parser.error(
     72                  'you must specify an instance-id, image name, and bucket')
     73          instanceid, image_name, bucket = args
     74          self.bucket = bucket
     75          self.image_name = image_name
     76          cfg = self.cfg
     77          i = self.ec2.get_instance(instanceid)
     78          if not self.opts.confirm:
     79              for group in i.groups:
     80                  if group.id.startswith(static.SECURITY_GROUP_PREFIX):
     81                      log.warn("Instance %s is a StarCluster instance" % i.id)
     82                      print
     83                      log.warn(
     84                          "Creating an image from a StarCluster instance can " +
     85                          "lead to problems when attempting to use the " +
     86                          "resulting image with StarCluster later on")
     87                      print
     88                      log.warn(
     89                          "The recommended way to re-image a StarCluster AMI " +
     90                          "is to launch a single instance using either " +
     91                          "ElasticFox, the EC2 command line tools, or the AWS" +
     92                          "management console. Then login to the instance, " +
     93                          "modify it, and use this command to create a new " +
     94                          "AMI from it.")
     95                      print
     96                      resp = raw_input("Continue anyway (y/n)? ")
     97                      if resp not in ['y', 'Y', 'yes']:
     98                          log.info("Aborting...")
     99                          sys.exit(1)
    100                      break
    101          key_location = cfg.get_key(i.key_name).get('key_location')
    102          aws_user_id = cfg.aws.get('aws_user_id')
    103          ec2_cert = cfg.aws.get('ec2_cert')
    104          ec2_private_key = cfg.aws.get('ec2_private_key')
    105          self.catch_ctrl_c()
    106          ami_id = self.ec2.create_s3_image(instanceid, key_location,
    107                                            aws_user_id, ec2_cert,
    108                                            ec2_private_key, bucket,
    109                                            image_name=image_name,
    110                                            **self.specified_options_dict)
    111          log.info("Your new AMI id is: %s" % ami_id)
    112