* Removed building PlanetLab-Bootstrap.tar.bz2 from the package. It
[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     Sets the following variables:
52     None
53     """
54
55     log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
56
57     try:
58         SYSIMG_PATH= vars["SYSIMG_PATH"]
59         if SYSIMG_PATH == "":
60             raise ValueError, "SYSIMG_PATH"
61
62     except KeyError, var:
63         raise BootManagerException, "Missing variable in vars: %s\n" % var
64     except ValueError, var:
65         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
66
67
68     try:
69         network_settings= vars['NETWORK_SETTINGS']
70     except KeyError, e:
71         raise BootManagerException, "No network settings found in vars."
72
73     try:
74         hostname= network_settings['hostname']
75         domainname= network_settings['domainname']
76         method= network_settings['method']
77         ip= network_settings['ip']
78         gateway= network_settings['gateway']
79         network= network_settings['network']
80         netmask= network_settings['netmask']
81         dns1= network_settings['dns1']
82         mac= network_settings['mac']
83     except KeyError, e:
84         raise BootManagerException, "Missing value %s in network settings." % str(e)
85
86     try:
87         dns2= ''
88         dns2= network_settings['dns2']
89     except KeyError, e:
90         pass
91
92
93     # Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST
94     log.write("Writing /etc/planetlab/plc_config\n")
95     utils.makedirs("%s/etc/planetlab" % SYSIMG_PATH)
96     plc_config = file("%s/etc/planetlab/plc_config" % SYSIMG_PATH, "w")
97
98     bs= BootServerRequest.BootServerRequest()
99     if bs.BOOTSERVER_CERTS:
100         print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
101
102     api_url = vars['BOOT_API_SERVER']
103     (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
104     parts = netloc.split(':')
105     host = parts[0]
106     if len(parts) > 1:
107         port = parts[1]
108     else:
109         port = '80'
110     print >> plc_config, "PLC_API_HOST='%s'" % host
111     print >> plc_config, "PLC_API_PORT='%s'" % port
112     print >> plc_config, "PLC_API_PATH='%s'" % path
113
114     plc_config.close()
115
116
117     log.write( "Writing /etc/hosts\n" )
118     hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )    
119     hosts_file.write( "127.0.0.1       localhost\n" )
120     if method == "static":
121         hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
122     hosts_file.close()
123     hosts_file= None
124     
125
126     log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
127     eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
128                     SYSIMG_PATH, "w" )
129     eth0_file.write( "DEVICE=eth0\n" )
130     if method == "static":
131         eth0_file.write( "BOOTPROTO=static\n" )
132         eth0_file.write( "IPADDR=%s\n" % ip )
133         eth0_file.write( "NETMASK=%s\n" % netmask )
134         eth0_file.write( "GATEWAY=%s\n" % gateway )
135     else:
136         eth0_file.write( "BOOTPROTO=dhcp\n" )
137         eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
138     if mac != "":
139         eth0_file.write( "HWADDR=%s\n" % mac )
140     eth0_file.write( "ONBOOT=yes\n" )
141     eth0_file.write( "USERCTL=no\n" )
142     eth0_file.close()
143     eth0_file= None
144
145     if method == "static":
146         log.write( "Writing /etc/resolv.conf\n" )
147         resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
148         if dns1 != "":
149             resolv_file.write( "nameserver %s\n" % dns1 )
150         if dns2 != "":
151             resolv_file.write( "nameserver %s\n" % dns2 )
152         resolv_file.write( "search %s\n" % domainname )
153         resolv_file.close()
154         resolv_file= None
155
156     log.write( "Writing /etc/sysconfig/network\n" )
157     network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
158     network_file.write( "NETWORKING=yes\n" )
159     network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
160     if method == "static":
161         network_file.write( "GATEWAY=%s\n" % gateway )
162     network_file.close()
163     network_file= None