Home | Trees | Indices | Help |
---|
|
1 # 2 # Uncomplicated VM Builder 3 # Copyright (C) 2007-2009 Canonical Ltd. 4 # 5 # See AUTHORS for list of contributors 6 # 7 # This program is free software: you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License version 3, as 9 # published by the Free Software Foundation. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # 19 # Virtual network management 20 21 import logging 22 import re 23 import struct 24 import socket 25 26 from VMBuilder import register_hypervisor_plugin, register_distro_plugin 27 from VMBuilder.plugins import Plugin 28 from VMBuilder.exception import VMBuilderUserError 2931 valid_mac_address = re.compile("^([0-9a-f]{2}:){5}([0-9a-f]{2})$", re.IGNORECASE) 32 if not valid_mac_address.match(mac): 33 return False 34 else: 35 return True36 3941 try: 42 return struct.unpack('I', socket.inet_aton(dotted_ip))[0] 43 except socket.error: 44 raise VMBuilderUserError('%s is not a valid ip address' % dotted_ip)4547 first_octet = numip & 0xFF 48 49 if (first_octet > 0) and (first_octet <= 127): 50 return 0xFF 51 elif (first_octet > 128) and (first_octet < 192): 52 return 0xFFFF 53 elif (first_octet < 224): 54 return 0xFFFFFF 55 else: 56 raise VMBuilderUserError('Could not guess network class of: %s' % numeric_to_dotted_ip(numip))57 60 63 667769 group = self.setting_group('Network') 70 domainname = '.'.join(socket.gethostbyname_ex(socket.gethostname())[0].split('.')[1:]) or "defaultdomain" 71 group.add_setting('domain', metavar='DOMAIN', default=domainname, help='Set DOMAIN as the domain name of the guest [default: %default].')7274 domain = self.context.get_setting('domain') 75 if domain == '': 76 raise VMBuilderUserError('Domain is undefined and host has no domain set.')156 157 register_distro_plugin(NetworkDistroPlugin) 158 register_hypervisor_plugin(NetworkHypervisorPlugin) 15980 group = self.setting_group('Network') 81 group.add_setting('ip', metavar='ADDRESS', default='dhcp', help='IP address in dotted form [default: %default].') 82 group.add_setting('mac', metavar='MAC', help='MAC address of the guest [default: random].') 83 group.add_setting('mask', metavar='VALUE', help='IP mask in dotted form [default: based on ip setting]. Ignored if ip is not specified.') 84 group.add_setting('net', metavar='ADDRESS', help='IP net address in dotted form [default: based on ip setting]. Ignored if ip is not specified.') 85 group.add_setting('bcast', metavar='VALUE', help='IP broadcast in dotted form [default: based on ip setting]. Ignored if ip is not specified.') 86 group.add_setting('gw', metavar='ADDRESS', help='Gateway (router) address in dotted form [default: based on ip setting (first valid address in the network)]. Ignored if ip is not specified.') 87 group.add_setting('dns', metavar='ADDRESS', help='DNS address in dotted form [default: based on ip setting (first valid address in the network)] Ignored if ip is not specified.')88 8991 """ 92 Validate the ip configuration given and set defaults 93 """ 94 95 ip = self.context.get_setting('ip') 96 logging.debug("ip: %s" % ip) 97 98 mac = self.context.get_setting('mac') 99 if mac: 100 if not validate_mac(mac): 101 raise VMBuilderUserError("Malformed MAC address entered: %s" % mac) 102 103 if ip != 'dhcp': 104 # num* are numeric representations 105 numip = dotted_to_numeric_ip(ip) 106 107 mask = self.context.get_setting('mask') 108 if not mask: 109 nummask = guess_mask_from_ip(numip) 110 else: 111 nummask = dotted_to_numeric_ip(mask) 112 113 numnet = calculate_net_address_from_ip_and_netmask(numip, nummask) 114 115 net = self.context.get_setting('net') 116 if not net: 117 self.context.set_setting_default('net', numeric_to_dotted_ip(numnet)) 118 119 bcast = self.context.get_setting('bcast') 120 if not bcast: 121 numbcast = calculate_broadcast_address_from_ip_and_netmask(numnet, nummask) 122 self.context.set_setting_default('bcast', numeric_to_dotted_ip(numbcast)) 123 124 gw = self.context.get_setting('gw') 125 if not gw: 126 numgw = guess_gw_from_ip(numip) 127 self.context.set_setting_default('gw', numeric_to_dotted_ip(numgw)) 128 129 dns = self.context.get_setting('dns') 130 if not dns: 131 self.context.set_setting_default('dns', self.context.get_setting('gw')) 132 133 self.context.set_setting_default('mask', numeric_to_dotted_ip(nummask)) 134 135 logging.debug("net: %s" % self.context.get_setting('net')) 136 logging.debug("netmask: %s" % self.context.get_setting('mask')) 137 logging.debug("broadcast: %s" % self.context.get_setting('bcast')) 138 logging.debug("gateway: %s" % self.context.get_setting('gw')) 139 logging.debug("dns: %s" % self.context.get_setting('dns'))140142 if len(nics) > 0: 143 nic = nics[0] 144 145 ip = self.get_setting('ip') 146 if ip == 'dhcp': 147 nic.type = 'dhcp' 148 else: 149 nic.type = 'static' 150 nic.ip = ip 151 nic.network = self.context.get_setting('net') 152 nic.netmask = self.context.get_setting('mask') 153 nic.broadcast = self.context.get_setting('bcast') 154 nic.gateway = self.context.get_setting('gw') 155 nic.dns = self.context.get_setting('dns')
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Tue Aug 16 14:11:30 2011 | http://epydoc.sourceforge.net |