summaryrefslogtreecommitdiffstats
path: root/tools/bcfg2_local.py
blob: 3c90a3ea51be4d73eb3c3f7858b1df98f4dd1909 (plain)
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
#!/usr/bin/env python
""" This tool performs a full Bcfg2 run entirely against a local
repository, i.e., without a server.  It starts up a local instance of
the server core, then uses that to get probes, run them, and so on."""

import sys
import socket
import Bcfg2.Options
from Bcfg2.Client.Client import Client
from Bcfg2.Server.Core import BaseCore


class LocalCore(BaseCore):
    """ Local server core similar to the one started by bcfg2-info """

    def __init__(self, setup):
        saved = (setup['syslog'], setup['logging'])
        setup['syslog'] = False
        setup['logging'] = None
        Bcfg2.Server.Core.BaseCore.__init__(self, setup=setup)
        setup['syslog'], setup['logging'] = saved
        self.load_plugins()
        self.block_for_fam_events(handle_events=True)

    def _daemonize(self):
        return True

    def _run(self):
        return True

    def _block(self):
        pass


class LocalProxy(object):
    """ A local proxy (as opposed to XML-RPC) that proxies from the
    Client object to the LocalCore object, adding a client address
    pair to the argument list of each proxied call """

    def __init__(self, core):
        self.core = core
        self.hostname = socket.gethostname()
        self.ipaddr = socket.gethostbyname(self.hostname)

    def __getattr__(self, attr):
        if hasattr(self.core, attr):
            func = getattr(self.core, attr)
            if func.exposed:
                def inner(*args, **kwargs):
                    args = ((self.ipaddr, self.hostname), ) + args
                    return func(*args, **kwargs)
                return inner
        raise AttributeError(attr)


class LocalClient(Client):
    """ A version of the Client class that uses LocalProxy instead of
    an XML-RPC proxy to make its calls """

    def __init__(self, setup, proxy):
        Client.__init__(self, setup)
        self._proxy = proxy


def main():
    optinfo = Bcfg2.Options.CLIENT_COMMON_OPTIONS
    optinfo.update(Bcfg2.Options.SERVER_COMMON_OPTIONS)
    if 'bundle_quick' in optinfo:
        # CLIENT_BUNDLEQUICK option uses -Q, just like the server repo
        # option.  the server repo is more important for this
        # application.
        optinfo['bundle_quick'] = Bcfg2.Options.Option('bundlequick',
                                                       default=False)
    setup = Bcfg2.Options.OptionParser(optinfo)
    setup.parse(sys.argv[1:])

    core = LocalCore(setup)
    try:
        LocalClient(setup, LocalProxy(core)).run()
    finally:
        core.shutdown()

if __name__ == '__main__':
    sys.exit(main())