Copyright notice for both Intel and Princeton
[bootmanager.git] / source / compatibility.py
1 #!/usr/bin/python2
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 """
11 Various functions that are used to allow the boot manager to run on various
12 different cds are included here
13 """
14
15 import string
16 import os, sys
17
18 from Exceptions import *
19 import utils
20 import BootServerRequest
21
22
23 def setup_lvm_2x_cd( vars, log ):
24     """
25     make available a set of lvm utilities for 2.x cds that don't have them
26     on the cd.
27
28     Expect the following variables to be set:
29     TEMP_PATH                somewhere to store what we need to run
30     BOOT_CD_VERSION          A tuple of the current bootcd version
31     SUPPORT_FILE_DIR         directory on the boot servers containing
32                              scripts and support files
33     LVM_SETUP_2X_CD          indicates if lvm is downloaded and setup for 2.x cds
34     
35     Set the following variables upon successfully running:
36     LVM_SETUP_2X_CD          indicates if lvm is downloaded and setup for 2.x cds
37     """
38     
39     # make sure we have the variables we need
40     try:
41         TEMP_PATH= vars["TEMP_PATH"]
42         if TEMP_PATH == "":
43             raise ValueError, "TEMP_PATH"
44
45         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
46         if BOOT_CD_VERSION == "":
47             raise ValueError, "BOOT_CD_VERSION"
48
49         SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
50         if SUPPORT_FILE_DIR == None:
51             raise ValueError, "SUPPORT_FILE_DIR"
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
59     if BOOT_CD_VERSION[0] != 2:
60         log.write( "Only 2.x boot cds need lvm setup manually.\n" )
61         return 1
62     
63     LVM_SETUP_2X_CD= 0
64     if 'LVM_SETUP_2X_CD' in vars.keys():
65         LVM_SETUP_2X_CD= vars['LVM_SETUP_2X_CD']
66         
67     if LVM_SETUP_2X_CD:
68         log.write( "LVM already downloaded and setup\n" )
69         return 1
70
71     log.write( "Downloading additional libraries for lvm\n" )
72
73     bs_request= BootServerRequest.BootServerRequest()
74         
75     utils.makedirs(TEMP_PATH)
76
77     # download and extract support tarball for this step,
78     # which has everything we need to successfully run
79     step_support_file= "alpina-BootLVM.tar.gz"
80     source_file= "%s/%s" % (SUPPORT_FILE_DIR,step_support_file)
81     dest_file= "%s/%s" % (TEMP_PATH, step_support_file)
82
83     log.write( "Downloading support file for this step\n" )
84     result= bs_request.DownloadFile( source_file, None, None,
85                                      1, 1, dest_file )
86     if not result:
87         raise BootManagerException, "Download failed."
88
89     log.write( "Extracting support files\n" )
90     old_wd= os.getcwd()
91     utils.chdir( TEMP_PATH )
92     utils.sysexec( "tar -C / -xzf %s" % step_support_file, log )
93     utils.removefile( dest_file )
94     utils.chdir( old_wd )
95
96     utils.sysexec( "ldconfig", log )
97
98     # load lvm-mod
99     log.write( "Loading lvm module\n" )
100     utils.sysexec( "modprobe lvm-mod", log )
101
102     # take note that we have lvm setup
103     LVM_SETUP_2X_CD= 1
104     vars['LVM_SETUP_2X_CD']= LVM_SETUP_2X_CD
105
106     return 1
107
108
109
110 def setup_partdisks_2x_cd( vars, log ):
111     """
112     download necessary files to handle partitioning disks on 2.x cds
113
114     Expect the following variables to be set:
115     TEMP_PATH                somewhere to store what we need to run
116     BOOT_CD_VERSION          A tuple of the current bootcd version
117     SUPPORT_FILE_DIR         directory on the boot servers containing
118                              scripts and support files
119     PARTDISKS_SETUP_2X_CD    indicates if lvm is downloaded and setup for 2.x cds
120     
121     Set the following variables upon successfully running:
122     PARTDISKS_SETUP_2X_CD    indicates if lvm is downloaded and setup for 2.x cds
123     """
124
125     # make sure we have the variables we need
126     try:
127         TEMP_PATH= vars["TEMP_PATH"]
128         if TEMP_PATH == "":
129             raise ValueError, "TEMP_PATH"
130
131         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
132         if BOOT_CD_VERSION == "":
133             raise ValueError, "BOOT_CD_VERSION"
134
135         SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
136         if SUPPORT_FILE_DIR == None:
137             raise ValueError, "SUPPORT_FILE_DIR"
138         
139     except KeyError, var:
140         raise BootManagerException, "Missing variable in vars: %s\n" % var
141     except ValueError, var:
142         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
143
144
145     if BOOT_CD_VERSION[0] != 2:
146         log.write( "Only 2.x boot cds need partition disk tools setup manually.\n" )
147         return 1
148
149     PARTDISKS_SETUP_2X_CD= 0
150     if 'PARTDISKS_SETUP_2X_CD' in vars.keys():
151         PARTDISKS_SETUP_2X_CD= vars['PARTDISKS_SETUP_2X_CD']
152
153     if PARTDISKS_SETUP_2X_CD:
154         log.write( "Partition disk tools already downloaded and setup\n" )
155         return 1
156
157     log.write( "Downloading additional libraries for partitioning disks\n" )
158
159     bs_request= BootServerRequest.BootServerRequest()
160
161     # download and extract support tarball for this step,
162     # which has everything we need to successfully run
163     step_support_file= "alpina-PartDisks.tar.gz"
164     source_file= "%s/%s" % (SUPPORT_FILE_DIR,step_support_file)
165     dest_file= "%s/%s" % (TEMP_PATH, step_support_file)
166
167     log.write( "Downloading support file for this step\n" )
168     result= bs_request.DownloadFile( source_file, None, None,
169                                      1, 1, dest_file )
170     if not result:
171         raise BootManagerException, "Download failed."
172
173     log.write( "Extracting support files\n" )
174     old_wd= os.getcwd()
175     utils.chdir( TEMP_PATH )
176     utils.sysexec( "tar -xzf %s" % step_support_file, log )
177     utils.removefile( dest_file )
178     utils.chdir( old_wd )
179
180     # also included in the support package was a list of extra
181     # paths (lib-paths) for /etc/ld.so.conf.
182     # so add those, and rerun ldconfig
183     # so we can make our newly downloaded libraries available
184
185     ldconf_file= file("/etc/ld.so.conf","a+")
186     lib_paths_file= file( TEMP_PATH + "/lib-paths","r")
187
188     for line in lib_paths_file:
189         path= string.strip(line)
190         if path != "":
191             ldconf_file.write( "%s/%s\n" % (TEMP_PATH,path) )
192     ldconf_file.close()
193     lib_paths_file.close()
194
195     utils.sysexec( "ldconfig", log )
196
197     # update the PYTHONPATH to include the python modules in
198     # the support package
199     sys.path.append( TEMP_PATH + "/usr/lib/python2.2" )
200     sys.path.append( TEMP_PATH + "/usr/lib/python2.2/site-packages" )
201
202     # update the environment variable PATH to include
203     # TEMP_PATH/sbin and others there
204     new_paths= ('%s/sbin'% TEMP_PATH,
205                 '%s/bin'% TEMP_PATH,
206                 '%s/user/sbin'% TEMP_PATH,
207                 '%s/user/bin'% TEMP_PATH)
208
209     old_path= os.environ['PATH']
210     os.environ['PATH']= old_path + ":" + string.join(new_paths,":")
211
212     # everything should be setup to import parted. this 
213     # import is just to make sure it'll work when this step
214     # is being run
215     log.write( "Imported parted\n" )
216     try:
217         import parted
218     except ImportError:
219         raise BootManagerException, "Unable to import parted."
220
221     # take note that we have part disks setup
222     PARTDISKS_SETUP_2X_CD= 1
223     vars['PARTDISKS_SETUP_2X_CD']= PARTDISKS_SETUP_2X_CD
224
225
226