<s> """<STR_LIT>""" <EOL> import boto <EOL> import boto . ec2 <EOL> from boto . mashups . server import Server , ServerSet <EOL> from boto . mashups . iobject import IObject <EOL> from boto . pyami . config import Config <EOL> from boto . sdb . persist import get_domain , set_domain <EOL> import time , StringIO <EOL> InstanceTypes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Item ( IObject ) : <EOL> def __init__ ( self ) : <EOL> self . region = None <EOL> self . name = None <EOL> self . instance_type = None <EOL> self . quantity = <NUM_LIT:0> <EOL> self . zone = None <EOL> self . ami = None <EOL> self . groups = [ ] <EOL> self . key = None <EOL> self . ec2 = None <EOL> self . config = None <EOL> def set_userdata ( self , key , value ) : <EOL> self . userdata [ key ] = value <EOL> def get_userdata ( self , key ) : <EOL> return self . userdata [ key ] <EOL> def set_region ( self , region = None ) : <EOL> if region : <EOL> self . region = region <EOL> else : <EOL> l = [ ( r , r . name , r . endpoint ) for r in boto . ec2 . regions ( ) ] <EOL> self . region = self . choose_from_list ( l , prompt = '<STR_LIT>' ) <EOL> def set_name ( self , name = None ) : <EOL> if name : <EOL> self . name = name <EOL> else : <EOL> self . name = self . get_string ( '<STR_LIT:Name>' ) <EOL> def set_instance_type ( self , instance_type = None ) : <EOL> if instance_type : <EOL> self . instance_type = instance_type <EOL> else : <EOL> self . instance_type = self . choose_from_list ( InstanceTypes , '<STR_LIT>' ) <EOL> def set_quantity ( self , n = <NUM_LIT:0> ) : <EOL> if n > <NUM_LIT:0> : <EOL> self . quantity = n <EOL> else : <EOL> self . quantity = self . get_int ( '<STR_LIT>' ) <EOL> def set_zone ( self , zone = None ) : <EOL> if zone : <EOL> self . zone = zone <EOL> else : <EOL> l = [ ( z , z . name , z . state ) for z in self . ec2 . get_all_zones ( ) ] <EOL> self . zone = self . choose_from_list ( l , prompt = '<STR_LIT>' ) <EOL> def set_ami ( self , ami = None ) : <EOL> if ami : <EOL> self . ami = ami <EOL> else : <EOL> l = [ ( a , a . id , a . location ) for a in self . ec2 . get_all_images ( ) ] <EOL> self . ami = self . choose_from_list ( l , prompt = '<STR_LIT>' ) <EOL> def add_group ( self , group = None ) : <EOL> if group : <EOL> self . groups . append ( group ) <EOL> else : <EOL> l = [ ( s , s . name , s . description ) for s in self . ec2 . get_all_security_groups ( ) ] <EOL> self . groups . append ( self . choose_from_list ( l , prompt = '<STR_LIT>' ) ) <EOL> def set_key ( self , key = None ) : <EOL> if key : <EOL> self . key = key <EOL> else : <EOL> l = [ ( k , k . name , '<STR_LIT>' ) for k in self . ec2 . get_all_key_pairs ( ) ] <EOL> self . key = self . choose_from_list ( l , prompt = '<STR_LIT>' ) <EOL> def update_config ( self ) : <EOL> if not self . config . has_section ( '<STR_LIT>' ) : <EOL> self . config . add_section ( '<STR_LIT>' ) <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT>' , self . ec2 . aws_access_key_id ) <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT>' , self . ec2 . aws_secret_access_key ) <EOL> if not self . config . has_section ( '<STR_LIT>' ) : <EOL> self . config . add_section ( '<STR_LIT>' ) <EOL> sdb_domain = get_domain ( ) <EOL> if sdb_domain : <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT>' , sdb_domain ) <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT>' , self . name ) <EOL> def set_config ( self , config_path = None ) : <EOL> if not config_path : <EOL> config_path = self . get_filename ( '<STR_LIT>' ) <EOL> self . config = Config ( path = config_path ) <EOL> def get_userdata_string ( self ) : <EOL> s = StringIO . StringIO ( ) <EOL> self . config . write ( s ) <EOL> return s . getvalue ( ) <EOL> def enter ( self , ** params ) : <EOL> self . region = params . get ( '<STR_LIT>' , self . region ) <EOL> if not self . region : <EOL> self . set_region ( ) <EOL> self . ec2 = self . region . connect ( ) <EOL> self . name = params . get ( '<STR_LIT:name>' , self . name ) <EOL> if not self . name : <EOL> self . set_name ( ) <EOL> self . instance_type = params . get ( '<STR_LIT>' , self . instance_type ) <EOL> if not self . instance_type : <EOL> self . set_instance_type ( ) <EOL> self . zone = params . get ( '<STR_LIT>' , self . zone ) <EOL> if not self . zone : <EOL> self . set_zone ( ) <EOL> self . quantity = params . get ( '<STR_LIT>' , self . quantity ) <EOL> if not self . quantity : <EOL> self . set_quantity ( ) <EOL> self . ami = params . get ( '<STR_LIT>' , self . ami ) <EOL> if not self . ami : <EOL> self . set_ami ( ) <EOL> self . groups = params . get ( '<STR_LIT>' , self . groups ) <EOL> if not self . groups : <EOL> self . add_group ( ) <EOL> self . key = params . get ( '<STR_LIT:key>' , self . key ) <EOL> if not self . key : <EOL> self . set_key ( ) <EOL> self . config = params . get ( '<STR_LIT>' , self . config ) <EOL> if not self . config : <EOL> self . set_config ( ) <EOL> self . update_config ( ) <EOL> class Order ( IObject ) : <EOL> def __init__ ( self ) : <EOL> self . items = [ ] <EOL> self . reservation = None <EOL> def add_item ( self , ** params ) : <EOL> item = Item ( ) <EOL> item . enter ( ** params ) <EOL> self . items . append ( item ) <EOL> def display ( self ) : <EOL> print '<STR_LIT>' <EOL> print <EOL> print '<STR_LIT>' <EOL> for item in self . items : <EOL> print '<STR_LIT>' % ( item . quantity , item . name , item . instance_type , <EOL> item . ami . id , item . groups , item . key . name ) <EOL> def place ( self , block = True ) : <EOL> if get_domain ( ) == None : <EOL> print '<STR_LIT>' <EOL> domain_name = self . get_string ( '<STR_LIT>' ) <EOL> set_domain ( domain_name ) <EOL> s = ServerSet ( ) <EOL> for item in self . items : <EOL> r = item . ami . run ( min_count = <NUM_LIT:1> , max_count = item . quantity , <EOL> key_name = item . key . name , user_data = item . get_userdata_string ( ) , <EOL> security_groups = item . groups , instance_type = item . instance_type , <EOL> placement = item . zone . name ) <EOL> if block : <EOL> states = [ i . state for i in r . instances ] <EOL> if states . count ( '<STR_LIT>' ) != len ( states ) : <EOL> print states <EOL> time . sleep ( <NUM_LIT:15> ) <EOL> states = [ i . update ( ) for i in r . instances ] <EOL> for i in r . instances : <EOL> server = Server ( ) <EOL> server . name = item . name <EOL> server . instance_id = i . id <EOL> server . reservation = r <EOL> server . save ( ) <EOL> s . append ( server ) <EOL> if len ( s ) == <NUM_LIT:1> : <EOL> return s [ <NUM_LIT:0> ] <EOL> else : <EOL> return s </s>
