]> scripts.mit.edu Git - wizard.git/blob - wizard/command/__init__.py
c03cfdac723577d0ecb731eddc3899a16c365b7f
[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 import shutil
9
10 import wizard
11 from wizard import util
12
13 logging_setup = False
14
15 def boolish(val):
16     """
17     Parse the contents of an environment variable as a boolean.
18     This recognizes more values as ``False`` than :func:`bool` would.
19
20         >>> boolish("0")
21         False
22         >>> boolish("no")
23         False
24         >>> boolish("1")
25         True
26     """
27     try:
28         return bool(int(val))
29     except (ValueError, TypeError):
30         if val == "No" or val == "no" or val == "false" or val == "False":
31             return False
32         return bool(val)
33
34 def makeLogger(options, numeric_args):
35     global logging_setup
36     if logging_setup: return logging.getLogger()
37     logger = logging.getLogger()
38     logger.handlers = [] # under certain cases, a spurious stream handler is set. We don't know why
39     logger.setLevel(logging.INFO)
40     stderr = logging.StreamHandler(sys.stderr)
41     stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
42     if not options.quiet: logger.addHandler(stderr)
43     else: logger.addHandler(NullLogHandler()) # prevent default
44     if options.log_file:
45         file = logging.FileHandler(options.log_file)
46         logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M")
47         file.setFormatter(logformatter)
48         logger.addHandler(file)
49     if options.debug:
50         logger.setLevel(logging.DEBUG)
51     else:
52         stderr.setLevel(logging.WARNING)
53         if options.verbose:
54             stderr.setLevel(logging.INFO)
55         if options.log_file:
56             file.setLevel(logging.INFO)
57     def our_excepthook(type, value, tb):
58         logging.error("".join(traceback.format_exception(type,value,tb)))
59         sys.exit(1)
60     sys.excepthook = our_excepthook
61     logging_setup = True
62     return logger
63
64 def makeBaseArgs(options, **grab):
65     """Takes parsed options, and breaks them back into a command
66     line string that we can pass into a subcommand"""
67     args = []
68     grab["debug"]   = "--debug"
69     grab["verbose"] = "--verbose"
70     grab["quiet"]   = "--quiet"
71     #grab["log_db"] = "--log-db"
72     for k,flag in grab.items():
73         value = getattr(options, k)
74         if not value: continue
75         args.append(flag)
76         if type(value) is not bool:
77             args.append(str(value))
78     return args
79
80 def security_check_homedir(location):
81     """
82     Performs a check against a directory to determine if current
83     directory's owner has a home directory that is a parent directory.
84     This protects against malicious mountpoints, and is roughly equivalent
85     to the suexec checks.
86     """
87     try:
88         uid = util.get_dir_uid(location)
89         real = os.path.realpath(location)
90         if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
91             logging.error("Security check failed, owner of deployment and "
92                     "owner of home directory mismatch for %s" % location)
93             return False
94     except KeyError:
95         logging.error("Security check failed, could not look up "
96                 "owner of %s (uid %d)" % (location, uid))
97         return False
98     except OSError as e:
99         logging.error("OSError: %s" % str(e))
100         return False
101     return True
102
103 def calculate_log_name(log_dir, i):
104     """
105     Calculates a log entry given a numeric identifier, and
106     directory under operation.
107     """
108     return os.path.join(log_dir, "%04d.log" % i)
109
110 def create_logdir(log_dir):
111     """
112     Creates a log directory and chmods it 777 to enable de-priviledged
113     processes to create files.
114     """
115     try:
116         os.mkdir(log_dir)
117     except OSError as e:
118         if e.errno != errno.EEXIST:
119             raise
120         #if create_subdirs:
121         #    log_dir = os.path.join(log_dir, str(int(time.time())))
122         #    os.mkdir(log_dir) # if fails, be fatal
123         #    # XXX: update last symlink
124     os.chmod(log_dir, 0o777)
125
126 class Report(object):
127     #: Set of indices that should be skipped
128     skip = None
129     def __init__(self, names, fobjs, skip):
130         self.skip = skip
131         for name, fobj in zip(names, fobjs):
132             setattr(self, name, fobj)
133
134 def report_files(log_dir, names):
135     return [os.path.join(os.path.join(log_dir, "%s.txt" % x)) for x in names]
136
137 def read_reports(log_dir, names):
138     """
139     Reads a number of reports files.  The return value is a :class:`Report`
140     object with attributes that are open file objects that correspond to ``names``.
141     """
142     return Report(names, [open(f, "r") for f in report_files(log_dir, names)], set())
143
144 def open_reports(log_dir, names=('warnings', 'errors'), redo=False, append_names=()):
145     """
146     Returns a :class:`Report` object configured appropriately for the
147     parameters passed.  This object has attributes names + append_names which
148     contain file objects opened as "w".  ``names`` report files are cleared unconditionally
149     when they are opened (i.e. are not preserved from run to run.)  ``append_names``
150     report files are not cleared unless ``redo`` is True, and persist over
151     runs: assuming the convention that [0001] is the index of the deployment,
152     the ``skip`` attribute on the returned report object contains indexes that
153     should be skipped.
154     """
155     skip = set()
156     if not redo:
157         rr = read_reports(log_dir, append_names)
158         def build_set(skip, fobj):
159             skip |= set(int(l[1:5]) for l in fobj.read().splitlines())
160             fobj.close()
161         for name in append_names:
162             build_set(skip, getattr(rr, name))
163     else:
164         names += append_names
165         append_names = ()
166     files = report_files(log_dir, names)
167     append_files = report_files(log_dir, append_names)
168     # backup old reports
169     old_reports = os.path.join(log_dir, "old-reports")
170     rundir = os.path.join(old_reports, "run")
171     if not os.path.exists(old_reports):
172         os.mkdir(old_reports)
173     else:
174         util.safe_unlink(rundir)
175     for f in files:
176         if os.path.exists(f):
177             os.rename(f, rundir)
178     for f in append_files:
179         if os.path.exists(f):
180             shutil.copy(f, rundir)
181     return Report(names + append_names, [open(f, "w") for f in files] + [open(f, "a") for f in append_files], skip)
182
183 class NullLogHandler(logging.Handler):
184     """Log handler that doesn't do anything"""
185     def emit(self, record):
186         pass
187
188 class WizardOptionParser(optparse.OptionParser):
189     """Configures some default user-level options"""
190     def __init__(self, *args, **kwargs):
191         kwargs["add_help_option"] = False
192         optparse.OptionParser.__init__(self, *args, **kwargs)
193     def parse_all(self, argv):
194         self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
195         group = optparse.OptionGroup(self, "Common Options")
196         group.add_option("-v", "--verbose", dest="verbose", action="store_true",
197                 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output.  Envvar is WIZARD_VERBOSE")
198         group.add_option("--debug", dest="debug", action="store_true",
199                 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output.  Envvar is WIZARD_DEBUG")
200         group.add_option("-q", "--quiet", dest="quiet", action="store_true",
201                 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
202         group.add_option("--log-file", dest="log_file", metavar="FILE",
203                 default=None, help="Logs verbose output to file")
204         self.add_option_group(group)
205         options, numeric_args = self.parse_args(argv)
206         makeLogger(options, numeric_args)
207         # we're going to process the global --log-dir/--seen dependency here
208         if hasattr(options, "seen") and hasattr(options, "log_dir"):
209             if not options.seen and options.log_dir:
210                 options.seen = os.path.join(options.log_dir, "seen.txt")
211         return options, numeric_args
212
213 class OptionBaton(object):
214     """Command classes may define options that they sub-commands may
215     use.  Since wizard --global-command subcommand is not a supported
216     mode of operation, these options have to be passed down the command
217     chain until a option parser is ready to take it; this baton is
218     what is passed down."""
219     def __init__(self):
220         self.store = {}
221     def add(self, *args, **kwargs):
222         key = kwargs["dest"] # require this to be set
223         self.store[key] = optparse.make_option(*args, **kwargs)
224     def push(self, option_parser, *args):
225         """Hands off parameters to option parser"""
226         for key in args:
227             option_parser.add_option(self.store[key])
228
229 class Error(wizard.Error):
230     """Base error class for all command errors"""
231     pass