Skip to content

Commit

Permalink
add a dispatch to Ephemeral class for is_FreeBSD()
Browse files Browse the repository at this point in the history
start adding FreeBSD support to EphemeralIPv4Network. Very crudely.
  • Loading branch information
igalic committed May 18, 2020
1 parent 8bcf1c0 commit eb85916
Showing 1 changed file with 93 additions and 16 deletions.
109 changes: 93 additions & 16 deletions cloudinit/net/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,15 +1069,52 @@ def __exit__(self, excp_type, excp_value, excp_traceback):
for cmd in self.cleanup_cmds:
util.subp(cmd, capture=True)

def _delete_address(self, address, prefix):
def __delete_address_on_linux(self, cidr):
"""Perform the ip command to remove the specified address."""
util.subp(
['ip', '-family', 'inet', 'addr', 'del',
'%s/%s' % (address, prefix), 'dev', self.interface],
capture=True)
util.subp( ['ip', '-family', 'inet', 'addr', 'del', cidr, 'dev',
self.interface], capture=True)

def _bringup_device(self):
"""Perform the ip comands to fully setup the device."""
def __delete_address_on_freebsd(self, cidr):
"""Perform the ifconfig command to remove the specified address."""
util.subp(['ifconfig', self.interface, 'inet', cidr, 'broadcast',
self.broadcast, 'delete'], capture=True)

def _delete_address(self, address, prefix):
"""Perform the command to remove the specified address."""
cidr = '{0}/{1}'.format(address, prefix)
if util.is_FreeBSD():
self.__delete_address_freebsd(cidr)
else:
self.__delete_address_on_linux(cidr)

def __bringup_device_on_freebsd(self):
"""Perform the ifconfig commands to fully setup the device"""
cidr = '{0}/{1}'.format(self.ip, self.prefix)
LOG.debug(
'Attempting setup of ephemeral network on %s with %s brd %s',
self.interface, cidr, self.broadcast)
try:
util.subp(
['ifconfig', self.interface, 'inet', cidr, 'broadcast',
self.broadcast],
capture=True, update_env={'LANG': 'C'})
except util.ProcessExecutionError as e:
if "File exists" not in e.stderr:
raise
LOG.debug(
'Skip ephemeral network setup, %s already has address %s',
self.interface, self.ip)
else:
# Address creation success, setup queue cleanup
self.cleanup_cmds.append(
['ifconfig', self.interface, 'inet', cidr, 'broadcast',
self.broadcast, 'delete'])
self.cleanup_cmds.append(
['ifconfig', self.interface, 'inet', cidr, 'broadcast',
self.broadcast, 'down'])

def __bringup_device_on_linux(self):
"""Perform the ip commands to fully setup the device."""
cidr = '{0}/{1}'.format(self.ip, self.prefix)
LOG.debug(
'Attempting setup of ephemeral network on %s with %s brd %s',
Expand Down Expand Up @@ -1105,21 +1142,54 @@ def _bringup_device(self):
['ip', '-family', 'inet', 'addr', 'del', cidr, 'dev',
self.interface])

def _bringup_device(self):
"""Perform the commands to fully setup the device."""
if util.is_FreeBSD():
self.__bringup_device_on_freebsd()
else:
self.__bringup_device_on_linux()

def __bringup_static_routes_on_freebsd(self, net_address, gateway):
util.subp(
['route', '-4', 'add', '-net', net_address, gateway], capture=True)
self.cleanup_cmds.insert(
0, ['route', '-4', 'delete', '-net', net_address, gateway], capture=True)

def __bringup_static_routes_on_linux(self, net_address, gateway):
via_arg = ['via', gateway]
util.subp(
['ip', '-4', 'route', 'add', net_address] + via_arg +
['dev', self.interface], capture=True)
self.cleanup_cmds.insert(
0, ['ip', '-4', 'route', 'del', net_address] + via_arg +
['dev', self.interface])

def _bringup_static_routes(self):
# static_routes = [("169.254.169.254/32", "130.56.248.255"),
# ("0.0.0.0/0", "130.56.240.1")]
for net_address, gateway in self.static_routes:
via_arg = []
if gateway != "0.0.0.0/0":
via_arg = ['via', gateway]
util.subp(
['ip', '-4', 'route', 'add', net_address] + via_arg +
['dev', self.interface], capture=True)
self.cleanup_cmds.insert(
0, ['ip', '-4', 'route', 'del', net_address] + via_arg +
['dev', self.interface])
if util.is_FreeBSD():
self.__bringup_static_routes_on_freebsd()
else:
self.__bringup_static_routes_on_linux()

def _bringup_router(self):
def __bringup_router_on_freebsd(self):
"""Perform the commands to fully setup the router if needed."""
# Check if a default route exists and exit if it does
out, _ = util.subp(['route', 'show', 'default'], capture=True)
if 'default' in out:
LOG.debug(
'Skip ephemeral route setup. %s already has default route: %s',
self.interface, out.strip())
return
util.subp(
['route', '-4', 'add', 'default', self.router], capture=True)
self.cleanup_cmds.insert(
0,
['route', '-4', 'delete', 'default', self.router])

def __bringup_router_on_linux(self):
"""Perform the ip commands to fully setup the router if needed."""
# Check if a default route exists and exit if it does
out, _ = util.subp(['ip', 'route', 'show', '0.0.0.0/0'], capture=True)
Expand All @@ -1141,6 +1211,13 @@ def _bringup_router(self):
self.cleanup_cmds.insert(
0, ['ip', '-4', 'route', 'del', 'default', 'dev', self.interface])

def _bringup_router(self):
"""Perform the commands to fully setup the router if needed."""
if util.is_FreeBSD():
self.__bringup_router_on_freebsd()
else:
self.__bringup_router_on_linux()


class RendererNotFoundError(RuntimeError):
pass
Expand Down

0 comments on commit eb85916

Please sign in to comment.