當前位置: 首頁>>代碼示例>>Python>>正文


Python model.NetworkEnvironment類代碼示例

本文整理匯總了Python中aquilon.aqdb.model.NetworkEnvironment的典型用法代碼示例。如果您正苦於以下問題:Python NetworkEnvironment類的具體用法?Python NetworkEnvironment怎麽用?Python NetworkEnvironment使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了NetworkEnvironment類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: render

 def render(self, session, network, ip, network_environment, all, style,
            type=False, hosts=False, **arguments):
     options = [undefer('comments'), joinedload('location')]
     if hosts or style == "proto":
         options.extend([subqueryload("assignments"),
                         joinedload("assignments.interface"),
                         joinedload("assignments.dns_records"),
                         subqueryload("dynamic_stubs")])
     dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                          network_environment)
     dbnetwork = network and get_network_byname(session, network, dbnet_env,
                                                query_options=options) or None
     dbnetwork = ip and get_network_byip(session, ip, dbnet_env,
                                         query_options=options) or dbnetwork
     q = session.query(Network)
     q = q.filter_by(network_environment=dbnet_env)
     q = q.options(*options)
     if dbnetwork:
         if hosts:
             return NetworkHostList([dbnetwork])
         else:
             return dbnetwork
     if type:
         q = q.filter_by(network_type=type)
     dblocation = get_location(session, **arguments)
     if dblocation:
         childids = dblocation.offspring_ids()
         q = q.filter(Network.location_id.in_(childids))
     q = q.order_by(Network.ip)
     q = q.options(*options)
     if hosts:
         return NetworkHostList(q.all())
     else:
         return SimpleNetworkList(q.all())
開發者ID:jrha,項目名稱:aquilon,代碼行數:34,代碼來源:show_network.py

示例2: get_net_id_from_ip

def get_net_id_from_ip(session, ip, network_environment=None):
    """Requires a session, and will return the Network for a given ip."""
    if ip is None:
        return None

    if isinstance(network_environment, NetworkEnvironment):
        dbnet_env = network_environment
    else:
        dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                             network_environment)

    # Query the last network having an address smaller than the given ip. There
    # is no guarantee that the returned network does in fact contain the given
    # ip, so this must be checked separately.
    subq = session.query(Network.ip)
    subq = subq.filter_by(network_environment=dbnet_env)
    subq = subq.filter(Network.ip <= ip)
    subq = subq.order_by(desc(Network.ip)).limit(1)
    q = session.query(Network)
    q = q.filter_by(network_environment=dbnet_env)
    q = q.filter(Network.ip == subq.as_scalar())
    net = q.first()
    if not net or not ip in net.network:
        raise NotFoundException("Could not determine network containing IP "
                                "address %s." % ip)
    return net
開發者ID:piojo,項目名稱:aquilon,代碼行數:26,代碼來源:network.py

示例3: del_dynamic_range

 def del_dynamic_range(self, session, logger, startip, endip):
     dbnet_env = NetworkEnvironment.get_unique_or_default(session)
     startnet = get_net_id_from_ip(session, startip, dbnet_env)
     endnet = get_net_id_from_ip(session, endip, dbnet_env)
     if startnet != endnet:
         raise ArgumentError("IP addresses %s (%s) and %s (%s) must be "
                             "on the same subnet." %
                             (startip, startnet.ip, endip, endnet.ip))
     q = session.query(ARecord)
     q = q.filter_by(network=startnet)
     q = q.filter(ARecord.ip >= startip)
     q = q.filter(ARecord.ip <= endip)
     q = q.order_by(ARecord.ip)
     q = q.options(joinedload('fqdn'),
                   joinedload('fqdn.aliases'),
                   joinedload('fqdn.srv_records'),
                   joinedload('reverse_ptr'))
     existing = q.all()
     if not existing:
         raise ArgumentError("Nothing found in range.")
     if existing[0].ip != startip:
         raise ArgumentError("No system found with IP address %s." % startip)
     if existing[-1].ip != endip:
         raise ArgumentError("No system found with IP address %s." % endip)
     invalid = [s for s in existing if s.dns_record_type != 'dynamic_stub']
     if invalid:
         raise ArgumentError("The range contains non-dynamic systems:\n" +
                             "\n".join([format(i, "a") for i in invalid]))
     self.del_dynamic_stubs(session, logger, existing)
開發者ID:jrha,項目名稱:aquilon,代碼行數:29,代碼來源:del_dynamic_range.py

