]> scripts.mit.edu Git - wizard.git/blob - lib/wizard/command/migrate.py
Remove misleading docs.
[wizard.git] / lib / wizard / command / migrate.py
1 import optparse
2 import sys
3 import os
4 import shutil
5
6 import wizard.deploy as wd
7 import wizard.shell as sh
8
9 def main(argv, global_options):
10     usage = """usage: %prog migrate [ARGS] DIR
11
12 Migrates a directory to our Git-based autoinstall format.
13 Performs basic sanity checking and intelligently determines
14 what repository and tag to use."""
15     parser = optparse.OptionParser(usage)
16     parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
17             default=False, help="Print all commands and outputs")
18     parser.add_option("--dry-run", dest="dry_run", action="store_true",
19             default=False, help="Prints would would be run without changing anything")
20     options, args = parser.parse_args(argv)
21     if len(args) > 1:
22         parser.error("too many arguments")
23     elif not args:
24         parser.error("must specify directory")
25     dir = args[0]
26     print "Changing working directory to autoinstall directory"
27     try:
28         os.chdir(dir)
29     except OSError as e:
30         if e.errno == 13:
31             print
32             print "ERROR: You don't have permissions to access this directory."
33             print "Do you have tickets for AFS with your root instance, and"
34             print "is your root instance on scripts-security-upd?"
35             print
36             print "You can check by running the commands 'klist' and"
37             print "'blanche scripts-security-upd'.  We recommend getting"
38             print "root tickets using Nelson Elhage's krbroot script"
39             print "at /mit/nelhage/Public/krbroot (for which you run"
40             print "'krbroot shell' and then 'aklog')."
41             raise SystemExit(-1)
42         elif e.errno == 2:
43             print
44             print "ERROR: No such directory... check your typing"
45             raise SystemExit(-1)
46         else: raise e
47     try:
48         deploy = wd.Deployment.fromDir(".")
49         version = deploy.getAppVersion()
50     except IOError as e:
51         if e.errno == 2:
52             print
53             print "ERROR: Could not find .scripts-version file.  Are you sure"
54             print "this is an autoinstalled application?"
55             raise SystemExit(-1)
56         else: raise e
57     # calculate the repository we'll be pulling out of
58     application = version.application
59     app = application.name
60     repo = os.path.join("/afs/athena.mit.edu/contrib/scripts/wizard/srv", app + ".git")
61     if not os.path.isdir(repo):
62         print
63         print "ERROR: Could not find repository for this application. Have"
64         print "you converted the repository over? Is the name %s" % app
65         print "the same as the the name of the foo.git folder?"
66         raise SystemExit(-1)
67     # begin the command line process
68     shell = sh.Shell(options.verbose, options.dry_run)
69     # check if the version we're trying to convert exists. We assume
70     # a convention here, namely, v1.2.3-scripts is what we want. If
71     # you broke the convention... shame on you.
72     try:
73         tag = "v%s-scripts" % version.version
74         shell.call("git", "--git-dir", repo, "rev-parse", tag)
75     except sh.CalledProcessError:
76         print
77         print "ERROR: Could not find tag v%s-scripts for" % version.version
78         print "this application's version.  Double check and make sure"
79         print "the repository was prepared with all necessary tags!"
80         raise SystemExit(-1)
81     did_git_init = False
82     did_git_checkout_scripts = False
83     try:
84         # create repository
85         shell.call("git", "init")
86         did_git_init = True
87         # configure our remote
88         shell.call("git", "remote", "add", "origin", repo)
89         # configure what would normally be set up on a 'git clone' for consistency
90         shell.call("git", "config", "branch.master.remote", "origin")
91         shell.call("git", "config", "branch.master.merge", "refs/heads/master")
92         # perform the initial fetch
93         shell.call("git", "fetch", "origin")
94         # soft reset to our tag
95         shell.call("git", "reset", tag)
96         # checkout the .scripts directory
97         shell.call("git", "checkout", ".scripts")
98         did_git_checkout_scripts = True
99         # XXX: setup .scripts/version???
100         # for verbose purposes, give us a git status and git diff
101         shell.call("git", "status")
102         shell.call("git", "diff")
103     except:
104         print
105         print "ERROR: Exception detected! Rolling back..."
106         if did_git_init:
107             print "Deleting .git directory"
108             shell.call("rm", "-Rf", ".git")
109         if did_git_checkout_scripts:
110             print "Deleting .scripts directory"
111             shell.call("rm", "-Rf", ".scripts")
112         raise