Beispiel #1
0
    def __init__(self):
        """
        Construct a new AMQPClientHandler instance based on the configuration
        stored in the environment.

        @type env: Environment
        @param env: L{Environment} object
        """
        env = Environment.getInstance()
        self.log = logging.getLogger(__name__)
        self.log.debug("initializing AMQP client handler")
        self.env = env

        # Enable debugging for qpid if we're in debug mode
        #if self.env.config.get('core.loglevel') == 'DEBUG':
        #    enable('qpid', DEBUG)

        # Load configuration
        self.url = parseURL(self.env.config.get('amqp.url', None))
        self.domain = self.env.config.get('ampq.domain', default="org.gosa")
        self.dns_domain = socket.getfqdn().split('.', 1)[1]

        # Use zeroconf if there's no URL
        if not self.url:
            url = ZeroconfClient.discover(['_amqps._tcp', '_amqp._tcp'], domain=self.domain)[0]

            o = urlparse(url)
            # pylint: disable=E1101
            self.domain = o.path[1::]
            self.log.info("using service '%s'" % url)

            # Configure system
            user = self.env.config.get('amqp.id', default=None)
            if not user:
                user = self.env.uuid

            key = self.env.config.get('amqp.key')
            if key:
                # pylint: disable=E1101
                self.url = parseURL('%s://%s:%s@%s' % (o.scheme, user, key, o.netloc))
            else:
                self.url = parseURL(url)

        # Set params and go for it
        self.reconnect = self.env.config.get('amqp.reconnect', True)
        self.reconnect_interval = self.env.config.get('amqp.reconnect-interval', 3)
        self.reconnect_limit = self.env.config.get('amqp.reconnect-limit', 0)

        # Check if credentials are supplied
        if not self.env.config.get("amqp.key"):
            raise Exception("no key supplied - please join the client")

        # Start connection
        self.start()
Beispiel #2
0
    def get_service(self):

        # Try to load url/key from configuration
        (svc_url, svc_id, svc_key) = self.get_service_from_config()
        if svc_url and svc_key:
            self.svc_id = svc_id if svc_id else self.uuid
            self.url = svc_url
            self.key = svc_key
            return

        # Check for svc information
        with open("/proc/cmdline", "r") as f:
            line = f.readlines()[0]

        # Scan command line for svc_ entries
        for dummy, var, data in re.findall(r"(([a-z0-9_]+)=([^\s]+))",
            line, flags=re.IGNORECASE):

            # Save relevant values
            if var == "svc_url":
                svc_url = data
            if var == "svc_key":
                tmp = self.decrypt(self.uuid.replace("-", ""), b64decode(data))
                svc_id = tmp[0:36]
                svc_key = tmp[36:]
                self._need_config_refresh = True

        # If there's no url, try to find it using zeroconf
        if not svc_url:
            svc_url = ZeroconfClient.discover(['_amqps._tcp', '_amqp._tcp'], domain=self.domain)[0]

        self.svc_id = svc_id
        self.url = svc_url
        self.key = svc_key

        if self._need_config_refresh:
            config = self.env.config.get("core.config")
            parser = ConfigParser.RawConfigParser()
            parser.read(config)

            # Section present?
            try:
                parser.get("amqp", "url")
            except ConfigParser.NoSectionError:
                parser.add_section("amqp")

            # Set url and key
            parser.set("amqp", "url", self.url)
            parser.set("core", "id", self.svc_id)
            parser.set("amqp", "key", self.key)

            # Write back to file
            with open(config, "wb") as f:
                parser.write(f)