示例4: render

    def render(self, session, gateway, ip, netmask, prefixlen,
               network_environment, **arguments):
        dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                             network_environment)
        dbnetwork = get_net_id_from_ip(session, gateway, dbnet_env)

        if netmask:
            dest = IPv4Network("%s/%s" % (ip, netmask))
        else:
            dest = IPv4Network("%s/%s" % (ip, prefixlen))

        q = session.query(StaticRoute)
        q = q.filter_by(network=dbnetwork)
        q = q.filter_by(gateway_ip=gateway)
        q = q.filter_by(dest_ip=dest.ip)
        q = q.filter_by(dest_cidr=dest.prefixlen)

        try:
            dbroute = q.one()
        except NoResultFound:
            raise NotFoundException("Static Route to {0} using gateway {1} "
                                    "not found.".format(dest, gateway))

        session.delete(dbroute)
        session.flush()

        # TODO: refresh affected host templates
        return
開發者ID:jrha,項目名稱:aquilon,代碼行數:28,代碼來源:del_static_route.py

示例5: render

    def render(self, session, dbuser,
               ip, fqdn, network_environment, **arguments):
        dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                             network_environment)
        self.az.check_network_environment(dbuser, dbnet_env)
        if fqdn:
            dbdns_rec = ARecord.get_unique(session, fqdn=fqdn,
                                           dns_environment=dbnet_env.dns_environment,
                                           compel=True)
            ip = dbdns_rec.ip
        elif not ip:
            raise ArgumentError("Please specify either --ip or --fqdn.")

        dbnetwork = get_net_id_from_ip(session, ip, dbnet_env)
        dbrouter = None
        for rtaddr in dbnetwork.routers:
            if rtaddr.ip == ip:
                dbrouter = rtaddr
                break
        if not dbrouter:
            raise NotFoundException("IP address {0} is not a router on "
                                    "{1:l}.".format(ip, dbnetwork))

        map(delete_dns_record, dbrouter.dns_records)
        dbnetwork.routers.remove(dbrouter)
        session.flush()

        # TODO: update the templates of Zebra hosts on the network

        return
開發者ID:piojo,項目名稱:aquilon,代碼行數:30,代碼來源:del_router_address.py

示例6: render

    def render(self, session, gateway, ip, netmask, prefixlen, network_environment, comments, **arguments):
        dbnet_env = NetworkEnvironment.get_unique_or_default(session, network_environment)
        dbnetwork = get_net_id_from_ip(session, gateway, dbnet_env)

        if netmask:
            dest = IPv4Network("%s/%s" % (ip, netmask))
        else:
            dest = IPv4Network("%s/%s" % (ip, prefixlen))

        # TODO: this will have to be changed if we want equal cost multipath
        # etc.
        for route in dbnetwork.static_routes:
            if dest.overlaps(route.destination):
                raise ArgumentError(
                    "{0} already has an overlapping route to "
                    "{1} using gateway {2}.".format(dbnetwork, route.destination, route.gateway_ip)
                )

        route = StaticRoute(
            network=dbnetwork, dest_ip=dest.ip, dest_cidr=dest.prefixlen, gateway_ip=gateway, comments=comments
        )
        session.add(route)
        session.flush()

        # TODO: refresh affected host templates
        return
開發者ID:stdweird,項目名稱:aquilon,代碼行數:26,代碼來源:add_static_route.py

示例7: render

    def render(self, session, ip, fqdn, all, network_environment, **arguments):
        dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                             network_environment)

        q = session.query(RouterAddress)

        q = q.join(Network)
        q = q.filter_by(network_environment=dbnet_env)
        q = q.options(contains_eager('network'))
        q = q.reset_joinpoint()

        q = q.options(undefer(RouterAddress.comments))
        q = q.options(joinedload('location'))
        q = q.options(joinedload('dns_records'))

        if all:
            return q.all()

        if fqdn:
            dbdns_rec = ARecord.get_unique(session, fqdn=fqdn, compel=True)
            ip = dbdns_rec.ip
            errmsg = "named %s" % fqdn
        elif ip:
            errmsg = "with IP address %s" % ip
        else:
            raise ArgumentError("Please specify either --ip or --fqdn.")

        q = q.filter(RouterAddress.ip == ip)

        try:
            return q.one()
        except NoResultFound:
            raise NotFoundException("Router %s not found." % errmsg)
開發者ID:piojo,項目名稱:aquilon,代碼行數:33,代碼來源:show_router_address.py

示例8: render

    def render(self, session, network_environment, **arguments):
        options = [undefer("comments"),
                   joinedload("dns_environment"),
                   undefer("dns_environment.comments")]
        dbnet_env = NetworkEnvironment.get_unique(session, network_environment,
                                                  compel=True,
                                                  query_options=options)

        return dbnet_env
