#!/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, sys if __name__ == '__main__': opts = { 'agent-port': (('-p', '', 'agent TCP port'), False, ('communication', 'agent-port'), '6789', False), 'host': (('-H', '', 'agent host'), False, False, False, False), 'setup': (('-C', '', "config file path"), False, False, '/etc/bcfg2.conf', False), 'key': (('-k', '', 'ssl key path'), False, ('communication', 'key'), False, False), 'debug': (('-d', '', '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) port = setup['agent-port'] status = 0 if setup['host'] == '-': urls = ["https://%s:%s" % (host, port) for host in \ [line.strip() for line in sys.stdin.readlines()]] else: host = setup['host'] urls = ["https://%s:%s" % (host, port)] for url in urls: server = ServerProxy(url, transport) try: result = server.run() except socket.error, serr: if serr.args[0] == 111: logger.error("Failed to connect to %s" % url) elif serr.args[0] == 32: logger.error("Connection to %s dropped; authentication failure?" % url) elif serr.args[0] == -5: logger.error("Lookup failure for %s" % url) else: logger.error("Got unexpected socket error %s for %s" % \ (serr.args[0], url), exc_info=1) status = 1 raise SystemExit(status)