-
Notifications
You must be signed in to change notification settings - Fork 0
/
createschedules.py
65 lines (58 loc) · 2.2 KB
/
createschedules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
import confighelper
from datetime import datetime
import jirahelpers
import logging
import mongohelper
module = 'createschedules'
logger = logging.getLogger(module)
class Schedule(object):
def __init__(self, primary, start_date, end_date, backups):
self.primary = primary
self.start_date = start_date
self.end_date = end_date
self.backups = backups
def main():
logger.info('%s started' % (module))
config = confighelper.get_config()
logger.info('Obtained config')
logger.debug(config.__dict__)
mongohelper.initialize(config)
logger.info('Mongo initialized')
mongohelper.upsert_schedules(config, get_schedules_from_files('groups.txt','schedules.txt'))
def get_schedules_from_files(groupfile, schedulefile):
groups = {}
with open(groupfile, 'r') as f:
for line in f:
logger.debug('Processing %s' % (line))
group = line.replace("\n","").split(',')
groups[group[0].strip()]=[elem.strip() for elem in group[1:len(group)]]
schedules = []
with open(schedulefile, 'r') as f:
for line in f:
schedule = line.split(',')
primary = schedule[0].strip()
start_date = datetime.strptime(schedule[1].strip(),'%Y/%m/%d')
end_date = datetime.strptime(schedule[2].strip(),'%Y/%m/%d')
backups = get_backups(groups[schedule[3].strip()], primary)
curr_schedule = Schedule(primary,
start_date,
end_date,
backups)
schedules.append(curr_schedule)
return schedules
def get_backups(members, primary):
memlength = len(members)
backups = []
logger.debug('Getting backup of "%s" in %s' % (primary, members.__str__()))
position = members.index(primary)
x = 0
for i in range(memlength):
position = position + 1
backups.append(members[position%memlength])
logger.debug("Backups of %s is %s" % (primary, backups.__str__()))
return backups
if __name__ == '__main__':
logfilename = module+'.log'
logging.basicConfig(filename=logfilename, level=logging.ERROR)
main()