5 import logging.handlers
8 import wizard.deploy as wd
9 import wizard.shell as sh
11 class PermissionsError(UserException):
12 def __init__(self, dir):
17 ERROR: You don't have permissions to access this directory.
18 Do you have tickets for AFS with your root instance, and
19 is your root instance on scripts-security-upd?
21 You can check by running the commands 'klist' and
22 'blanche scripts-security-upd'. We recommend getting
23 root tickets using Nelson Elhage's krbroot script
24 at /mit/nelhage/Public/krbroot (for which you run
25 'krbroot shell' and then 'aklog').
28 class NoSuchDirectoryError(UserException):
29 def __init__(self, dir):
34 ERROR: No such directory... check your typing
37 class AlreadyMigratedError(UserException):
38 def __init__(self, dir):
43 ERROR: Directory already contains a .git directory.
44 Did you already migrate it?
47 class NotAutoinstallError(UserException):
48 def __init__(self, dir):
53 ERROR: Could not find .scripts-version file. Are you sure
54 this is an autoinstalled application?
57 class NoRepositoryError(UserException):
58 def __init__(self, app):
63 ERROR: Could not find repository for this application. Have
64 you converted the repository over? Is the name %s
65 the same as the name of the .git folder?
68 class NoTagError(UserException):
69 def __init__(self, version):
70 self.version = version
74 ERROR: Could not find tag v%s-scripts in repository
75 for %s. Double check and make sure
76 the repository was prepared with all necessary tags!
77 """ % (self.version.version, self.version.application.name)
79 def migrate(argv, global_options, logger = None):
80 usage = """usage: %prog migrate [ARGS] DIR
82 Migrates a directory to our Git-based autoinstall format.
83 Performs basic sanity checking and intelligently determines
84 what repository and tag to use."""
85 parser = WizardOptionParser(usage)
86 parser.add_option("--dry-run", dest="dry_run", action="store_true",
87 default=False, help="Prints would would be run without changing anything")
88 options, args, logger = parser.parse_all(argv, logger)
90 parser.error("too many arguments")
92 parser.error("must specify directory")
98 raise PermissionsError(dir)
100 raise NoSuchDirectoryError(dir)
102 if os.path.isdir(".git"):
103 raise AlreadyMigratedError(dir)
105 deploy = wd.Deployment.fromDir(".")
106 version = deploy.getAppVersion()
109 raise NotAutoinstallError(dir)
111 # calculate the repository we'll be pulling out of
112 application = version.application
113 app = application.name
114 repo = os.path.join("/afs/athena.mit.edu/contrib/scripts/wizard/srv", app + ".git")
115 if not os.path.isdir(repo):
116 raise NoRepositoryError(app)
117 # begin the command line process
118 shell = sh.Shell(logger, options.dry_run)
119 # check if the version we're trying to convert exists. We assume
120 # a convention here, namely, v1.2.3-scripts is what we want. If
121 # you broke the convention... shame on you.
123 tag = "v%s-scripts" % version.version
124 shell.call("git", "--git-dir", repo, "rev-parse", tag)
125 except sh.CalledProcessError:
126 raise NoTagError(version)
128 did_git_checkout_scripts = False
131 shell.call("git", "init")
133 # configure our remote
134 shell.call("git", "remote", "add", "origin", repo)
135 # configure what would normally be set up on a 'git clone' for consistency
136 shell.call("git", "config", "branch.master.remote", "origin")
137 shell.call("git", "config", "branch.master.merge", "refs/heads/master")
138 # perform the initial fetch
139 shell.call("git", "fetch", "origin")
140 # soft reset to our tag
141 shell.call("git", "reset", tag)
142 # checkout the .scripts directory
143 shell.call("git", "checkout", ".scripts")
144 did_git_checkout_scripts = True
145 # XXX: setup .scripts/version???
146 # for verbose purposes, give us a git status and git diff
147 raise NotImplementedError
149 shell.call("git", "status")
150 shell.call("git", "diff")
152 logger.error("ERROR: Exception detected! Rolling back...")
154 shell.call("rm", "-Rf", ".git")
155 if did_git_checkout_scripts:
156 shell.call("rm", "-Rf", ".scripts")