ó
è?F[c           @   s–   d  Z  d d l Z d d l m Z d d l m Z d d l Z d d l Z d d l m	 Z	 d d l
 m Z d Z d Z d	 Z d
 e	 f d „  ƒ  YZ d S(   s  
Automated installer to attach, format and mount an EBS volume.
This installer assumes that you want the volume formatted as
an XFS file system.  To drive this installer, you need the
following section in the boto config passed to the new instance.
You also need to install dateutil by listing python-dateutil
in the list of packages to be installed in the Pyami seciont
of your boto config file.

If there is already a device mounted at the specified mount point,
the installer assumes that it is the ephemeral drive and unmounts
it, remounts it as /tmp and chmods it to 777.

Config file section::

    [EBS]
    volume_id = <the id of the EBS volume, should look like vol-xxxxxxxx>
    logical_volume_name = <the name of the logical volume that contaings
        a reference to the physical volume to be mounted. If this parameter
        is supplied, it overrides the volume_id setting.>
    device = <the linux device the EBS volume should be mounted on>
    mount_point = <directory to mount device, defaults to /ebs>

iÿÿÿÿN(   t   Volume(   t   EC2ResponseError(   t	   Installer(   t   Templatesc  #!/usr/bin/env python
# Backup EBS volume
import boto
from boto.pyami.scriptbase import ScriptBase
import traceback

class Backup(ScriptBase):

    def main(self):
        try:
            ec2 = boto.connect_ec2()
            self.run("/usr/sbin/xfs_freeze -f ${mount_point}", exit_on_error = True)
            snapshot = ec2.create_snapshot('${volume_id}')
            boto.log.info("Snapshot created: %s " %  snapshot)
        except Exception as e:
            self.notify(subject="${instance_id} Backup Failed", body=traceback.format_exc())
            boto.log.info("Snapshot created: ${volume_id}")
        except Exception as e:
            self.notify(subject="${instance_id} Backup Failed", body=traceback.format_exc())
        finally:
            self.run("/usr/sbin/xfs_freeze -u ${mount_point}")

if __name__ == "__main__":
    b = Backup()
    b.main()
sœ   #!/usr/bin/env python
import boto
from boto.manage.volume import Volume

# Cleans Backups of EBS volumes

for v in Volume.all():
    v.trim_snapshots(True)
sr   #!/usr/bin/env python
import boto

# Cleans Backups of EBS volumes

ec2 = boto.connect_ec2()
ec2.trim_snapshots()
t   EBSInstallerc           B   se   e  Z d  Z d
 d „ Z d „  Z d „  Z d „  Z e d „ Z	 d „  Z
 d „  Z d „  Z d	 „  Z RS(   s   
    Set up the EBS stuff
    c         C   s˜   t  t |  ƒ j | ƒ t j j d d ƒ |  _ t j j d d d ƒ |  _ t j j d d ƒ |  _ t j j d d ƒ |  _	 t j j d d d	 ƒ |  _
 d  S(
   Nt   Instances   instance-idt   EBSt   devices   /dev/sdpt	   volume_idt   logical_volume_namet   mount_points   /ebs(   t   superR   t   __init__t   botot   configt   gett   instance_idR   R   R	   R
   (   t   selft   config_file(    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyR   h   s    c         C   s»  t  j ƒ  } |  j r? t t j d |  j ƒ ƒ } | j |  _ n  | j |  j g ƒ d } xC | j	 ƒ  d k r t  j
 j d | j | j f ƒ t j d ƒ q[ W| j |  j g ƒ d } t } x‘ | rPy& | j |  j |  j |  j ƒ t } WqÀ t k
 rL} | j d k rCt  j
 j d |  j |  j | j f ƒ t j d ƒ qM| ‚ qÀ XqÀ Wt  j
 j d	 |  j |  j |  j f ƒ x= t j j |  j ƒ s¶t  j
 j d
 |  j ƒ t j d ƒ qzWd  S(   Nt   namei    t	   availables1   Volume %s not yet available. Current status = %s.i   t   IncorrectStates]   Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.i   s.   Attached volume %s to instance %s as device %ss*   %s still does not exist, waiting 2 seconds(   R   t   connect_ec2R	   t   nextR    t   findt
   _volume_idR   t   get_all_volumest   updatet   logt   infot   idt   statust   timet   sleept   get_only_instancesR   t   Truet   attach_volumeR   t   FalseR   t
   error_codet   errorst   ost   patht   exists(   R   t   ec2t   logical_volumet   volumet   instancet   attempt_attacht   e(    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   attachp   s.    	 	
&&c         C   sM   t  j j d ƒ |  j d |  j ƒ } | d k rI |  j d |  j ƒ n  d  S(   Ns
   make_fs...s   fsck %si    s   mkfs -t xfs %s(   R   R   R   t   runR   (   R   t   has_fs(    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   make_fs‘   s    c         C   sj   t  t ƒ } | j d |  j d |  j d |  j ƒ } t d d ƒ } | j | ƒ | j ƒ  |  j	 d ƒ d  S(   NR   R   R
   s   /usr/local/bin/ebs_backupt   ws"   chmod +x /usr/local/bin/ebs_backup(
   R   t   BackupScriptTemplatet
   substituteR   R   R
   t   opent   writet   closeR2   (   R   t   tt   st   fp(    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   create_backup_script—   s    
c         C   sM   t  d d ƒ } | r% | j t ƒ n | j t ƒ | j ƒ  |  j d ƒ d  S(   Ns!   /usr/local/bin/ebs_backup_cleanupR5   s*   chmod +x /usr/local/bin/ebs_backup_cleanup(   R8   R9   t   TagBasedBackupCleanupScriptt   BackupCleanupScriptR:   R2   (   R   t   use_tag_based_cleanupR=   (    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   create_backup_cleanup_script    s    
c         C   s9  t  j j d ƒ t j j |  j ƒ sL t  j j d ƒ |  j d |  j ƒ n« t  j j d ƒ |  j d ƒ |  j j	 j
 d ƒ } xv | D]n } | j
 ƒ  } | r… | d |  j k r… | d |  j k ró |  j d	 |  j ƒ |  j d
 | d ƒ Pqó q… q… W|  j d ƒ |  j d |  j |  j f ƒ |  j d |  j ƒ d  S(   Nt   handle_mount_points   making directorys   mkdir %ss   directory exists alreadys   mount -ls   
i   i    s	   umount %ss   mount %s /tmps   chmod 777 /tmps   mount %s %ss   xfs_growfs %s(   R   R   R   R(   R)   t   isdirR
   R2   t   last_commandt   outputt   splitR   (   R   t   linest   lineR;   (    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyRC   ©   s"    c         C   s:   t  d d ƒ } | j d |  j |  j f ƒ | j ƒ  d  S(   Ns
   /etc/fstabt   as   %s	%s	xfs	defaults 0 0
(   R8   R9   R   R
   R:   (   R   t   f(    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   update_fstabÁ   s    c         C   s  |  j  ƒ  |  j d ƒ |  j ƒ  |  j ƒ  |  j ƒ  t j j d d d ƒ } t j j d d d ƒ } |  j d d d	 | d
 | ƒt j j d d ƒ } t j j d d ƒ } | d  k	 r| d  k	 rt j j
 d d ƒ } |  j | ƒ |  j d d d	 | d
 | ƒn  |  j ƒ  d  S(   Ns#   apt-get -y install xfsprogs xfsdumpR   t   backup_cron_minutet   0t   backup_cron_hours   4,16t
   ebs_backups   /usr/local/bin/ebs_backupt   minutet   hourt   backup_cleanup_cron_minutet   backup_cleanup_cron_hourt   use_tag_based_snapshot_cleanupt   ebs_backup_cleanups!   /usr/local/bin/ebs_backup_cleanup(   R1   R2   R4   RC   R>   R   R   R   t   add_cront   Nonet
   has_optionRB   RL   (   R   RQ   RR   RA   (    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   installÆ   s    



c         C   s=   t  j j |  j ƒ s" |  j ƒ  n t j j d |  j ƒ d  S(   Ns5   Device %s is already attached, skipping EBS Installer(   R(   R)   R*   R   RZ   R   R   R   (   R   (    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   mainê   s    N(   t   __name__t
   __module__t   __doc__RX   R   R1   R4   R>   R%   RB   RC   RL   RZ   R[   (    (    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyR   c   s   	!							$(   R^   R   t   boto.manage.volumeR    t   boto.exceptionR   R(   R    t&   boto.pyami.installers.ubuntu.installerR   t   stringR   R6   R@   R?   R   (    (    (    s?   lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt   <module>-   s   
	