blob: d26a235c46a5e755b674565b6d9610d08a4490e4 [file] [log] [blame]
Max Rasskazov26787df2015-06-05 14:47:27 +03001#-*- coding: utf-8 -*-
2
3import datetime
4import os
5
6import utils
7
8from rsync_remote import RsyncRemote
9from utils import singleton
10
11
12@singleton
13class 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
26class 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,
Max Rasskazov01271622015-06-17 02:35:09 +030032 snapshot_dir='snapshots',
33 latest_successful_postfix='latest',
Max Rasskazov26787df2015-06-05 14:47:27 +030034 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 Rasskazov46cc0732015-06-05 19:23:24 +030041 self.snapshot_dir = self.url.a_dir(snapshot_dir)
Max Rasskazov26787df2015-06-05 14:47:27 +030042 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
Max Rasskazov01271622015-06-17 02:35:09 +030055 if self.url.url_type != 'path':
56 server_root = RsyncRemote(self.url.root)
57 return server_root.mkdir(
58 self.url.a_dir(self.url.path, self.snapshot_dir)
59 )
Max Rasskazov26787df2015-06-05 14:47:27 +030060
61 def push(self, source, repo_name, extra=None):
Max Rasskazov46cc0732015-06-05 19:23:24 +030062 latest_path = self.url.a_file(
Max Rasskazov26787df2015-06-05 14:47:27 +030063 self.snapshot_dir,
Max Rasskazov46cc0732015-06-05 19:23:24 +030064 '{}-{}'.format(self.url.a_file(repo_name),
Max Rasskazov26787df2015-06-05 14:47:27 +030065 self.latest_successful_postfix)
66 )
Max Rasskazov46cc0732015-06-05 19:23:24 +030067 snapshot_name = self.url.a_file(
68 '{}-{}'.format(self.url.a_file(repo_name), self.timestamp)
Max Rasskazov26787df2015-06-05 14:47:27 +030069 )
Max Rasskazov46cc0732015-06-05 19:23:24 +030070 repo_path = self.url.a_file(self.snapshot_dir, snapshot_name)
Max Rasskazov26787df2015-06-05 14:47:27 +030071
Max Rasskazov854399e2015-06-05 16:35:17 +030072 extra = '--link-dest={}'.format(
Max Rasskazov46cc0732015-06-05 19:23:24 +030073 self.url.a_file(self.url.path, latest_path)
Max Rasskazov854399e2015-06-05 16:35:17 +030074 )
Max Rasskazov26787df2015-06-05 14:47:27 +030075 result = super(RsyncVersioned, self).push(source, repo_path, extra)
76 super(RsyncVersioned, self).symlink(repo_name, repo_path)
77 super(RsyncVersioned, self).symlink(latest_path, snapshot_name)
78 return result