<s> """<STR_LIT>""" <EOL> import boto <EOL> from boto . ec2 . ec2object import EC2Object , TaggedEC2Object <EOL> from boto . resultset import ResultSet <EOL> from boto . ec2 . address import Address <EOL> from boto . ec2 . blockdevicemapping import BlockDeviceMapping <EOL> from boto . ec2 . image import ProductCodes <EOL> from boto . ec2 . networkinterface import NetworkInterface <EOL> from boto . ec2 . group import Group <EOL> import base64 <EOL> class Reservation ( EC2Object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection = None ) : <EOL> EC2Object . __init__ ( self , connection ) <EOL> self . id = None <EOL> self . owner_id = None <EOL> self . groups = [ ] <EOL> self . instances = [ ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . id <EOL> def startElement ( self , name , attrs , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . instances = ResultSet ( [ ( '<STR_LIT>' , Instance ) ] ) <EOL> return self . instances <EOL> elif name == '<STR_LIT>' : <EOL> self . groups = ResultSet ( [ ( '<STR_LIT>' , Group ) ] ) <EOL> return self . groups <EOL> else : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . owner_id = value <EOL> else : <EOL> setattr ( self , name , value ) <EOL> def stop_all ( self ) : <EOL> for instance in self . instances : <EOL> instance . stop ( ) <EOL> class Instance ( TaggedEC2Object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection = None ) : <EOL> TaggedEC2Object . __init__ ( self , connection ) <EOL> self . id = None <EOL> self . dns_name = None <EOL> self . public_dns_name = None <EOL> self . private_dns_name = None <EOL> self . state = None <EOL> self . state_code = None <EOL> self . key_name = None <EOL> self . shutdown_state = None <EOL> self . previous_state = None <EOL> self . instance_type = None <EOL> self . launch_time = None <EOL> self . image_id = None <EOL> self . placement = None <EOL> self . kernel = None <EOL> self . ramdisk = None <EOL> self . product_codes = ProductCodes ( ) <EOL> self . ami_launch_index = None <EOL> self . monitored = False <EOL> self . spot_instance_request_id = None <EOL> self . subnet_id = None <EOL> self . vpc_id = None <EOL> self . private_ip_address = None <EOL> self . ip_address = None <EOL> self . requester_id = None <EOL> self . _in_monitoring_element = False <EOL> self . persistent = False <EOL> self . root_device_name = None <EOL> self . root_device_type = None <EOL> self . block_device_mapping = None <EOL> self . state_reason = None <EOL> self . group_name = None <EOL> self . client_token = None <EOL> self . eventsSet = None <EOL> self . groups = [ ] <EOL> self . platform = None <EOL> self . interfaces = [ ] <EOL> self . hypervisor = None <EOL> self . virtualization_type = None <EOL> self . architecture = None <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . id <EOL> def startElement ( self , name , attrs , connection ) : <EOL> retval = TaggedEC2Object . startElement ( self , name , attrs , connection ) <EOL> if retval is not None : <EOL> return retval <EOL> if name == '<STR_LIT>' : <EOL> self . _in_monitoring_element = True <EOL> elif name == '<STR_LIT>' : <EOL> self . block_device_mapping = BlockDeviceMapping ( ) <EOL> return self . block_device_mapping <EOL> elif name == '<STR_LIT>' : <EOL> return self . product_codes <EOL> elif name == '<STR_LIT>' : <EOL> self . state_reason = SubParse ( '<STR_LIT>' ) <EOL> return self . state_reason <EOL> elif name == '<STR_LIT>' : <EOL> self . groups = ResultSet ( [ ( '<STR_LIT>' , Group ) ] ) <EOL> return self . groups <EOL> elif name == "<STR_LIT>" : <EOL> self . eventsSet = SubParse ( '<STR_LIT>' ) <EOL> return self . eventsSet <EOL> elif name == '<STR_LIT>' : <EOL> self . interfaces = ResultSet ( [ ( '<STR_LIT>' , NetworkInterface ) ] ) <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . image_id = value <EOL> elif name == '<STR_LIT>' or name == '<STR_LIT>' : <EOL> self . dns_name = value <EOL> self . public_dns_name = value <EOL> elif name == '<STR_LIT>' : <EOL> self . private_dns_name = value <EOL> elif name == '<STR_LIT>' : <EOL> self . key_name = value <EOL> elif name == '<STR_LIT>' : <EOL> self . ami_launch_index = value <EOL> elif name == '<STR_LIT>' : <EOL> self . shutdown_state = value <EOL> elif name == '<STR_LIT>' : <EOL> self . previous_state = value <EOL> elif name == '<STR_LIT:name>' : <EOL> self . state = value <EOL> elif name == '<STR_LIT:code>' : <EOL> try : <EOL> self . state_code = int ( value ) <EOL> except ValueError : <EOL> boto . log . warning ( '<STR_LIT>' % value ) <EOL> self . state_code = value <EOL> elif name == '<STR_LIT>' : <EOL> self . instance_type = value <EOL> elif name == '<STR_LIT>' : <EOL> self . root_device_name = value <EOL> elif name == '<STR_LIT>' : <EOL> self . root_device_type = value <EOL> elif name == '<STR_LIT>' : <EOL> self . launch_time = value <EOL> elif name == '<STR_LIT>' : <EOL> self . placement = value <EOL> elif name == '<STR_LIT>' : <EOL> self . platform = value <EOL> elif name == '<STR_LIT>' : <EOL> pass <EOL> elif name == '<STR_LIT>' : <EOL> self . kernel = value <EOL> elif name == '<STR_LIT>' : <EOL> self . ramdisk = value <EOL> elif name == '<STR_LIT:state>' : <EOL> if self . _in_monitoring_element : <EOL> if value == '<STR_LIT>' : <EOL> self . monitored = True <EOL> self . _in_monitoring_element = False <EOL> elif name == '<STR_LIT>' : <EOL> self . spot_instance_request_id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . subnet_id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . vpc_id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . private_ip_address = value <EOL> elif name == '<STR_LIT>' : <EOL> self . ip_address = value <EOL> elif name == '<STR_LIT>' : <EOL> self . requester_id = value <EOL> elif name == '<STR_LIT>' : <EOL> if value == '<STR_LIT:true>' : <EOL> self . persistent = True <EOL> else : <EOL> self . persistent = False <EOL> elif name == '<STR_LIT>' : <EOL> if self . _in_monitoring_element : <EOL> self . group_name = value <EOL> elif name == '<STR_LIT>' : <EOL> self . client_token = value <EOL> elif name == "<STR_LIT>" : <EOL> self . events = value <EOL> elif name == '<STR_LIT>' : <EOL> self . hypervisor = value <EOL> elif name == '<STR_LIT>' : <EOL> self . virtualization_type = value <EOL> elif name == '<STR_LIT>' : <EOL> self . architecture = value <EOL> else : <EOL> setattr ( self , name , value ) <EOL> def _update ( self , updated ) : <EOL> self . __dict__ . update ( updated . __dict__ ) <EOL> def update ( self , validate = False ) : <EOL> """<STR_LIT>""" <EOL> rs = self . connection . get_all_instances ( [ self . id ] ) <EOL> if len ( rs ) > <NUM_LIT:0> : <EOL> r = rs [ <NUM_LIT:0> ] <EOL> for i in r . instances : <EOL> if i . id == self . id : <EOL> self . _update ( i ) <EOL> elif validate : <EOL> raise ValueError ( '<STR_LIT>' % self . id ) <EOL> return self . state <EOL> def terminate ( self ) : <EOL> """<STR_LIT>""" <EOL> rs = self . connection . terminate_instances ( [ self . id ] ) <EOL> if len ( rs ) > <NUM_LIT:0> : <EOL> self . _update ( rs [ <NUM_LIT:0> ] ) <EOL> def stop ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> rs = self . connection . stop_instances ( [ self . id ] , force ) <EOL> if len ( rs ) > <NUM_LIT:0> : <EOL> self . _update ( rs [ <NUM_LIT:0> ] ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> rs = self . connection . start_instances ( [ self . id ] ) <EOL> if len ( rs ) > <NUM_LIT:0> : <EOL> self . _update ( rs [ <NUM_LIT:0> ] ) <EOL> def reboot ( self ) : <EOL> return self . connection . reboot_instances ( [ self . id ] ) <EOL> def get_console_output ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . get_console_output ( self . id ) <EOL> def confirm_product ( self , product_code ) : <EOL> return self . connection . confirm_product_instance ( self . id , product_code ) <EOL> def use_ip ( self , ip_address ) : <EOL> if isinstance ( ip_address , Address ) : <EOL> ip_address = ip_address . public_ip <EOL> return self . connection . associate_address ( self . id , ip_address ) <EOL> def monitor ( self ) : <EOL> return self . connection . monitor_instance ( self . id ) <EOL> def unmonitor ( self ) : <EOL> return self . connection . unmonitor_instance ( self . id ) <EOL> def get_attribute ( self , attribute ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . get_instance_attribute ( self . id , attribute ) <EOL> def modify_attribute ( self , attribute , value ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . modify_instance_attribute ( self . id , attribute , <EOL> value ) <EOL> def reset_attribute ( self , attribute ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . reset_instance_attribute ( self . id , attribute ) <EOL> class ConsoleOutput : <EOL> def __init__ ( self , parent = None ) : <EOL> self . parent = parent <EOL> self . instance_id = None <EOL> self . timestamp = None <EOL> self . output = None <EOL> def startElement ( self , name , attrs , connection ) : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . instance_id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . timestamp = value <EOL> elif name == '<STR_LIT>' : <EOL> self . output = base64 . b64decode ( value ) <EOL> else : <EOL> setattr ( self , name , value ) <EOL> class InstanceAttribute ( dict ) : <EOL> ValidValues = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def __init__ ( self , parent = None ) : <EOL> dict . __init__ ( self ) <EOL> self . instance_id = None <EOL> self . request_id = None <EOL> self . _current_value = None <EOL> def startElement ( self , name , attrs , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self [ name ] = BlockDeviceMapping ( ) <EOL> return self [ name ] <EOL> elif name == '<STR_LIT>' : <EOL> self [ name ] = ResultSet ( [ ( '<STR_LIT>' , Group ) ] ) <EOL> return self [ name ] <EOL> else : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . instance_id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . request_id = value <EOL> elif name == '<STR_LIT:value>' : <EOL> self . _current_value = value <EOL> elif name in self . ValidValues : <EOL> self [ name ] = self . _current_value <EOL> class SubParse ( dict ) : <EOL> def __init__ ( self , section , parent = None ) : <EOL> dict . __init__ ( self ) <EOL> self . section = section <EOL> def startElement ( self , name , attrs , connection ) : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name != self . section : <EOL> self [ name ] = value </s>
