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