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