]> scripts.mit.edu Git - wizard.git/blob - wizard/command/__init__.py
Implement 'wizard backup'. Other minor refactorings:
[wizard.git] / wizard / command / __init__.py
1 import logging
2 import traceback
3 import os
4 import sys
5 import optparse
6 import errno
7 import pwd
8
9 import wizard
10 from wizard import util
11
12 logging_setup = False
13
14 def boolish(val):
15     """
16     Parse the contents of an environment variable as a boolean.
17     This recognizes more values as ``False`` than :func:`bool` would.
18
19         >>> boolish("0")
20         False
21         >>> boolish("no")
22         False
23         >>> boolish("1")
24         True
25     """
26     try:
27         return bool(int(val))
28     except (ValueError, TypeError):
29         if val == "No" or val == "no" or val == "false" or val == "False":
30             return False
31         return bool(val)
32
33 def makeLogger(options, numeric_args):
34     global logging_setup
35     if logging_setup: return logging.getLogger()
36     logger = logging.getLogger()
37     logger.setLevel(logging.INFO)
38     stderr = logging.StreamHandler(sys.stderr)
39     stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
40     if not options.quiet: logger.addHandler(stderr)
41     else: logger.addHandler(NullLogHandler()) # prevent default
42     if options.log_file:
43         file = logging.FileHandler(options.log_file)
44         logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S")
45         file.setFormatter(logformatter)
46         logger.addHandler(file)
47     if options.debug:
48         logger.setLevel(logging.DEBUG)
49     else:
50         stderr.setLevel(logging.WARNING)
51         if options.verbose:
52             stderr.setLevel(logging.INFO)
53         if options.log_file:
54             file.setLevel(logging.INFO)
55     def our_excepthook(type, value, tb):
56         logging.error("".join(traceback.format_exception(type,value,tb)))
57         sys.exit(1)
58     sys.excepthook = our_excepthook
59     logging_setup = True
60     return logger
61
62 def makeBaseArgs(options, **grab):
63     """Takes parsed options, and breaks them back into a command
64     line string that we can pass into a subcommand"""
65     args = []
66     grab["debug"]   = "--debug"
67     grab["verbose"] = "--verbose"
68     grab["quiet"]   = "--quiet"
69     #grab["log_db"] = "--log-db"
70     for k,flag in grab.items():
71         value = getattr(options, k)
72         if not value: continue
73         args.append(flag)
74         if type(value) is not bool:
75             args.append(str(value))
76     return args
77
78 def security_check_homedir(location):
79     """
80     Performs a check against a directory to determine if current
81     directory's owner has a home directory that is a parent directory.
82     This protects against malicious mountpoints, and is roughly equivalent
83     to the suexec checks.
84     """
85     uid = util.get_dir_uid(location)
86     real = os.path.realpath(location)
87     try:
88         if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
89             logging.error("Security check failed, owner of deployment and"
90                     "owner of home directory mismatch for %s" % d.location)
91             return False
92     except KeyError:
93         logging.error("Security check failed, could not look up"
94                 "owner of %s (uid %d)" % (location, uid))
95         return False
96     return True
97
98 def calculate_log_name(log_dir, i, dir):
99     """
100     Calculates a log entry given a log directory, numeric identifier, and
101     directory under operation.
102     """
103     return os.path.join(log_dir, "%04d" % i + dir.replace('/', '-') + ".log")
104
105 def open_logs(log_dir, log_names=('warnings', 'errors')):
106     """
107     Opens a number of log files for auxiliary reporting.  You can override what
108     log files to generate using ``log_names``, which corresponds to the tuple
109     of log files you will receive, i.e. the default returns a tuple
110     ``(warnings.log file object, errors.log file object)``.
111
112     .. note::
113
114         The log directory is chmod'ed 777 after creation, to enable
115         de-priviledged processes to create files.
116     """
117     # must not be on AFS, since subprocesses won't be
118     # able to write to the logfiles do the to the AFS patch.
119     try:
120         os.mkdir(log_dir)
121     except OSError as e:
122         if e.errno != errno.EEXIST:
123             raise
124         #if create_subdirs:
125         #    log_dir = os.path.join(log_dir, str(int(time.time())))
126         #    os.mkdir(log_dir) # if fails, be fatal
127         #    # XXX: update last symlink
128     os.chmod(log_dir, 0o777)
129     return (open(os.path.join(os.path.join(log_dir, "%s.log" % x)), "a") for x in log_names)
130
131 class NullLogHandler(logging.Handler):
132     """Log handler that doesn't do anything"""
133     def emit(self, record):
134         pass
135
136 class WizardOptionParser(optparse.OptionParser):
137     """Configures some default user-level options"""
138     def __init__(self, *args, **kwargs):
139         kwargs["add_help_option"] = False
140         optparse.OptionParser.__init__(self, *args, **kwargs)
141     def parse_all(self, argv):
142         self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
143         group = optparse.OptionGroup(self, "Common Options")
144         group.add_option("-v", "--verbose", dest="verbose", action="store_true",
145                 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output.  Envvar is WIZARD_VERBOSE")
146         group.add_option("--debug", dest="debug", action="store_true",
147                 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output.  Envvar is WIZARD_DEBUG")
148         group.add_option("-q", "--quiet", dest="quiet", action="store_true",
149                 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
150         group.add_option("--log-file", dest="log_file", metavar="FILE",
151                 default=None, help="Logs verbose output to file")
152         self.add_option_group(group)
153         options, numeric_args = self.parse_args(argv)
154         makeLogger(options, numeric_args)
155         return options, numeric_args
156
157 class OptionBaton(object):
158     """Command classes may define options that they sub-commands may
159     use.  Since wizard --global-command subcommand is not a supported
160     mode of operation, these options have to be passed down the command
161     chain until a option parser is ready to take it; this baton is
162     what is passed down."""
163     def __init__(self):
164         self.store = {}
165     def add(self, *args, **kwargs):
166         key = kwargs["dest"] # require this to be set
167         self.store[key] = optparse.make_option(*args, **kwargs)
168     def push(self, option_parser, *args):
169         """Hands off parameters to option parser"""
170         for key in args:
171             option_parser.add_option(self.store[key])
172
173 class Error(wizard.Error):
174     """Base error class for all command errors"""
175     pass