U
    W+d                     @   sN   d dl Z d dlZd dlZd dlZd dlZdZdd Zdd ZedkrJe  dS )    Nai	  
SYNOPSIS
    launch_ami.py -a ami_id [-b script_bucket] [-s script_name]
                  [-m module] [-c class_name] [-r]
                  [-g group] [-k key_name] [-n num_instances]
                  [-w] [extra_data]
    Where:
        ami_id - the id of the AMI you wish to launch
        module - The name of the Python module containing the class you
                 want to run when the instance is started.  If you use this
                 option the Python module must already be stored on the
                 instance in a location that is on the Python path.
        script_file - The name of a local Python module that you would like
                      to have copied to S3 and then run on the instance
                      when it is started.  The specified module must be
                      import'able (i.e. in your local Python path).  It
                      will then be copied to the specified bucket in S3
                      (see the -b option).  Once the new instance(s)
                      start up the script will be copied from S3 and then
                      run locally on the instance.
        class_name - The name of the class to be instantiated within the
                     module or script file specified.
        script_bucket - the name of the bucket in which the script will be
                        stored
        group - the name of the security group the instance will run in
        key_name - the name of the keypair to use when launching the AMI
        num_instances - how many instances of the AMI to launch (default 1)
        input_queue_name - Name of SQS to read input messages from
        output_queue_name - Name of SQS to write output messages to
        extra_data - additional name-value pairs that will be passed as
                     userdata to the newly launched instance.  These should
                     be of the form "name=value"
        The -r option reloads the Python module to S3 without launching
        another instance.  This can be useful during debugging to allow
        you to test a new version of your script without shutting down
        your instance and starting up another one.
        The -w option tells the script to run synchronously, meaning to
        wait until the instance is actually up and running.  It then prints
        the IP address and internal and external DNS names before exiting.
c                   C   s   t t t  d S )N)printusage_stringsysexit r   r   9/tmp/pip-unpacked-wheel-dlxw5sjy/boto/pyami/launch_ami.pyusageF   s    r   c                  C   s  z:t  tjdd  dddddddd	d
dddddg\} }W n   t  Y nX d d d d dd d dd d d
}d }d }| D ]\}}|dkr||d< |dkr||d< |dkr||d< |dkr||d< |dkrt  |dkr||d< |dkr||d	< |dkr||d< |dkrt||d< |d kr"||d!< |d"kr0d#}|d$krB||d< |d%krtd#}qtdg}|D ]&}||d sZtd&|  t  qZ|d r|rtd'|d   ntd(|d   t|d }	t	
 }
|
|d }||d d) }||	d*  |j|d+< g }	| D ]"\}}|r|	d,||f  qt	 }
|	d-|
j  |	d.|
j  |D ]}|	| q^d/|	}|s|
|d g}|d* }|j||d	 |d g|ddd0}td1|d |jf  td2|j  |jD ]}td3|j  q|rd4}|s\td5 d6d7 |jD  d8d7 |jD }t| |d9t|jkrd#}q|jD ]0}td:|j  td;|j  td<|j   qbd S )=N   za:b:c:g:hi:k:m:n:o:rs:wamibucketclassgrouphelpZ
inputqueuekeypairmoduleZnuminstancesZoutputqueuereloadscript_namewaitdefault)
module_namer   
class_namescript_bucketr   r   r
   num_instancesinput_queue_nameoutput_queue_name)z-az--ami)z-bz--bucketr   )z-cz--classr   )z-gz--group)z-hz--help)z-iz--inputqueuer   )z-kz	--keypair)z-mz--moduler   )z-nz--num_instancesr   )z-oz--outputqueuer   )z-rz--reloadT)z-sz--script)z-wz--waitz%s is requiredzReloading module %s to S3zCopying module %s to S3z.pyr   Z
script_md5z%s=%szaws_access_key_id=%szaws_secret_access_key=%s|)	user_dataZkey_nameZsecurity_groups	max_countzAMI: %s - %s (Started)z0Reservation %s contains the following instances:z	%sF   c                 S   s   g | ]}|  qS r   )update.0ir   r   r   
<listcomp>   s     zmain.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   )stater    r   r   r   r#      s     runningzInstance: %szPublic DNS Name: %szPrivate DNS Name: %s)!getoptr   argvr   intgetr   impfind_modulebotoZ
connect_s3Z
get_bucketZnew_keyZset_contents_from_filemd5itemsappendZconnect_ec2Zaws_access_key_idZaws_secret_access_keyjoinZget_all_imagesrunlocationidZ	instancestimesleepcountlenZami_launch_indexZpublic_dns_nameZprivate_dns_name)optsargsparamsr   r   oarequiredZpnamelcr   keykvkvsrsimgrr"   r%   statusr   r   r   mainJ   s    
     












rI   __main__)	r&   r   r*   r4   r,   r   r   rI   __name__r   r   r   r   <module>   s   )f