<s> """<STR_LIT>""" <EOL> import boto <EOL> from boto . manage . volume import Volume <EOL> from boto . exception import EC2ResponseError <EOL> import os , time <EOL> from boto . pyami . installers . ubuntu . installer import Installer <EOL> from string import Template <EOL> BackupScriptTemplate = """<STR_LIT>""" <EOL> BackupCleanupScript = """<STR_LIT>""" <EOL> TagBasedBackupCleanupScript = """<STR_LIT>""" <EOL> class EBSInstaller ( Installer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config_file = None ) : <EOL> super ( EBSInstaller , self ) . __init__ ( config_file ) <EOL> self . instance_id = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . device = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . volume_id = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . logical_volume_name = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . mount_point = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def attach ( self ) : <EOL> ec2 = boto . connect_ec2 ( ) <EOL> if self . logical_volume_name : <EOL> logical_volume = next ( Volume . find ( name = self . logical_volume_name ) ) <EOL> self . volume_id = logical_volume . _volume_id <EOL> volume = ec2 . get_all_volumes ( [ self . volume_id ] ) [ <NUM_LIT:0> ] <EOL> while volume . update ( ) != '<STR_LIT>' : <EOL> boto . log . info ( '<STR_LIT>' % ( volume . id , volume . status ) ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> instance = ec2 . get_only_instances ( [ self . instance_id ] ) [ <NUM_LIT:0> ] <EOL> attempt_attach = True <EOL> while attempt_attach : <EOL> try : <EOL> ec2 . attach_volume ( self . volume_id , self . instance_id , self . device ) <EOL> attempt_attach = False <EOL> except EC2ResponseError as e : <EOL> if e . error_code != '<STR_LIT>' : <EOL> boto . log . info ( '<STR_LIT>' % ( self . volume_id , self . instance_id , e . errors ) ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> else : <EOL> raise e <EOL> boto . log . info ( '<STR_LIT>' % ( self . volume_id , self . instance_id , self . device ) ) <EOL> while not os . path . exists ( self . device ) : <EOL> boto . log . info ( '<STR_LIT>' % self . device ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> def make_fs ( self ) : <EOL> boto . log . info ( '<STR_LIT>' ) <EOL> has_fs = self . run ( '<STR_LIT>' % self . device ) <EOL> if has_fs != <NUM_LIT:0> : <EOL> self . run ( '<STR_LIT>' % self . device ) <EOL> def create_backup_script ( self ) : <EOL> t = Template ( BackupScriptTemplate ) <EOL> s = t . substitute ( volume_id = self . volume_id , instance_id = self . instance_id , <EOL> mount_point = self . mount_point ) <EOL> fp = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> fp . write ( s ) <EOL> fp . close ( ) <EOL> self . run ( '<STR_LIT>' ) <EOL> def create_backup_cleanup_script ( self , use_tag_based_cleanup = False ) : <EOL> fp = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> if use_tag_based_cleanup : <EOL> fp . write ( TagBasedBackupCleanupScript ) <EOL> else : <EOL> fp . write ( BackupCleanupScript ) <EOL> fp . close ( ) <EOL> self . run ( '<STR_LIT>' ) <EOL> def handle_mount_point ( self ) : <EOL> boto . log . info ( '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . mount_point ) : <EOL> boto . log . info ( '<STR_LIT>' ) <EOL> self . run ( "<STR_LIT>" % self . mount_point ) <EOL> else : <EOL> boto . log . info ( '<STR_LIT>' ) <EOL> self . run ( '<STR_LIT>' ) <EOL> lines = self . last_command . output . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> t = line . split ( ) <EOL> if t and t [ <NUM_LIT:2> ] == self . mount_point : <EOL> if t [ <NUM_LIT:0> ] != self . device : <EOL> self . run ( '<STR_LIT>' % self . mount_point ) <EOL> self . run ( '<STR_LIT>' % t [ <NUM_LIT:0> ] ) <EOL> break <EOL> self . run ( '<STR_LIT>' ) <EOL> self . run ( "<STR_LIT>" % ( self . device , self . mount_point ) ) <EOL> self . run ( '<STR_LIT>' % self . mount_point ) <EOL> def update_fstab ( self ) : <EOL> f = open ( "<STR_LIT>" , "<STR_LIT:a>" ) <EOL> f . write ( '<STR_LIT>' % ( self . device , self . mount_point ) ) <EOL> f . close ( ) <EOL> def install ( self ) : <EOL> self . attach ( ) <EOL> self . run ( '<STR_LIT>' ) <EOL> self . make_fs ( ) <EOL> self . handle_mount_point ( ) <EOL> self . create_backup_script ( ) <EOL> minute = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> hour = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add_cron ( "<STR_LIT>" , "<STR_LIT>" , minute = minute , hour = hour ) <EOL> minute = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> hour = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( minute is not None ) and ( hour is not None ) : <EOL> use_tag_based_cleanup = boto . config . has_option ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . create_backup_cleanup_script ( use_tag_based_cleanup ) <EOL> self . add_cron ( "<STR_LIT>" , "<STR_LIT>" , minute = minute , hour = hour ) <EOL> self . update_fstab ( ) <EOL> def main ( self ) : <EOL> if not os . path . exists ( self . device ) : <EOL> self . install ( ) <EOL> else : <EOL> boto . log . info ( "<STR_LIT>" % self . device ) </s>
