Commit b4d8b102 authored by Johan Ström's avatar Johan Ström

clean up python transport loading a bit

Do not load both when only one is going to be used, in case both where available
Also, give a more detailed error if it fails to load.
parent 4f2f9b67
Pipeline #453 passed with stage
in 2 minutes and 50 seconds
......@@ -4,6 +4,7 @@ import agoclient._logging
import argparse
from agoclient.agotransport import AgoTransportConfigError
from . import config
import logging
import os.path
......@@ -143,10 +144,14 @@ class AgoApp:
App specific setup should be done in setup_app method.
"""
self.setup_logging()
self.setup_connection()
if not self.setup_connection():
return False
self.setup_signals()
self.setup_app()
return True
def cleanup(self):
"""Cleanup after shutdown.
......@@ -237,9 +242,12 @@ class AgoApp:
def setup_connection(self):
"""Create an AgoConnection instance, assigned to self.connection"""
self.connection = AgoConnection(self.app_short_name)
if not self.connection.start():
raise StartupError()
try:
self.connection = AgoConnection(self.app_short_name)
except AgoTransportConfigError as e:
return False
return self.connection.start()
def cleanup_connection(self):
"""Shutdown and clean up our AgoConnection instance"""
......@@ -327,7 +335,8 @@ class AgoApp:
try:
try:
self.setup()
if not self.setup():
return 1
except StartupError:
return 1
......
import errno
import logging
import json
import sys
import time
import uuid
from agoclient import agoproto
from agoclient.agotransport import AgoTransportConfigError
from agoclient.config import get_config_option, get_config_path
__all__ = ["AgoConnection"]
try:
from agoclient.agotransport_qpid import AgoQpidTransport
HAS_QPID = True
except ImportError:
HAS_QPID = False
try:
from agoclient.agotransport_mqtt import AgoMqttTransport
HAS_MQTT = True
except ImportError:
HAS_MQTT = False
class AgoConnection:
"""This is class will handle the connection to ago control."""
......@@ -30,18 +20,28 @@ class AgoConnection:
self.instance = instance
self.uuidmap_file = get_config_path('uuidmap/' + self.instance + '.json')
self.log = logging.getLogger('connection')
self.transport = None
messaging = str(get_config_option("system", "messaging", "qpid"))
broker = str(get_config_option("system", "broker", "localhost"))
username = str(get_config_option("system", "username", "agocontrol"))
password = str(get_config_option("system", "password", "letmein"))
self.log.debug("Connecting to broker %s", broker)
if messaging == 'qpid':
if not HAS_QPID: raise RuntimeError('Qpid support is not available')
try:
from agoclient.agotransport_qpid import AgoQpidTransport
except ImportError:
self.log.exception("Cannot use qpid messaging, failed to load transport")
raise AgoTransportConfigError()
self.transport = AgoQpidTransport(broker, username, password)
elif messaging == 'mqtt':
if not HAS_MQTT: raise RuntimeError('MQTT support is not available')
try:
from agoclient.agotransport_mqtt import AgoMqttTransport
except ImportError:
self.log.exception("Cannot use mqtt messaging, failed to load transport")
raise AgoTransportConfigError()
self.transport = AgoMqttTransport(instance, broker, username, password)
else:
raise RuntimeError('Invalid messaging type ' + messaging)
......
from abc import abstractmethod
class AgoTransportConfigError(RuntimeError):
pass
class AgoTransport:
@abstractmethod
def start(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment