Use the correct MAC-address.
[bootmanager.git] / source / steps / WriteNetworkConfig.py
1 #!/usr/bin/python2
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8 # expected /proc/partitions format
9
10 import os, string
11
12 from Exceptions import *
13 import utils
14 import BootServerRequest
15 import ModelOptions
16 import urlparse
17
18 def Run( vars, log ):
19     """
20     Write out the network configuration for this machine:
21     /etc/hosts
22     /etc/sysconfig/network-scripts/ifcfg-eth0
23     /etc/resolv.conf (if applicable)
24     /etc/sysconfig/network
25
26     The values to be used for the network settings are to be set in vars
27     in the variable 'NETWORK_SETTINGS', which is a dictionary
28     with keys:
29
30      Key               Used by this function
31      -----------------------------------------------
32      node_id
33      node_key
34      method            x
35      ip                x
36      mac               x (optional)
37      gateway           x
38      network           x
39      broadcast         x
40      netmask           x
41      dns1              x
42      dns2              x (optional)
43      hostname          x
44      domainname        x
45
46     Expect the following variables from the store:
47     SYSIMG_PATH             the path where the system image will be mounted
48                             (always starts with TEMP_PATH)
49     NETWORK_SETTINGS  A dictionary of the values from the network
50                                 configuration file
51     NODE_NETWORKS           All the network associated with this node
52     Sets the following variables:
53     None
54     """
55
56     log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
57
58     try:
59         SYSIMG_PATH= vars["SYSIMG_PATH"]
60         if SYSIMG_PATH == "":
61             raise ValueError, "SYSIMG_PATH"
62
63     except KeyError, var:
64         raise BootManagerException, "Missing variable in vars: %s\n" % var
65     except ValueError, var:
66         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
67
68
69     try:
70         network_settings= vars['NETWORK_SETTINGS']
71     except KeyError, e:
72         raise BootManagerException, "No network settings found in vars."
73
74     try:
75         hostname= network_settings['hostname']
76         domainname= network_settings['domainname']
77         method= network_settings['method']
78         ip= network_settings['ip']
79         gateway= network_settings['gateway']
80         network= network_settings['network']
81         netmask= network_settings['netmask']
82         dns1= network_settings['dns1']
83         mac= network_settings['mac']
84     except KeyError, e:
85         raise BootManagerException, "Missing value %s in network settings." % str(e)
86
87     try:
88         dns2= ''
89         dns2= network_settings['dns2']
90     except KeyError, e:
91         pass
92
93
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")
98
99     bs= BootServerRequest.BootServerRequest()
100     if bs.BOOTSERVER_CERTS:
101         print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
102
103     api_url = vars['BOOT_API_SERVER']
104     (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
105     parts = netloc.split(':')
106     host = parts[0]
107     if len(parts) > 1:
108         port = parts[1]
109     else:
110         port = '80'
111     print >> plc_config, "PLC_API_HOST='%s'" % host
112     print >> plc_config, "PLC_API_PORT='%s'" % port
113     print >> plc_config, "PLC_API_PATH='%s'" % path
114
115     plc_config.close()
116
117
118     log.write( "Writing /etc/hosts\n" )
119     hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )    
120     hosts_file.write( "127.0.0.1       localhost\n" )
121     if method == "static":
122         hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
123     hosts_file.close()
124     hosts_file= None
125     
126
127     log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
128     eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
129                     SYSIMG_PATH, "w" )
130     eth0_file.write( "DEVICE=eth0\n" )
131     if method == "static":
132         eth0_file.write( "BOOTPROTO=static\n" )
133         eth0_file.write( "IPADDR=%s\n" % ip )
134         eth0_file.write( "NETMASK=%s\n" % netmask )
135         eth0_file.write( "GATEWAY=%s\n" % gateway )
136     else:
137         eth0_file.write( "BOOTPROTO=dhcp\n" )
138         eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
139     if mac != "":
140         eth0_file.write( "HWADDR=%s\n" % mac )
141     eth0_file.write( "ONBOOT=yes\n" )
142     eth0_file.write( "USERCTL=no\n" )
143     eth0_file.close()
144     eth0_file= None
145
146     if method == "static":
147         log.write( "Writing /etc/resolv.conf\n" )
148         resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
149         if dns1 != "":
150             resolv_file.write( "nameserver %s\n" % dns1 )
151         if dns2 != "":
152             resolv_file.write( "nameserver %s\n" % dns2 )
153         resolv_file.write( "search %s\n" % domainname )
154         resolv_file.close()
155         resolv_file= None
156
157     log.write( "Writing /etc/sysconfig/network\n" )
158     network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
159     network_file.write( "NETWORKING=yes\n" )
160     network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
161     if method == "static":
162         network_file.write( "GATEWAY=%s\n" % gateway )
163     network_file.close()
164     network_file= None
165
166     interface = 1
167     for network in vars['NODE_NETWORKS']:
168         if network['is_primary'] == 1:
169             continue
170         if method == "static" or method == "dhcp":
171             f = file("%s/etc/sysconfig/network-scripts/ifcfg-eth%d" %
172                      (SYSIMG_PATH, interface), "w")
173             f.write("DEVICE=eth%d\n" % interface)
174             f.write("HWADDR=%s\n" % network['mac'])
175             f.write("ONBOOT=yes\n")
176             f.write("USERCTL=no\n")
177             if method == "static":
178                 f.write("BOOTPROTO=static\n")
179                 f.write("IPADDR=%s\n" % network['ip'])
180                 f.write("NETMASK=%s\n" % network['netmask'])
181             elif method == "dhcp":
182                 f.write("BOOTPROTO=dhcp\n")
183                 if network['hostname']:
184                     f.write("DHCP_HOSTNAME=%s\n" % network['hostname'])
185                 else:
186                     f.write("DHCP_HOSTNAME=%s\n" % hostname)
187                 f.write("DHCLIENTARGS='-R subnet-mask'\n")
188             f.close()
189