import logging import traceback import os import sys import optparse import errno import wizard logging_setup = False def boolish(val): """ Parse the contents of an environment variable as a boolean. This recognizes more values as ``False`` than :func:`bool` would. >>> boolish("0") False >>> boolish("no") False >>> boolish("1") True """ try: return bool(int(val)) except (ValueError, TypeError): if val == "No" or val == "no" or val == "false" or val == "False": return False return bool(val) def makeLogger(options, numeric_args): global logging_setup if logging_setup: return logging.getLogger() logger = logging.getLogger() logger.setLevel(logging.INFO) stderr = logging.StreamHandler(sys.stderr) stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) if not options.quiet: logger.addHandler(stderr) else: logger.addHandler(NullLogHandler()) # prevent default if options.log_file: file = logging.FileHandler(options.log_file) logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S") file.setFormatter(logformatter) logger.addHandler(file) if options.debug: logger.setLevel(logging.DEBUG) else: stderr.setLevel(logging.WARNING) if options.verbose or hasattr(options, "dry_run") and options.dry_run: stderr.setLevel(logging.INFO) if options.log_file: file.setLevel(logging.INFO) def our_excepthook(type, value, tb): logging.error("".join(traceback.format_exception(type,value,tb))) sys.exit(1) sys.excepthook = our_excepthook logging_setup = True return logger def makeBaseArgs(options, **grab): """Takes parsed options, and breaks them back into a command line string that we can pass into a subcommand""" args = [] grab["debug"] = "--debug" grab["verbose"] = "--verbose" grab["quiet"] = "--quiet" #grab["log_db"] = "--log-db" for k,flag in grab.items(): value = getattr(options, k) if not value: continue args.append(flag) if type(value) is not bool: args.append(str(value)) return args def security_check_homedir(location): """ Performs a check against a directory to determine if current directory's owner has a home directory that is a parent directory. This protects against malicious mountpoints, and is roughly equivalent to the suexec checks. """ uid = util.get_dir_uid(location) real = os.path.realpath(location) try: if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"): logging.error("Security check failed, owner of deployment and" "owner of home directory mismatch for %s" % d.location) return False except KeyError: logging.error("Security check failed, could not look up" "owner of %s (uid %d)" % (location, uid)) return False return True def calculate_log_name(log_dir, i, dir): """ Calculates a log entry given a log directory, numeric identifier, and directory under operation. """ return os.path.join(log_dir, "%04d" % i + dir.replace('/', '-') + ".log") def open_logs(log_dir, log_names=('warnings', 'errors')): """ Opens a number of log files for auxiliary reporting. You can override what log files to generate using ``log_names``, which corresponds to the tuple of log files you will receive, i.e. the default returns a tuple ``(warnings.log file object, errors.log file object)``. .. note:: The log directory is chmod'ed 777 after creation, to enable de-priviledged processes to create files. """ # must not be on AFS, since subprocesses won't be # able to write to the logfiles do the to the AFS patch. try: os.mkdir(log_dir) except OSError as e: if e.errno != errno.EEXIST: raise #if create_subdirs: # log_dir = os.path.join(log_dir, str(int(time.time()))) # os.mkdir(log_dir) # if fails, be fatal # # XXX: update last symlink os.chmod(log_dir, 0o777) return (open(os.path.join(os.path.join(log_dir, "%s.log" % x)), "a") for x in log_names) class NullLogHandler(logging.Handler): """Log handler that doesn't do anything""" def emit(self, record): pass class WizardOptionParser(optparse.OptionParser): """Configures some default user-level options""" def __init__(self, *args, **kwargs): kwargs["add_help_option"] = False optparse.OptionParser.__init__(self, *args, **kwargs) def parse_all(self, argv): self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP) group = optparse.OptionGroup(self, "Common Options") group.add_option("-v", "--verbose", dest="verbose", action="store_true", default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output. Environment variable is WIZARD_VERBOSE") group.add_option("--debug", dest="debug", action="store_true", default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output. Environment variable is WIZARD_DEBUG") group.add_option("-q", "--quiet", dest="quiet", action="store_true", default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Environment variable is WIZARD_QUIET") group.add_option("--log-file", dest="log_file", metavar="FILE", default=None, help="Logs verbose output to file") self.add_option_group(group) options, numeric_args = self.parse_args(argv) makeLogger(options, numeric_args) return options, numeric_args class OptionBaton(object): """Command classes may define options that they sub-commands may use. Since wizard --global-command subcommand is not a supported mode of operation, these options have to be passed down the command chain until a option parser is ready to take it; this baton is what is passed down.""" def __init__(self): self.store = {} def add(self, *args, **kwargs): key = kwargs["dest"] # require this to be set self.store[key] = optparse.make_option(*args, **kwargs) def push(self, option_parser, *args): """Hands off parameters to option parser""" for key in args: option_parser.add_option(self.store[key]) class Error(wizard.Error): """Base error class for all command errors""" pass