]> scripts.mit.edu Git - wizard.git/blob - wizard/command/restore.py
060ab7860ac862ed21647bb2e0394e95e274cbe6
[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     sh = shell.Shell()
42     try:
43         sh.call("git", "rev-parse", tag)
44     except shell.CallError:
45         raise Exception("Tag %s doesn't exist in repository" % tag)
46     sh.call("git", "reset", "-q", "--hard", tag)
47     d.restore(backup, options)
48
49 def parse_args(argv, baton):
50     usage = """usage: %prog restore [ARGS] ID
51
52 Takes a backup from the backups/ directory and does
53 a full restore back to it.  CURRENT DATA IS DESTROYED,
54 so you may want to do a backup before you do a restore.
55
56 You can specify 'top' as the ID in order to restore from
57 the latest backup."""
58     parser = command.WizardOptionParser(usage)
59     options, args = parser.parse_all(argv)
60     if len(args) > 1:
61         parser.error("too many arguments")
62     return options, args
63