blob: b67280cf7e8d727a61d263d34c91749943ba31ab [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,
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))
41 self.snapshot_dir = self.root.dirname(snapshot_dir)
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):
61 latest_path = self.root.filename(
62 self.snapshot_dir,
63 '{}-{}'.format(self.root.filename(repo_name),
64 self.latest_successful_postfix)
65 )
66 snapshot_name = self.root.filename(
67 '{}-{}'.format(self.root.filename(repo_name), self.timestamp)
68 )
69 repo_path = self.root.filename(self.snapshot_dir, snapshot_name)
70
71 # TODO: check --link-dest !!!!
72 # FILESROOT=fwm/files
73 # OPTIONS="--archive --verbose --force --ignore-errors
74 # --delete-excluded --no-owner --no-group ${RSYNC_EXTRA_PARAMS}
75 # --delete --link-dest=/$FILESROOT/$PROJECTNAME-staging"
76 extra = '--link-dest={}'.format(latest_path)
77 result = super(RsyncVersioned, self).push(source, repo_path, extra)
78 super(RsyncVersioned, self).symlink(repo_name, repo_path)
79 super(RsyncVersioned, self).symlink(latest_path, snapshot_name)
80 return result