blob: 0dbb1401100f01716af3c16cf3cc203724f97932 [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
Max Rasskazov26787df2015-06-05 14:47:27 +030045 if init_directory_structure is True:
46 self.init_directory_structure()
47
48 def init_directory_structure(self):
49 # TODO: self.rsyncRemote.mkdir
Max Rasskazov01271622015-06-17 02:35:09 +030050 if self.url.url_type != 'path':
51 server_root = RsyncRemote(self.url.root)
52 return server_root.mkdir(
53 self.url.a_dir(self.url.path, self.snapshot_dir)
54 )
Max Rasskazov26787df2015-06-05 14:47:27 +030055
56 def push(self, source, repo_name, extra=None):
Max Rasskazov46cc0732015-06-05 19:23:24 +030057 latest_path = self.url.a_file(
Max Rasskazov26787df2015-06-05 14:47:27 +030058 self.snapshot_dir,
Max Rasskazov46cc0732015-06-05 19:23:24 +030059 '{}-{}'.format(self.url.a_file(repo_name),
Max Rasskazov26787df2015-06-05 14:47:27 +030060 self.latest_successful_postfix)
61 )
Max Rasskazov46cc0732015-06-05 19:23:24 +030062 snapshot_name = self.url.a_file(
63 '{}-{}'.format(self.url.a_file(repo_name), self.timestamp)
Max Rasskazov26787df2015-06-05 14:47:27 +030064 )
Max Rasskazov46cc0732015-06-05 19:23:24 +030065 repo_path = self.url.a_file(self.snapshot_dir, snapshot_name)
Max Rasskazov26787df2015-06-05 14:47:27 +030066
Max Rasskazov854399e2015-06-05 16:35:17 +030067 extra = '--link-dest={}'.format(
Max Rasskazov46cc0732015-06-05 19:23:24 +030068 self.url.a_file(self.url.path, latest_path)
Max Rasskazov854399e2015-06-05 16:35:17 +030069 )
Max Rasskazov26787df2015-06-05 14:47:27 +030070 result = super(RsyncVersioned, self).push(source, repo_path, extra)
71 super(RsyncVersioned, self).symlink(repo_name, repo_path)
72 super(RsyncVersioned, self).symlink(latest_path, snapshot_name)
73 return result