def obtain_lease(self): """Perform dhcp discovery in a sandboxed environment if possible. @return: A dict representing dhcp options on the most recent lease obtained from the dhclient discovery if run, otherwise an error is raised. @raises: NoDHCPLeaseError if no leases could be obtained. """ if self.lease: return self.lease try: leases = maybe_perform_dhcp_discovery(self.iface) except InvalidDHCPLeaseFileError: raise NoDHCPLeaseError() if not leases: raise NoDHCPLeaseError() self.lease = leases[-1] LOG.debug("Received dhcp lease on %s for %s/%s", self.lease['interface'], self.lease['fixed-address'], self.lease['subnet-mask']) nmap = {'interface': 'interface', 'ip': 'fixed-address', 'prefix_or_mask': 'subnet-mask', 'broadcast': 'broadcast-address', 'router': 'routers'} kwargs = dict([(k, self.lease.get(v)) for k, v in nmap.items()]) if not kwargs['broadcast']: kwargs['broadcast'] = bcip(kwargs['prefix_or_mask'], kwargs['ip']) ephipv4 = EphemeralIPv4Network(**kwargs) ephipv4.__enter__() self._ephipv4 = ephipv4 return self.lease
def obtain_lease(self): """Perform dhcp discovery in a sandboxed environment if possible. @return: A dict representing dhcp options on the most recent lease obtained from the dhclient discovery if run, otherwise an error is raised. @raises: NoDHCPLeaseError if no leases could be obtained. """ if self.lease: return self.lease leases = maybe_perform_dhcp_discovery(self.iface, self.dhcp_log_func) if not leases: raise NoDHCPLeaseError() self.lease = leases[-1] LOG.debug( "Received dhcp lease on %s for %s/%s", self.lease["interface"], self.lease["fixed-address"], self.lease["subnet-mask"], ) nmap = { "interface": "interface", "ip": "fixed-address", "prefix_or_mask": "subnet-mask", "broadcast": "broadcast-address", "static_routes": [ "rfc3442-classless-static-routes", "classless-static-routes", ], "router": "routers", } kwargs = self.extract_dhcp_options_mapping(nmap) if not kwargs["broadcast"]: kwargs["broadcast"] = mask_and_ipv4_to_bcast_addr( kwargs["prefix_or_mask"], kwargs["ip"]) if kwargs["static_routes"]: kwargs["static_routes"] = parse_static_routes( kwargs["static_routes"]) if self.connectivity_url_data: kwargs["connectivity_url_data"] = self.connectivity_url_data ephipv4 = EphemeralIPv4Network(**kwargs) ephipv4.__enter__() self._ephipv4 = ephipv4 return self.lease
def obtain_lease(self): """Perform dhcp discovery in a sandboxed environment if possible. @return: A dict representing dhcp options on the most recent lease obtained from the dhclient discovery if run, otherwise an error is raised. @raises: NoDHCPLeaseError if no leases could be obtained. """ if self.lease: return self.lease try: leases = maybe_perform_dhcp_discovery(self.iface, self.dhcp_log_func) except InvalidDHCPLeaseFileError: raise NoDHCPLeaseError() if not leases: raise NoDHCPLeaseError() self.lease = leases[-1] LOG.debug("Received dhcp lease on %s for %s/%s", self.lease['interface'], self.lease['fixed-address'], self.lease['subnet-mask']) nmap = { 'interface': 'interface', 'ip': 'fixed-address', 'prefix_or_mask': 'subnet-mask', 'broadcast': 'broadcast-address', 'static_routes': ['rfc3442-classless-static-routes', 'classless-static-routes'], 'router': 'routers' } kwargs = self.extract_dhcp_options_mapping(nmap) if not kwargs['broadcast']: kwargs['broadcast'] = bcip(kwargs['prefix_or_mask'], kwargs['ip']) if kwargs['static_routes']: kwargs['static_routes'] = (parse_static_routes( kwargs['static_routes'])) if self.connectivity_url: kwargs['connectivity_url'] = self.connectivity_url ephipv4 = EphemeralIPv4Network(**kwargs) ephipv4.__enter__() self._ephipv4 = ephipv4 return self.lease
def __enter__(self): try: leases = maybe_perform_dhcp_discovery(self.iface) except InvalidDHCPLeaseFileError: raise NoDHCPLeaseError() if not leases: raise NoDHCPLeaseError() lease = leases[-1] LOG.debug("Received dhcp lease on %s for %s/%s", lease['interface'], lease['fixed-address'], lease['subnet-mask']) nmap = { 'interface': 'interface', 'ip': 'fixed-address', 'prefix_or_mask': 'subnet-mask', 'broadcast': 'broadcast-address', 'router': 'routers' } kwargs = dict([(k, lease.get(v)) for k, v in nmap.items()]) if not kwargs['broadcast']: kwargs['broadcast'] = bcip(kwargs['prefix_or_mask'], kwargs['ip']) ephipv4 = EphemeralIPv4Network(**kwargs) ephipv4.__enter__() self._ephipv4 = ephipv4 return lease