Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST in /etc/planetlab/plc_config
[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     It is assumed the caller mounted the root partition and the vserver partition
27     starting on SYSIMG_PATH - it is not checked here.
28
29     The values to be used for the network settings are to be set in vars
30     in the variable 'NETWORK_SETTINGS', which is a dictionary
31     with keys:
32
33      Key               Used by this function
34      -----------------------------------------------
35      node_id
36      node_key
37      method            x
38      ip                x
39      mac               x (optional)
40      gateway           x
41      network           x
42      broadcast         x
43      netmask           x
44      dns1              x
45      dns2              x (optional)
46      hostname          x
47      domainname        x
48
49     Expect the following variables from the store:
50     SYSIMG_PATH             the path where the system image will be mounted
51                             (always starts with TEMP_PATH)
52     NETWORK_SETTINGS  A dictionary of the values from the network
53                                 configuration file
54     Sets the following variables:
55     None
56     """
57
58     log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
59
60     try:
61         SYSIMG_PATH= vars["SYSIMG_PATH"]
62         if SYSIMG_PATH == "":
63             raise ValueError, "SYSIMG_PATH"
64
65     except KeyError, var:
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
69
70
71     try:
72         network_settings= vars['NETWORK_SETTINGS']
73     except KeyError, e:
74         raise BootManagerException, "No network settings found in vars."
75
76     try:
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']
86     except KeyError, e:
87         raise BootManagerException, "Missing value %s in network settings." % str(e)
88
89     try:
90         dns2= ''
91         dns2= network_settings['dns2']
92     except KeyError, e:
93         pass
94
95
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")
100
101     bs= BootServerRequest.BootServerRequest()
102     if bs.BOOTSERVER_CERTS:
103         print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
104
105     api_url = vars['BOOT_API_SERVER']
106     (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
107     parts = netloc.split(':')
108     host = parts[0]
109     if len(parts) > 1:
110         port = parts[1]
111     else:
112         port = '80'
113     print >> plc_config, "PLC_API_HOST='%s'" % host
114     print >> plc_config, "PLC_API_PORT='%s'" % port
115     print >> plc_config, "PLC_API_PATH='%s'" % path
116
117     plc_config.close()
118
119
120     log.write( "Writing /etc/hosts\n" )
121     hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )    
122     hosts_file.write( "127.0.0.1       localhost\n" )
123     if method == "static":
124         hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
125     hosts_file.close()
126     hosts_file= None
127     
128
129     log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
130     eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
131                     SYSIMG_PATH, "w" )
132     eth0_file.write( "DEVICE=eth0\n" )
133     if method == "static":
134         eth0_file.write( "BOOTPROTO=static\n" )
135         eth0_file.write( "IPADDR=%s\n" % ip )
136         eth0_file.write( "NETMASK=%s\n" % netmask )
137         eth0_file.write( "GATEWAY=%s\n" % gateway )
138     else:
139         eth0_file.write( "BOOTPROTO=dhcp\n" )
140         eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
141     if mac != "":
142         eth0_file.write( "HWADDR=%s\n" % mac )
143     eth0_file.write( "ONBOOT=yes\n" )
144     eth0_file.write( "USERCTL=no\n" )
145     eth0_file.close()
146     eth0_file= None
147
148     if method == "static":
149         log.write( "Writing /etc/resolv.conf\n" )
150         resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
151         if dns1 != "":
152             resolv_file.write( "nameserver %s\n" % dns1 )
153         if dns2 != "":
154             resolv_file.write( "nameserver %s\n" % dns2 )
155         resolv_file.write( "search %s\n" % domainname )
156         resolv_file.close()
157         resolv_file= None
158
159     log.write( "Writing /etc/sysconfig/network\n" )
160     network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
161     network_file.write( "NETWORKING=yes\n" )
162     network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
163     if method == "static":
164         network_file.write( "GATEWAY=%s\n" % gateway )
165     network_file.close()
166     network_file= None