-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprep-backup.py
More file actions
executable file
·74 lines (60 loc) · 2.56 KB
/
prep-backup.py
File metadata and controls
executable file
·74 lines (60 loc) · 2.56 KB
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
66
67
68
69
70
71
72
73
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import with_statement
import sys, random, optparse, logging, re,os
from ConfigParser import ConfigParser
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
from users import *
from questserver import QuestServer
def parse_args():
parser = optparse.OptionParser(description = "prepares new backup directory")
parser.set_defaults(bootMode = None)
parser.add_option("--boot", dest = "bootMode", type = "choice", choices = ["clean", "work"], metavar = "MODE",
help = "set preparation mode (clean: create new backup dir; work: change existing backup dir)")
parser.add_option("--users", dest = "usersFile", metavar = "FILE", help = "set file with user accounts")
opt, args = parser.parse_args()
if opt.bootMode is None or opt.usersFile is None:
parser.error("necessary options are skipped")
return opt, args
auth_voc = "abcdefghijklmnopqrstuvwxyz_0123456789"
def CreateAuthString():
return "".join(random.choice(auth_voc) for _ in xrange(10))
def GetUsersList(usersFile):
"""\
File format:
team USERNAME AUTHSTRING
org USERNAME AUTHSTRING"""
userList = []
with open(usersFile) as reader:
for line in reader:
if line.strip().startswith('#'):
continue
if not re.match("^[\x01-\x7F]+$", line):
continue
sRes = re.match("(\w+)\s+(\S+)\s+([<\w>]+)\s*$", line)
if sRes and sRes.group(1) in ("org", "team"):
userrole = sRes.group(1)
username = sRes.group(2)
authstring = sRes.group(3)
if authstring == '<generate>':
authstring = CreateAuthString()
userList.append((username, userrole, authstring))
return userList
def CreateBackup(userList, restoreFlag):
configurator = ConfigParser()
assert "questserver.cfg" in configurator.read("questserver.cfg")
srv = QuestServer(configurator, restoreFlag)
for username, userrole, authstring in userList:
if srv.CreateUser(username, userrole, authstring):
print "user: %s\trole: %s\tauth: %s\tcreated." % (username, userrole, authstring)
srv.Backup()
def main(opt, args):
logging.getLogger().setLevel(logging.DEBUG)
try:
userList = GetUsersList(opt.usersFile)
restoreFlag = {"work": True, "clean": False}[opt.bootMode]
CreateBackup(userList, restoreFlag)
except:
logging.exception("can't create backup directory")
if __name__ == "__main__":
main(*parse_args())