]> scripts.mit.edu Git - wizard.git/blob - wizard/command/restore.py
Suppress ^M characters from Git progress bars.
[wizard.git] / wizard / command / restore.py
1 import logging
2 import os
3 import optparse
4 import sys
5 import distutils.version
6
7 from wizard import command, deploy, git, shell, util
8
9 def main(argv, baton):
10     options, args = parse_args(argv, baton)
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 os.listdir(os.path.join(backups, d)):
22                 print d
23             else:
24                 count += 1
25         if count:
26             logging.warning("Pruned %d empty backups" % count)
27         return
28     backup = args[0]
29     if backup == "top":
30         try:
31             backup = sorted(os.listdir(backups))[-1]
32         except IndexError:
33             raise Exception("No restore points available")
34     bits = backup.split('-')
35     date = '-'.join(bits[-3:])
36     version = '-'.join(bits[0:-3])
37     shell.drop_priviledges(".", options.log_file)
38     d = deploy.Deployment(".")
39     d.verify()
40     d.verifyConfigured()
41     tag = "%s-%s" % (d.application.name, version)
42     sh = shell.Shell()
43     try:
44         sh.call("git", "rev-parse", tag)
45     except shell.CallError:
46         raise Exception("Tag %s doesn't exist in repository" % tag)
47     sh.call("git", "reset", "-q", "--hard", tag)
48     d.restore(backup, options)
49
50 def parse_args(argv, baton):
51     usage = """usage: %prog restore [ARGS] ID
52
53 Takes a backup from the backups/ directory and does
54 a full restore back to it.  CURRENT DATA IS DESTROYED,
55 so you may want to do a backup before you do a restore.
56
57 You can specify 'top' as the ID in order to restore from
58 the latest backup."""
59     parser = command.WizardOptionParser(usage)
60     options, args = parser.parse_all(argv)
61     if len(args) > 1:
62         parser.error("too many arguments")
63     return options, args
64