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