2021-10-28 21:09:09 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2021-11-02 18:04:11 +00:00
|
|
|
import glob
|
2021-10-28 21:09:09 +00:00
|
|
|
import json
|
2021-11-02 18:04:11 +00:00
|
|
|
import os
|
2021-10-28 21:09:09 +00:00
|
|
|
import socket
|
2021-10-29 21:16:16 +00:00
|
|
|
import sys
|
2021-10-28 21:09:09 +00:00
|
|
|
import time
|
2021-11-02 18:04:11 +00:00
|
|
|
import shlex
|
2021-10-28 21:09:09 +00:00
|
|
|
import subprocess
|
2023-09-01 14:07:51 +00:00
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2021-10-28 21:09:09 +00:00
|
|
|
try:
|
2022-02-03 15:09:28 +00:00
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
def load_source(mod, path):
|
|
|
|
return SourceFileLoader(mod, path).load_module()
|
|
|
|
except ImportError:
|
|
|
|
from imp import load_source
|
|
|
|
|
|
|
|
try:
|
|
|
|
apiclient = load_source('apiclient', '/opt/confluent/bin/apiclient')
|
|
|
|
except IOError:
|
|
|
|
apiclient = load_source('apiclient', '/etc/confluent/apiclient')
|
2021-10-28 21:09:09 +00:00
|
|
|
|
|
|
|
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])
|
2021-11-02 18:04:11 +00:00
|
|
|
try:
|
2022-10-28 16:10:03 +00:00
|
|
|
with open('/proc/sys/net/ipv6/conf/{0}/disable_ipv6'.format(iface), 'w') as setin:
|
|
|
|
setin.write('0')
|
2021-11-02 18:04:11 +00:00
|
|
|
subprocess.check_call(['ip', 'addr', 'add', 'dev', iface, lla, 'scope', 'link'])
|
|
|
|
except Exception:
|
|
|
|
return None
|
2021-10-28 21:09:09 +00:00
|
|
|
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
|
2021-11-02 18:04:11 +00:00
|
|
|
if 'UP' not in line[-1]:
|
|
|
|
subprocess.call(['ip', 'link', 'set', line[0], 'up'])
|
2021-10-28 21:09:09 +00:00
|
|
|
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:
|
2021-11-02 18:04:11 +00:00
|
|
|
newlla = add_lla(iface, ifaces[iface])
|
|
|
|
if newlla:
|
|
|
|
added[iface] = newlla
|
2021-10-28 21:09:09 +00:00
|
|
|
return added
|
|
|
|
|
|
|
|
def rm_tmp_llas(tmpllas):
|
|
|
|
for iface in tmpllas:
|
|
|
|
subprocess.check_call(['ip', 'addr', 'del', 'dev', iface, tmpllas[iface]])
|
|
|
|
|
|
|
|
def await_tentative():
|
2021-11-01 16:34:37 +00:00
|
|
|
maxwait = 10
|
2021-10-28 21:09:09 +00:00
|
|
|
while b'tentative' in subprocess.check_output(['ip', 'a']):
|
2021-11-01 16:34:37 +00:00
|
|
|
if maxwait == 0:
|
|
|
|
break
|
|
|
|
maxwait -= 1
|
2021-10-28 21:09:09 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def map_idx_to_name():
|
|
|
|
map = {}
|
2021-10-29 21:16:16 +00:00
|
|
|
devtype = {}
|
|
|
|
prevdev = None
|
2021-10-28 21:09:09 +00:00
|
|
|
for line in subprocess.check_output(['ip', 'l']).decode('utf8').splitlines():
|
2021-11-02 18:04:11 +00:00
|
|
|
if line.startswith(' ') and 'link/' in line:
|
2021-10-29 21:16:16 +00:00
|
|
|
typ = line.split()[0].split('/')[1]
|
|
|
|
devtype[prevdev] = typ if type != 'ether' else 'ethernet'
|
2021-11-02 18:04:11 +00:00
|
|
|
if line.startswith(' '):
|
2021-10-28 21:09:09 +00:00
|
|
|
continue
|
2021-10-29 21:16:16 +00: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 21:09:09 +00:00
|
|
|
idx = int(idx)
|
|
|
|
iface = iface.strip()
|
|
|
|
map[idx] = iface
|
2021-10-29 21:16:16 +00:00
|
|
|
return map, devtype
|
2021-10-28 21:09:09 +00: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
|
|
|
|
|
2023-09-01 14:07:51 +00:00
|
|
|
class NetplanManager(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.cfgbydev = {}
|
|
|
|
self.read_connections()
|
|
|
|
|
|
|
|
def read_connections(self):
|
|
|
|
for plan in glob.glob('/etc/netplan/*.y*ml'):
|
|
|
|
with open(plan) as planfile:
|
|
|
|
planinfo = yaml.safe_load(planfile)
|
|
|
|
if not planinfo:
|
|
|
|
continue
|
|
|
|
nicinfo = planinfo.get('network', {}).get('ethernets', {})
|
|
|
|
for devname in nicinfo:
|
|
|
|
if devname == 'lo':
|
|
|
|
continue
|
|
|
|
if 'gateway4' in nicinfo[devname]:
|
|
|
|
# normalize deprecated syntax on read in
|
|
|
|
gw4 = nicinfo[devname]['gateway4']
|
|
|
|
del nicinfo[devname]['gateway4']
|
|
|
|
routeinfo = nicinfo[devname].get('routes', [])
|
|
|
|
for ri in routeinfo:
|
|
|
|
if ri.get('via', None) == gw4 and ri.get('to', None) in ('default', '0.0.0.0/0', '0/0'):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
routeinfo.append({
|
|
|
|
'to': 'default',
|
|
|
|
'via': gw4
|
|
|
|
})
|
|
|
|
nicinfo[devname]['routes'] = routeinfo
|
|
|
|
self.cfgbydev[devname] = nicinfo[devname]
|
|
|
|
|
|
|
|
def apply_configuration(self, cfg):
|
|
|
|
devnames = cfg['interfaces']
|
|
|
|
if len(devnames) != 1:
|
|
|
|
raise Exception('Multi-nic team/bonds not yet supported')
|
|
|
|
stgs = cfg['settings']
|
|
|
|
needcfgapply = False
|
|
|
|
for devname in devnames:
|
|
|
|
needcfgwrite = False
|
|
|
|
if stgs['ipv4_method'] == 'static':
|
|
|
|
curraddr = stgs['ipv4_address']
|
|
|
|
currips = self.getcfgarrpath([devname, 'addresses'])
|
|
|
|
if curraddr not in currips:
|
|
|
|
needcfgwrite = True
|
|
|
|
currips.append(curraddr)
|
|
|
|
gwaddr = stgs.get('ipv4_gateway', None)
|
|
|
|
if gwaddr:
|
|
|
|
cfgroutes = self.getcfgarrpath([devname, 'routes'])
|
|
|
|
for rinfo in cfgroutes:
|
|
|
|
if rinfo.get('via', None) == gwaddr:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
needcfgwrite = True
|
|
|
|
cfgroutes.append({'via': gwaddr, 'to': 'default'})
|
|
|
|
if needcfgwrite:
|
|
|
|
needcfgaply = True
|
|
|
|
newcfg = {'network': {'version': 2, 'ethernets': {devname: self.cfgbydev[devname]}}}
|
|
|
|
with open('/etc/netplan/{0}-confluentcfg.yaml'.format(devname), 'w') as planout:
|
|
|
|
planout.write(yaml.dump(newcfg))
|
|
|
|
if needcfgapply:
|
|
|
|
subprocess.call(['netplan', 'apply'])
|
|
|
|
|
|
|
|
def getcfgarrpath(self, devpath):
|
|
|
|
currptr = self.cfgbydev
|
|
|
|
for k in devpath[:-1]:
|
|
|
|
if k not in currptr:
|
|
|
|
currptr[k] = {}
|
|
|
|
currptr = currptr[k]
|
|
|
|
if devpath[-1] not in currptr:
|
|
|
|
currptr[devpath[-1]] = []
|
|
|
|
return currptr[devpath[-1]]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-02 18:04:11 +00:00
|
|
|
class WickedManager(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.teamidx = 0
|
|
|
|
self.read_connections()
|
|
|
|
|
|
|
|
def read_connections(self):
|
|
|
|
self.cfgbydev = {}
|
|
|
|
for ifcfg in glob.glob('/etc/sysconfig/network/ifcfg-*'):
|
|
|
|
devname = ifcfg.replace('/etc/sysconfig/network/ifcfg-', '')
|
|
|
|
if devname == 'lo':
|
|
|
|
continue
|
|
|
|
currcfg = {}
|
|
|
|
self.cfgbydev[devname] = currcfg
|
|
|
|
for cfg in open(ifcfg).read().splitlines():
|
|
|
|
cfg = cfg.split('#', 1)[0]
|
|
|
|
kv = ' '.join(shlex.split(cfg)).split('=', 1)
|
|
|
|
if len(kv) != 2:
|
|
|
|
continue
|
|
|
|
k, v = kv
|
|
|
|
k = k.strip()
|
|
|
|
v = v.strip()
|
|
|
|
currcfg[k] = v
|
|
|
|
|
|
|
|
def apply_configuration(self, cfg):
|
|
|
|
stgs = cfg['settings']
|
|
|
|
ipcfg = 'STARTMODE=auto\n'
|
|
|
|
routecfg = ''
|
|
|
|
bootproto4 = stgs.get('ipv4_method', 'none')
|
|
|
|
bootproto6 = stgs.get('ipv6_method', 'none')
|
|
|
|
if bootproto4 == 'dhcp' and bootproto6 == 'dhcp':
|
|
|
|
ipcfg += 'BOOTPROTO=dhcp\n'
|
|
|
|
elif bootproto4 == 'dhcp':
|
|
|
|
ipcfg += 'BOOTPROTO=dhcp4\n'
|
|
|
|
elif bootproto6 == 'dhcp':
|
|
|
|
ipcfg += 'BOOTPROTO=dhcp6\n'
|
|
|
|
else:
|
|
|
|
ipcfg += 'BOOTPROTO=static\n'
|
|
|
|
if stgs.get('ipv4_address', None):
|
|
|
|
ipcfg += 'IPADDR=' + stgs['ipv4_address'] + '\n'
|
|
|
|
v4gw = stgs.get('ipv4_gateway', None)
|
|
|
|
if stgs.get('ipv6_address', None):
|
|
|
|
ipcfg += 'IPADDR_V6=' + stgs['ipv6_address'] + '\n'
|
|
|
|
v6gw = stgs.get('ipv6_gateway', None)
|
|
|
|
cname = None
|
|
|
|
if len(cfg['interfaces']) > 1: # creating new team
|
|
|
|
if not stgs.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 stgs.get('connection_name', None):
|
2022-03-24 13:10:42 +00:00
|
|
|
stgs['connection_name'] = 'bond{0}'.format(self.teamidx)
|
2021-11-02 18:04:11 +00:00
|
|
|
self.teamidx += 1
|
|
|
|
cname = stgs['connection_name']
|
|
|
|
with open('/etc/sysconfig/network/ifcfg-{0}'.format(cname), 'w') as teamout:
|
|
|
|
teamout.write(ipcfg)
|
2022-03-24 13:10:42 +00:00
|
|
|
if stgs['team_mode'] == 'lacp':
|
|
|
|
stgs['team_mode'] = '802.3ad'
|
|
|
|
teamout.write("BONDING_MODULE_OPTS='mode={0} miimon=100'\nBONDING_MASTER=yes\n".format(stgs['team_mode']))
|
2021-11-02 18:04:11 +00:00
|
|
|
idx = 1
|
|
|
|
for iface in cfg['interfaces']:
|
|
|
|
subprocess.call(['wicked', 'ifdown', iface])
|
|
|
|
try:
|
|
|
|
os.remove('/etc/sysconfig/network/ifcfg-{0}'.format(iface))
|
|
|
|
os.remove('/etc/sysconfig/network/ifroute-{0}'.format(iface))
|
|
|
|
except OSError:
|
|
|
|
pass
|
2022-03-24 13:10:42 +00:00
|
|
|
teamout.write('BONDING_SLAVE{0}={1}\n'.format(idx, iface))
|
2021-11-02 18:04:11 +00:00
|
|
|
idx += 1
|
|
|
|
else:
|
|
|
|
cname = list(cfg['interfaces'])[0]
|
|
|
|
priorcfg = self.cfgbydev.get(cname, {})
|
|
|
|
for cf in priorcfg:
|
|
|
|
if cf.startswith('TEAM_'):
|
|
|
|
ipcfg += '{0}={1}\n'.format(cf, priorcfg[cf])
|
|
|
|
with open('/etc/sysconfig/network/ifcfg-{0}'.format(cname), 'w') as iout:
|
|
|
|
iout.write(ipcfg)
|
|
|
|
if v4gw:
|
|
|
|
routecfg += 'default {0} - {1}\n'.format(v4gw, cname)
|
|
|
|
if v6gw:
|
|
|
|
routecfg += 'default {0} - {1}\n'.format(v6gw, cname)
|
|
|
|
if routecfg:
|
|
|
|
with open('/etc/sysconfig/network/ifroute-{0}'.format(cname), 'w') as routeout:
|
|
|
|
routeout.write(routecfg)
|
|
|
|
subprocess.call(['wicked', 'ifup', cname])
|
|
|
|
|
|
|
|
|
2021-10-29 21:16:16 +00:00
|
|
|
class NetworkManager(object):
|
|
|
|
def __init__(self, devtypes):
|
|
|
|
self.connections = {}
|
|
|
|
self.uuidbyname = {}
|
2021-11-01 16:34:37 +00:00
|
|
|
self.uuidbydev = {}
|
|
|
|
self.connectiondetail = {}
|
2021-10-29 21:16:16 +00:00
|
|
|
self.read_connections()
|
|
|
|
self.teamidx = 0
|
|
|
|
self.devtypes = devtypes
|
|
|
|
|
|
|
|
def read_connections(self):
|
|
|
|
self.connections = {}
|
|
|
|
self.uuidbyname = {}
|
2021-11-01 16:34:37 +00:00
|
|
|
self.uuidbydev = {}
|
|
|
|
self.connectiondetail = {}
|
2021-11-08 21:23:56 +00:00
|
|
|
ci = subprocess.check_output(['nmcli', '-t', 'c']).decode('utf8')
|
2021-10-29 21:16:16 +00:00
|
|
|
for inf in ci.splitlines():
|
2021-11-08 21:23:56 +00:00
|
|
|
n, u, t, dev = inf.split(':')
|
2021-10-29 21:16:16 +00:00
|
|
|
if n == 'NAME':
|
|
|
|
continue
|
|
|
|
if dev == '--':
|
|
|
|
dev = None
|
|
|
|
self.uuidbyname[n] = u
|
2021-11-01 16:34:37 +00:00
|
|
|
if dev:
|
|
|
|
self.uuidbydev[dev] = u
|
2021-10-29 21:16:16 +00:00
|
|
|
self.connections[u] = {'name': n, 'uuid': u, 'type': t, 'dev': dev}
|
2021-11-01 16:34:37 +00:00
|
|
|
deats = {}
|
|
|
|
for deat in subprocess.check_output(['nmcli', 'c', 's', u]).decode('utf8').splitlines():
|
|
|
|
k, v = deat.split(':', 1)
|
|
|
|
v = v.strip()
|
|
|
|
if v == '--':
|
|
|
|
continue
|
|
|
|
if '(default)' in v:
|
|
|
|
continue
|
|
|
|
deats[k] = v
|
|
|
|
self.connectiondetail[u] = deats
|
|
|
|
|
2021-10-29 21:16:16 +00:00
|
|
|
|
|
|
|
def add_team_member(self, team, member):
|
2021-11-01 16:34:37 +00:00
|
|
|
bondcfg = {}
|
|
|
|
if member in self.uuidbydev:
|
|
|
|
myuuid = self.uuidbydev[member]
|
|
|
|
deats = self.connectiondetail[myuuid]
|
|
|
|
currteam = deats.get('connection.master', None)
|
|
|
|
if currteam == team:
|
|
|
|
return
|
|
|
|
for stg in ('ipv4.dhcp-hostname', 'ipv4.dns', 'ipv6.dns', 'ipv6.dhcp-hostname'):
|
|
|
|
if deats.get(stg, None):
|
|
|
|
bondcfg[stg] = deats[stg]
|
2021-10-29 21:16:16 +00:00
|
|
|
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])
|
2021-11-01 16:34:37 +00:00
|
|
|
if bondcfg:
|
|
|
|
args = []
|
|
|
|
for parm in bondcfg:
|
|
|
|
args.append(parm)
|
|
|
|
args.append(bondcfg[parm])
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'm', team] + args)
|
2021-10-29 21:16:16 +00:00
|
|
|
|
|
|
|
def apply_configuration(self, cfg):
|
2021-11-01 16:34:37 +00:00
|
|
|
cmdargs = {}
|
2023-08-17 08:30:36 +00:00
|
|
|
cmdargs['connection.autoconnect'] = 'yes'
|
2021-10-29 21:16:16 +00:00
|
|
|
stgs = cfg['settings']
|
2021-11-01 16:34:37 +00:00
|
|
|
cmdargs['ipv6.method'] = stgs.get('ipv6_method', 'link-local')
|
2021-10-29 21:16:16 +00:00
|
|
|
if stgs.get('ipv6_address', None):
|
2021-11-01 16:34:37 +00:00
|
|
|
cmdargs['ipv6.addresses'] = stgs['ipv6_address']
|
|
|
|
cmdargs['ipv4.method'] = stgs.get('ipv4_method', 'disabled')
|
2021-10-29 21:16:16 +00:00
|
|
|
if stgs.get('ipv4_address', None):
|
2021-11-01 16:34:37 +00:00
|
|
|
cmdargs['ipv4.addresses'] = stgs['ipv4_address']
|
2021-10-29 21:16:16 +00:00
|
|
|
if stgs.get('ipv4_gateway', None):
|
2021-11-01 16:34:37 +00:00
|
|
|
cmdargs['ipv4.gateway'] = stgs['ipv4_gateway']
|
2021-10-29 21:16:16 +00:00
|
|
|
if stgs.get('ipv6_gateway', None):
|
2021-11-01 16:34:37 +00:00
|
|
|
cmdargs['ipv6.gateway'] = stgs['ipv6_gateway']
|
2021-10-29 21:16:16 +00:00
|
|
|
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']
|
2021-11-01 16:34:37 +00:00
|
|
|
cargs = []
|
|
|
|
for arg in cmdargs:
|
|
|
|
cargs.append(arg)
|
|
|
|
cargs.append(cmdargs[arg])
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'add', 'type', 'team', 'con-name', cname, 'connection.interface-name', cname, 'team.runner', stgs['team_mode']] + cargs)
|
2021-10-29 21:16:16 +00:00
|
|
|
for iface in cfg['interfaces']:
|
|
|
|
self.add_team_member(cname, iface)
|
2021-11-01 16:34:37 +00:00
|
|
|
subprocess.check_call(['nmcli', 'c', 'u', cname])
|
2021-10-29 21:16:16 +00:00
|
|
|
else:
|
|
|
|
cname = stgs.get('connection_name', None)
|
|
|
|
iname = list(cfg['interfaces'])[0]
|
|
|
|
if not cname:
|
|
|
|
cname = iname
|
2021-11-01 16:34:37 +00:00
|
|
|
u = self.uuidbyname.get(cname, None)
|
|
|
|
cargs = []
|
|
|
|
for arg in cmdargs:
|
|
|
|
cargs.append(arg)
|
|
|
|
cargs.append(cmdargs[arg])
|
|
|
|
if u:
|
|
|
|
cmdargs['connection.interface-name'] = iname
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'm', u] + cargs)
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'u', u])
|
|
|
|
else:
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'add', 'type', self.devtypes[iname], 'con-name', cname, 'connection.interface-name', iname] + cargs)
|
2023-02-28 17:04:20 +00:00
|
|
|
self.read_connections()
|
|
|
|
u = self.uuidbyname.get(cname, None)
|
|
|
|
if u:
|
|
|
|
subprocess.check_call(['nmcli', 'c', 'u', u])
|
2021-10-29 21:16:16 +00:00
|
|
|
|
|
|
|
|
2021-10-28 21:09:09 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-11-02 13:43:42 +00:00
|
|
|
havefirewall = subprocess.call(['systemctl', 'status', 'firewalld'])
|
|
|
|
havefirewall = havefirewall == 0
|
|
|
|
if havefirewall:
|
|
|
|
subprocess.check_call(['systemctl', 'stop', 'firewalld'])
|
2021-10-28 21:09:09 +00:00
|
|
|
tmpllas = add_missing_llas()
|
|
|
|
await_tentative()
|
2021-10-29 21:16:16 +00:00
|
|
|
idxmap, devtypes = map_idx_to_name()
|
2021-10-28 21:09:09 +00:00
|
|
|
netname_to_interfaces = {}
|
|
|
|
myaddrs = apiclient.get_my_addresses()
|
|
|
|
srvs, _ = apiclient.scan_confluents()
|
|
|
|
doneidxs = set([])
|
|
|
|
for srv in srvs:
|
2023-06-12 12:49:13 +00:00
|
|
|
try:
|
|
|
|
s = socket.create_connection((srv, 443))
|
|
|
|
except socket.error:
|
|
|
|
continue
|
2021-10-28 21:09:09 +00:00
|
|
|
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:
|
2022-02-03 16:50:34 +00:00
|
|
|
if myname == addr[1].tobytes():
|
2021-10-28 21:09:09 +00:00
|
|
|
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:
|
2022-03-24 11:44:13 +00:00
|
|
|
for iname in iname.split(','):
|
|
|
|
if 'default' in netname_to_interfaces:
|
|
|
|
netname_to_interfaces['default']['interfaces'].add(iname)
|
|
|
|
else:
|
|
|
|
netname_to_interfaces['default'] = {'interfaces': set([iname]), 'settings': nc['default']}
|
2021-10-28 21:09:09 +00:00
|
|
|
for netname in nc.get('extranets', {}):
|
|
|
|
uname = '_' + netname
|
|
|
|
iname = get_interface_name(idxmap[curridx], nc['extranets'][netname])
|
|
|
|
if iname:
|
2022-03-24 11:44:13 +00:00
|
|
|
for iname in iname.split(','):
|
|
|
|
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]}
|
2021-10-28 21:09:09 +00:00
|
|
|
doneidxs.add(curridx)
|
2023-02-14 19:41:50 +00:00
|
|
|
if 'default' in netname_to_interfaces:
|
|
|
|
for netn in netname_to_interfaces:
|
|
|
|
if netn == 'default':
|
|
|
|
continue
|
|
|
|
netname_to_interfaces['default']['interfaces'] -= netname_to_interfaces[netn]['interfaces']
|
|
|
|
if not netname_to_interfaces['default']['interfaces']:
|
|
|
|
del netname_to_interfaces['default']
|
2021-10-28 21:09:09 +00:00
|
|
|
rm_tmp_llas(tmpllas)
|
2023-09-01 14:07:51 +00:00
|
|
|
if os.path.exists('/usr/sbin/netplan'):
|
|
|
|
nm = NetplanManager()
|
2021-11-02 18:04:11 +00:00
|
|
|
if os.path.exists('/usr/bin/nmcli'):
|
|
|
|
nm = NetworkManager(devtypes)
|
|
|
|
elif os.path.exists('/usr/sbin/wicked'):
|
|
|
|
nm = WickedManager()
|
2021-10-29 21:16:16 +00:00
|
|
|
for netn in netname_to_interfaces:
|
|
|
|
nm.apply_configuration(netname_to_interfaces[netn])
|
2021-11-02 13:43:42 +00:00
|
|
|
if havefirewall:
|
|
|
|
subprocess.check_call(['systemctl', 'start', 'firewalld'])
|
|
|
|
await_tentative()
|
2021-10-28 21:09:09 +00:00
|
|
|
|