]> scripts.mit.edu Git - wizard.git/blob - wizard/command/restore.py
Rewrite parametrize to use new parametrizeWithVars
[wizard.git] / wizard / command / restore.py
1 import logging
2 import os
3 import sys
4
5 from wizard import command, deploy, shell
6
7 def main(argv, baton):
8     options, args = parse_args(argv, baton)
9     d = deploy.ProductionCopy(".")
10     d.verify()
11     backups = ".scripts/backups"
12     if not args:
13         if not os.path.exists(backups):
14             print "No restore points available"
15             return
16         sys.stderr.write("Available backups:\n")
17         count = 0
18         for d in reversed(sorted(os.listdir(backups))):
19             if ".bak" in d:
20                 continue
21             if d.startswith("."):
22                 continue
23             if os.listdir(os.path.join(backups, d)):
24                 print d
25             else:
26                 count += 1
27         if count:
28             logging.warning("Pruned %d empty backups" % count)
29         return
30     backup = args[0]
31     if backup == "top":
32         try:
33             backup = sorted(os.listdir(backups))[-1]
34             logging.warning("Using backup %s" % backup)
35         except IndexError:
36             raise Exception("No restore points available")
37     bits = backup.split('-')
38     date = '-'.join(bits[-3:])
39     version = '-'.join(bits[0:-3])
40     shell.drop_priviledges(".", options.log_file)
41     d = deploy.ProductionCopy(".")
42     d.verify()
43     d.verifyConfigured()
44     tag = "%s-%s" % (d.application.name, version)
45     try:
46         shell.call("git", "rev-parse", tag)
47     except shell.CallError:
48         raise Exception("Tag %s doesn't exist in repository" % tag)
49     shell.call("git", "reset", "-q", "--hard", tag)
50     d.restore(backup, options)
51
52 def parse_args(argv, baton):
53     usage = """usage: %prog restore [ARGS] ID
54
55 Takes a backup from the backups/ directory and does
56 a full restore back to it.  CURRENT DATA IS DESTROYED,
57 so you may want to do a backup before you do a restore.
58
59 You can specify 'top' as the ID in order to restore from
60 the latest backup."""
61     parser = command.WizardOptionParser(usage)
62     options, args = parser.parse_all(argv)
63     if len(args) > 1:
64         parser.error("too many arguments")
65     return options, args
66