<s> from datetime import datetime <EOL> from os . path import exists , join <EOL> from os import listdir <EOL> from tempfile import mkdtemp <EOL> from cloudbio . deploy . util import eval_template <EOL> from boto . exception import S3ResponseError <EOL> from boto . s3 . key import Key <EOL> import yaml <EOL> from fabric . api import local , lcd , env <EOL> DEFAULT_BUCKET_NAME = '<STR_LIT>' <EOL> DEFAULT_CLOUDMAN_PASSWORD = '<STR_LIT>' <EOL> DEFAULT_CLOUDMAN_CLUSTER_NAME = '<STR_LIT>' <EOL> def bundle_cloudman ( vm_launcher , options ) : <EOL> cloudman_options = options . get ( '<STR_LIT>' ) <EOL> cloudman_repository_path = cloudman_options [ '<STR_LIT>' ] <EOL> delete_repository = False <EOL> bucket_source = cloudman_options . get ( "<STR_LIT>" ) <EOL> if cloudman_repository_path . startswith ( "<STR_LIT:http>" ) : <EOL> temp_directory = mkdtemp ( ) <EOL> if cloudman_repository_path . endswith ( "<STR_LIT>" ) : <EOL> branch_opts = "<STR_LIT>" <EOL> repository_branch = cloudman_options . get ( '<STR_LIT>' , None ) <EOL> if repository_branch : <EOL> branch_opts = "<STR_LIT>" % repository_branch <EOL> clone_command = "<STR_LIT>" + branch_opts + "<STR_LIT>" <EOL> else : <EOL> clone_command = "<STR_LIT>" <EOL> local ( clone_command % ( cloudman_repository_path , temp_directory ) ) <EOL> cloudman_repository_path = temp_directory <EOL> delete_repository = True <EOL> try : <EOL> with lcd ( cloudman_repository_path ) : <EOL> try : <EOL> local ( "<STR_LIT>" ) <EOL> local ( "<STR_LIT>" % bucket_source ) <EOL> finally : <EOL> local ( "<STR_LIT>" ) <EOL> finally : <EOL> if delete_repository : <EOL> local ( "<STR_LIT>" % cloudman_repository_path ) <EOL> def cloudman_launch ( vm_launcher , options ) : <EOL> cloudman_options = options . get ( '<STR_LIT>' ) <EOL> image_id = cloudman_options . get ( '<STR_LIT>' , None ) <EOL> if str ( image_id ) . lower ( ) == "<STR_LIT>" : <EOL> bucket_source = cloudman_options . get ( "<STR_LIT>" ) <EOL> snaps_path = join ( bucket_source , "<STR_LIT>" ) <EOL> if not exists ( snaps_path ) : <EOL> raise Exception ( "<STR_LIT>" % snaps_path ) <EOL> snaps = { } <EOL> with open ( snaps_path , "<STR_LIT:r>" ) as in_handle : <EOL> snaps = yaml . load ( in_handle ) <EOL> clouds = snaps [ "<STR_LIT>" ] <EOL> if len ( clouds ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> regions = clouds [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> if len ( regions ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> deployments = regions [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> if len ( deployments ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> image_id = deployments [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> size_id = cloudman_options . get ( '<STR_LIT>' , None ) <EOL> user_data = _prepare_user_data ( vm_launcher , cloudman_options ) <EOL> vm_launcher . create_node ( '<STR_LIT>' , <EOL> image_id = image_id , <EOL> size_id = size_id , <EOL> ex_userdata = user_data ) <EOL> def sync_cloudman_bucket ( vm_launcher , options ) : <EOL> bucket = options . get ( "<STR_LIT>" , None ) <EOL> if not bucket : <EOL> bucket = __get_bucket_default ( options ) <EOL> bucket_source = options . get ( "<STR_LIT>" , { } ) . get ( "<STR_LIT>" , None ) <EOL> if not bucket or not bucket_source : <EOL> print "<STR_LIT>" <EOL> return <EOL> conn = vm_launcher . boto_s3_connection ( ) <EOL> for file_name in listdir ( bucket_source ) : <EOL> _save_file_to_bucket ( conn , bucket , file_name , join ( bucket_source , file_name ) ) <EOL> def _save_file_to_bucket ( conn , bucket_name , remote_filename , local_file , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> b = _get_bucket ( conn , bucket_name ) <EOL> if b is not None : <EOL> k = Key ( b , remote_filename ) <EOL> print "<STR_LIT>" % ( remote_filename , bucket_name ) <EOL> try : <EOL> k . set_metadata ( '<STR_LIT>' , str ( datetime . utcnow ( ) ) ) <EOL> for args_key in kwargs : <EOL> print "<STR_LIT>" % ( remote_filename , args_key , kwargs [ args_key ] ) <EOL> k . set_metadata ( args_key , kwargs [ args_key ] ) <EOL> print "<STR_LIT>" % local_file <EOL> k . set_contents_from_filename ( local_file ) <EOL> print "<STR_LIT>" % ( remote_filename , bucket_name ) <EOL> make_public = True <EOL> if make_public : <EOL> k . make_public ( ) <EOL> except S3ResponseError , e : <EOL> print "<STR_LIT>" % ( local_file , bucket_name , remote_filename , e ) <EOL> return False <EOL> return True <EOL> else : <EOL> return False <EOL> def __get_bucket_default ( options ) : <EOL> cloudman_options = options . get ( "<STR_LIT>" , { } ) <EOL> user_data = cloudman_options = cloudman_options . get ( '<STR_LIT>' , None ) or { } <EOL> bucket = user_data . get ( "<STR_LIT>" , None ) <EOL> return bucket <EOL> def _prepare_user_data ( vm_launcher , cloudman_options ) : <EOL> cloudman_user_data = cloudman_options . get ( '<STR_LIT>' , None ) or { } <EOL> cluster_name = cloudman_options . get ( '<STR_LIT>' , DEFAULT_CLOUDMAN_CLUSTER_NAME ) <EOL> password = cloudman_options . get ( '<STR_LIT:password>' , DEFAULT_CLOUDMAN_PASSWORD ) <EOL> access_key = vm_launcher . access_id ( ) <EOL> secret_key = vm_launcher . secret_key ( ) <EOL> _set_property_if_needed ( cloudman_user_data , '<STR_LIT>' , access_key ) <EOL> _set_property_if_needed ( cloudman_user_data , '<STR_LIT>' , secret_key ) <EOL> cluster_name = eval_template ( env , cluster_name ) <EOL> _set_property_if_needed ( cloudman_user_data , '<STR_LIT>' , cluster_name ) <EOL> _set_property_if_needed ( cloudman_user_data , '<STR_LIT:password>' , password ) <EOL> return yaml . dump ( cloudman_user_data ) <EOL> def _set_property_if_needed ( user_data , property , value ) : <EOL> if property not in user_data : <EOL> user_data [ property ] = value <EOL> def _get_bucket ( s3_conn , bucket_name ) : <EOL> b = None <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:5> ) : <EOL> try : <EOL> b = s3_conn . get_bucket ( bucket_name ) <EOL> break <EOL> except S3ResponseError : <EOL> print "<STR_LIT>" % ( bucket_name , i ) <EOL> return None <EOL> return b <EOL> local_actions = { <EOL> "<STR_LIT>" : cloudman_launch , <EOL> "<STR_LIT>" : sync_cloudman_bucket , <EOL> "<STR_LIT>" : bundle_cloudman , <EOL> } </s>
