<s> from optparse import OptionParser <EOL> from boto . services . servicedef import ServiceDef <EOL> from boto . services . submit import Submitter <EOL> from boto . services . result import ResultProcessor <EOL> import boto <EOL> import sys , os , StringIO <EOL> class BS ( object ) : <EOL> Usage = "<STR_LIT>" <EOL> Commands = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> def __init__ ( self ) : <EOL> self . service_name = None <EOL> self . parser = OptionParser ( usage = self . Usage ) <EOL> self . parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT:path>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . set_defaults ( leave = False ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . set_defaults ( num_instances = <NUM_LIT:1> ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , type = "<STR_LIT:string>" , dest = "<STR_LIT:ignore>" , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> def print_command_help ( self ) : <EOL> print '<STR_LIT>' <EOL> for key in self . Commands . keys ( ) : <EOL> print '<STR_LIT>' % ( key , self . Commands [ key ] ) <EOL> def do_reset ( self ) : <EOL> iq = self . sd . get_obj ( '<STR_LIT>' ) <EOL> if iq : <EOL> print '<STR_LIT>' <EOL> i = <NUM_LIT:0> <EOL> m = iq . read ( ) <EOL> while m : <EOL> i += <NUM_LIT:1> <EOL> iq . delete_message ( m ) <EOL> m = iq . read ( ) <EOL> print '<STR_LIT>' % i <EOL> ob = self . sd . get_obj ( '<STR_LIT>' ) <EOL> ib = self . sd . get_obj ( '<STR_LIT>' ) <EOL> if ob : <EOL> if ib and ob . name == ib . name : <EOL> return <EOL> print '<STR_LIT>' <EOL> i = <NUM_LIT:0> <EOL> for k in ob : <EOL> i += <NUM_LIT:1> <EOL> k . delete ( ) <EOL> print '<STR_LIT>' % i <EOL> def do_submit ( self ) : <EOL> if not self . options . path : <EOL> self . parser . error ( '<STR_LIT>' ) <EOL> if not os . path . exists ( self . options . path ) : <EOL> self . parser . error ( '<STR_LIT>' % self . options . path ) <EOL> s = Submitter ( self . sd ) <EOL> t = s . submit_path ( self . options . path , None , self . options . ignore , None , <EOL> None , True , self . options . path ) <EOL> print '<STR_LIT>' % t [ <NUM_LIT:1> ] <EOL> print '<STR_LIT>' % t [ <NUM_LIT:0> ] <EOL> def do_start ( self ) : <EOL> ami_id = self . sd . get ( '<STR_LIT>' ) <EOL> instance_type = self . sd . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> security_group = self . sd . get ( '<STR_LIT>' , '<STR_LIT:default>' ) <EOL> if not ami_id : <EOL> self . parser . error ( '<STR_LIT>' ) <EOL> ec2 = boto . connect_ec2 ( ) <EOL> if not self . sd . has_section ( '<STR_LIT>' ) : <EOL> self . sd . add_section ( '<STR_LIT>' ) <EOL> self . sd . set ( '<STR_LIT>' , '<STR_LIT>' , ec2 . aws_access_key_id ) <EOL> self . sd . set ( '<STR_LIT>' , '<STR_LIT>' , ec2 . aws_secret_access_key ) <EOL> s = StringIO . StringIO ( ) <EOL> self . sd . write ( s ) <EOL> rs = ec2 . get_all_images ( [ ami_id ] ) <EOL> img = rs [ <NUM_LIT:0> ] <EOL> r = img . run ( user_data = s . getvalue ( ) , key_name = self . options . keypair , <EOL> max_count = self . options . num_instances , <EOL> instance_type = instance_type , <EOL> security_groups = [ security_group ] ) <EOL> print '<STR_LIT>' % ami_id <EOL> print '<STR_LIT>' % r . id <EOL> for i in r . instances : <EOL> print '<STR_LIT>' % i . id <EOL> def do_status ( self ) : <EOL> iq = self . sd . get_obj ( '<STR_LIT>' ) <EOL> if iq : <EOL> print '<STR_LIT>' % ( iq . id , iq . count ( ) ) <EOL> ob = self . sd . get_obj ( '<STR_LIT>' ) <EOL> ib = self . sd . get_obj ( '<STR_LIT>' ) <EOL> if ob : <EOL> if ib and ob . name == ib . name : <EOL> return <EOL> total = <NUM_LIT:0> <EOL> for k in ob : <EOL> total += <NUM_LIT:1> <EOL> print '<STR_LIT>' % ( ob . name , total ) <EOL> def do_retrieve ( self ) : <EOL> if not self . options . path : <EOL> self . parser . error ( '<STR_LIT>' ) <EOL> if not os . path . exists ( self . options . path ) : <EOL> self . parser . error ( '<STR_LIT>' % self . options . path ) <EOL> if not self . options . batch : <EOL> self . parser . error ( '<STR_LIT>' ) <EOL> s = ResultProcessor ( self . options . batch , self . sd ) <EOL> s . get_results ( self . options . path , get_file = ( not self . options . leave ) ) <EOL> def do_batches ( self ) : <EOL> d = self . sd . get_obj ( '<STR_LIT>' ) <EOL> if d : <EOL> print '<STR_LIT>' <EOL> rs = d . query ( "<STR_LIT>" ) <EOL> for item in rs : <EOL> print '<STR_LIT>' % item . name <EOL> else : <EOL> self . parser . error ( '<STR_LIT>' ) <EOL> def main ( self ) : <EOL> self . options , self . args = self . parser . parse_args ( ) <EOL> if self . options . help_commands : <EOL> self . print_command_help ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if len ( self . args ) != <NUM_LIT:2> : <EOL> self . parser . error ( "<STR_LIT>" ) <EOL> self . config_file = self . args [ <NUM_LIT:0> ] <EOL> self . sd = ServiceDef ( self . config_file ) <EOL> self . command = self . args [ <NUM_LIT:1> ] <EOL> if hasattr ( self , '<STR_LIT>' % self . command ) : <EOL> method = getattr ( self , '<STR_LIT>' % self . command ) <EOL> method ( ) <EOL> else : <EOL> self . parser . error ( '<STR_LIT>' % self . command ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> bs = BS ( ) <EOL> bs . main ( ) </s>
