def main()

in tools/ami-creator/create_ami.py [0:0]


def main():
    parser = OptionParser()
    parser.add_option("-i", "--instance-type", dest="instance_type",
        help="Instance type to create")
    parser.add_option("-a", "--ami", dest="ami",
        help="AMI to start with")
    parser.add_option("-n", "--name", dest="name",
        help="Prefix to use for AMI name (timestamp will be appended)")
    parser.add_option("-d", "--disk-size", dest="disk_size", type="int",
        default=10, help="Size of disk to use for image creation (in GB)")
    parser.add_option("-s", "--security-group", dest="security_group",
        help="Security group ID for instance")
    parser.add_option("-k", "--key-name", dest="ssh_key",
        help="SSH key pair name to use")
    parser.add_option("-l", "--launch-templates", dest="launch_templates",
        help="Comma separated list of launch template IDs to update with newly created AMI")
    parser.add_option("-p", "--private-key", dest="private_key",
        help="Private key used to SSH into instance or decrypt windows password")
    parser.add_option("-u", "--userdata", dest="userdata",
        help="UserData file to use")
    parser.add_option("-q", "--quiet",
        action="store_false", dest="verbose", default=True,
        help="don't print status messages to stdout")

    (options, args) = parser.parse_args()

    # ensure required parameters are passed
    if options.instance_type is None:
        logging.error("You must pass --instance-type option")
        sys.exit(-1)
    if options.name is None:
        logging.error("You must pass --name option")
        sys.exit(-1)
    if options.security_group is None:
        logging.error("You must pass --security-group option")
        sys.exit(-1)
    if options.userdata is None:
        logging.error("You must pass --userdata option")
        sys.exit(-1)
    if options.private_key is None:
        logging.error("You must pass --private-key option")
        sys.exit(-1)

    loglev = logging.WARNING
    if options.verbose:
        loglev = logging.INFO
    logging.basicConfig(
        level = loglev,
        format = '%(asctime)s %(levelname)s %(message)s'
    )
    if options.userdata:
        userdata = options.userdata
    else:
        userdata = 'userdata/{}.txt'.format(options.name)


    instance = create_instance(
        instance_type=options.instance_type,
        disk_size=options.disk_size,
        userdata_file=userdata,
        ami=options.ami,
        security_group=options.security_group,
        ssh_key=options.ssh_key
    )
    wait_for_instance(instance, options.private_key)
    image = create_ami(options.name, instance)
    wait_for_ami(image)
    terminate_instance(instance)

    lt_list = options.launch_templates.split(",")
    if len(lt_list) > 0:
        logging.info("Updating launch templates with new AMI")
        for lt_id in lt_list:
            new_version = update_launch_template(lt_id, image.id)
            if new_version is None:
                logging.error("Unable to update LT %s", lt_id)
            else:
                logging.info("Created new version %s of LT %s with new AMI", new_version, lt_id)