Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 1 | #-*- coding: utf-8 -*- |
| 2 | |
| 3 | import datetime |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 4 | |
| 5 | import utils |
| 6 | |
| 7 | from rsync_remote import RsyncRemote |
| 8 | from utils import singleton |
| 9 | |
| 10 | |
| 11 | @singleton |
| 12 | class TimeStamp(object): |
| 13 | def __init__(self): |
| 14 | self.now = datetime.datetime.utcnow() |
| 15 | self.staging_snapshot_stamp_format = r'%Y-%m-%d-%H%M%S' |
| 16 | self.staging_snapshot_stamp_regexp = \ |
| 17 | r'[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}' |
| 18 | self.staging_snapshot_stamp = \ |
| 19 | self.now.strftime(self.staging_snapshot_stamp_format) |
| 20 | |
| 21 | def __str__(self): |
| 22 | return self.staging_snapshot_stamp |
| 23 | |
| 24 | |
Max Rasskazov | 3e83758 | 2015-06-17 18:44:15 +0300 | [diff] [blame] | 25 | class TRsync(RsyncRemote): |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 26 | # retry and other function with mirror |
| 27 | # add all the needed directory functions here, like mkdir, ls, rm etc |
| 28 | # possible check that rsync url is exists |
| 29 | def __init__(self, |
| 30 | rsync_url, |
Max Rasskazov | 0127162 | 2015-06-17 02:35:09 +0300 | [diff] [blame] | 31 | snapshot_dir='snapshots', |
| 32 | latest_successful_postfix='latest', |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 33 | save_latest_days=14, |
| 34 | init_directory_structure=True, |
| 35 | ): |
Max Rasskazov | 3e83758 | 2015-06-17 18:44:15 +0300 | [diff] [blame] | 36 | super(TRsync, self).__init__(rsync_url) |
| 37 | self.logger = utils.logger.getChild('TRsync.' + rsync_url) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 38 | self.timestamp = TimeStamp() |
| 39 | self.logger.info('Using timestamp {}'.format(self.timestamp)) |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 40 | self.snapshot_dir = self.url.a_dir(snapshot_dir) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 41 | self.latest_successful_postfix = latest_successful_postfix |
| 42 | self.save_latest_days = save_latest_days |
| 43 | |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 44 | if init_directory_structure is True: |
| 45 | self.init_directory_structure() |
| 46 | |
| 47 | def init_directory_structure(self): |
| 48 | # TODO: self.rsyncRemote.mkdir |
Max Rasskazov | 0127162 | 2015-06-17 02:35:09 +0300 | [diff] [blame] | 49 | if self.url.url_type != 'path': |
| 50 | server_root = RsyncRemote(self.url.root) |
| 51 | return server_root.mkdir( |
| 52 | self.url.a_dir(self.url.path, self.snapshot_dir) |
| 53 | ) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 54 | |
| 55 | def push(self, source, repo_name, extra=None): |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 56 | latest_path = self.url.a_file( |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 57 | self.snapshot_dir, |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 58 | '{}-{}'.format(self.url.a_file(repo_name), |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 59 | self.latest_successful_postfix) |
| 60 | ) |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 61 | snapshot_name = self.url.a_file( |
| 62 | '{}-{}'.format(self.url.a_file(repo_name), self.timestamp) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 63 | ) |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 64 | repo_path = self.url.a_file(self.snapshot_dir, snapshot_name) |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 65 | |
Max Rasskazov | 854399e | 2015-06-05 16:35:17 +0300 | [diff] [blame] | 66 | extra = '--link-dest={}'.format( |
Max Rasskazov | 46cc073 | 2015-06-05 19:23:24 +0300 | [diff] [blame] | 67 | self.url.a_file(self.url.path, latest_path) |
Max Rasskazov | 854399e | 2015-06-05 16:35:17 +0300 | [diff] [blame] | 68 | ) |
Max Rasskazov | a591185 | 2015-06-17 18:29:58 +0300 | [diff] [blame] | 69 | # TODO: retry on base class!!!!!!!!!!!!!!! |
| 70 | # TODO: locking - symlink dir-timestamp.lock -> dir-timestamp |
| 71 | # TODO: write yaml file with symlink info |
| 72 | transaction = list() |
| 73 | try: |
| 74 | # start transaction |
Max Rasskazov | 3e83758 | 2015-06-17 18:44:15 +0300 | [diff] [blame] | 75 | result = super(TRsync, self).push(source, repo_path, extra) |
Max Rasskazov | a591185 | 2015-06-17 18:29:58 +0300 | [diff] [blame] | 76 | transaction.append('repo_dir_created') |
| 77 | self.logger.info('{}'.format(result)) |
| 78 | |
| 79 | try: |
| 80 | old_repo_name_symlink_target = \ |
| 81 | [_[1] for _ in self.ls_symlinks(repo_name)][0] |
| 82 | self.logger.info('Previous {} -> {}' |
| 83 | ''.format(repo_name, |
| 84 | old_repo_name_symlink_target)) |
| 85 | status = 'updated' |
| 86 | except: |
| 87 | status = 'created' |
| 88 | self.symlink(repo_name, repo_path) |
| 89 | transaction.append('symlink_repo_name_{}'.format(status)) |
| 90 | |
| 91 | try: |
| 92 | old_latest_path_symlink_target = \ |
| 93 | [_[1] for _ in self.ls_symlinks(latest_path)][0] |
| 94 | self.logger.info('Previous {} -> {}' |
| 95 | ''.format(latest_path, |
| 96 | old_latest_path_symlink_target)) |
| 97 | status = 'updated' |
| 98 | except: |
| 99 | status = 'created' |
| 100 | self.symlink(latest_path, snapshot_name) |
| 101 | transaction.append('symlink_latest_path_{}'.format(status)) |
| 102 | |
| 103 | self._remove_old_snapshots(repo_name) |
| 104 | transaction.append('old_snapshots_deleted') |
| 105 | |
Max Rasskazov | ad1518a | 2015-06-18 11:24:16 +0300 | [diff] [blame^] | 106 | except RuntimeError: |
Max Rasskazov | a591185 | 2015-06-17 18:29:58 +0300 | [diff] [blame] | 107 | # deleting of old snapshots ignored when assessing the transaction |
| 108 | # only warning |
| 109 | if 'old_snapshots_deleted' not in transaction: |
| 110 | self.logger.warn("Old snapshots are not deleted. Ignore. " |
| 111 | "May be next time.") |
| 112 | transaction.append('old_snapshots_deleted') |
| 113 | |
| 114 | if len(transaction) < 4: |
| 115 | # rollback transaction if some of sync operations failed |
| 116 | |
| 117 | if 'symlink_latest_path_updated' in transaction: |
| 118 | self.logger.info('Restoring symlink {} -> {}' |
| 119 | ''.format(latest_path, |
| 120 | old_latest_path_symlink_target)) |
| 121 | self.symlink(latest_path, old_latest_path_symlink_target) |
| 122 | elif 'symlink_latest_path_created' in transaction: |
| 123 | self.logger.info('Deleting symlink {}'.format(latest_path)) |
| 124 | self.rmfile(latest_path) |
| 125 | |
| 126 | if 'symlink_repo_name_updated' in transaction: |
| 127 | self.logger.info('Restoring symlink {} -> {}' |
| 128 | ''.format(repo_name, |
| 129 | old_repo_name_symlink_target)) |
| 130 | self.symlink(repo_name, old_repo_name_symlink_target) |
| 131 | elif 'symlink_repo_name_created' in transaction: |
| 132 | self.logger.info('Deleting symlink {}'.format(repo_name)) |
| 133 | self.rmfile(repo_name) |
| 134 | |
| 135 | if 'repo_dir_created' in transaction: |
| 136 | self.logger.info('Removing snapshot {}'.format(repo_path)) |
| 137 | self.rmdir(repo_path) |
| 138 | raise |
| 139 | |
Max Rasskazov | 26787df | 2015-06-05 14:47:27 +0300 | [diff] [blame] | 140 | return result |
Max Rasskazov | 452138b | 2015-06-17 02:37:34 +0300 | [diff] [blame] | 141 | |
| 142 | def _remove_old_snapshots(self, repo_name, save_latest_days=None): |
| 143 | if save_latest_days is None: |
| 144 | save_latest_days = self.save_latest_days |
| 145 | if save_latest_days is None or save_latest_days is False: |
| 146 | # delete all snapshots |
| 147 | self.logger.info('Deletion all of the old snapshots ' |
| 148 | '(save_latest_days == {})' |
| 149 | ''.format(save_latest_days)) |
| 150 | save_latest_days = -1 |
| 151 | elif save_latest_days == 0: |
| 152 | # skipping deletion |
| 153 | self.logger.info('Skip deletion of old snapshots ' |
| 154 | '(save_latest_days == {})' |
| 155 | ''.format(save_latest_days)) |
| 156 | return |
| 157 | else: |
| 158 | # delete snapshots older than |
| 159 | self.logger.info('Deletion all of the unlinked snapshots older ' |
| 160 | 'than {0} days (save_latest_days == {0})' |
| 161 | ''.format(save_latest_days)) |
| 162 | warn_date = \ |
| 163 | self.timestamp.now - datetime.timedelta(days=save_latest_days) |
| 164 | warn_date = datetime.datetime.combine(warn_date, datetime.time(0)) |
| 165 | snapshots = self.ls_dirs( |
| 166 | self.url.a_dir(self.snapshot_dir), |
| 167 | pattern=r'^{}-{}$'.format( |
| 168 | repo_name, |
| 169 | self.timestamp.staging_snapshot_stamp_regexp |
| 170 | ) |
| 171 | ) |
| 172 | links = self.ls_symlinks(self.url.a_dir()) |
| 173 | links += self.ls_symlinks(self.url.a_dir(self.snapshot_dir)) |
| 174 | for s in snapshots: |
| 175 | s_date = datetime.datetime.strptime( |
| 176 | s, |
| 177 | '{}-{}'.format(repo_name, |
| 178 | self.timestamp.staging_snapshot_stamp_format) |
| 179 | ) |
| 180 | s_date = datetime.datetime.combine(s_date, datetime.time(0)) |
| 181 | s_path = self.url.a_dir(self.snapshot_dir, s) |
| 182 | if s_date < warn_date: |
| 183 | s_links = [_[0] for _ in links |
| 184 | if _[1] == s |
| 185 | or _[1].endswith('/{}'.format(s)) |
| 186 | ] |
| 187 | if not s_links: |
| 188 | self.rmdir(s_path) |
| 189 | else: |
| 190 | self.logger.info('Skip deletion of "{}" because there are ' |
| 191 | 'symlinks found: {}'.format(s, s_links)) |
| 192 | else: |
| 193 | self.logger.info('Skip deletion of "{}" because it newer than ' |
| 194 | '{} days'.format(s, save_latest_days)) |