3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
12 from Exceptions import *
14 import BootServerRequest
22 Write out the network configuration for this machine:
24 /etc/sysconfig/network-scripts/ifcfg-eth0
25 /etc/resolv.conf (if applicable)
26 /etc/sysconfig/network
28 The values to be used for the network settings are to be set in vars
29 in the variable 'NETWORK_SETTINGS', which is a dictionary
32 Key Used by this function
33 -----------------------------------------------
48 Expect the following variables from the store:
49 SYSIMG_PATH the path where the system image will be mounted
50 (always starts with TEMP_PATH)
51 NETWORK_SETTINGS A dictionary of the values from the network
53 NODE_NETWORKS All the network associated with this node
54 Sets the following variables:
58 log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
61 SYSIMG_PATH= vars["SYSIMG_PATH"]
63 raise ValueError, "SYSIMG_PATH"
66 raise BootManagerException, "Missing variable in vars: %s\n" % var
67 except ValueError, var:
68 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
72 network_settings= vars['NETWORK_SETTINGS']
74 raise BootManagerException, "No network settings found in vars."
77 hostname= network_settings['hostname']
78 domainname= network_settings['domainname']
79 method= network_settings['method']
80 ip= network_settings['ip']
81 gateway= network_settings['gateway']
82 network= network_settings['network']
83 netmask= network_settings['netmask']
84 dns1= network_settings['dns1']
85 mac= network_settings['mac']
87 raise BootManagerException, "Missing value %s in network settings." % str(e)
91 dns2= network_settings['dns2']
96 # Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST
97 log.write("Writing /etc/planetlab/plc_config\n")
98 utils.makedirs("%s/etc/planetlab" % SYSIMG_PATH)
99 plc_config = file("%s/etc/planetlab/plc_config" % SYSIMG_PATH, "w")
101 api_url = vars['BOOT_API_SERVER']
102 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
103 parts = netloc.split(':')
110 log.write("getting via https://%s/PlanetLabConf/get_plc_config.php" % host)
111 bootserver = httplib.HTTPSConnection(host, port)
113 bootserver.request("GET","https://%s/PlanetLabConf/get_plc_config.php" % host)
114 plc_config.write("%s" % bootserver.getresponse().read())
117 log.write("Failed. Using old method.")
118 bs= BootServerRequest.BootServerRequest()
119 if bs.BOOTSERVER_CERTS:
120 print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
121 print >> plc_config, "PLC_API_HOST='%s'" % host
122 print >> plc_config, "PLC_API_PORT='%s'" % port
123 print >> plc_config, "PLC_API_PATH='%s'" % path
128 log.write( "Writing /etc/hosts\n" )
129 hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )
130 hosts_file.write( "127.0.0.1 localhost\n" )
131 if method == "static":
132 hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
137 if method == "static":
138 log.write( "Writing /etc/resolv.conf\n" )
139 resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
141 resolv_file.write( "nameserver %s\n" % dns1 )
143 resolv_file.write( "nameserver %s\n" % dns2 )
144 resolv_file.write( "search %s\n" % domainname )
148 log.write( "Writing /etc/sysconfig/network\n" )
149 network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
150 network_file.write( "NETWORKING=yes\n" )
151 network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
152 if method == "static":
153 network_file.write( "GATEWAY=%s\n" % gateway )
159 for network in vars['NODE_NETWORKS']:
160 if method == "static" or method == "dhcp":
161 if network['is_primary'] == 1:
169 int['HWADDR'] = network['mac']
171 if network['method'] == "static":
172 int['BOOTPROTO'] = "static"
173 int['IPADDR'] = network['ip']
174 int['NETMASK'] = network['netmask']
176 elif network['method'] == "dhcp":
177 int['BOOTPROTO'] = "dhcp"
178 if network['hostname']:
179 int['DHCP_HOSTNAME'] = network['hostname']
181 int['DHCP_HOSTNAME'] = hostname
182 if not network['is_primary']:
183 int['DHCLIENTARGS'] = "-R subnet-mask"
187 if len(network['nodenetwork_setting_ids']) > 0:
188 settings = BootAPI.call_api_function(vars, "GetNodeNetworkSettings",
189 ({'nodenetwork_setting_id': network['nodenetwork_setting_ids']},))
190 for setting in settings:
191 # to explicitly set interface name
192 if setting['name'].upper() == "IFNAME":
193 ifname=setting['value']
194 elif setting['name'].upper() == "DRIVER":
195 # xxx not sure how to do that yet - probably add a line in modprobe.conf
197 elif setting['name'].upper() == "ALIAS":
198 alias = ":" + setting['value']
200 # a hack for testing before a new setting is hardcoded here
201 # use the backdoor setting and put as a value 'var=value'
202 elif setting['name'].upper() == "BACKDOOR":
203 [var,value]=setting['value'].split('=',1)
206 elif setting['name'].lower() in \
207 [ "mode", "essid", "nw", "freq", "channel", "sens", "rate",
208 "key", "key1", "key2", "key3", "key4", "securitymode",
209 "iwconfig", "iwpriv" ] :
210 int [setting['name'].upper()] = setting['value']
211 int ['TYPE']='Wireless'
213 log.write("Warning - ignored setting named %s\n"%setting['name'])
215 if alias and 'HWADDR' in int:
216 for (dev, i) in interfaces.iteritems():
217 if i['HWADDR'] == int['HWADDR']:
220 interfaces[dev + alias] = int
224 ifname="eth%d" % ifnum
227 interfaces[ifname] = int
229 for (dev, int) in interfaces.iteritems():
230 path = "%s/etc/sysconfig/network-scripts/ifcfg-%s" % (
233 log.write("Writing %s\n" % path.replace(SYSIMG_PATH, ""))
235 f.write("DEVICE=%s\n" % dev)
236 f.write("ONBOOT=yes\n")
237 f.write("USERCTL=no\n")
238 for (key, val) in int.iteritems():
239 f.write('%s="%s"\n' % (key, val))