summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-remote
blob: 4b45965c69671f7e06fc71ac469bd88cf91a5c0e (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
#!/usr/bin/env python
__revision__ = '$Revision$'

from Bcfg2.tlslite.api import parsePEMKey, X509, X509CertChain
from xmlrpclib import ServerProxy
from Bcfg2.tlslite.integration.XMLRPCTransport import XMLRPCTransport
import Bcfg2.Options, Bcfg2.Logging, logging, socket

if __name__ == '__main__':
    opts = {
        'agent-port': (('-p', '<agent tcp port>', 'agent TCP port'),
                       False, ('communication', 'agent-port'),
                       '6789', False),
        'host': (('-H', '<agent host>', 'agent host'),
                 False, False, False, False),
        'setup': (('-C', '<configfile>', "config file path"),
                  False, False, '/etc/bcfg2.conf', False),
        'key': (('-k', '<ssl key>', 'ssl key path'),
                False, ('communication', 'key'), False, False),
        'debug': (('-d', '<debug level>', 'debug level (0-40)'),
                  False, False, 0, False),
        }
    optparser = Bcfg2.Options.OptionParser('bcfg2', opts)
    setup = optparser.parse()
    Bcfg2.Logging.setup_logging('bcfg2-remote',
                                to_syslog=False,
                                level=int(setup['debug']))
    logger = logging.getLogger('bcfg2-remote')
    if not setup['host']:
        logger.error("-H option is required")
        logger.error(optparser.helpmsg)
        raise SystemExit(1)
    s = open(setup['key']).read()
    x509 = X509()
    x509.parse(s)
    certChain = X509CertChain([x509])
    #s = open("/etc/bcfg2.key").read()
    privateKey = parsePEMKey(s, private=True)

    transport = XMLRPCTransport(certChain=certChain,
                                privateKey=privateKey)
    host = setup['host']
    port = setup['agent-port']
    server = ServerProxy("https://%s:%s" % (host, port),  transport)

    try:
        result = server.run()
    except socket.error, serr:
        if serr.args[0] == 111:
            logger.error("Failed to connect to client %s" % host)
        elif serr.args[0] == 32:
            logger.error("Connection to client %s dropped; authentication failure?" % host)
        else:
            logger.error("Got unexpected socket error %d" % serr.args[0])
        raise SystemExit(1)