add logging

This commit is contained in:
Nico Schottelius 2020-01-19 12:55:06 +01:00
parent bd9dbb12b7
commit 8888f5d9f7
3 changed files with 57 additions and 28 deletions

View file

@ -11,17 +11,7 @@ from uncloud.common import settings
from uncloud import UncloudException
from uncloud.common.cli import resolve_otp_credentials
def exception_hook(exc_type, exc_value, exc_traceback):
logging.getLogger(__name__).error(
'Uncaught exception',
exc_info=(exc_type, exc_value, exc_traceback)
)
sys.excepthook = exception_hook
# the components that use etcd
# Components that use etcd
ETCD_COMPONENTS = ['api', 'scheduler', 'host', 'filescanner',
'imagescanner', 'metadata', 'configure', 'hack']
@ -30,10 +20,6 @@ ALL_COMPONENTS = ETCD_COMPONENTS.copy()
if __name__ == '__main__':
# Setting up root logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
arg_parser = argparse.ArgumentParser()
subparsers = arg_parser.add_subparsers(dest='command')
@ -84,11 +70,18 @@ if __name__ == '__main__':
mod = importlib.import_module('uncloud.{}.main'.format(name))
main = getattr(mod, 'main')
if arguments['debug']:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
try:
main(arguments)
except UncloudException as err:
logger.error(err)
log.error(err)
# except ConnectionFailedError as err:
# logger.error('Cannot connect to etcd: {}'.format(err))
# log.error('Cannot connect to etcd: {}'.format(err))
except Exception as err:
logger.exception(err)
log.exception(err)

View file

@ -20,6 +20,7 @@ arg_parser.add_argument('--vxlan-uplink-device', help="The VXLAN underlay device
arg_parser.add_argument('--vni', help="VXLAN ID (decimal)", type=int)
arg_parser.add_argument('--run-dns-ra', action='store_true',
help="Provide router advertisements and DNS resolution via dnsmasq")
arg_parser.add_argument('--use-sudo', help="Use sudo for command requiring root!", action='store_true')
@ -45,8 +46,12 @@ def main(arguments):
if not arguments['network'] or not arguments['vni'] or not arguments['vxlan_uplink_device']:
raise UncloudException("Initialising the network requires an IPv6 network and a VNI. You can use fd00::/64 and vni=1 for testing (non production!)")
vb = VXLANBridge(vni=arguments['vni'],
uplinkdev=arguments['vxlan_uplink_device'])
route=arguments['network'],
uplinkdev=arguments['vxlan_uplink_device'],
use_sudo=arguments['use_sudo'])
vb._setup_vxlan()
vb._setup_bridge()
vb._route_network()
if arguments['run_dns_ra']:
if not arguments['network']:

View file

@ -1,15 +1,20 @@
import subprocess
import ipaddress
import logging
from uncloud import UncloudException
log = logging.getLogger(__name__)
class VXLANBridge(object):
cmd_create_vxlan = "ip -6 link add {vxlandev} type vxlan id {vni_dec} dstport 4789 group {multicast_address} dev {uplinkdev} ttl 5"
cmd_up_dev = "ip link set {dev} up"
cmd_create_bridge="ip link add {bridgedev} type bridge"
cmd_add_to_bridge="ip link set {vxlandev} master {bridgedev} up"
cmd_add_addr="ip addr add {ip} dev {bridgedev}"
cmd_create_vxlan = "{sudo}ip -6 link add {vxlandev} type vxlan id {vni_dec} dstport 4789 group {multicast_address} dev {uplinkdev} ttl 5"
cmd_up_dev = "{sudo}ip link set {dev} up"
cmd_create_bridge="{sudo}ip link add {bridgedev} type bridge"
cmd_add_to_bridge="{sudo}ip link set {vxlandev} master {bridgedev} up"
cmd_add_addr="{sudo}ip addr add {ip} dev {bridgedev}"
cmd_add_route_dev="{sudo}ip route add {route} dev {bridgedev}"
# VXLAN ids are at maximum 24 bit - use a /104
multicast_network = ipaddress.IPv6Network("ff05::/104")
@ -17,16 +22,28 @@ class VXLANBridge(object):
def __init__(self,
vni,
uplinkdev):
uplinkdev,
route=None,
use_sudo=False):
self.config = {}
if vni > self.max_vni:
raise UncloudException("VNI must be in the range of 0 .. {}".format(self.max_vni))
if use_sudo:
self.config['sudo'] = 'sudo '
self.config['vni_dec'] = vni
self.config['vni_hex'] = "{:x}".format(vni)
self.config['multicast_address'] = self.multicast_network[vni]
#try:
self.config['route_network'] = ipaddress.IPv6Network(route)
#except Exception as e:
# print("Ahhhhhhhhhhhhhhhhh, die: {}".format(e))
self.config['route'] = route
self.config['uplinkdev'] = uplinkdev
self.config['vxlandev'] = "vx{}".format(self.config['vni_hex'])
self.config['bridgedev'] = "br{}".format(self.config['vni_hex'])
@ -36,9 +53,23 @@ class VXLANBridge(object):
pass
def _setup_vxlan(self):
# check for device first (?)
cmd = self.cmd_create_vxlan.format(**self.config)
print(cmd)
self._execute_cmd(self.cmd_create_vxlan)
self._execute_cmd(self.cmd_up_dev, dev=self.config['vxlandev'])
def _setup_bridge(self):
self._execute_cmd(self.cmd_create_bridge)
self._execute_cmd(self.cmd_up_dev, dev=self.config['bridgedev'])
def _route_network(self):
self._execute_cmd(self.cmd_add_route_dev)
def _add_vxlan_to_bridge(self):
self._execute_cmd(self.cmd_add_to_bridge)
def _execute_cmd(self, cmd_string, **kwargs):
cmd = cmd_string.format(**self.config, **kwargs)
log.info("Executing: {}".format(cmd))
print("Executing: {}".format(cmd))
subprocess.run(cmd.split())
class ManagementBridge(VXLANBridge):