def attach_network_to_router(network_id, router_id, subnet_id): network = Network.objects.get(pk=network_id) router = Router.objects.get(pk=router_id) subnet = Subnet.objects.get(pk=subnet_id) rc = create_rc_by_router(router) begin = datetime.datetime.now() try: LOG.info("Start to attach network[%s] to router[%s]", network.name, router.name) router_inf = neutron.router_add_interface( rc, router.router_id, subnet_id=subnet.subnet_id) except Exception as e: end = datetime.datetime.now() LOG.info("Attach network to router api apply [%s] seconds", \ (end-begin).seconds) LOG.exception("Failed to attach network[%s] to router[%s], " "exception:%s", network.name, router.name, e) network.change_status(NETWORK_STATE_ERROR) else: end = datetime.datetime.now() LOG.info("Attach network to router api apply [%s] seconds", \ (end-begin).seconds) RouterInterface.objects.create( network_id=network_id, router=router, subnet=subnet, user=subnet.user, user_data_center=subnet.user_data_center, os_port_id=router_inf['port_id']) network.change_status(NETWORK_STATE_ACTIVE)
def router_create_task(router=None): rc = create_rc_by_router(router) router_params = {"name": "router-%s" % router.id, "distributed": False, "ha": False} begin = datetime.datetime.now() try: rot = neutron.router_create(rc, **router_params) end = datetime.datetime.now() LOG.info("Create router api apply [%s] seconds", \ (end-begin).seconds) router.router_id = rot.id if router.is_gateway: router_add_gateway_task(router) router.status = NETWORK_STATE_ACTIVE router.save() except Exception as ex: end = datetime.datetime.now() LOG.info("Create router api apply [%s] seconds", \ (end-begin).seconds) router.status = NETWORK_STATE_ERROR router.save() LOG.info("delete router error,id:[%s], msg:[%s]" % (router.id, ex)) raise ex return router
def router_add_gateway_task(router=None): rc = create_rc_by_router(router) # find external network search_opts = {'router:external': True} networks = neutron.network_list(rc, **search_opts) ext_net = filter( lambda n: n.name.lower() == router.user_data_center.data_center. ext_net, networks) ext_net_id = None if ext_net and len(ext_net) > 0: ext_net_id = ext_net[0].id # set external gateway neutron.router_add_gateway(rc, router.router_id, ext_net_id) time.sleep(5) # update cloud db router gateway info os_router = neutron.router_get(rc, router.router_id) ext_fixed_ips = os_router["external_gateway_info"].get( "external_fixed_ips", []) router.gateway = ext_fixed_ips[0].get( "ip_address") if ext_fixed_ips else "---" router.status = NETWORK_STATE_ACTIVE router.is_gateway = True router.save() return True
def router_add_interface_task(router=None, subnet=None, router_interface=None): rc = create_rc_by_router(router) router_inf = neutron.router_add_interface(rc, router.router_id, subnet_id=subnet.subnet_id) router_interface.os_port_id = router_inf["port_id"] router_interface.save() router.status = NETWORK_STATE_ACTIVE router.save()
def router_remove_gateway_task(router=None): if not router: return rc = create_rc_by_router(router) neutron.router_remove_gateway(rc, router.router_id) router.gateway = '' router.status = NETWORK_STATE_ACTIVE router.is_gateway = False router.save()
def router_remove_interface_task(router=None, subnet=None, router_interface=None): rc = create_rc_by_router(router) try: neutron.router_remove_interface(rc, router.router_id, subnet.subnet_id, router_interface.os_port_id) router.status = NETWORK_STATE_ACTIVE router.save() except Exception as e: router_interface.deleted = False router_interface.save() LOG.error("detach network to router error,msg:[%S]" % e) router.status = NETWORK_STATE_ACTIVE router.save()
def router_add_gateway_task(router=None): if not router: return rc = create_rc_by_router(router) LOG.info("Begin set gateway [Router:%s][%s]", router.id, router.name) # 1. find external network search_opts = {'router:external': True} networks = neutron.network_list(rc, **search_opts) ext_net_name = router.user_data_center.data_center.ext_net ext_net = filter(lambda n: n.name.lower() == ext_net_name, networks) ext_net_id = None if ext_net and len(ext_net) > 0: ext_net_id = ext_net[0].id if not ext_net_id: router.set_gateway_failed() LOG.error("No external network [%s] was found!", ext_net_name) return False # 2. set external gateway try: neutron.router_add_gateway(rc, router.router_id, ext_net_id) time.sleep(3) except Exception as ex: router.set_gateway_failed() LOG.exception(ex) return False # 3.update cloud db router gateway info try: os_router = neutron.router_get(rc, router.router_id) if os_router.external_gateway_info: LOG.info("Router [%s][%s] set gateway info [%s]", router.id, router.name, os_router.external_gateway_info) ext_fixed_ips = os_router.external_gateway_info.get( "external_fixed_ips", []) if ext_fixed_ips: router.gateway = ext_fixed_ips[0].get("ip_address", "N/A") router.is_gateway = True else: router.gateway = None router.is_gateway = False router.status = NETWORK_STATE_ACTIVE router.save() except Exception as ex: router.set_gateway_failed() LOG.exception(ex) LOG.info("End set gateway [Router:%s][%s]", router.id, router.name) return True
def router_add_gateway_task(router=None): if not router: return rc = create_rc_by_router(router) LOG.info("Begin set gateway [Router:%s][%s]", router.id, router.name) # 1. find external network search_opts = {'router:external': True} networks = neutron.network_list(rc, **search_opts) ext_net_name = router.user_data_center.data_center.ext_net ext_net = filter(lambda n: n.name.lower() == ext_net_name, networks) ext_net_id = None if ext_net and len(ext_net) > 0: ext_net_id = ext_net[0].id if not ext_net_id: router.set_gateway_failed() LOG.error("No external network [%s] was found!", ext_net_name) return False # 2. set external gateway try: neutron.router_add_gateway(rc, router.router_id, ext_net_id) time.sleep(3) except Exception as ex: router.set_gateway_failed() LOG.exception(ex) return False # 3.update cloud db router gateway info try: os_router = neutron.router_get(rc, router.router_id) if os_router.external_gateway_info: LOG.info("Router [%s][%s] set gateway info [%s]", router.id, router.name, os_router.external_gateway_info) ext_fixed_ips = os_router.external_gateway_info.get("external_fixed_ips", []) if ext_fixed_ips: router.gateway = ext_fixed_ips[0].get("ip_address", "N/A") router.is_gateway = True else: router.gateway = None router.is_gateway = False router.status = NETWORK_STATE_ACTIVE router.save() except Exception as ex: router.set_gateway_failed() LOG.exception(ex) LOG.info("End set gateway [Router:%s][%s]", router.id, router.name) return True
def router_delete_task(router=None): rc = create_rc_by_router(router) LOG.info("delete router,id:[%s],name[%s]" % (router.id, router.name)) try: neutron.router_delete(rc, router.router_id) router.router_id = None router.deleted = True router.save() except Exception as ex: router.status = NETWORK_STATE_ERROR router.save() LOG.info("delete router error,id:[%s],name[%s],msg:[%s]" % (network.id, network.name, ex)) raise ex return network
def router_remove_gateway_task(router=None): if not router: return rc = create_rc_by_router(router) LOG.info("Begin clean gateway [Router:%s][%s]", router.id, router.name) try: neutron.router_remove_gateway(rc, router.router_id) router.gateway = None router.status = NETWORK_STATE_ACTIVE router.is_gateway = False router.save() except Exception as ex: router.status = NETWORK_STATE_ACTIVE router.save() LOG.exception(ex) LOG.info("End clean gateway [Router:%s][%s]", router.id, router.name)
def router_create_task(router=None): rc = create_rc_by_router(router) router_params = {"name": "router-%s" % router.id, "distributed": False, "ha": False} try: rot = neutron.router_create(rc, **router_params) router.router_id = rot.id if router.is_gateway: router_add_gateway_task(router) router.status = NETWORK_STATE_ACTIVE router.save() except Exception as ex: router.status = NETWORK_STATE_ERROR router.save() raise ex return router
def delete_user_router_interface(router=None): rc = create_rc_by_router(router) router_remove_gateway_(router) time.sleep(1) LOG.info("delete router,id:[%s],name[%s]" % (router.id, router.name)) try: neutron.router_delete(rc, router.router_id) router.router_id = None router.deleted = True router.save() except Exception as ex: router.status = NETWORK_STATE_ERROR router.save() LOG.info("delete router error,id:[%s],name[%s],msg:[%s]" % (network.id, network.name, ex)) raise ex return network
def router_add_gateway_task(router=None): rc = create_rc_by_router(router) # find external network search_opts = {'router:external': True} networks = neutron.network_list(rc, **search_opts) ext_net = filter(lambda n: n.name.lower() == router.user_data_center.data_center.ext_net, networks) ext_net_id = None if ext_net and len(ext_net) > 0: ext_net_id = ext_net[0].id # set external gateway neutron.router_add_gateway(rc, router.router_id, ext_net_id) time.sleep(5) # update cloud db router gateway info os_router = neutron.router_get(rc, router.router_id) ext_fixed_ips = os_router["external_gateway_info"].get("external_fixed_ips", []) router.gateway = ext_fixed_ips[0].get("ip_address") if ext_fixed_ips else "---" router.status = NETWORK_STATE_ACTIVE router.is_gateway = True router.save() return True
def router_remove_interface_task(router=None, subnet=None, router_interface=None): rc = create_rc_by_router(router) neutron.router_remove_interface(rc, router.router_id, subnet.subnet_id, router_interface.os_port_id)