Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 1 | #-*- coding: utf-8 -*- |
| 2 | |
| 3 | import datetime |
| 4 | import os |
| 5 | |
| 6 | import utils |
| 7 | |
| 8 | from rsync_remote import RsyncRemote |
| 9 | from utils import singleton |
| 10 | |
| 11 | |
| 12 | @singleton |
| 13 | class TimeStamp(object): |
| 14 | def __init__(self): |
| 15 | self.now = datetime.datetime.utcnow() |
| 16 | self.staging_snapshot_stamp_format = r'%Y-%m-%d-%H%M%S' |
| 17 | self.staging_snapshot_stamp_regexp = \ |
| 18 | r'[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}' |
| 19 | self.staging_snapshot_stamp = \ |
| 20 | self.now.strftime(self.staging_snapshot_stamp_format) |
| 21 | |
| 22 | def __str__(self): |
| 23 | return self.staging_snapshot_stamp |
| 24 | |
| 25 | |
| 26 | class RsyncVersioned(RsyncRemote): |
| 27 | # retry and other function with mirror |
| 28 | # add all the needed directory functions here, like mkdir, ls, rm etc |
| 29 | # possible check that rsync url is exists |
| 30 | def __init__(self, |
| 31 | rsync_url, |
| 32 | snapshot_dir='files', |
| 33 | latest_successful_postfix='staging', |
| 34 | save_latest_days=14, |
| 35 | init_directory_structure=True, |
| 36 | ): |
| 37 | super(RsyncVersioned, self).__init__(rsync_url) |
| 38 | self.logger = utils.logger.getChild('RsyncVersioned.' + rsync_url) |
| 39 | self.timestamp = TimeStamp() |
| 40 | self.logger.info('Using timestamp {}'.format(self.timestamp)) |
Max Rasskazov | d3f57d8 | 2015-06-05 15:56:53 +0300 | [diff] [blame] | 41 | self.snapshot_dir = self.url.dirname(snapshot_dir) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 42 | self.latest_successful_postfix = latest_successful_postfix |
| 43 | self.save_latest_days = save_latest_days |
| 44 | |
| 45 | # TODO: mirror_name - name of synced directory |
| 46 | # it located inside rsync_url/ |
| 47 | # UPD: not needed - take it in self.push.dest patameter |
| 48 | self.mirror_name = os.path.split(rsync_url)[1] |
| 49 | |
| 50 | if init_directory_structure is True: |
| 51 | self.init_directory_structure() |
| 52 | |
| 53 | def init_directory_structure(self): |
| 54 | # TODO: self.rsyncRemote.mkdir |
| 55 | #if self.root.url_type != 'path': |
| 56 | #server_root = rsyncRemote(self.root.urlroot) |
| 57 | #server_root.mkdir(self.root.path) |
| 58 | pass |
| 59 | |
| 60 | def push(self, source, repo_name, extra=None): |
Max Rasskazov | d3f57d8 | 2015-06-05 15:56:53 +0300 | [diff] [blame] | 61 | latest_path = self.url.filename( |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 62 | self.snapshot_dir, |
Max Rasskazov | d3f57d8 | 2015-06-05 15:56:53 +0300 | [diff] [blame] | 63 | '{}-{}'.format(self.url.filename(repo_name), |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 64 | self.latest_successful_postfix) |
| 65 | ) |
Max Rasskazov | d3f57d8 | 2015-06-05 15:56:53 +0300 | [diff] [blame] | 66 | snapshot_name = self.url.filename( |
| 67 | '{}-{}'.format(self.url.filename(repo_name), self.timestamp) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 68 | ) |
Max Rasskazov | d3f57d8 | 2015-06-05 15:56:53 +0300 | [diff] [blame] | 69 | repo_path = self.url.filename(self.snapshot_dir, snapshot_name) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 70 | |
Max Rasskazov | 854399e | 2015-06-05 16:35:17 +0300 | [diff] [blame] | 71 | extra = '--link-dest={}'.format( |
| 72 | self.url.filename(self.url.path, latest_path) |
| 73 | ) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 74 | result = super(RsyncVersioned, self).push(source, repo_path, extra) |
| 75 | super(RsyncVersioned, self).symlink(repo_name, repo_path) |
| 76 | super(RsyncVersioned, self).symlink(latest_path, snapshot_name) |
| 77 | return result |