uncloud-mravi/uncloud_etcd_based/uncloud/hack/net.py

117 lines
3.6 KiB
Python
Raw Normal View History

2020-01-15 11:40:37 +00:00
import subprocess
2020-01-19 10:30:41 +00:00
import ipaddress
2020-01-19 11:55:06 +00:00
import logging
2020-01-15 11:40:37 +00:00
2020-01-19 10:30:41 +00:00
from uncloud import UncloudException
2020-01-19 08:16:29 +00:00
2020-01-19 11:55:06 +00:00
log = logging.getLogger(__name__)
2020-01-15 11:40:37 +00:00
2020-01-19 10:30:41 +00:00
class VXLANBridge(object):
2020-01-19 11:55:06 +00:00
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}"
2020-01-15 11:40:37 +00:00
2020-01-19 10:30:41 +00:00
# VXLAN ids are at maximum 24 bit - use a /104
multicast_network = ipaddress.IPv6Network("ff05::/104")
max_vni = (2**24)-1
def __init__(self,
vni,
2020-01-19 11:55:06 +00:00
uplinkdev,
route=None,
use_sudo=False):
2020-01-19 10:30:41 +00:00
self.config = {}
if vni > self.max_vni:
raise UncloudException("VNI must be in the range of 0 .. {}".format(self.max_vni))
2020-01-19 11:55:06 +00:00
if use_sudo:
self.config['sudo'] = 'sudo '
2020-01-24 13:10:08 +00:00
else:
self.config['sudo'] = ''
2020-01-19 11:55:06 +00:00
2020-01-19 10:30:41 +00:00
self.config['vni_dec'] = vni
self.config['vni_hex'] = "{:x}".format(vni)
self.config['multicast_address'] = self.multicast_network[vni]
2020-01-19 11:55:06 +00:00
self.config['route_network'] = ipaddress.IPv6Network(route)
self.config['route'] = route
2020-01-19 10:30:41 +00:00
self.config['uplinkdev'] = uplinkdev
self.config['vxlandev'] = "vx{}".format(self.config['vni_hex'])
self.config['bridgedev'] = "br{}".format(self.config['vni_hex'])
def setup_networking(self):
pass
def _setup_vxlan(self):
2020-01-19 11:55:06 +00:00
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))
2020-01-19 10:30:41 +00:00
subprocess.run(cmd.split())
class ManagementBridge(VXLANBridge):
pass
2020-01-15 11:40:37 +00:00
class DNSRA(object):
2020-01-23 17:43:41 +00:00
# VXLAN ids are at maximum 24 bit
max_vni = (2**24)-1
# Command to start dnsmasq
2020-01-29 17:45:50 +00:00
cmd_start_dnsmasq="{sudo}dnsmasq --interface={bridgedev} --bind-interfaces --dhcp-range={route},ra-only,infinite --enable-ra --no-daemon"
2020-01-23 17:43:41 +00:00
def __init__(self,
vni,
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))
2020-01-15 12:26:05 +00:00
2020-01-23 17:43:41 +00:00
if use_sudo:
self.config['sudo'] = 'sudo '
else:
self.config['sudo'] = ''
2020-01-23 17:43:41 +00:00
#TODO: remove if not needed
#self.config['vni_dec'] = vni
self.config['vni_hex'] = "{:x}".format(vni)
# dnsmasq only wants the network without the prefix, therefore, cut it off
self.config['route'] = ipaddress.IPv6Network(route).network_address
self.config['bridgedev'] = "br{}".format(self.config['vni_hex'])
def _setup_dnsmasq(self):
self._execute_cmd(self.cmd_start_dnsmasq)
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())
2020-01-15 12:26:05 +00:00
class Firewall(object):
pass