開發者ID:jrha,項目名稱:aquilon,代碼行數:9,代碼來源:show_network_environment_network_environment.py

示例9: render

    def render(self, session, dbuser,
               ip, netmask, prefixlen, network_environment, **arguments):
        if netmask:
            # There must me a faster way, but this is the easy one
            net = IPv4Network("127.0.0.0/%s" % netmask)
            prefixlen = net.prefixlen
        if prefixlen is None or prefixlen < 8 or prefixlen > 31:
            raise ArgumentError("The prefix length must be between 8 and 31.")

        dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                             network_environment)
        self.az.check_network_environment(dbuser, dbnet_env)

        dbnetwork = get_net_id_from_ip(session, ip,
                                       network_environment=dbnet_env)

        if prefixlen >= dbnetwork.cidr:
            raise ArgumentError("The specified --prefixlen must be smaller "
                                "than the current value.")

        # IPv4Network has a supernet() object, but that does not normalize the
        # IP address, i.e. IPv4Network('1.2.3.0/24').supernet() will return
        # IPv4Network('1.2.3.0/23'). Do the normalization manually.
        supernet = dbnetwork.network.supernet(new_prefix=prefixlen)
        supernet = IPv4Network("%s/%d" % (supernet.network, supernet.prefixlen))

        q = session.query(Network)
        q = q.filter_by(network_environment=dbnet_env)
        q = q.filter(and_(Network.ip >= supernet.ip,
                          Network.ip < supernet.broadcast))
        q = q.order_by(Network.ip)
        dbnets = q.all()

        if dbnets[0].ip == supernet.ip:
            dbsuper = dbnets.pop(0)
            dbsuper.cidr = prefixlen
        else:
            # Create a new network, copying the parameters from the one
            # specified on the command line
            dbsuper = Network(name=dbnetwork.name, network=supernet,
                              network_environment=dbnet_env,
                              location=dbnetwork.location,
                              side=dbnetwork.side,
                              comments=dbnetwork.comments)
            session.add(dbsuper)

        for oldnet in dbnets:
            # Delete routers of the old subnets
            for dbrouter in oldnet.routers:
                map(delete_dns_record, dbrouter.dns_records)
            oldnet.routers = []

            fix_foreign_links(session, oldnet, dbsuper)
            session.delete(oldnet)

        session.flush()
開發者ID:jrha,項目名稱:aquilon,代碼行數:56,代碼來源:merge_network.py

示例10: del_dynamic_network

 def del_dynamic_network(self, session, logger, network):
     dbnet_env = NetworkEnvironment.get_unique_or_default(session)
     dbnetwork = Network.get_unique(session, network,
                                    network_environment=dbnet_env,
                                    compel=True)
     q = session.query(DynamicStub)
     q = q.filter_by(network=dbnetwork)
     q = q.order_by(asc(DynamicStub.ip))
     existing = q.all()
     if not existing:
         raise ArgumentError("No dynamic stubs found on network.")
     self.del_dynamic_stubs(session, logger, existing)
開發者ID:jrha,項目名稱:aquilon,代碼行數:12,代碼來源:del_dynamic_range_clearnetwork.py

示例11: render

    def render(self, session, network_environment, dns_environment, comments,
               **arguments):
        validate_nlist_key("network environment", network_environment)
        NetworkEnvironment.get_unique(session, network_environment,
                                      preclude=True)
        dbdns_env = DnsEnvironment.get_unique(session, dns_environment,
                                              compel=True)

        # Currently input.xml lists --building only, but that may change
        location = get_location(session, **arguments)

        dbnet_env = NetworkEnvironment(name=network_environment,
                                       dns_environment=dbdns_env,
                                       location=location, comments=comments)

        if dbdns_env.is_default != dbnet_env.is_default:
            raise ArgumentError("Only the default network environment may be "
                                "associated with the default DNS environment.")

        session.add(dbnet_env)
        session.flush()
        return
開發者ID:piojo,項目名稱:aquilon,代碼行數:22,代碼來源:add_network_environment.py

示例12: render

    def render(self, session, network_environment, **arguments):
        dbnet_env = NetworkEnvironment.get_unique(session, network_environment,
                                                  compel=True)

        if dbnet_env.is_default:
            raise ArgumentError("{0} is the default network environment, "
                                "therefore it cannot be deleted."
                                .format(dbnet_env))
        if session.query(Network).filter_by(network_environment=dbnet_env).first():
            raise ArgumentError("{0} still has networks defined, delete them "
                                "first.".format(dbnet_env))
        session.delete(dbnet_env)
        session.flush()
        return
