make BootManager write out /etc/planetlab/node_id
[bootmanager.git] / source / steps / ValidateNodeInstall.py
1 import os
2
3 from Exceptions import *
4 import utils
5 from systeminfo import systeminfo
6 import compatibility
7
8
9 def Run( vars, log ):
10     """
11     See if a node installation is valid. More checks should certainly be
12     done in the future, but for now, make sure that the sym links kernel-boot
13     and initrd-boot exist in /boot
14     
15     Expect the following variables to be set:
16     SYSIMG_PATH              the path where the system image will be mounted
17                              (always starts with TEMP_PATH)
18     BOOT_CD_VERSION          A tuple of the current bootcd version
19     ROOT_MOUNTED             the node root file system is mounted
20     NODE_ID                  The db node_id for this machine
21     PLCONF_DIR               The directory to store the configuration file in
22     
23     Set the following variables upon successfully running:
24     ROOT_MOUNTED             the node root file system is mounted
25     """
26
27     log.write( "\n\nStep: Validating node installation.\n" )
28
29     # make sure we have the variables we need
30     try:
31         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
32         if BOOT_CD_VERSION == "":
33             raise ValueError, "BOOT_CD_VERSION"
34
35         SYSIMG_PATH= vars["SYSIMG_PATH"]
36         if SYSIMG_PATH == "":
37             raise ValueError, "SYSIMG_PATH"
38
39         NODE_ID= vars["NODE_ID"]
40         if NODE_ID == "":
41             raise ValueError, "NODE_ID"
42
43         PLCONF_DIR= vars["PLCONF_DIR"]
44         if PLCONF_DIR == "":
45             raise ValueError, "PLCONF_DIR"
46         
47     except KeyError, var:
48         raise BootManagerException, "Missing variable in vars: %s\n" % var
49     except ValueError, var:
50         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
51
52
53     ROOT_MOUNTED= 0
54     if 'ROOT_MOUNTED' in vars.keys():
55         ROOT_MOUNTED= vars['ROOT_MOUNTED']
56
57     # mount the root system image if we haven't already.
58     # capture BootManagerExceptions during the vgscan/change and mount
59     # calls, so we can return 0 instead
60     if ROOT_MOUNTED == 0:
61         # old cds need extra utilities to run lvm
62         if BOOT_CD_VERSION[0] == 2:
63             compatibility.setup_lvm_2x_cd( vars, log )
64             
65         # simply creating an instance of this class and listing the system
66         # block devices will make them show up so vgscan can find the planetlab
67         # volume group
68         systeminfo().get_block_device_list()
69
70         try:
71             utils.sysexec( "vgscan", log )
72             utils.sysexec( "vgchange -ay planetlab", log )
73         except BootManagerException, e:
74             log.write( "BootManagerException during vgscan/vgchange: %s\n" %
75                        str(e) )
76             return 0
77             
78         utils.makedirs( SYSIMG_PATH )
79
80         try:
81             utils.sysexec( "mount /dev/planetlab/root %s" % SYSIMG_PATH, log )
82             utils.sysexec( "mount /dev/planetlab/vservers %s/vservers" %
83                            SYSIMG_PATH, log )
84         except BootManagerException, e:
85             log.write( "BootManagerException during vgscan/vgchange: %s\n" %
86                        str(e) )
87             return 0
88
89         ROOT_MOUNTED= 1
90         vars['ROOT_MOUNTED']= 1
91         
92     valid= 0
93     
94     if os.access("%s/boot/kernel-boot" % SYSIMG_PATH, os.F_OK | os.R_OK) and \
95            os.access("%s/boot/initrd-boot" % SYSIMG_PATH, os.F_OK | os.R_OK):
96         valid= 1
97
98     if not valid:
99         log.write( "Node does not appear to be installed correctly:\n" )
100         log.write( "missing file /boot/ initrd-boot or kernel-boot\n" )
101         return 0
102
103     # write out the node id to /etc/planetlab/node_id. if this fails, return
104     # 0, indicating the node isn't a valid install.
105     try:
106         node_id_file_path= "%s/%s/node_id" % (SYSIMG_PATH,PLCONF_DIR)
107         node_id_file= file( node_id_file_path, "w" )
108         node_id_file.write( str(NODE_ID) )
109         node_id_file.close()
110         node_id_file= None
111         log.write( "Updated /etc/planetlab/node_id" )
112     except IOError, e:
113         log.write( "Unable to write out /etc/planetlab/node_id" )
114         return 0
115
116     log.write( "Everything appears to be ok\n" )
117     
118     return 1