Detangled steps. No step makes calls into another step.
[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 BootAPI
15 import ModelOptions
16
17 def Run( vars, log ):
18     """
19     Write out the network configuration for this machine:
20     /etc/hosts
21     /etc/sysconfig/network-scripts/ifcfg-eth0
22     /etc/resolv.conf (if applicable)
23     /etc/sysconfig/network
24
25     It is assumed the caller mounted the root partition and the vserver partition
26     starting on SYSIMG_PATH - it is not checked here.
27
28     The values to be used for the network settings are to be set in vars
29     in the variable 'NETWORK_SETTINGS', which is a dictionary
30     with keys:
31
32      Key               Used by this function
33      -----------------------------------------------
34      node_id
35      node_key
36      method            x
37      ip                x
38      mac               x (optional)
39      gateway           x
40      network           x
41      broadcast         x
42      netmask           x
43      dns1              x
44      dns2              x (optional)
45      hostname          x
46      domainname        x
47
48     Expect the following variables from the store:
49     SYSIMG_PATH             the path where the system image will be mounted
50                             (always starts with TEMP_PATH)
51     NETWORK_SETTINGS  A dictionary of the values from the network
52                                 configuration file
53     Sets the following variables:
54     None
55     """
56
57     log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
58
59     try:
60         SYSIMG_PATH= vars["SYSIMG_PATH"]
61         if SYSIMG_PATH == "":
62             raise ValueError, "SYSIMG_PATH"
63
64     except KeyError, var:
65         raise BootManagerException, "Missing variable in vars: %s\n" % var
66     except ValueError, var:
67         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
68
69
70     try:
71         network_settings= vars['NETWORK_SETTINGS']
72     except KeyError, e:
73         raise BootManagerException, "No network settings found in vars."
74
75     try:
76         hostname= network_settings['hostname']
77         domainname= network_settings['domainname']
78         method= network_settings['method']
79         ip= network_settings['ip']
80         gateway= network_settings['gateway']
81         network= network_settings['network']
82         netmask= network_settings['netmask']
83         dns1= network_settings['dns1']
84         mac= network_settings['mac']
85     except KeyError, e:
86         raise BootManagerException, "Missing value %s in network settings." % str(e)
87
88     try:
89         dns2= ''
90         dns2= network_settings['dns2']
91     except KeyError, e:
92         pass
93
94         
95     log.write( "Writing /etc/hosts\n" )
96     hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )    
97     hosts_file.write( "127.0.0.1       localhost\n" )
98     if method == "static":
99         hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
100     hosts_file.close()
101     hosts_file= None
102     
103
104     log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
105     eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
106                     SYSIMG_PATH, "w" )
107     eth0_file.write( "DEVICE=eth0\n" )
108     if method == "static":
109         eth0_file.write( "BOOTPROTO=static\n" )
110         eth0_file.write( "IPADDR=%s\n" % ip )
111         eth0_file.write( "NETMASK=%s\n" % netmask )
112         eth0_file.write( "GATEWAY=%s\n" % gateway )
113     else:
114         eth0_file.write( "BOOTPROTO=dhcp\n" )
115         eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
116     if mac != "":
117         eth0_file.write( "HWADDR=%s\n" % mac )
118     eth0_file.write( "ONBOOT=yes\n" )
119     eth0_file.write( "USERCTL=no\n" )
120     eth0_file.close()
121     eth0_file= None
122
123     if method == "static":
124         log.write( "Writing /etc/resolv.conf\n" )
125         resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
126         if dns1 != "":
127             resolv_file.write( "nameserver %s\n" % dns1 )
128         if dns2 != "":
129             resolv_file.write( "nameserver %s\n" % dns2 )
130         resolv_file.write( "search %s\n" % domainname )
131         resolv_file.close()
132         resolv_file= None
133
134     log.write( "Writing /etc/sysconfig/network\n" )
135     network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
136     network_file.write( "NETWORKING=yes\n" )
137     network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
138     if method == "static":
139         network_file.write( "GATEWAY=%s\n" % gateway )
140     network_file.close()
141     network_file= None