blob: 076d6914d256d1a1c9d8816afaa77563826f028f [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))
Max Rasskazovd3f57d82015-06-05 15:56:53 +030041 self.snapshot_dir = self.url.dirname(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
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 Rasskazovd3f57d82015-06-05 15:56:53 +030061 latest_path = self.url.filename(
Max Rasskazov26787df2015-06-05 14:47:27 +030062 self.snapshot_dir,
Max Rasskazovd3f57d82015-06-05 15:56:53 +030063 '{}-{}'.format(self.url.filename(repo_name),
Max Rasskazov26787df2015-06-05 14:47:27 +030064 self.latest_successful_postfix)
65 )
Max Rasskazovd3f57d82015-06-05 15:56:53 +030066 snapshot_name = self.url.filename(
67 '{}-{}'.format(self.url.filename(repo_name), self.timestamp)
Max Rasskazov26787df2015-06-05 14:47:27 +030068 )
Max Rasskazovd3f57d82015-06-05 15:56:53 +030069 repo_path = self.url.filename(self.snapshot_dir, snapshot_name)
Max Rasskazov26787df2015-06-05 14:47:27 +030070
Max Rasskazov854399e2015-06-05 16:35:17 +030071 extra = '--link-dest={}'.format(
72 self.url.filename(self.url.path, latest_path)
73 )
Max Rasskazov26787df2015-06-05 14:47:27 +030074 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