check in all bootmanager sources
[bootmanager.git] / source / steps / UpdateNodeConfiguration.py
1 import os
2
3 import InstallWriteConfig
4 import InstallBuildVServer
5 from Exceptions import *
6 import utils
7
8
9
10 def Run( vars, log ):
11     """
12     Reconfigure a node if necessary, including rewriting any network init
13     scripts based on what PLC has. Also, update any slivers on the machine
14     incase their network files are out of date (primarily /etc/hosts).
15
16     This step expects the root to be already mounted on SYSIMG_PATH.
17     
18     Except the following keys to be set:
19     SYSIMG_PATH              the path where the system image will be mounted
20                              (always starts with TEMP_PATH)
21     ROOT_MOUNTED             the node root file system is mounted
22     NETWORK_SETTINGS  A dictionary of the values from the network
23                                 configuration file
24     """
25     
26     log.write( "\n\nStep: Updating node configuration.\n" )
27
28     # make sure we have the variables we need
29     try:
30         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
31         if NETWORK_SETTINGS == "":
32             raise ValueError, "NETWORK_SETTINGS"
33
34         SYSIMG_PATH= vars["SYSIMG_PATH"]
35         if SYSIMG_PATH == "":
36             raise ValueError, "SYSIMG_PATH"
37
38         ROOT_MOUNTED= vars["ROOT_MOUNTED"]
39         if ROOT_MOUNTED == "":
40             raise ValueError, "ROOT_MOUNTED"
41
42     except KeyError, var:
43         raise BootManagerException, "Missing variable in vars: %s\n" % var
44     except ValueError, var:
45         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
46
47     try:
48         ip= NETWORK_SETTINGS['ip']
49         method= NETWORK_SETTINGS['method']
50         hostname= NETWORK_SETTINGS['hostname']
51         domainname= NETWORK_SETTINGS['domainname']
52     except KeyError, var:
53         raise BootManagerException, \
54               "Missing network value %s in var NETWORK_SETTINGS\n" % var
55
56     
57     if not ROOT_MOUNTED:
58         raise BootManagerException, "Root isn't mounted on SYSIMG_PATH\n"
59
60
61     log.write( "Updating node network configuration\n" )
62     InstallWriteConfig.write_network_configuration( vars, log )
63
64
65     log.write( "Updating vserver's /etc/hosts and /etc/resolv.conf files\n" )
66
67     # create a list of the full directory paths of all the vserver images that
68     # need to be updated.
69     update_path_list= []
70
71     for base_dir in ('/vservers','/vservers/.vcache'):
72         try:
73             full_dir_path= "%s/%s" % (SYSIMG_PATH,base_dir)
74             slices= os.listdir( full_dir_path )
75
76             try:
77                 slices.remove("lost+found")
78             except ValueError, e:
79                 pass
80             
81             update_path_list= update_path_list + map(lambda x: \
82                                                      full_dir_path+"/"+x,
83                                                      slices)
84         except OSError, e:
85             continue
86
87
88     log.write( "Updating network configuration in:\n" )
89     if len(update_path_list) == 0:
90         log.write( "No vserver images found to update.\n" )
91     else:
92         for base_dir in update_path_list:
93             log.write( "%s\n" % base_dir )
94
95
96     # now, update /etc/hosts and /etc/resolv.conf in each dir if
97     # the update flag is there
98     for base_dir in update_path_list:
99         InstallBuildVServer.update_vserver_network_files(base_dir,vars,log)
100         
101     return