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