4 # Copyright (c) 2003 Intel Corporation
7 # Copyright (c) 2004-2006 The Trustees of Princeton University
9 # expected /proc/partitions format
18 from Exceptions import *
19 import BootServerRequest
25 Write out the network configuration for this machine:
27 /etc/sysconfig/network-scripts/ifcfg-<ifname>
28 /etc/resolv.conf (if applicable)
29 /etc/sysconfig/network
31 The values to be used for the network settings are to be set in vars
32 in the variable 'INTERFACE_SETTINGS', which is a dictionary
35 Key Used by this function
36 -----------------------------------------------
51 Expect the following variables from the store:
52 SYSIMG_PATH the path where the system image will be mounted
53 (always starts with TEMP_PATH)
54 INTERFACES All the interfaces associated with this node
55 INTERFACE_SETTINGS dictionary
56 Sets the following variables:
60 log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
63 SYSIMG_PATH= vars["SYSIMG_PATH"]
65 raise ValueError, "SYSIMG_PATH"
68 raise BootManagerException, "Missing variable in vars: %s\n" % var
69 except ValueError, var:
70 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
74 interface_settings= vars['INTERFACE_SETTINGS']
76 raise BootManagerException, "No interface settings found in vars."
79 hostname= interface_settings['hostname']
80 domainname= interface_settings['domainname']
81 method= interface_settings['method']
82 ip= interface_settings['ip']
83 gateway= interface_settings['gateway']
84 network= interface_settings['network']
85 netmask= interface_settings['netmask']
86 dns1= interface_settings['dns1']
87 mac= interface_settings['mac']
89 raise BootManagerException, "Missing value %s in interface settings." % str(e)
91 # dns2 is not required to be set
92 dns2 = interface_settings.get('dns2','')
94 # Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST
95 log.write("Writing /etc/planetlab/plc_config\n")
96 utils.makedirs("%s/etc/planetlab" % SYSIMG_PATH)
97 plc_config = file("%s/etc/planetlab/plc_config" % SYSIMG_PATH, "w")
99 api_url = vars['BOOT_API_SERVER']
100 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
101 parts = netloc.split(':')
108 log.write("getting via https://%s/PlanetLabConf/get_plc_config.php " % host)
109 bootserver = httplib.HTTPSConnection(host, int(port))
111 bootserver.request("GET","https://%s/PlanetLabConf/get_plc_config.php" % host)
112 plc_config.write("%s" % bootserver.getresponse().read())
116 log.write(" .. Failed. Using old method. -- stack trace follows\n")
117 traceback.print_exc(file=log.OutputFile)
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 interface in vars['INTERFACES']:
160 if method == "static" or method == "dhcp":
161 if interface['is_primary'] == 1:
164 ifnum = interface_count
169 inter['HWADDR'] = interface['mac']
171 if interface['method'] == "static":
172 inter['BOOTPROTO'] = "static"
173 inter['IPADDR'] = interface['ip']
174 inter['NETMASK'] = interface['netmask']
176 elif interface['method'] == "dhcp":
177 inter['BOOTPROTO'] = "dhcp"
178 inter['DHCP_HOSTNAME'] = interface.get('hostname',hostname)
179 if not interface['is_primary']:
180 inter['DHCLIENTARGS'] = "-R subnet-mask"
184 if len(interface['interface_setting_ids']) > 0:
186 BootAPI.call_api_function(vars, "GetInterfaceSettings",
187 ({'interface_setting_id':
188 interface['interface_setting_ids']},))
189 for setting in settings:
190 # to explicitly set interface name
191 if setting['name'].upper() == "IFNAME":
192 ifname=setting['value']
193 elif setting['name'].upper() == "DRIVER":
194 # xxx not sure how to do that yet - probably add a line in modprobe.conf
196 elif setting['name'].upper() == "ALIAS":
197 alias = ":" + setting['value']
199 # a hack for testing before a new setting is hardcoded here
200 # use the backdoor setting and put as a value 'var=value'
201 elif setting['name'].upper() == "BACKDOOR":
202 [var,value]=setting['value'].split('=',1)
205 elif setting['name'].lower() in \
206 [ "mode", "essid", "nw", "freq", "channel", "sens", "rate",
207 "key", "key1", "key2", "key3", "key4", "securitymode",
208 "iwconfig", "iwpriv" ] :
209 inter [setting['name'].upper()] = setting['value']
210 inter ['TYPE']='Wireless'
212 log.write("Warning - ignored setting named %s\n"%setting['name'])
214 if alias and 'HWADDR' in inter:
215 for (dev, i) in interfaces.iteritems():
216 if i['HWADDR'] == inter['HWADDR']:
219 interfaces[dev + alias] =inter
223 ifname="eth%d" % ifnum
226 interfaces[ifname] =inter
228 for (dev, inter) in interfaces.iteritems():
229 path = "%s/etc/sysconfig/network-scripts/ifcfg-%s" % (
232 log.write("Writing %s\n" % path.replace(SYSIMG_PATH, ""))
234 f.write("DEVICE=%s\n" % dev)
235 f.write("ONBOOT=yes\n")
236 f.write("USERCTL=no\n")
237 for (key, val) in inter.iteritems():
238 f.write('%s="%s"\n' % (key, val))