def ipv4_net(): import virtinst.util as util from polls.IPy import IP ipv4 = [] xml_forward = net.XMLDesc(0) fw = util.get_xml_path(xml_forward, "/network/forward/@mode") forwardDev = util.get_xml_path(xml_forward, "/network/forward/@dev") ipv4.append([fw, forwardDev]) xml_net = net.XMLDesc(0) addrStr = util.get_xml_path(xml_net, "/network/ip/@address") netmaskStr = util.get_xml_path(xml_net, "/network/ip/@netmask") netmask = IP(netmaskStr) gateway = IP(addrStr) network = IP(gateway.int() & netmask.int()) ipv4.append(IP(str(network) + "/" + netmaskStr)) xml_dhcp = net.XMLDesc(0) dhcpstart = util.get_xml_path(xml_dhcp, "/network/ip/dhcp/range[1]/@start") dhcpend = util.get_xml_path(xml_dhcp, "/network/ip/dhcp/range[1]/@end") if not dhcpstart or not dhcpend: pass else: ipv4.append([IP(dhcpstart), IP(dhcpend)]) return ipv4
def network(request, host_id, pool): """ Networks block """ from libvirt import libvirtError if not request.user.is_authenticated(): return HttpResponseRedirect('/login') def all_networks(): virtnet = {} for network in conn.listNetworks(): net = conn.networkLookupByName(network) status = net.isActive() virtnet[network] = status for network in conn.listDefinedNetworks(): net = conn.networkLookupByName(network) status = net.isActive() virtnet[network] = status return virtnet def add_new_pool(name, forward, gw, netmask, dhcp): xml = """ <network> <name>%s</name>""" % (name) if forward == "nat" or "route": xml += """<forward mode='%s'/>""" % (forward) xml += """<bridge stp='on' delay='0' /> <ip address='%s' netmask='%s'>""" % (gw, netmask) if dhcp[0] == '1': xml += """<dhcp> <range start='%s' end='%s' /> </dhcp>""" % (dhcp[1], dhcp[2]) xml += """</ip> </network>""" conn.networkDefineXML(xml) def net_info(): info = [] info.append(net.isActive()) info.append(net.bridgeName()) return info def ipv4_net(): import virtinst.util as util from polls.IPy import IP ipv4 = [] xml_net = net.XMLDesc(0) fw = util.get_xml_path(xml_net, "/network/forward/@mode") forwardDev = util.get_xml_path(xml_net, "/network/forward/@dev") if fw and forwardDev: ipv4.append([fw, forwardDev]) else: ipv4.append(None) # Subnet block addrStr = util.get_xml_path(xml_net, "/network/ip/@address") netmaskStr = util.get_xml_path(xml_net, "/network/ip/@netmask") if addrStr and netmaskStr: netmask = IP(netmaskStr) gateway = IP(addrStr) network = IP(gateway.int() & netmask.int()) ipv4.append(IP(str(network) + "/" + netmaskStr)) else: ipv4.append(None) # DHCP block dhcpstart = util.get_xml_path(xml_net, "/network/ip/dhcp/range[1]/@start") dhcpend = util.get_xml_path(xml_net, "/network/ip/dhcp/range[1]/@end") if not dhcpstart or not dhcpend: pass else: ipv4.append([IP(dhcpstart), IP(dhcpend)]) return ipv4 host = Host.objects.get(id=host_id) conn = libvirt_conn(host) if type(conn) == dict: return HttpResponseRedirect('/overview/%s/' % host_id) else: networks = all_networks() if pool == None: if len(networks) == 0: return HttpResponseRedirect('/network/%s/add/' % (host_id)) else: return HttpResponseRedirect('/network/%s/%s/' % (host_id, networks.keys()[0])) if pool == 'add': if request.method == 'POST': if 'addpool' in request.POST: dhcp = [] pool_name = request.POST.get('name', '') net_addr = request.POST.get('net_addr', '') forward = request.POST.get('forward', '') dhcp.append(request.POST.get('dhcp', '')) import re errors = [] name_have_simbol = re.search('[^a-zA-Z0-9\_]+', pool_name) ip_have_simbol = re.search('[^0-9\.\/]+', net_addr) if not pool_name: msg = 'No pool name has been entered' errors.append(msg) elif len(pool_name) > 12: msg = 'The host name must not exceed 20 characters' errors.append(msg) else: if name_have_simbol: msg = 'The pool name must not contain any characters and Russian characters' errors.append(msg) if not net_addr: msg = 'No subnet has been entered' errors.append(msg) elif ip_have_simbol: msg = 'The pool name must not contain any characters' errors.append(msg) if pool_name in networks.keys(): msg = 'Pool name already use' errors.append(msg) try: from polls.IPy import IP netmask = IP(net_addr).strNetmask() ipaddr = IP(net_addr) gateway = ipaddr[0].strNormal()[-1] if gateway == '0': gw = ipaddr[1].strNormal() dhcp_start = ipaddr[2].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() else: gw = ipaddr[0].strNormal() dhcp_start = ipaddr[1].strNormal() end = ipaddr.len() - 2 dhcp_end = ipaddr[end].strNormal() dhcp.append(dhcp_start) dhcp.append(dhcp_end) except: msg = 'Input subnet pool error' errors.append(msg) if not errors: try: add_new_pool(pool_name, forward, gw, netmask, dhcp) net = conn.networkLookupByName(pool_name) net.create() net.setAutostart(1) msg = 'Create network pool: %s' % pool_name return HttpResponseRedirect('/network/%s/%s/' % (host_id, pool_name)) except libvirtError as error_msg: errors.append(error_msg.message) else: all_vm = get_all_vm(conn) net = conn.networkLookupByName(pool) info = net_info() if info[0] == True: ipv4_net = ipv4_net() if request.method == 'POST': if 'start' in request.POST: try: net.create() return HttpResponseRedirect('/network/%s/%s' % (host_id, pool)) except libvirtError as error_msg: errors.append(error_msg.message) if 'stop' in request.POST: try: net.destroy() except libvirtError as error_msg: errors.append(error_msg.message) return HttpResponseRedirect('/network/%s/%s' % (host_id, pool)) if 'delete' in request.POST: try: net.undefine() except libvirtError as error_msg: errors.append(error_msg.message) return HttpResponseRedirect('/network/%s/' % host_id) conn.close() return render_to_response('network.html', locals(), context_instance=RequestContext(request))