Ejemplo n.º 1
0
def get_routing_table_from_ns(namespace):
    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)
        ns = cfg.namespaces[namespace]
        return jsonify([route.to_json() for route in ns.routes])
    else:
        try:
            routes = util.list_routes(namespace)
            return jsonify(routes)
        except subprocess.CalledProcessError as e:
            LOGGER.exception("status: %s, out: %s, err: %s", e.returncode,
                             e.stdout, e.stderr)
            return jsonify({})
Ejemplo n.º 2
0
def add_route_from_ns(namespace):
    namespace = util.process_namespace(namespace, allow_none=True)

    subnet = request.get_json(force=True).get("subnet")
    gateway = request.get_json(force=True).get("gateway", None)
    interface = request.get_json(force=True).get("interface", None)
    if gateway is None and interface is None:
        raise exceptions.ServerError("gateway or interface should be set.")

    if app.simulate:
        cfg = get_config()
        if namespace not in cfg.namespaces:
            abort(404)

        ns = cfg.namespaces[namespace]
        route = Route(subnet, gateway, interface)
        if route in ns.routes:
            abort(409)
        else:
            ns.routes.append(route)
        return jsonify([route.to_json() for route in ns.routes])
    else:
        try:
            util.run_in_ns(
                namespace,
                [
                    "ip",
                    "route",
                    "add",
                    subnet,
                    *(["via", gateway] if gateway is not None else []),
                    *(["dev", interface] if interface is not None else []),
                ],
            )
            routes = util.list_routes(namespace)
            return jsonify(routes)
        except subprocess.CalledProcessError as e:
            LOGGER.exception("status: %s, out: %s, err: %s", e.returncode, e.stdout, e.stderr)
            return jsonify({})