Make flake8 happy
diff --git a/voltha/coordinator.py b/voltha/coordinator.py
index bd55049..ab85947 100644
--- a/voltha/coordinator.py
+++ b/voltha/coordinator.py
@@ -18,12 +18,13 @@
# TODO move this to the consul.twisted async client once it is available.
# Note:
-# We use https://github.com/cablehead/python-consul for consul client. It's master
-# branch already provides support for Twisted, but the latest released version (0.6.1)
-# was cut before twisted support was added. So keep an eye on when 0.6.2 comes out and
-# move over to the twisted interface once it's available.
+# We use https://github.com/cablehead/python-consul for consul client.
+# It's master branch already provides support for Twisted, but the latest
+# released version (0.6.1) was cut before twisted support was added. So keep
+# an eye on when 0.6.2 comes out and move over to the twisted interface once
+# it's available.
-from consul import Check, ConsulException
+from consul import ConsulException
from consul.twisted import Consul
from requests import ConnectionError
from structlog import get_logger
@@ -60,7 +61,9 @@
host = consul.split(':')[0].strip()
port = int(consul.split(':')[1].strip())
- self.consul = Consul(host=host, port=port) # TODO need to handle reconnect events properly
+
+ # TODO need to handle reconnect events properly
+ self.consul = Consul(host=host, port=port)
reactor.callLater(0, self.async_init)
self.log.info('initialized-coordinator')
@@ -71,20 +74,21 @@
@inlineCallbacks
def async_init(self):
- # yield self.kv_put('voltha/instances/%s/status' % self.instance_id, 'up')
yield self.create_session()
yield self.create_membership_record()
yield self.elect_leader()
def backoff(self, msg):
- wait_time = self.RETRY_BACKOFF[min(self.retries, len(self.RETRY_BACKOFF) - 1)]
+ wait_time = self.RETRY_BACKOFF[min(self.retries,
+ len(self.RETRY_BACKOFF) - 1)]
self.retries += 1
self.log.error(msg + ', retrying in %s second(s)' % wait_time)
return asleep(wait_time)
def clear_backoff(self):
if self.retries:
- self.log.info('Reconnected to consul agent after %d retries' % self.retries)
+ self.log.info('Reconnected to consul agent after %d retries'
+ % self.retries)
self.retries = 0
@inlineCallbacks
@@ -118,7 +122,8 @@
@inlineCallbacks
def _renew_session():
try:
- result = yield self.consul.session.renew(session_id=self.session_id)
+ result = yield self.consul.session.renew(
+ session_id=self.session_id)
self.log.debug('just renewed session', result=result)
except Exception, e:
self.log.exception('could-not-renew-session', e=e)
@@ -127,7 +132,8 @@
def _create_session():
# create consul session
- self.session_id = yield self.consul.session.create(behavior='delete', ttl=10)
+ self.session_id = yield self.consul.session.create(
+ behavior='delete', ttl=10)
self.log.info('created-consul-session', session_id=self.session_id)
# start renewing session it 3 times within the ttl
@@ -142,21 +148,28 @@
@inlineCallbacks
def create_membership_record(self):
- # create ephemeral k/v registering this instance in the service/voltha/members/<instance-id> node
- result = yield self.consul.kv.put('service/voltha/members/%s' % self.instance_id, 'alive',
- acquire=self.session_id)
+ # create ephemeral k/v registering this instance in the
+ # service/voltha/members/<instance-id> node
+ result = yield self.consul.kv.put(
+ 'service/voltha/members/%s' % self.instance_id, 'alive',
+ acquire=self.session_id)
assert result is True
@inlineCallbacks
def elect_leader(self):
- """Attempt to become the leader by acquiring the leader key and track the leader anyway"""
+ """
+ Attempt to become the leader by acquiring the leader key and
+ track the leader anyway
+ """
# attempt acquire leader lock
- result = yield self.consul.kv.put('service/voltha/leader', self.instance_id,
+ result = yield self.consul.kv.put('service/voltha/leader',
+ self.instance_id,
acquire=self.session_id)
- # read it back before being too happy; seeing our session id is a proof and now we have
- # the change id that we can use to reliably track any changes
+ # read it back before being too happy; seeing our session id is a
+ # proof and now we have the change id that we can use to reliably
+ # track any changes
# TODO continue from here !!!
if result is True:
diff --git a/voltha/dockerhelpers.py b/voltha/dockerhelpers.py
index ee6cd7a..0927711 100644
--- a/voltha/dockerhelpers.py
+++ b/voltha/dockerhelpers.py
@@ -30,9 +30,10 @@
def get_my_containers_name():
"""
Return the docker containers name in which this process is running.
- To look up the container name, we use the container ID extracted from the $HOSTNAME
- environment variable (which is set by docker conventions).
- :return: String with the docker container name (or None if any issue is encountered)
+ To look up the container name, we use the container ID extracted from the
+ $HOSTNAME environment variable (which is set by docker conventions).
+ :return: String with the docker container name (or None if any issue is
+ encountered)
"""
my_container_id = os.environ.get('HOSTNAME', None)
diff --git a/voltha/main.py b/voltha/main.py
index a85fb2f..2426cf1 100755
--- a/voltha/main.py
+++ b/voltha/main.py
@@ -35,8 +35,10 @@
instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
config=os.environ.get('CONFIG', './voltha.yml'),
interface=os.environ.get('INTERFACE', get_my_primary_interface()),
- internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()),
- external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()),
+ internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
+ get_my_primary_local_ipv4()),
+ external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
+ get_my_primary_local_ipv4()),
fluentd=os.environ.get('FLUENTD', None),
rest_port=os.environ.get('REST_PORT', 8880),
)
@@ -46,59 +48,106 @@
parser = argparse.ArgumentParser()
- parser.add_argument('-c', '--config', dest='config', action='store',
+ _help = ('Path to voltha.yml config file (default: %s). '
+ 'If relative, it is relative to main.py of voltha.'
+ % defs['config'])
+ parser.add_argument('-c', '--config',
+ dest='config',
+ action='store',
default=defs['config'],
- help='Path to voltha.yml config file (default: %s). '
- 'If relative, it is relative to main.py of voltha.' % defs['config'])
+ help=_help)
- parser.add_argument('-C', '--consul', dest='consul', action='store',
- default=defs['consul'],
- help='<hostname>:<port> to consul agent (default: %s)' % defs['consul'])
+ _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
+ parser.add_argument(
+ '-C', '--consul', dest='consul', action='store',
+ default=defs['consul'],
+ help=_help)
- parser.add_argument('-E', '--external-host-address', dest='external_host_address', action='store',
+ _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
+ 'the cluster (default: %s)' % defs['external_host_address'])
+ parser.add_argument('-E', '--external-host-address',
+ dest='external_host_address',
+ action='store',
default=defs['external_host_address'],
- help='<hostname> or <ip> at which Voltha is reachable from outside the cluster'
- '(default: %s)' % defs['external_host_address'])
+ help=_help)
- parser.add_argument('-F', '--fluentd', dest='fluentd', action='store',
+ _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
+ 'specified (None), the address from the config file is used'
+ % defs['fluentd'])
+ parser.add_argument('-F', '--fluentd',
+ dest='fluentd',
+ action='store',
default=defs['fluentd'],
- help='<hostname>:<port> to fluentd server (default: %s).'
- '(If not specified (None), the address from the config file is used'
- % defs['fluentd'])
+ help=_help)
- parser.add_argument('-H', '--internal-host-address', dest='internal_host_address', action='store',
+ _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
+ 'cluster (default: %s)' % defs['internal_host_address'])
+ parser.add_argument('-H', '--internal-host-address',
+ dest='internal_host_address',
+ action='store',
default=defs['internal_host_address'],
- help='<hostname> or <ip> at which Voltha is reachable from inside the cluster'
- '(default: %s)' % defs['internal_host_address'])
+ help=_help)
- parser.add_argument('-i', '--instance-id', dest='instance_id', action='store',
+ _help = ('unique string id of this voltha instance (default: %s)'
+ % defs['interface'])
+ parser.add_argument('-i', '--instance-id',
+ dest='instance_id',
+ action='store',
default=defs['instance_id'],
- help='unique string id of this voltha instance (default: %s)' % defs['interface'])
+ help=_help)
# TODO placeholder, not used yet
- parser.add_argument('-I', '--interface', dest='interface', action='store',
+ _help = 'ETH interface to send (default: %s)' % defs['interface']
+ parser.add_argument('-I', '--interface',
+ dest='interface',
+ action='store',
default=defs['interface'],
- help='ETH interface to send (default: %s)' % defs['interface'])
+ help=_help)
- parser.add_argument('-n', '--no-banner', dest='no_banner', action='store_true', default=False,
- help='omit startup banner log lines')
+ _help = 'omit startup banner log lines'
+ parser.add_argument('-n', '--no-banner',
+ dest='no_banner',
+ action='store_true',
+ default=False,
+ help=_help)
- parser.add_argument('-N', '--no-heartbeat', dest='no_heartbeat', action='store_true', default=False,
- help='do not emit periodic heartbeat log messages')
+ _help = 'do not emit periodic heartbeat log messages'
+ parser.add_argument('-N', '--no-heartbeat',
+ dest='no_heartbeat',
+ action='store_true',
+ default=False,
+ help=_help)
- parser.add_argument('-R', '--rest-port', dest='rest_port', action='store', type=int,
+ _help = ('port number for the rest service (default: %d)'
+ % defs['rest_port'])
+ parser.add_argument('-R', '--rest-port',
+ dest='rest_port',
+ action='store',
+ type=int,
default=defs['rest_port'],
- help='port number for the rest service (default: %d)' % defs['rest_port'])
+ help=_help)
- parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False,
- help="suppress debug and info logs")
+ _help = "suppress debug and info logs"
+ parser.add_argument('-q', '--quiet',
+ dest='quiet',
+ action='store_true',
+ default=False,
+ help=_help)
- parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False,
- help='enable verbose logging')
+ _help = 'enable verbose logging'
+ parser.add_argument('-v', '--verbose',
+ dest='verbose',
+ action='store_true',
+ default=False,
+ help=_help)
- parser.add_argument('--instance-id-is-container-name', dest='instance_id_is_container_name', action='store_true',
- default=False, help='use docker container name as voltha instance id'
- ' (overrides -i/--instance-id option)')
+ _help = ('use docker container name as voltha instance id'
+ ' (overrides -i/--instance-id option)')
+ parser.add_argument('--instance-id-is-container-name',
+ dest='instance_id_is_container_name',
+ action='store_true',
+ default=False,
+ help=_help)
args = parser.parse_args()
@@ -135,7 +184,9 @@
def __init__(self):
self.args = args = parse_args()
self.config = load_config(args)
- self.log = setup_logging(self.config.get('logging', {}), args.instance_id, fluentd=args.fluentd)
+ self.log = setup_logging(self.config.get('logging', {}),
+ args.instance_id,
+ fluentd=args.fluentd)
# components
self.coordinator = None
@@ -170,8 +221,10 @@
def start_reactor(self):
from twisted.internet import reactor
- reactor.callWhenRunning(lambda: self.log.info('twisted-reactor-started'))
- reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown_components)
+ reactor.callWhenRunning(
+ lambda: self.log.info('twisted-reactor-started'))
+ reactor.addSystemEventTrigger('before', 'shutdown',
+ self.shutdown_components)
reactor.run()
def start_heartbeat(self):
diff --git a/voltha/nethelpers.py b/voltha/nethelpers.py
index 2fa7bd5..52f7f4c 100644
--- a/voltha/nethelpers.py
+++ b/voltha/nethelpers.py
@@ -26,7 +26,8 @@
def get_my_primary_interface():
gateways = ni.gateways()
assert 'default' in gateways, \
- "No default gateway on host/container, cannot determine primary interface"
+ ("No default gateway on host/container, "
+ "cannot determine primary interface")
default_gw_index = gateways['default'].keys()[0]
# gateways[default_gw_index] has the format (example):
# [('10.15.32.1', 'en0', True)]
@@ -34,9 +35,9 @@
return interface_name
-def get_my_primary_local_ipv4(iface_name=None):
- iface_name = get_my_primary_interface() if iface_name is None else iface_name
- addresses = ni.ifaddresses(iface_name)
+def get_my_primary_local_ipv4(ifname=None):
+ ifname = get_my_primary_interface() if ifname is None else ifname
+ addresses = ni.ifaddresses(ifname)
ipv4 = addresses[AF_INET][0]['addr']
return ipv4
diff --git a/voltha/structlog_setup.py b/voltha/structlog_setup.py
index 299b0ac..92e7b41 100644
--- a/voltha/structlog_setup.py
+++ b/voltha/structlog_setup.py
@@ -52,7 +52,8 @@
try:
if not self:
return '{}'
- return '{%s}' % ", ".join("%s: %s" % (k, v) for k, v in self.items())
+ return '{%s}' % ", ".join("%s: %s" % (k, v)
+ for k, v in self.items())
finally:
del _repr_running[call_key]
@@ -60,10 +61,11 @@
def setup_logging(log_config, instance_id, fluentd=None):
"""
Set up logging such that:
- - The primary logging entry method is structlog (see http://structlog.readthedocs.io/en/stable/index.html)
+ - The primary logging entry method is structlog
+ (see http://structlog.readthedocs.io/en/stable/index.html)
- By default, the logging backend is Python standard lib logger
- - Alternatively, fluentd can be configured with to be the backend, providing direct
- bridge to a fluent logging agent.
+ - Alternatively, fluentd can be configured with to be the backend,
+ providing direct bridge to a fluent logging agent.
"""
def add_exc_info_flag_for_exception(_, name, event_dict):
@@ -74,7 +76,8 @@
def add_instance_id(_, __, event_dict):
event_dict['instance_id'] = instance_id
return event_dict
- # if fluentd is specified, we need to override the config data with its host and port info
+ # if fluentd is specified, we need to override the config data with
+ # its host and port info
if fluentd is not None:
fluentd_host = fluentd.split(':')[0].strip()
fluentd_port = int(fluentd.split(':')[1].strip())