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)
93 dns2= interface_settings['dns2']
98 # Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST
99 log.write("Writing /etc/planetlab/plc_config\n")
100 utils.makedirs("%s/etc/planetlab" % SYSIMG_PATH)
101 plc_config = file("%s/etc/planetlab/plc_config" % SYSIMG_PATH, "w")
103 api_url = vars['BOOT_API_SERVER']
104 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
105 parts = netloc.split(':')
112 log.write("getting via https://%s/PlanetLabConf/get_plc_config.php " % host)
113 bootserver = httplib.HTTPSConnection(host, port)
115 bootserver.request("GET","https://%s/PlanetLabConf/get_plc_config.php" % host)
116 plc_config.write("%s" % bootserver.getresponse().read())
120 log.write(" .. Failed. Using old method. -- stack trace follows\n")
121 traceback.print_exc(file=log.OutputFile)
122 bs= BootServerRequest.BootServerRequest()
123 if bs.BOOTSERVER_CERTS:
124 print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
125 print >> plc_config, "PLC_API_HOST='%s'" % host
126 print >> plc_config, "PLC_API_PORT='%s'" % port
127 print >> plc_config, "PLC_API_PATH='%s'" % path
132 log.write( "Writing /etc/hosts\n" )
133 hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )
134 hosts_file.write( "127.0.0.1 localhost\n" )
135 if method == "static":
136 hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
141 if method == "static":
142 log.write( "Writing /etc/resolv.conf\n" )
143 resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
145 resolv_file.write( "nameserver %s\n" % dns1 )
147 resolv_file.write( "nameserver %s\n" % dns2 )
148 resolv_file.write( "search %s\n" % domainname )
152 log.write( "Writing /etc/sysconfig/network\n" )
153 network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
154 network_file.write( "NETWORKING=yes\n" )
155 network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
156 if method == "static":
157 network_file.write( "GATEWAY=%s\n" % gateway )
163 for interface in vars['INTERFACES']:
164 if method == "static" or method == "dhcp":
165 if interface['is_primary'] == 1:
168 ifnum = interface_count
173 int['HWADDR'] = interface['mac']
175 if interface['method'] == "static":
176 int['BOOTPROTO'] = "static"
177 int['IPADDR'] = interface['ip']
178 int['NETMASK'] = interface['netmask']
180 elif interface['method'] == "dhcp":
181 int['BOOTPROTO'] = "dhcp"
182 if interface['hostname']:
183 int['DHCP_HOSTNAME'] = interface['hostname']
185 int['DHCP_HOSTNAME'] = hostname
186 if not interface['is_primary']:
187 int['DHCLIENTARGS'] = "-R subnet-mask"
191 if len(interface['interface_setting_ids']) > 0:
193 BootAPI.call_api_function(vars, "GetInterfaceSettings",
194 ({'interface_setting_id':
195 interface['interface_setting_ids']},))
196 for setting in settings:
197 # to explicitly set interface name
198 if setting['name'].upper() == "IFNAME":
199 ifname=setting['value']
200 elif setting['name'].upper() == "DRIVER":
201 # xxx not sure how to do that yet - probably add a line in modprobe.conf
203 elif setting['name'].upper() == "ALIAS":
204 alias = ":" + setting['value']
206 # a hack for testing before a new setting is hardcoded here
207 # use the backdoor setting and put as a value 'var=value'
208 elif setting['name'].upper() == "BACKDOOR":
209 [var,value]=setting['value'].split('=',1)
212 elif setting['name'].lower() in \
213 [ "mode", "essid", "nw", "freq", "channel", "sens", "rate",
214 "key", "key1", "key2", "key3", "key4", "securitymode",
215 "iwconfig", "iwpriv" ] :
216 int [setting['name'].upper()] = setting['value']
217 int ['TYPE']='Wireless'
219 log.write("Warning - ignored setting named %s\n"%setting['name'])
221 if alias and 'HWADDR' in int:
222 for (dev, i) in interfaces.iteritems():
223 if i['HWADDR'] == int['HWADDR']:
226 interfaces[dev + alias] = int
230 ifname="eth%d" % ifnum
233 interfaces[ifname] = int
235 for (dev, int) in interfaces.iteritems():
236 path = "%s/etc/sysconfig/network-scripts/ifcfg-%s" % (
239 log.write("Writing %s\n" % path.replace(SYSIMG_PATH, ""))
241 f.write("DEVICE=%s\n" % dev)
242 f.write("ONBOOT=yes\n")
243 f.write("USERCTL=no\n")
244 for (key, val) in int.iteritems():
245 f.write('%s="%s"\n' % (key, val))