<s> from bigjob import logger <EOL> import os <EOL> import uuid <EOL> import time <EOL> import traceback <EOL> import sys <EOL> from boto . ec2 . connection import EC2Connection <EOL> from boto . ec2 . regioninfo import RegionInfo <EOL> import boto . ec2 <EOL> import saga <EOL> PLACEMENT_GROUP = None <EOL> SECURITY_GROUP = "<STR_LIT:default>" <EOL> AWS_ACCESS_KEY_ID = None <EOL> AWS_SECRET_ACCESS_KEY = None <EOL> class State : <EOL> UNKNOWN = "<STR_LIT>" <EOL> PENDING = "<STR_LIT>" <EOL> RUNNING = "<STR_LIT>" <EOL> class Service ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource_url , pilot_compute_description = None ) : <EOL> """<STR_LIT>""" <EOL> self . resource_url = resource_url <EOL> self . pilot_compute_description = pilot_compute_description <EOL> def create_job ( self , job_description ) : <EOL> j = Job ( job_description , self . resource_url , self . pilot_compute_description ) <EOL> return j <EOL> def __del__ ( self ) : <EOL> pass <EOL> class Job ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , job_description , resource_url , pilot_compute_description ) : <EOL> self . job_description = job_description <EOL> logger . debug ( "<STR_LIT>" + str ( resource_url ) + "<STR_LIT>" + str ( type ( resource_url ) ) ) <EOL> self . resource_url = saga . Url ( str ( resource_url ) ) <EOL> self . pilot_compute_description = pilot_compute_description <EOL> self . id = "<STR_LIT>" + str ( uuid . uuid1 ( ) ) <EOL> self . network_ip = None <EOL> self . ec2_conn = None <EOL> if self . resource_url . scheme == "<STR_LIT>" or self . resource_url . scheme == "<STR_LIT>" : <EOL> host = self . resource_url . host <EOL> path = "<STR_LIT>" <EOL> if self . resource_url . path != None : <EOL> path = self . resource_url . path <EOL> port = <NUM_LIT> <EOL> if self . resource_url . port != None : <EOL> port = self . resource_url . port <EOL> region = None <EOL> logger . debug ( "<STR_LIT>" % ( host , path , port ) ) <EOL> if self . resource_url . scheme == "<STR_LIT>" : <EOL> region = RegionInfo ( name = "<STR_LIT>" , endpoint = host ) <EOL> elif self . resource_url . scheme == "<STR_LIT>" : <EOL> region = RegionInfo ( name = "<STR_LIT>" , endpoint = host ) <EOL> logger . debug ( "<STR_LIT>" % ( self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> self . pilot_compute_description [ "<STR_LIT>" ] ) ) <EOL> self . ec2_conn = EC2Connection ( aws_access_key_id = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> aws_secret_access_key = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> region = region , <EOL> is_secure = False , <EOL> port = port , <EOL> path = path ) <EOL> else : <EOL> aws_region = None <EOL> if self . pilot_compute_description . has_key ( "<STR_LIT>" ) : <EOL> region = self . pilot_compute_description [ "<STR_LIT>" ] <EOL> logger . debug ( "<STR_LIT>" % ( str ( region ) ) ) <EOL> aws_region = boto . ec2 . get_region ( region , <EOL> aws_access_key_id = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> aws_secret_access_key = self . pilot_compute_description [ "<STR_LIT>" ] <EOL> ) <EOL> self . ec2_conn = EC2Connection ( aws_access_key_id = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> aws_secret_access_key = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> region = aws_region ) <EOL> self . instance = None <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> reservation = self . ec2_conn . run_instances ( self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> key_name = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> instance_type = self . pilot_compute_description [ "<STR_LIT>" ] , <EOL> security_groups = [ SECURITY_GROUP ] ) <EOL> self . instance = reservation . instances [ <NUM_LIT:0> ] <EOL> self . instance_id = self . instance . id <EOL> logger . debug ( "<STR_LIT>" % self . instance_id ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . wait_for_running ( ) <EOL> if self . resource_url . scheme != "<STR_LIT>" and self . resource_url . scheme != "<STR_LIT>" : <EOL> self . ec2_conn . create_tags ( [ self . instance_id ] , { "<STR_LIT:Name>" : self . id } ) <EOL> self . network_ip = self . instance . ip_address <EOL> url = "<STR_LIT>" + str ( self . network_ip ) <EOL> logger . debug ( "<STR_LIT>" % ( url ) ) <EOL> ctx = saga . Context ( "<STR_LIT>" ) <EOL> ctx . user_id = self . pilot_compute_description [ "<STR_LIT>" ] <EOL> ctx . user_key = self . pilot_compute_description [ "<STR_LIT>" ] <EOL> session = saga . Session ( ) <EOL> session . add_context ( ctx ) <EOL> TRIAL_MAX = <NUM_LIT:30> <EOL> trials = <NUM_LIT:0> <EOL> while trials < TRIAL_MAX : <EOL> try : <EOL> js = saga . job . Service ( url , session = session ) <EOL> logger . debug ( "<STR_LIT>" + str ( type ( self . job_description ) ) ) <EOL> job = js . create_job ( self . job_description ) <EOL> logger . debug ( "<STR_LIT>" % ( trials , str ( url ) ) ) <EOL> job . run ( ) <EOL> if job . get_state ( ) == saga . job . FAILED : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> trials = trials + <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:30> ) <EOL> continue <EOL> else : <EOL> break <EOL> except : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> logger . warning ( "<STR_LIT>" + str ( exc_value ) ) <EOL> trials = trials + <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:30> ) <EOL> if trials == TRIAL_MAX : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> logger . debug ( "<STR_LIT>" % ( job . get_state ( ) ) ) <EOL> def wait_for_running ( self ) : <EOL> while self . get_state ( ) != State . RUNNING : <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> def get_state ( self ) : <EOL> result = State . UNKNOWN <EOL> try : <EOL> self . instance . update ( ) <EOL> result = self . instance . state <EOL> except : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return result <EOL> def cancel ( self ) : <EOL> self . instance . terminate ( ) <EOL> def __print_traceback ( self ) : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> print "<STR_LIT>" <EOL> traceback . print_tb ( exc_traceback , limit = <NUM_LIT:1> , file = sys . stdout ) <EOL> print "<STR_LIT>" <EOL> traceback . print_exception ( exc_type , exc_value , exc_traceback , <EOL> limit = <NUM_LIT:2> , file = sys . stdout ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> ec2_service = Service ( "<STR_LIT>" ) <EOL> j = ec2_service . create_job ( "<STR_LIT>" ) <EOL> j . run ( ) <EOL> print j . get_state ( ) </s>
