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