0a7750d3d3dccc0167be38195178799c5d327bb8
[bootmanager.git] / source / steps / InstallBootstrapRPM.py
1 # Copyright (c) 2003 Intel Corporation
2 # All rights reserved.
3
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7
8 #     * Redistributions of source code must retain the above copyright
9 #       notice, this list of conditions and the following disclaimer.
10
11 #     * Redistributions in binary form must reproduce the above
12 #       copyright notice, this list of conditions and the following
13 #       disclaimer in the documentation and/or other materials provided
14 #       with the distribution.
15
16 #     * Neither the name of the Intel Corporation nor the names of its
17 #       contributors may be used to endorse or promote products derived
18 #       from this software without specific prior written permission.
19
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
33 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
34 # export regulations applicable in licensee's jurisdiction. Under
35 # CURRENT (May 2000) U.S. export regulations this software is eligible
36 # for export from the U.S. and can be downloaded by or otherwise
37 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
38 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
39 # Afghanistan and any other country to which the U.S. has embargoed
40 # goods and services.
41
42
43
44 import os, sys, string
45 import popen2
46
47 from Exceptions import *
48 import utils
49 import BootServerRequest
50
51
52 def Run( vars, log ):
53     """
54     Download enough files to run rpm and yum from a chroot in
55     the system image directory
56     
57     Expect the following variables from the store:
58     SYSIMG_PATH          the path where the system image will be mounted
59     PARTITIONS           dictionary of generic part. types (root/swap)
60                          and their associated devices.
61     SUPPORT_FILE_DIR     directory on the boot servers containing
62                          scripts and support files
63     NODE_ID              the id of this machine
64     
65     Sets the following variables:
66     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
67                          path
68     """
69
70     log.write( "\n\nStep: Install: Bootstrapping RPM.\n" )
71
72     # make sure we have the variables we need
73     try:
74         SYSIMG_PATH= vars["SYSIMG_PATH"]
75         if SYSIMG_PATH == "":
76             raise ValueError, "SYSIMG_PATH"
77
78         PARTITIONS= vars["PARTITIONS"]
79         if PARTITIONS == None:
80             raise ValueError, "PARTITIONS"
81
82         SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
83         if SUPPORT_FILE_DIR == None:
84             raise ValueError, "SUPPORT_FILE_DIR"
85
86         NODE_ID= vars["NODE_ID"]
87         if NODE_ID == "":
88             raise ValueError, "NODE_ID"
89
90     except KeyError, var:
91         raise BootManagerException, "Missing variable in vars: %s\n" % var
92     except ValueError, var:
93         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
94
95
96     try:
97         # make sure the required partitions exist
98         val= PARTITIONS["root"]
99         val= PARTITIONS["swap"]
100         val= PARTITIONS["vservers"]
101     except KeyError, part:
102         log.write( "Missing partition in PARTITIONS: %s\n" % part )
103         return 0   
104
105     bs_request= BootServerRequest.BootServerRequest()
106     
107     log.write( "turning on swap space\n" )
108     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
109
110     # make sure the sysimg dir is present
111     utils.makedirs( SYSIMG_PATH )
112
113     log.write( "mounting root file system\n" )
114     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
115
116     log.write( "mounting vserver partition in root file system\n" )
117     utils.makedirs( SYSIMG_PATH + "/vservers" )
118     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
119                                                      SYSIMG_PATH), log )
120
121     # download and extract support tarball for
122     # this step, which has everything
123     # we need to successfully run
124     step_support_file= "alpina-BootstrapRPM.tar.bz2"
125     source_file= "%s/%s" % (SUPPORT_FILE_DIR,step_support_file)
126     dest_file= "%s/%s" % (SYSIMG_PATH, step_support_file)
127
128     # 30 is the connect timeout, 7200 is the max transfer time
129     # in seconds (2 hours)
130     log.write( "downloading %s\n" % step_support_file )
131     result= bs_request.DownloadFile( source_file, None, None,
132                                           1, 1, dest_file,
133                                           30, 7200)
134     if not result:
135         raise BootManagerException, "Unable to download %s from server." % \
136               source_file
137
138     log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
139     result= utils.sysexec( "tar -C %s -xpjf %s" % (SYSIMG_PATH,dest_file), log )
140     utils.removefile( dest_file )
141
142     # get the yum configuration file for this node (yum.conf).
143     # this needs to come from the configuration file service,
144     # so, if its a beta node, it'll install the beta rpms from
145     # the beginning. The configuration file service will return
146     # the url for the file we need to request to get the actual
147     # conf file, so two requests need to be made.
148
149     # the only changes we will need to make to it are to change
150     # the cache and log directories, so when we run yum from
151     # the chrooted tempfs mount, it'll cache the rpms on the
152     # sysimg partition
153
154     log.write( "Fetching URL for yum.conf from configuration file service\n" )
155
156     postVars= {"node_id" : NODE_ID,
157                "file" : "/etc/yum.conf"}
158
159     yum_conf_url_file= "/tmp/yumconf.url"
160
161     result= bs_request.DownloadFile(
162         "/db/plnodeconf/getsinglefile.php",
163         None, postVars, 1, 1, yum_conf_url_file)
164     
165     if result == 0:
166         log.write( "Unable to make request to get url for yum.conf\n" )
167         return 0
168
169     try:
170         yum_conf_url= file(yum_conf_url_file,"r").read()
171         yum_conf_url= string.strip(yum_conf_url)
172         if yum_conf_url == "":
173             raise BootManagerException, \
174                   "Downloaded yum configuration file URL is empty."
175     except IOError:
176         raise BootManagerException, \
177               "Unable to open downloaded yum configuration file URL."
178
179     # now, get the actual contents of yum.conf for this node
180     log.write( "Fetching yum.conf contents from configuration file service\n" )
181
182     postVars= {}
183     download_file_loc= "%s/etc/yum.conf" % SYSIMG_PATH
184
185     result= bs_request.DownloadFile( yum_conf_url,
186                                      None, postVars, 1, 1,
187                                      download_file_loc)
188
189     if result == 0:
190         log.write( "Unable to make request to get yum.conf\n" )
191         return 0
192
193     # copy resolv.conf from the base system into our temp dir
194     # so DNS lookups work correctly while we are chrooted
195     log.write( "Copying resolv.conf to temp dir\n" )
196     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
197
198     # mount the boot cd in the temp path, under /mnt/cdrom. this way,
199     # we can use the certs when programs are running
200     # chrooted in the temp path
201     cdrom_mount_point= "%s/mnt/cdrom" % SYSIMG_PATH
202     utils.makedirs( cdrom_mount_point )
203     log.write( "Copying contents of /usr/bootme to /mnt/cdrom\n" )
204     utils.sysexec( "cp -r /usr/bootme %s/mnt/cdrom/" % SYSIMG_PATH, log )
205
206     return 1