2c8ea1b30ec73f0733fe597a5799563c6cebf543
[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     """
28     
29     log.write( "\n\nStep: Updating node configuration.\n" )
30
31     # make sure we have the variables we need
32     try:
33         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
34         if NETWORK_SETTINGS == "":
35             raise ValueError, "NETWORK_SETTINGS"
36
37         SYSIMG_PATH= vars["SYSIMG_PATH"]
38         if SYSIMG_PATH == "":
39             raise ValueError, "SYSIMG_PATH"
40
41         ROOT_MOUNTED= vars["ROOT_MOUNTED"]
42         if ROOT_MOUNTED == "":
43             raise ValueError, "ROOT_MOUNTED"
44
45     except KeyError, var:
46         raise BootManagerException, "Missing variable in vars: %s\n" % var
47     except ValueError, var:
48         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
49
50     try:
51         ip= NETWORK_SETTINGS['ip']
52         method= NETWORK_SETTINGS['method']
53         hostname= NETWORK_SETTINGS['hostname']
54         domainname= NETWORK_SETTINGS['domainname']
55     except KeyError, var:
56         raise BootManagerException, \
57               "Missing network value %s in var NETWORK_SETTINGS\n" % var
58
59     
60     if not ROOT_MOUNTED:
61         raise BootManagerException, "Root isn't mounted on SYSIMG_PATH\n"
62
63
64     log.write( "Updating node network configuration\n" )
65     InstallWriteConfig.write_network_configuration( vars, log )
66
67
68     log.write( "Updating vserver's /etc/hosts and /etc/resolv.conf files\n" )
69
70     # create a list of the full directory paths of all the vserver images that
71     # need to be updated.
72     update_path_list= []
73
74     for base_dir in ('/vservers','/vservers/.vcache'):
75         try:
76             full_dir_path= "%s/%s" % (SYSIMG_PATH,base_dir)
77             slices= os.listdir( full_dir_path )
78
79             try:
80                 slices.remove("lost+found")
81             except ValueError, e:
82                 pass
83             
84             update_path_list= update_path_list + map(lambda x: \
85                                                      full_dir_path+"/"+x,
86                                                      slices)
87         except OSError, e:
88             continue
89
90
91     log.write( "Updating network configuration in:\n" )
92     if len(update_path_list) == 0:
93         log.write( "No vserver images found to update.\n" )
94     else:
95         for base_dir in update_path_list:
96             log.write( "%s\n" % base_dir )
97
98
99     # now, update /etc/hosts and /etc/resolv.conf in each dir if
100     # the update flag is there
101     for base_dir in update_path_list:
102         InstallBuildVServer.update_vserver_network_files(base_dir,vars,log)
103     
104     return