]> scripts.mit.edu Git - wizard.git/blobdiff - bin/wizard
Add 'wizard quota' command.
[wizard.git] / bin / wizard
index ac69d8ece66a42ea16dc1947dd1488755aa47567..b71048d8c76352d5288903a022d42ce6803a713f 100755 (executable)
 #!/usr/bin/env python
 
-"""
-This script does everything autoinstalls!
-
-Specifically, it:
-* Generates basic statistics about autoinstall versions
-* Migrates autoinstalls to the new Git format
-"""
-
 import os
 import optparse
 import sys
+import logging
+import traceback
+
+# import some non-standard modules to make it fail out early
+import decorator
+
+sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
-# Add lib to path
-sys.path.insert(0,os.path.abspath(os.path.join(__file__,'../../lib')))
-import wizard.command
+import wizard
+from wizard import command, prompt
 
 def main():
-    usage = """usage: %prog [-d|--version-dir] COMMAND [ARGS]
+    usage = """usage: %prog COMMAND [ARGS]
 
 Wizard is a Git-based autoinstall management system for scripts.
 
-Its commands are:
-    stat        Generate statistics about autoinstalls
+User commands:
+    backup          Backup data not on filesystem (database, etc)
+    install         Installs an application
+    migrate         Migrate autoinstalls from old format to Git-based format
+    remove          Removes an autoinstall, databases and other files
+    restore         Restores files and database to previous version
+    upgrade         Upgrades an autoinstall to the latest version
 
-See '%prog help COMMAND' for more information on a specific command.
-"""
-# migrate     Migrate autoinstalls from old format to Git-based format
+Administrative commands:
+    blacklist       Marks an autoinstall to not try upgrades
+    errors          Lists all broken autoinstall metadata
+    list            Lists autoinstalls, with optional filtering
+    mass-migrate    Performs mass migration of autoinstalls of an application
+    mass-upgrade    Performs mass upgrade of autoinstalls of an application
+    research        Print statistics about a possible upgrade
+    summary         Generate statistics (see help for subcommands)
+
+Utility commands:
+    prepare-pristine    Downloads and extracts pristine upstream files
+    prepare-new     Prepares a new repository
+    prepare-config  Prepares configuration files for versioning
+    quota           Prints the usage and available quota of a directory
+
+See '%prog help COMMAND' for more information on a specific command."""
 
     parser = optparse.OptionParser(usage)
-    parser.add_option("-d", "--version-dir", dest="version_dir",
-            default="/afs/athena.mit.edu/contrib/scripts/sec-tools/store/versions",
-            help="Location of parallel-find output")
-    # Find the end of the "global" options
-    i = 1
+    parser.disable_interspersed_args()
+    _, args = parser.parse_args() # no global options
+    rest_argv = args[1:]
+    baton = command.OptionBaton()
+    baton.add("--versions-path", dest="versions_path", metavar="PATH",
+        default=getenvpath("WIZARD_VERSIONS_PATH") or "/afs/athena.mit.edu/contrib/scripts/sec-tools/store/versions",
+        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.")
+    baton.add("--srv-path", dest="srv_path", metavar="PATH",
+        default=getenvpath("WIZARD_SRV_PATH") or "/afs/athena.mit.edu/contrib/scripts/git/autoinstalls",
+        help="Location of autoinstall Git repositories, such that $REPO_PATH/$APP.git is a repository (for development work).  Environment variable is WIZARD_SRV_PATH.")
+    baton.add("--dry-run", dest="dry_run", action="store_true",
+            default=False, help="Performs the operation without actually modifying any files.  Use in combination with --verbose to see commands that will be run.")
+    # common variables for mass commands
+    baton.add("--seen", dest="seen",
+            default=None, help="File to read/write paths of successfully modified installs;"
+            "these will be skipped on re-runs.  If --log-dir is specified, this is automatically enabled.")
+    baton.add("--no-parallelize", dest="no_parallelize", action="store_true",
+            default=False, help="Turn off parallelization")
+    baton.add("--max-processes", dest="max_processes", type="int", metavar="N",
+            default=10, help="Maximum subprocesses to run concurrently")
+    baton.add("--limit", dest="limit", type="int",
+            default=None, help="Limit the number of autoinstalls to look at.")
+    baton.add("--user", "-u", dest="user",
+            default=None, help="Only mass migrate a certain user's installs.  No effect if versions_path is a file.")
     try:
-        while not sys.argv[i] or sys.argv[i][0] == '-':
-            if sys.argv[i] == "-h" or sys.argv[i] == "--help":
-                parser.print_usage()
-                raise SystemExit(-1)
-            i += 1
+        command_name = args[0]
     except IndexError:
-        parser.error("no action specified")
-    options, args = parser.parse_args(sys.argv[1:i+1])
-    rest_argv = sys.argv[i+1:]
-    command = args[0] # shouldn't fail
-    if command == "help":
+        parser.print_help()
+        sys.exit(1)
+    baton.add("--log-dir", dest="log_dir",
+        default=getenvpath("WIZARD_LOG_DIR") or "/tmp/wizard-%s" % command_name,
+        help="Log files for Wizard children processes are placed here.")
+    if command_name == "help":
         try:
-            getattr(wizard.command, rest_argv[0])(['-h'], options)
-        except KeyError:
+            help_module = get_command(rest_argv[0])
+        except ImportError:
             parser.error("invalid action")
         except IndexError:
-            parser.print_usage()
-            raise SystemExit(-1)
+            parser.print_help()
+            sys.exit(1)
+        help_module.main(['--help'], baton)
     # Dispatch commands
+    command_module = get_command(command_name)
     try:
-        getattr(wizard.command, command).main(rest_argv, options)
-    except AttributeError:
-        parser.error("invalid action")
+        command_module.main(rest_argv, baton)
+    except prompt.UserCancel as e:
+        print str(e)
+        sys.exit(1)
+    except Exception as e:
+        # log the exception
+        msg = traceback.format_exc()
+        if command.logging_setup:
+            outfun = logging.error
+        else:
+            outfun = sys.stderr.write
+        if isinstance(e, wizard.Error):
+            if e.quiet and not command.debug:
+                msg = str(e)
+                if command.logging_setup:
+                    msg = msg.replace("ERROR: ", "")
+            outfun(msg)
+            sys.exit(e.exitcode)
+        else:
+            outfun(msg)
+            sys.exit(1)
+
+def get_command(name):
+    name = name.replace("-", "_")
+    __import__("wizard.command." + name)
+    return getattr(wizard.command, name)
+
+def getenvpath(name):
+    val = os.getenv(name)
+    if val:
+        val = os.path.abspath(val)
+    return val
 
 if __name__ == "__main__":
     main()