(no commit message)
[bootmanager.git] / source / steps / InstallBootstrapFS.py
1 #!/usr/bin/python
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2007 The Trustees of Princeton University
7 # All rights reserved.
8 # expected /proc/partitions format
9
10 import os, sys, string
11 import popen2
12 import shutil
13 import traceback 
14
15 from Exceptions import *
16 import utils
17 import BootServerRequest
18 import BootAPI
19
20
21 def Run( vars, log ):
22     """
23     Download enough files to run rpm and yum from a chroot in
24     the system image directory
25     
26     Expect the following variables from the store:
27     SYSIMG_PATH          the path where the system image will be mounted
28     PARTITIONS           dictionary of generic part. types (root/swap)
29                          and their associated devices.
30     NODE_ID              the id of this machine
31     
32     Sets the following variables:
33     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
34                          path
35     ROOT_MOUNTED         set to 1 when the the base logical volumes
36                          are mounted.
37     """
38
39     log.write( "\n\nStep: Install: bootstrapfs tarball.\n" )
40
41     # make sure we have the variables we need
42     try:
43         SYSIMG_PATH= vars["SYSIMG_PATH"]
44         if SYSIMG_PATH == "":
45             raise ValueError, "SYSIMG_PATH"
46
47         PARTITIONS= vars["PARTITIONS"]
48         if PARTITIONS == None:
49             raise ValueError, "PARTITIONS"
50
51         NODE_ID= vars["NODE_ID"]
52         if NODE_ID == "":
53             raise ValueError, "NODE_ID"
54
55     except KeyError, var:
56         raise BootManagerException, "Missing variable in vars: %s\n" % var
57     except ValueError, var:
58         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
59
60
61     try:
62         # make sure the required partitions exist
63         val= PARTITIONS["root"]
64         val= PARTITIONS["swap"]
65         val= PARTITIONS["vservers"]
66     except KeyError, part:
67         log.write( "Missing partition in PARTITIONS: %s\n" % part )
68         return 0   
69
70     bs_request= BootServerRequest.BootServerRequest(vars)
71     
72     log.write( "turning on swap space\n" )
73     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
74
75     # make sure the sysimg dir is present
76     utils.makedirs( SYSIMG_PATH )
77
78     log.write( "mounting root file system\n" )
79     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
80
81     log.write( "mounting vserver partition in root file system\n" )
82     utils.makedirs( SYSIMG_PATH + "/vservers" )
83     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
84                                                      SYSIMG_PATH), log )
85
86     vars['ROOT_MOUNTED']= 1
87
88     # fetch deployment tag (like, 'alpha' or the like)
89     try:
90         deployment = BootAPI.call_api_function(vars, "GetNodeDeployment", (NODE_ID,) )
91     except:
92         log.write("WARNING : Failed to query tag 'deployment'\n")
93         deployment = ""
94
95     # which extensions are we part of ?
96     utils.breakpoint("Checking for the extension(s) tags")
97     extensions = []
98     try:
99         extension_tag = BootAPI.call_api_function(vars, "GetNodeExtensions", (NODE_ID,) )
100         if extension_tag:
101             extensions = extension_tag.split()
102
103     except:
104         log.write("WARNING : Failed to query tag 'extensions'\n")
105         log.write(traceback.format_exc())
106
107     if not extensions:
108         log.write("installing only core software\n")
109     
110     # check if the plain-bootstrapfs tag is set
111     download_suffix=".tar.bz2"
112     untar_option="-j"
113     try:
114         if BootAPI.call_api_function (vars, "GetNodePlainBootstrapfs", (NODE_ID,) ):
115             download_suffix=".tar"
116             untar_option=""
117     except:
118         log.write("WARNING : Failed to query tag 'plain-bootstrapfs'\n")
119         log.write(traceback.format_exc())
120
121     if not untar_option:
122         log.write("Using uncompressed bootstrapfs images\n")
123
124     # see also GetBootMedium in PLCAPI that does similar things
125     # figuring the default node family:
126     # (1) get node's tags 'arch' and 'pldistro'
127     # (2) if unsuccessful search /etc/planetlab/nodefamily on the bootcd
128     # (3) if that fails, set to planetlab-i386
129
130     try:
131         api_pldistro = BootAPI.call_api_function(vars, "GetNodePldistro", (NODE_ID,) )
132     except:
133         log.write("WARNING : Failed to query tag 'pldistro'\n")
134         api_pldistro = None
135     try:
136         api_arch = BootAPI.call_api_function(vars, "GetNodeArch", (NODE_ID,) )
137     except:
138         log.write("WARNING : Failed to query tag 'arch'\n")
139         api_arch = None
140     try:
141         (etc_pldistro,etc_arch) = file("/etc/planetlab/nodefamily").read().strip().split("-")
142     except:
143         log.write("WARNING : Failed to parse /etc/planetlab/nodefamily\n")
144         (etc_pldistro,etc_arch)=(None,None)
145     default_pldistro="planetlab"
146     default_arch="i386"
147
148     if api_pldistro:
149         pldistro = api_pldistro
150         log.write ("Using pldistro from pldistro API tag\n")
151     elif etc_pldistro:
152         pldistro = etc_pldistro
153         log.write ("Using pldistro from /etc/planetlab/nodefamily\n")
154     else:
155         pldistro = default_pldistro
156         log.write ("Using default pldistro\n")
157
158     if api_arch:
159         arch = api_arch
160         log.write ("Using arch from arch API tag\n")
161     elif etc_arch:
162         arch = etc_arch
163         log.write ("Using arch from /etc/planetlab/nodefamily\n")
164     else:
165         arch = default_arch
166         log.write ("Using default arch\n")
167
168     log.write ("Using nodefamily=%s-%s\n"%(pldistro,arch))
169
170     # deployment has no arch nor extensions, let operators put what they want in there
171     if deployment:
172         bootstrapfs_names = [ deployment ]
173     else:
174         bootstrapfs_names = [ "%s-%s"%(x,arch) for x in [ pldistro ] + extensions ]
175
176     # download and extract support tarball for this step, which has
177     # everything we need to successfully run
178
179     # installing extensions through yum has been dismantled
180     yum_extensions = []
181     # download and extract support tarball for this step, 
182     for name in bootstrapfs_names:
183         tarball = "bootstrapfs-%s%s"%(name,download_suffix)
184         source_file= "/boot/%s" % (SUPPORT_FILE_DIR,tarball)
185         dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
186
187         # 30 is the connect timeout, 14400 is the max transfer time in
188         # seconds (4 hours)
189         log.write( "downloading %s\n" % source_file )
190         result= bs_request.DownloadFile( source_file, None, None,
191                                          1, 1, dest_file,
192                                          30, 14400)
193         if result:
194             log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
195             result= utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,untar_option), log )
196             log.write( "Done\n")
197             utils.removefile( dest_file )
198         else:
199             # the main tarball is required
200             if name == "%s-%s"%(pldistro,arch):
201                 raise BootManagerException, "Unable to download main tarball %s from server." % \
202                     source_file
203             else:
204                 log.write("tarball for %s not found, scheduling a yum attempt\n"%(name))
205                 yum_extensions.append(name)
206
207     # copy resolv.conf from the base system into our temp dir
208     # so DNS lookups work correctly while we are chrooted
209     log.write( "Copying resolv.conf to temp dir\n" )
210     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
211
212     # Copy the boot server certificate(s) and GPG public key to
213     # /usr/boot in the temp dir.
214     log.write( "Copying boot server certificates and public key\n" )
215
216     if os.path.exists("/usr/boot"):
217         utils.makedirs(SYSIMG_PATH + "/usr")
218         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
219     elif os.path.exists("/usr/bootme"):
220         utils.makedirs(SYSIMG_PATH + "/usr/boot")
221         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
222         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
223                     SYSIMG_PATH + "/usr/boot/cacert.pem")
224         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
225         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
226         
227     # For backward compatibility
228     if os.path.exists("/usr/bootme"):
229         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
230         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
231
232     # Import the GPG key into the RPM database so that RPMS can be verified
233     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
234     utils.sysexec("gpg --homedir=/root --export --armor" \
235                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
236                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
237     utils.sysexec_chroot(SYSIMG_PATH, "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab")
238
239     # the yum config has changed entirely; 
240     # in addition yum installs have more or less never worked - let's forget about this
241     # maybe NodeManager could profitably do the job instead
242     if yum_extensions:
243         log.write("WARNING : yum installs for node extensions are not supported anymore\n")
244
245     return 1