~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import signal
import os
import sys

from logging import (getLevelName, getLogger,
                     FileHandler, StreamHandler, Formatter, info)

from optparse import OptionParser
from ConfigParser import ConfigParser

import dbus.glib # Side-effects rule!

from twisted.application.service import Application, Service
from twisted.application.app import startApplication

from landscape import VERSION
from landscape.lib.persist import Persist
from landscape.lib.dbus_util import get_bus
from landscape.lib import bpickle_dbus
from landscape.log import rotate_logs
from landscape.reactor import TwistedReactor

from landscape.upgraders import UPGRADE_MANAGERS


def init_logging(configuration, program_name):
    """Given a basic configuration, set up logging."""
    handlers = []
    if not os.path.exists(configuration.log_dir):
        os.makedirs(configuration.log_dir)
    log_filename = os.path.join(configuration.log_dir, program_name+".log")
    handlers.append(FileHandler(log_filename))
    if not configuration.quiet:
        handlers.append(StreamHandler(sys.stdout))
    getLogger().setLevel(getLevelName(configuration.log_level.upper()))
    for handler in handlers:
        getLogger().addHandler(handler)
        format = ("%(asctime)s %(levelname)-8s [%(threadName)-10s] "
                  "%(message)s")
        handler.setFormatter(Formatter(format))


class BaseConfiguration(object):
    """Base class for configuration implementations.

    @var required_options: Optionally, a sequence of key names to require when
        reading or writing a configuration.
    @var unsaved_options: Optionally, a sequence of key names to never write
        to the configuration file.  This is useful when you want to provide
        command-line options that should never end up in a configuration file.
    @var default_config_filenames: A sequence of filenames to check when
        reading or writing a configuration.
    """

    required_options = ()
    unsaved_options = ()
    default_config_filenames = ["/etc/landscape/client.conf"]
    if (os.path.dirname(os.path.abspath(sys.argv[0]))
        == os.path.abspath("scripts")):
        default_config_filenames.insert(0, "landscape-client.conf")
    default_config_filenames = tuple(default_config_filenames)
    config_section = "client"

    def __init__(self):
        self._set_options = {}
        self._command_line_args = []
        self._command_line_options = {}
        self._config_filename = None
        self._config_file_options = {}
        self._parser = self.make_parser()
        self._command_line_defaults = self._parser.defaults.copy()
        # We don't want them mixed with explicitly given options,
        # otherwise we can't define the precedence properly.
        self._parser.defaults.clear()

    def __getattr__(self, name):
        """Find and return the value of the given configuration parameter.

        The following sources will be searched:
          * The attributes that were explicitly set on this object,
          * The parameters specified on the command line,
          * The parameters specified in the configuration file, and
          * The defaults.

        If no values are found and the parameter does exist as a possible
        parameter, C{None} is returned.

        Otherwise C{AttributeError} is raised.
        """
        for options in [self._set_options,
                        self._command_line_options,
                        self._config_file_options,
                        self._command_line_defaults]:
            if name in options:
                value = options[name]
                break
        else:
            if self._parser.has_option("--" + name.replace("_", "-")):
                value = None
            else:
                raise AttributeError(name)
        if isinstance(value, basestring):
            option = self._parser.get_option("--" + name.replace("_", "-"))
            if option is not None:
                value = option.convert_value(None, value)
        return value

    def get(self, name, default=None):
        try:
            return self.__getattr__(name)
        except AttributeError:
            return default

    def __setattr__(self, name, value):
        """Set a configuration parameter.

        If the name begins with C{_}, it will only be set on this object and
        not stored in the configuration file.
        """
        if name.startswith("_"):
            super(BaseConfiguration, self).__setattr__(name, value)
        else:
            self._set_options[name] = value

    def reload(self):
        self.load(self._command_line_args)

    def load(self, args, accept_unexistent_config=False):
        """
        Load configuration data from command line arguments and a config file.

        @raise: A SystemExit if the arguments are bad.
        """
        self.load_command_line(args)

        # Parse configuration file, if found.
        if self.config:
            if os.path.isfile(self.config):
                self.load_configuration_file(self.config)
            elif not accept_unexistent_config:
                sys.exit("error: file not found: %s" % self.config)
        else:
            for potential_config_file in self.default_config_filenames:
                if os.access(potential_config_file, os.R_OK):
                    self.load_configuration_file(potential_config_file)
                    break

        # Check that all needed options were given.
        for option in self.required_options:
            if not getattr(self, option):
                sys.exit("error: must specify --%s "
                         "or the '%s' directive in the config file."
                         % (option.replace('_','-'), option))

        if self.bus not in ("session", "system"):
            sys.exit("error: bus must be one of 'session' or 'system'")

    def load_command_line(self, args):
        """Load configuration data from the given command line."""
        self._command_line_args = args
        values = self._parser.parse_args(args)[0]
        self._command_line_options = vars(values)

    def load_configuration_file(self, filename):
        """Load configuration data from the given file name.

        If any data has already been set on this configuration object,
        then the old data will take precedence.
        """
        self._config_filename = filename
        config_parser = ConfigParser()
        config_parser.read(filename)
        self._config_file_options = dict(
            config_parser.items(self.config_section))

    def write(self):
        """Write back configuration to the configuration file.

        Values which match the default option in the parser won't be saved.

        Options are considered in the following precedence:

        1. Manually set options (config.option = value)
        2. Options passed in the command line
        3. Previously existent options in the configuration file

        The filename picked for saving configuration options is:

        1. self.config, if defined
        2. The last loaded configuration file, if any
        3. The first filename in self.default_config_filenames
        """
        config_parser = ConfigParser()
        config_parser.add_section("client")
        all_options = self._config_file_options.copy()
        all_options.update(self._command_line_options)
        all_options.update(self._set_options)
        for name, value in all_options.items():
            if (name != "config" and
                name not in self.unsaved_options and
                value != self._command_line_defaults.get(name)):
                config_parser.set("client", name, value)
        filename = (self.config or self._config_filename or
                    self.default_config_filenames[0])
        config_file = open(filename, "w")
        config_parser.write(config_file)
        config_file.close()

    def make_parser(self):
        """
        Return an L{OptionParser} preset with options that all
        landscape-related programs accept.
        """
        parser = OptionParser(version=VERSION)
        parser.add_option("-c", "--config", metavar="FILE",
                          help="Use config from this file (any command line "
                               "options override settings from the file).")
        parser.add_option("--bus", default="system",
                          help="Which DBUS bus to use. One of 'session' "
                               "or 'system'.")
        return parser

    def get_config_filename(self):
        return self._config_filename

    def get_command_line_options(self):
        return self._command_line_options


