]> scripts.mit.edu Git - wizard.git/blob - bin/wizard
Add 'wizard quota' command.
[wizard.git] / bin / wizard
1 #!/usr/bin/env python
2
3 import os
4 import optparse
5 import sys
6 import logging
7 import traceback
8
9 # import some non-standard modules to make it fail out early
10 import decorator
11
12 sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13
14 import wizard
15 from wizard import command, prompt
16
17 def main():
18     usage = """usage: %prog COMMAND [ARGS]
19
20 Wizard is a Git-based autoinstall management system for scripts.
21
22 User commands:
23     backup          Backup data not on filesystem (database, etc)
24     install         Installs an application
25     migrate         Migrate autoinstalls from old format to Git-based format
26     remove          Removes an autoinstall, databases and other files
27     restore         Restores files and database to previous version
28     upgrade         Upgrades an autoinstall to the latest version
29
30 Administrative commands:
31     blacklist       Marks an autoinstall to not try upgrades
32     errors          Lists all broken autoinstall metadata
33     list            Lists autoinstalls, with optional filtering
34     mass-migrate    Performs mass migration of autoinstalls of an application
35     mass-upgrade    Performs mass upgrade of autoinstalls of an application
36     research        Print statistics about a possible upgrade
37     summary         Generate statistics (see help for subcommands)
38
39 Utility commands:
40     prepare-pristine    Downloads and extracts pristine upstream files
41     prepare-new     Prepares a new repository
42     prepare-config  Prepares configuration files for versioning
43     quota           Prints the usage and available quota of a directory
44
45 See '%prog help COMMAND' for more information on a specific command."""
46
47     parser = optparse.OptionParser(usage)
48     parser.disable_interspersed_args()
49     _, args = parser.parse_args() # no global options
50     rest_argv = args[1:]
51     baton = command.OptionBaton()
52     baton.add("--versions-path", dest="versions_path", metavar="PATH",
53         default=getenvpath("WIZARD_VERSIONS_PATH") or "/afs/athena.mit.edu/contrib/scripts/sec-tools/store/versions",
54         help="Location of parallel-find output directory, or a file containing a newline separated list of 'all autoinstalls' (for development work).  Environment variable is WIZARD_VERSIONS_PATH.")
55     baton.add("--srv-path", dest="srv_path", metavar="PATH",
56         default=getenvpath("WIZARD_SRV_PATH") or "/afs/athena.mit.edu/contrib/scripts/git/autoinstalls",
57         help="Location of autoinstall Git repositories, such that $REPO_PATH/$APP.git is a repository (for development work).  Environment variable is WIZARD_SRV_PATH.")
58     baton.add("--dry-run", dest="dry_run", action="store_true",
59             default=False, help="Performs the operation without actually modifying any files.  Use in combination with --verbose to see commands that will be run.")
60     # common variables for mass commands
61     baton.add("--seen", dest="seen",
62             default=None, help="File to read/write paths of successfully modified installs;"
63             "these will be skipped on re-runs.  If --log-dir is specified, this is automatically enabled.")
64     baton.add("--no-parallelize", dest="no_parallelize", action="store_true",
65             default=False, help="Turn off parallelization")
66     baton.add("--max-processes", dest="max_processes", type="int", metavar="N",
67             default=10, help="Maximum subprocesses to run concurrently")
68     baton.add("--limit", dest="limit", type="int",
69             default=None, help="Limit the number of autoinstalls to look at.")
70     baton.add("--user", "-u", dest="user",
71             default=None, help="Only mass migrate a certain user's installs.  No effect if versions_path is a file.")
72     try:
73         command_name = args[0]
74     except IndexError:
75         parser.print_help()
76         sys.exit(1)
77     baton.add("--log-dir", dest="log_dir",
78         default=getenvpath("WIZARD_LOG_DIR") or "/tmp/wizard-%s" % command_name,
79         help="Log files for Wizard children processes are placed here.")
80     if command_name == "help":
81         try:
82             help_module = get_command(rest_argv[0])
83         except ImportError:
84             parser.error("invalid action")
85         except IndexError:
86             parser.print_help()
87             sys.exit(1)
88         help_module.main(['--help'], baton)
89     # Dispatch commands
90     command_module = get_command(command_name)
91     try:
92         command_module.main(rest_argv, baton)
93     except prompt.UserCancel as e:
94         print str(e)
95         sys.exit(1)
96     except Exception as e:
97         # log the exception
98         msg = traceback.format_exc()
99         if command.logging_setup:
100             outfun = logging.error
101         else:
102             outfun = sys.stderr.write
103         if isinstance(e, wizard.Error):
104             if e.quiet and not command.debug:
105                 msg = str(e)
106                 if command.logging_setup:
107                     msg = msg.replace("ERROR: ", "")
108             outfun(msg)
109             sys.exit(e.exitcode)
110         else:
111             outfun(msg)
112             sys.exit(1)
113
114 def get_command(name):
115     name = name.replace("-", "_")
116     __import__("wizard.command." + name)
117     return getattr(wizard.command, name)
118
119 def getenvpath(name):
120     val = os.getenv(name)
121     if val:
122         val = os.path.abspath(val)
123     return val
124
125 if __name__ == "__main__":
126     main()
127