開發者ID:jrha,項目名稱:aquilon,代碼行數:14,代碼來源:del_network_environment.py

示例13: render

    def render(self, session, network_environment, clear_location, comments,
               **arguments):
        dbnet_env = NetworkEnvironment.get_unique(session, network_environment,
                                                  compel=True)

        # Currently input.xml lists --building only, but that may change
        location = get_location(session, **arguments)
        if location:
            dbnet_env.location = location
        if clear_location:
            dbnet_env.location = None
        if comments is not None:
            dbnet_env.comments = comments
        session.flush()
        return
開發者ID:jrha,項目名稱:aquilon,代碼行數:15,代碼來源:update_network_environment.py

示例14: poll_vlan

    def poll_vlan(self, session, logger, switch, now, ssh_args):
        if not switch.primary_ip:
            raise ArgumentError("Cannot poll VLAN info for {0:l} without "
                                "a registered IP address.".format(switch))
        session.query(ObservedVlan).filter_by(switch=switch).delete()
        session.flush()

        # Restrict operations to the internal network
        dbnet_env = NetworkEnvironment.get_unique_or_default(session)

        args = []
        if ssh_args:
            args.extend(ssh_args)
        args.append(self.config.get("broker", "vlan2net"))
        args.append("-ip")
        args.append(switch.primary_ip)
        out = run_command(args)

        try:
            reader = DictReader(StringIO(out))
            for row in reader:
                vlan = row.get("vlan", None)
                network = row.get("network", None)
                bitmask = row.get("bitmask", None)
                if vlan is None or network is None or bitmask is None or \
                   len(vlan) == 0 or len(network) == 0 or len(bitmask) == 0:
                    logger.info("Missing value for vlan, network or bitmask in "
                                "output line #%d: %s" % (reader.line_num, row))
                    continue
                try:
                    vlan_int = int(vlan)
                except ValueError, e:
                    logger.info("Error parsing vlan number in output "
                                "line #%d: %s error: %s" %
                                (reader.line_num, row, e))
                    continue
                try:
                    network = force_ipv4("network", network)
                except ArgumentError, e:
                    raise InternalError(e)
                try:
                    bitmask_int = int(bitmask)
                except ValueError, e:
                    logger.info("Error parsing bitmask in output "
                                "line #%d: %s error: %s" %
                                (reader.line_num, row, e))
                    continue
開發者ID:jrha,項目名稱:aquilon,代碼行數:47,代碼來源:poll_switch.py

示例15: render

    def render(self, session, dbuser,
               fqdn, building, ip, network_environment, comments, **arguments):
        dbnet_env = NetworkEnvironment.get_unique_or_default(session,
                                                             network_environment)
        self.az.check_network_environment(dbuser, dbnet_env)

        if building:
            dbbuilding = Building.get_unique(session, building, compel=True)
        else:
            dbbuilding = None

        (short, dbdns_domain) = parse_fqdn(session, fqdn)
        dbfqdn = Fqdn.get_or_create(session, name=short,
                                    dns_domain=dbdns_domain,
                                    dns_environment=dbnet_env.dns_environment)
        if ip:
            dbnetwork = get_net_id_from_ip(session, ip, dbnet_env)
            dbdns_rec = ARecord.get_or_create(session, fqdn=dbfqdn, ip=ip,
                                              network=dbnetwork)
        else:
            dbdns_rec = ARecord.get_unique(session, dbfqdn, compel=True)
            ip = dbdns_rec.ip
            dbnetwork = dbdns_rec.network

        assert ip in dbnetwork.network, "IP %s is outside network %s" % (ip,
                                                                         dbnetwork.ip)

        if ip in dbnetwork.router_ips:
            raise ArgumentError("IP address {0} is already present as a router "
                                "for {1:l}.".format(ip, dbnetwork))

        # Policy checks are valid only for internal networks
        if dbnetwork.is_internal:
            if ip >= dbnetwork.first_usable_host or \
               int(ip) - int(dbnetwork.ip) in dbnetwork.reserved_offsets:
                raise ArgumentError("IP address {0} is not a valid router address "
                                    "on {1:l}.".format(ip, dbnetwork))

        dbnetwork.routers.append(RouterAddress(ip=ip, location=dbbuilding,
                                               dns_environment=dbdns_rec.fqdn.dns_environment,
                                               comments=comments))
        session.flush()

        # TODO: update the templates of Zebra hosts on the network

        return
開發者ID:piojo,項目名稱:aquilon,代碼行數:46,代碼來源:add_router_address.py


注:本文中的aquilon.aqdb.model.NetworkEnvironment類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。