class Configuration(BaseConfiguration):
    """Configuration data for Landscape client.

    This contains all simple data, some of it calculated.
    """

    @property
    def hashdb_filename(self):
        return os.path.join(self.data_path, "hash.db")

    def make_parser(self):
        """
        Return an L{OptionParser} preset with options that all
        landscape-related programs accept.
        """
        parser = super(Configuration, self).make_parser()
        parser.add_option("-d", "--data-path", metavar="PATH",
                          default="/var/lib/landscape/client/",
                          help="The directory to store data files in.")
        parser.add_option("-q", "--quiet", default=False, action="store_true",
                          help="Do not log to the standard output.")
        parser.add_option("-l", "--log-dir", metavar="FILE",
                          help="The directory to write log files to.",
                          default="/var/log/landscape")
        parser.add_option("--log-level", default="info",
                          help="One of debug, info, warning, error or "
                               "critical.")
        parser.add_option("--ignore-sigint", action="store_true", default=False,
                          help="Ignore interrupt signals. ")

        return parser


def get_versioned_persist(service):
    """
    Load a Persist database for the given service and upgrade or mark
    as current, as necessary.
    """
    persist = Persist(filename=service.persist_filename)
    upgrade_manager = UPGRADE_MANAGERS[service.service_name]
    if os.path.exists(service.persist_filename):
        upgrade_manager.apply(persist)
    else:
        upgrade_manager.initialize(persist)
    persist.save(service.persist_filename)
    return persist


class LandscapeService(Service, object):
    """
    A utility superclass for defining Landscape services.

    This sets up the reactor, bpickle/dbus integration, a Persist object, and
    connects to the bus when started.

    @cvar service_name: The lower-case name of the service. This is used to
        generate the bpickle filename.
    """
    reactor_factory = TwistedReactor
    persist_filename = None

    def __init__(self, config):
        self.config = config
        bpickle_dbus.install()
        self.reactor = self.reactor_factory()
        if self.persist_filename:
            self.persist = get_versioned_persist(self)
        signal.signal(signal.SIGUSR1, lambda signal, frame: rotate_logs())

    def startService(self):
        Service.startService(self)
        self.bus = get_bus(self.config.bus)
        info("%s started on '%s' bus with config %s" % (
                self.service_name.capitalize(), self.config.bus,
                self.config.get_config_filename()))

    def stopService(self):
        Service.stopService(self)
        info("%s stopped on '%s' bus with config %s" % (
                self.service_name.capitalize(), self.config.bus,
                self.config.get_config_filename()))


def assert_unowned_bus_name(bus, bus_name):
    dbus_object = bus.get_object("org.freedesktop.DBus",
                                 "/org/freedesktop/DBus")
    if dbus_object.NameHasOwner(bus_name,
                                dbus_interface="org.freedesktop.DBus"):
        sys.exit("error: DBus name %s is owned. "
                 "Is the process already running?" % bus_name)


def run_landscape_service(configuration_class, service_class, args, bus_name):
    """Run a Landscape service.

    @param configuration_class: A subclass of L{Configuration} for processing
        the C{args} and config file.
    @param service_class: A subclass of L{LandscapeService} to create and start.
    @param args: Command line arguments.
    @param bus_name: A bus name used to verify if the service is already
        running.
    """
    from twisted.internet.glib2reactor import install
    install()
    from twisted.internet import reactor

    # Let's consider adding this:
#     from twisted.python.log import startLoggingWithObserver, PythonLoggingObserver
#     startLoggingWithObserver(PythonLoggingObserver().emit, setStdout=False)

    configuration = configuration_class()
    configuration.load(args)
    init_logging(configuration, service_class.service_name)

    assert_unowned_bus_name(get_bus(configuration.bus), bus_name)

    application = Application("landscape-%s" % (service_class.service_name,))
    service_class(configuration).setServiceParent(application)

    startApplication(application, False)

    if configuration.ignore_sigint:
        signal.signal(signal.SIGINT, signal.SIG_IGN)

    reactor.run()