2021-10-28 17:09:09 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import json
|
|
|
|
import socket
|
2021-10-29 17:16:16 -04:00
|
|
|
import sys
|
2021-10-28 17:09:09 -04:00
|
|
|
import time
|
|
|
|
import subprocess
|
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
try:
|
|
|
|
apiclient = SourceFileLoader('apiclient', '/opt/confluent/bin/apiclient').load_module()
|
|
|
|
except FileNotFoundError:
|
|
|
|
apiclient = SourceFileLoader('apiclient', '/etc/confluent/apiclient').load_module()
|
|
|
|
|
|
|
|
def add_lla(iface, mac):
|
|
|
|
pieces = mac.split(':')
|
|
|
|
initbyte = int(pieces[0], 16) ^ 2
|
|
|
|
lla = 'fe80::{0:x}{1}:{2}ff:fe{3}:{4}{5}/64'.format(initbyte, pieces[1], pieces[2], pieces[3], pieces[4], pieces[5])
|
|
|
|
subprocess.check_call(['ip', 'addr', 'add', 'dev', iface, lla, 'scope', 'link'])
|
|
|
|
return lla
|
|
|
|
|
|
|
|
#cli = apiclient.HTTPSClient(json=True)
|
|
|
|
#c = cli.grab_url_with_status('/confluent-api/self/netcfg')
|
|
|
|
def add_missing_llas():
|
|
|
|
#NetworkManager goes out of its way to suppress ipv6 lla, so will just add some
|
|
|
|
added = {}
|
|
|
|
linkinfo = subprocess.check_output(['ip', '-br', 'l']).decode('utf8')
|
|
|
|
ifaces = {}
|
|
|
|
for line in linkinfo.split('\n'):
|
|
|
|
line = line.strip().split()
|
|
|
|
if not line or 'LOOPBACK' in line[-1] or 'NO-CARRIER' in line[-1]:
|
|
|
|
continue
|
|
|
|
ifaces[line[0]] = line[2]
|
|
|
|
ips = {}
|
|
|
|
ipinfo = subprocess.check_output(['ip', '-br', '-6', 'a']).decode('utf8')
|
|
|
|
for line in ipinfo.split('\n'):
|
|
|
|
line = line.strip().split(None, 2)
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
ips[line[0]] = line[2]
|
|
|
|
for iface in ifaces:
|
|
|
|
for addr in ips.get(iface, '').split():
|
|
|
|
if addr.startswith('fe80::'):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
added[iface] = add_lla(iface, ifaces[iface])
|
|
|
|
return added
|
|
|
|
|
|
|
|
def rm_tmp_llas(tmpllas):
|
|
|
|
for iface in tmpllas:
|
|
|
|
subprocess.check_call(['ip', 'addr', 'del', 'dev', iface, tmpllas[iface]])
|
|
|
|
|
|
|
|
def await_tentative():
|
|
|
|
while b'tentative' in subprocess.check_output(['ip', 'a']):
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def map_idx_to_name():
|
|
|
|
map = {}
|
2021-10-29 17:16:16 -04:00
|
|
|
devtype = {}
|
|
|
|
prevdev = None
|
2021-10-28 17:09:09 -04:00
|
|
|
for line in subprocess.check_output(['ip', 'l']).decode('utf8').splitlines():
|
|
|
|
if line.startswith(' '):
|
2021-10-29 17:16:16 -04:00
|
|
|
typ = line.split()[0].split('/')[1]
|
|
|
|
devtype[prevdev] = typ if type != 'ether' else 'ethernet'
|
2021-10-28 17:09:09 -04:00
|
|
|
continue
|
2021-10-29 17:16:16 -04:00
|
|
|
idx, iface, rst = line.split(':', 2)
|
|
|
|
prevdev = iface.strip()
|
|
|
|
rst = rst.split()
|
|
|
|
try:
|
|
|
|
midx = rst.index('master')
|
|
|
|
continue
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2021-10-28 17:09:09 -04:00
|
|
|
idx = int(idx)
|
|
|
|
iface = iface.strip()
|
|
|
|
map[idx] = iface
|
2021-10-29 17:16:16 -04:00
|
|
|
return map, devtype
|
2021-10-28 17:09:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_interface_name(iname, settings):
|
|
|
|
explicitname = settings.get('interface_names', None)
|
|
|
|
if explicitname:
|
|
|
|
return explicitname
|
|
|
|
if settings.get('current_nic', False):
|
|
|
|
return iname
|
|
|
|
return None
|
|
|
|
|
2021-10-29 17:16:16 -04:00
|
|
|
class NetworkManager(object):
|
|
|
|
def __init__(self, devtypes):
|
|
|
|
self.connections = {}
|
|
|
|
self.uuidbyname = {}
|
|
|
|
self.read_connections()
|
|
|
|
self.teamidx = 0
|
|
|
|
self.devtypes = devtypes
|
|
|
|
|
|
|
|
def read_connections(self):
|
|
|
|
self.connections = {}
|
|
|
|
self.uuidbyname = {}
|
|
|
|
ci = subprocess.check_output(['nmcli', 'c']).decode('utf8')
|
|
|
|
for inf in ci.splitlines():
|
|
|
|
n, u, t, dev = inf.split()
|
|
|
|
if n == 'NAME':
|
|
|
|
continue
|
|
|
|
if dev == '--':
|
|
|
|
dev = None
|
|
|
|
self.uuidbyname[n] = u
|
|
|
|
self.connections[u] = {'name': n, 'uuid': u, 'type': t, 'dev': dev}
|
|
|
|
|
|
|
|
def add_team_member(self, team, member):
|
|
|
|
if member in self.uuidbyname:
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'del', self.uuidbyname[member]])
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'add', 'type', 'team-slave', 'master', team, 'con-name', member, 'connection.interface-name', member])
|
|
|
|
|
|
|
|
def apply_configuration(self, cfg):
|
|
|
|
cmdargs = []
|
|
|
|
stgs = cfg['settings']
|
|
|
|
cmdargs.extend(['ipv6.method', stgs.get('ipv6_method', 'link-local')])
|
|
|
|
if stgs.get('ipv6_address', None):
|
|
|
|
cmdargs.extend(['ipv6.addresses', stgs['ipv6_address']])
|
|
|
|
cmdargs.extend(['ipv4.method', stgs.get('ipv4_method', 'disabled')])
|
|
|
|
if stgs.get('ipv4_address', None):
|
|
|
|
cmdargs.extend(['ipv4.addresses', stgs['ipv4_address']])
|
|
|
|
if stgs.get('ipv4_gateway', None):
|
|
|
|
cmdargs.extend(['ipv4.gateway', stgs['ipv4_gateway']])
|
|
|
|
if stgs.get('ipv6_gateway', None):
|
|
|
|
cmdargs.extend(['ipv6.gateway', stgs['ipv6_gateway']])
|
|
|
|
if len(cfg['interfaces']) > 1: # team time.. should be..
|
|
|
|
if not cfg['settings'].get('team_mode', None):
|
|
|
|
sys.stderr.write("Warning, multiple interfaces ({0}) without a team_mode, skipping setup\n".format(','.join(cfg['interfaces'])))
|
|
|
|
return
|
|
|
|
if not cfg['settings'].get('connection_name', None):
|
|
|
|
cfg['settings']['connection_name'] = 'team{0}'.format(self.teamidx)
|
|
|
|
self.teamidx += 1
|
|
|
|
cname = cfg['settings']['connection_name']
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'add', 'type', 'team', 'con-name', cname, 'connection.interface-name', cname, 'team.runner', stgs['team_mode']] + cmdargs)
|
|
|
|
for iface in cfg['interfaces']:
|
|
|
|
self.add_team_member(cname, iface)
|
|
|
|
else:
|
|
|
|
cname = stgs.get('connection_name', None)
|
|
|
|
iname = list(cfg['interfaces'])[0]
|
|
|
|
if not cname:
|
|
|
|
cname = iname
|
|
|
|
if cname in self.uuidbyname:
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'del', self.uuidbyname[cname]])
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'add', 'type', self.devtypes[iname], 'con-name', cname, 'connection.interface-name', iname] + cmdargs)
|
|
|
|
|
|
|
|
|
2021-10-28 17:09:09 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
tmpllas = add_missing_llas()
|
|
|
|
await_tentative()
|
2021-10-29 17:16:16 -04:00
|
|
|
idxmap, devtypes = map_idx_to_name()
|
2021-10-28 17:09:09 -04:00
|
|
|
netname_to_interfaces = {}
|
|
|
|
myaddrs = apiclient.get_my_addresses()
|
|
|
|
srvs, _ = apiclient.scan_confluents()
|
|
|
|
doneidxs = set([])
|
|
|
|
for srv in srvs:
|
|
|
|
s = socket.create_connection((srv, 443))
|
|
|
|
myname = s.getsockname()
|
|
|
|
s.close()
|
|
|
|
if len(myname) == 4:
|
|
|
|
curridx = myname[-1]
|
|
|
|
else:
|
|
|
|
myname = myname[0]
|
|
|
|
myname = socket.inet_pton(socket.AF_INET, myname)
|
|
|
|
for addr in myaddrs:
|
|
|
|
if myname == bytes(addr[1]):
|
|
|
|
curridx = addr[-1]
|
|
|
|
if curridx in doneidxs:
|
|
|
|
continue
|
|
|
|
status, nc = apiclient.HTTPSClient(usejson=True, host=srv).grab_url_with_status('/confluent-api/self/netcfg')
|
|
|
|
nc = json.loads(nc)
|
|
|
|
iname = get_interface_name(idxmap[curridx], nc.get('default', {}))
|
|
|
|
if iname:
|
|
|
|
if 'default' in netname_to_interfaces:
|
|
|
|
netname_to_interfaces['default']['interfaces'].add(iname)
|
|
|
|
else:
|
|
|
|
netname_to_interfaces['default'] = {'interfaces': set([iname]), 'settings': nc['default']}
|
|
|
|
for netname in nc.get('extranets', {}):
|
|
|
|
uname = '_' + netname
|
|
|
|
iname = get_interface_name(idxmap[curridx], nc['extranets'][netname])
|
|
|
|
if iname:
|
|
|
|
if uname in netname_to_interfaces:
|
|
|
|
netname_to_interfaces[uname]['interfaces'].add(iname)
|
|
|
|
else:
|
|
|
|
netname_to_interfaces[uname] = {'interfaces': set([iname]), 'settings': nc['extranets'][netname]}
|
|
|
|
doneidxs.add(curridx)
|
|
|
|
rm_tmp_llas(tmpllas)
|
2021-10-29 17:16:16 -04:00
|
|
|
nm = NetworkManager(devtypes)
|
|
|
|
for netn in netname_to_interfaces:
|
|
|
|
nm.apply_configuration(netname_to_interfaces[netn])
|
2021-10-28 17:09:09 -04:00
|
|
|
|