b404ce24152a35e5b15eec1b1bb941eabf6c5f35
[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     Also write out /etc/planetlab/session, a random string that gets
17     a new value at every request of BootGetNodeDetails (ie, every boot)
18
19     This step expects the root to be already mounted on SYSIMG_PATH.
20     
21     Except the following keys to be set:
22     SYSIMG_PATH              the path where the system image will be mounted
23                              (always starts with TEMP_PATH)
24     ROOT_MOUNTED             the node root file system is mounted
25     NETWORK_SETTINGS  A dictionary of the values from the network
26                                 configuration file
27     NODE_SESSION             the unique session val set when we requested
28                              the current boot state
29     PLCONF_DIR               The directory to store PL configuration files in
30     """
31     
32     log.write( "\n\nStep: Updating node configuration.\n" )
33
34     # make sure we have the variables we need
35     try:
36         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
37         if NETWORK_SETTINGS == "":
38             raise ValueError, "NETWORK_SETTINGS"
39
40         SYSIMG_PATH= vars["SYSIMG_PATH"]
41         if SYSIMG_PATH == "":
42             raise ValueError, "SYSIMG_PATH"
43
44         ROOT_MOUNTED= vars["ROOT_MOUNTED"]
45         if ROOT_MOUNTED == "":
46             raise ValueError, "ROOT_MOUNTED"
47
48         PLCONF_DIR= vars["PLCONF_DIR"]
49         if PLCONF_DIR == "":
50             raise ValueError, "PLCONF_DIR"
51
52         # its ok if this is blank
53         NODE_SESSION= vars["NODE_SESSION"]
54
55     except KeyError, var:
56         raise BootManagerException, "Missing variable in vars: %s\n" % var
57     except ValueError, var:
58         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
59
60     try:
61         ip= NETWORK_SETTINGS['ip']
62         method= NETWORK_SETTINGS['method']
63         hostname= NETWORK_SETTINGS['hostname']
64         domainname= NETWORK_SETTINGS['domainname']
65     except KeyError, var:
66         raise BootManagerException, \
67               "Missing network value %s in var NETWORK_SETTINGS\n" % var
68
69     
70     if not ROOT_MOUNTED:
71         raise BootManagerException, "Root isn't mounted on SYSIMG_PATH\n"
72
73
74     log.write( "Updating node network configuration\n" )
75     InstallWriteConfig.write_network_configuration( vars, log )
76
77
78     log.write( "Updating vserver's /etc/hosts and /etc/resolv.conf files\n" )
79
80     # create a list of the full directory paths of all the vserver images that
81     # need to be updated.
82     update_path_list= []
83
84     for base_dir in ('/vservers','/vservers/.vcache'):
85         try:
86             full_dir_path= "%s/%s" % (SYSIMG_PATH,base_dir)
87             slices= os.listdir( full_dir_path )
88
89             try:
90                 slices.remove("lost+found")
91             except ValueError, e:
92                 pass
93             
94             update_path_list= update_path_list + map(lambda x: \
95                                                      full_dir_path+"/"+x,
96                                                      slices)
97         except OSError, e:
98             continue
99
100
101     log.write( "Updating network configuration in:\n" )
102     if len(update_path_list) == 0:
103         log.write( "No vserver images found to update.\n" )
104     else:
105         for base_dir in update_path_list:
106             log.write( "%s\n" % base_dir )
107
108
109     # now, update /etc/hosts and /etc/resolv.conf in each dir if
110     # the update flag is there
111     for base_dir in update_path_list:
112         InstallBuildVServer.update_vserver_network_files(base_dir,vars,log)
113
114
115     # write out the session value /etc/planetlab/session
116     try:
117         session_file_path= "%s/%s/session" % (SYSIMG_PATH,PLCONF_DIR)
118         session_file= file( session_file_path, "w" )
119         session_file.write( str(NODE_SESSION) )
120         session_file.close()
121         session_file= None
122         log.write( "Updated /etc/planetlab/session" )
123     except IOError, e:
124         log.write( "Unable to write out /etc/planetlab/session, continuing anyway" )
125     
126     return