- Copy the boot server certificate(s) and GPG public key to /usr/boot
[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 import shutil
47
48 from Exceptions import *
49 import utils
50 import BootServerRequest
51
52
53 def Run( vars, log ):
54     """
55     Download enough files to run rpm and yum from a chroot in
56     the system image directory
57     
58     Expect the following variables from the store:
59     SYSIMG_PATH          the path where the system image will be mounted
60     PARTITIONS           dictionary of generic part. types (root/swap)
61                          and their associated devices.
62     SUPPORT_FILE_DIR     directory on the boot servers containing
63                          scripts and support files
64     NODE_ID              the id of this machine
65     
66     Sets the following variables:
67     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
68                          path
69     ROOT_MOUNTED         set to 1 when the the base logical volumes
70                          are mounted.
71     """
72
73     log.write( "\n\nStep: Install: Bootstrapping RPM.\n" )
74
75     # make sure we have the variables we need
76     try:
77         SYSIMG_PATH= vars["SYSIMG_PATH"]
78         if SYSIMG_PATH == "":
79             raise ValueError, "SYSIMG_PATH"
80
81         PARTITIONS= vars["PARTITIONS"]
82         if PARTITIONS == None:
83             raise ValueError, "PARTITIONS"
84
85         SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
86         if SUPPORT_FILE_DIR == None:
87             raise ValueError, "SUPPORT_FILE_DIR"
88
89         NODE_ID= vars["NODE_ID"]
90         if NODE_ID == "":
91             raise ValueError, "NODE_ID"
92
93     except KeyError, var:
94         raise BootManagerException, "Missing variable in vars: %s\n" % var
95     except ValueError, var:
96         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
97
98
99     try:
100         # make sure the required partitions exist
101         val= PARTITIONS["root"]
102         val= PARTITIONS["swap"]
103         val= PARTITIONS["vservers"]
104     except KeyError, part:
105         log.write( "Missing partition in PARTITIONS: %s\n" % part )
106         return 0   
107
108     bs_request= BootServerRequest.BootServerRequest()
109     
110     log.write( "turning on swap space\n" )
111     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
112
113     # make sure the sysimg dir is present
114     utils.makedirs( SYSIMG_PATH )
115
116     log.write( "mounting root file system\n" )
117     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
118
119     log.write( "mounting vserver partition in root file system\n" )
120     utils.makedirs( SYSIMG_PATH + "/vservers" )
121     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
122                                                      SYSIMG_PATH), log )
123
124     vars['ROOT_MOUNTED']= 1
125     
126
127     # download and extract support tarball for
128     # this step, which has everything
129     # we need to successfully run
130     for step_support_file in [ "PlanetLab-Bootstrap.tar.bz2",
131                                "alpina-BootstrapRPM.tar.bz2" ]: 
132         source_file= "%s/%s" % (SUPPORT_FILE_DIR,step_support_file)
133         dest_file= "%s/%s" % (SYSIMG_PATH, step_support_file)
134
135         # 30 is the connect timeout, 7200 is the max transfer time
136         # in seconds (2 hours)
137         log.write( "downloading %s\n" % step_support_file )
138         result= bs_request.DownloadFile( source_file, None, None,
139                                          1, 1, dest_file,
140                                          30, 7200)
141         if result:
142             # New bootstrap tarball contains everything necessary to
143             # boot, no need to bootstrap further.
144             vars['SKIP_INSTALL_BASE']= (step_support_file == "PlanetLab-Bootstrap.tar.bz2")
145             break
146
147     if not result:
148         raise BootManagerException, "Unable to download %s from server." % \
149               source_file
150
151     log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
152     result= utils.sysexec( "tar -C %s -xpjf %s" % (SYSIMG_PATH,dest_file), log )
153     utils.removefile( dest_file )
154
155     # get the yum configuration file for this node (yum.conf).
156     # this needs to come from the configuration file service,
157     # so, if its a beta node, it'll install the beta rpms from
158     # the beginning. The configuration file service will return
159     # the url for the file we need to request to get the actual
160     # conf file, so two requests need to be made.
161
162     # the only changes we will need to make to it are to change
163     # the cache and log directories, so when we run yum from
164     # the chrooted tempfs mount, it'll cache the rpms on the
165     # sysimg partition
166
167     log.write( "Fetching URL for yum.conf from configuration file service\n" )
168
169     postVars= {"node_id" : NODE_ID,
170                "file" : "/etc/yum.conf"}
171
172     yum_conf_url_file= "/tmp/yumconf.url"
173
174     result= bs_request.DownloadFile(
175         "/db/plnodeconf/getsinglefile.php",
176         None, postVars, 1, 1, yum_conf_url_file)
177     
178     if result == 0:
179         log.write( "Unable to make request to get url for yum.conf\n" )
180         return 0
181
182     try:
183         yum_conf_url= file(yum_conf_url_file,"r").read()
184         yum_conf_url= string.strip(yum_conf_url)
185         if yum_conf_url == "":
186             raise BootManagerException, \
187                   "Downloaded yum configuration file URL is empty."
188     except IOError:
189         raise BootManagerException, \
190               "Unable to open downloaded yum configuration file URL."
191
192     # now, get the actual contents of yum.conf for this node
193     log.write( "Fetching yum.conf contents from configuration file service\n" )
194
195     postVars= {}
196     download_file_loc= "%s/etc/yum.conf" % SYSIMG_PATH
197
198     result= bs_request.DownloadFile( yum_conf_url,
199                                      None, postVars, 1, 1,
200                                      download_file_loc)
201
202     if result == 0:
203         log.write( "Unable to make request to get yum.conf\n" )
204         return 0
205
206     # copy resolv.conf from the base system into our temp dir
207     # so DNS lookups work correctly while we are chrooted
208     log.write( "Copying resolv.conf to temp dir\n" )
209     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
210
211     # Copy the boot server certificate(s) and GPG public key to
212     # /usr/boot in the temp dir.
213     log.write( "Copying boot server certificates and public key\n" )
214
215     if os.path.exists("/usr/boot"):
216         utils.makedirs(SYSIMG_PATH + "/usr")
217         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
218     elif os.path.exists("/usr/bootme"):
219         utils.makedirs(SYSIMG_PATH + "/usr/boot")
220         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
221         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
222                     SYSIMG_PATH + "/usr/boot/cacert.pem")
223         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
224         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
225         
226     # Import the GPG key into the RPM database so that RPMS can be verified
227     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
228     utils.sysexec("gpg --homedir=/root --export --armor" \
229                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
230                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
231     utils.sysexec("chroot %s rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % \
232                   SYSIMG_PATH)
233
234     return 1