Commits

Fabian Topfstedt committed bef4c18

prefix support; minor refactorings

Comments (0)

Files changed (2)

 try:
     import snapshotter_conf as conf
 except ImportError:
-    conf = None
+    raise RuntimeError('snapshotter_conf.py expected but missing')
 
 
 AWS_KEY = getattr(conf, 'AWS_KEY', None)
 AWS_SECRET = getattr(conf, 'AWS_SECRET', None)
-MYSQL_HOST = getattr(conf, 'MYSQL_HOST', None)
-MYSQL_SOCKET = getattr(conf, 'MYSQL_SOCKET', None)
+MYSQL_HOST = getattr(conf, 'MYSQL_HOST', 'localhost')
+MYSQL_SOCKET = getattr(conf, 'MYSQL_SOCKET', '/var/run/mysqld/mysqld.sock')
 MYSQL_USERNAME = getattr(conf, 'MYSQL_USERNAME', None)
 MYSQL_PASSWORD = getattr(conf, 'MYSQL_PASSWORD', None)
+PREFIX = getattr(conf, 'PREFIX', 'snapshotter')
 REGION = getattr(conf, 'REGION', 'us-east-1')
-REGION_INFO = RegionInfo(endpoint='%s.ec2.amazonaws.com' % REGION, name=REGION)
 
 
 class Snapshotter(object):
     to the instance and give them custom descriptions that are easy to parse
     and keep track of.
     """
-    def __init__(self, aws_key, aws_secret, region=REGION, mountpoint='/',
-        mysql_host=None, mysql_socket=None, mysql_username=None,
-        mysql_password=None):
+    def __init__(self, aws_key, aws_secret, mysql_username, mysql_password,
+        mysql_host, mysql_socket, prefix, region, mountpoint='/'):
         self.aws_key = aws_key
         self.aws_secret = aws_secret
+        self.mysql_username = mysql_username
+        self.mysql_password = mysql_password
+        self.mysql_host = mysql_host
+        self.mysql_socket = mysql_socket
+        self.prefix = prefix
         self.region = region
         self.mountpoint = mountpoint
-        self.mysql_host = mysql_host
-        self.mysql_socket = mysql_socket
-        self.mysql_username = mysql_username
-        self.mysql_password = mysql_password
         self.instance_id = self._get_instance_id()
         self.instance = self._get_instance_by_id(self.instance_id)
         self.volume_ids = self._get_volume_ids(self.instance)
 
+    def _get_region_info(self):
+        return RegionInfo(endpoint='%s.ec2.amazonaws.com' % self.region,
+            name=self.region)
+
     def _get_instance_id(self):
         url = 'http://169.254.169.254/latest/meta-data/instance-id/'
         response = urllib2.urlopen(url)
         return response.read()
 
     def _get_instance_by_id(self, instance_id):
-        conn = EC2Connection(self.aws_key, self.aws_secret, region=REGION_INFO)
+        region_info = self._get_region_info()
+        conn = EC2Connection(self.aws_key, self.aws_secret, region=region_info)
         for reservation in conn.get_all_instances([instance_id]):
             for instance in reservation.instances:
                 if instance.id == instance_id:
             * One every week within the last 31 days
             * One every month within the last year
         """
-        c = EC2Connection(self.aws_key, self.aws_secret, region=REGION_INFO)
+        region_info = self._get_region_info()
+        c = EC2Connection(self.aws_key, self.aws_secret, region=region_info)
         snapshots = [s for s in c.get_all_snapshots(owner='self') if \
-            s.description.startswith('snapshotter:')]
+            s.description.startswith('%s:' % self.prefix)]
         for s in snapshots:
             now = datetime.now()
             dt = datetime(*map(int, s.description.split(':')[1:6]))
         Create snapshots of all EBS volumes that are currently attached.
         The description property of the snapshot include all the following
         data separated by colons (in that order):
-            * Prefix 'snapshotter' (String)
+            * Prefix (String)
             * Year (int)
             * Month (int)
             * Day (int)
         """
         for volume_id in self.volume_ids:
             now = datetime.now()
-            description = 'snapshotter:%(year)d:%(month)d:%(day)d:%(hour)d:'\
+            description = '%(prefix)s:%(year)d:%(month)d:%(day)d:%(hour)d:'\
                 '%(minute)d:%(weekday)d:%(day_of_year)d:'\
                 '%(days_since_epoche)d:%(volume_id)s' % {
                 'year': now.year,
                 'day_of_year': now.timetuple().tm_yday,
                 'days_since_epoche': (now - datetime(1970, 1, 1)).days,
                 'volume_id': volume_id,
+                'prefix': self.prefix,
             }
 
             mysql_option = ''
 
 if __name__ == '__main__':
     s = Snapshotter(aws_key=AWS_KEY, aws_secret=AWS_SECRET,
+        mysql_username=MYSQL_USERNAME, mysql_password=MYSQL_PASSWORD,
         mysql_host=MYSQL_HOST, mysql_socket=MYSQL_SOCKET,
-        mysql_username=MYSQL_USERNAME,
-        mysql_password=MYSQL_PASSWORD)
+        prefix=PREFIX, region=REGION, mountpoint='/')
     s.create_snapshots()
     s.delete_dispensable_snapshots()

snapshotter_conf.py.example

 MYSQL_SOCKET = '/var/run/mysqld/mysqld.sock'
 MYSQL_USERNAME = 'user'
 MYSQL_PASSWORD = 'password'
-REGION = 'us-east-1'
+PREFIX = 'snapshotter'
+REGION = 'us-east-1'