本文整理汇总了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())
示例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
示例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)
示例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
示例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
示例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
示例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)
示例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
示例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()
示例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)
示例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
示例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
示例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
示例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
示例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