fix how bm gets tags - test node now installs in 1'40'' for download+extract
[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     SUPPORT_FILE_DIR     directory on the boot servers containing
31                          scripts and support files
32     NODE_ID              the id of this machine
33     
34     Sets the following variables:
35     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
36                          path
37     ROOT_MOUNTED         set to 1 when the the base logical volumes
38                          are mounted.
39     """
40
41     log.write( "\n\nStep: Install: bootstrapfs tarball.\n" )
42
43     # make sure we have the variables we need
44     try:
45         SYSIMG_PATH= vars["SYSIMG_PATH"]
46         if SYSIMG_PATH == "":
47             raise ValueError, "SYSIMG_PATH"
48
49         PARTITIONS= vars["PARTITIONS"]
50         if PARTITIONS == None:
51             raise ValueError, "PARTITIONS"
52
53         SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
54         if SUPPORT_FILE_DIR == None:
55             raise ValueError, "SUPPORT_FILE_DIR"
56
57         NODE_ID= vars["NODE_ID"]
58         if NODE_ID == "":
59             raise ValueError, "NODE_ID"
60
61     except KeyError, var:
62         raise BootManagerException, "Missing variable in vars: %s\n" % var
63     except ValueError, var:
64         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
65
66
67     try:
68         # make sure the required partitions exist
69         val= PARTITIONS["root"]
70         val= PARTITIONS["swap"]
71         val= PARTITIONS["vservers"]
72     except KeyError, part:
73         log.write( "Missing partition in PARTITIONS: %s\n" % part )
74         return 0   
75
76     bs_request= BootServerRequest.BootServerRequest()
77     
78     log.write( "turning on swap space\n" )
79     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
80
81     # make sure the sysimg dir is present
82     utils.makedirs( SYSIMG_PATH )
83
84     log.write( "mounting root file system\n" )
85     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
86
87     log.write( "mounting vserver partition in root file system\n" )
88     utils.makedirs( SYSIMG_PATH + "/vservers" )
89     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
90                                                      SYSIMG_PATH), log )
91
92     vars['ROOT_MOUNTED']= 1
93
94     # which extensions are we part of ?
95     utils.breakpoint("Getting the extension tag on node")
96     extensions = []
97     try:
98         extension_tag = BootAPI.call_api_function(vars, "GetNodeExtensions", (NODE_ID,) )
99         if extension_tag:
100             extensions = extension_tag.split()
101
102     except:
103         log.write("WARNING : Failed to query tag 'extensions'")
104         log.write(traceback.format_exc())
105
106     if not extensions:
107         log.write("installing only core software\n")
108     
109     # check if the plain-bootstrapfs tag is set
110     download_suffix=".tar.bz2"
111     untar_option="-j"
112     try:
113         if BootAPI.call_api_function (vars, "GetNodePlainBootstrapfs", (NODE_ID,) ):
114             download_suffix=".tar"
115             untar_option=""
116     except:
117         log.write("WARNING : Failed to query tag 'plain-bootstrapfs'")
118         log.write(traceback.format_exc())
119
120     if not untar_option:
121         log.write("Using uncompressed bootstrapfs images\n")
122
123     # see also GetBootMedium in PLCAPI that does similar things
124     # figuring the default node family:
125     # (1) look at /etc/planetlab/nodefamily on the bootcd
126     # (2) if that fails, set to planetlab-i386
127     try:
128         (pldistro,arch) = file("/etc/planetlab/nodefamily").read().strip().split("-")
129     except:
130         # what arch should be used for this node
131         utils.breakpoint("Getting the arch tag on node")
132         pldistro="planetlab"
133         default_arch="i386"
134         try:
135             arch = BootAPI.call_api_function(vars, "GetNodeArch", (NODE_ID,) )
136         except:
137             log.write("WARNING : Failed to query tag 'arch'")
138             log.write(traceback.format_exc())
139             arch = default_arch
140
141     log.write ("Using arch=%s\n"%arch)
142
143     bootstrapfs_names = [ pldistro ] + extensions
144
145     # download and extract support tarball for this step, which has
146     # everything we need to successfully run
147
148     # we first try to find a tarball, if it is not found we use yum instead
149     yum_extensions = []
150     # download and extract support tarball for this step, 
151     for bootstrapfs_name in bootstrapfs_names:
152         tarball = "bootstrapfs-%s-%s%s"%(bootstrapfs_name,arch,download_suffix)
153         source_file= "%s/%s" % (SUPPORT_FILE_DIR,tarball)
154         dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
155
156         # 30 is the connect timeout, 14400 is the max transfer time in
157         # seconds (4 hours)
158         log.write( "downloading %s\n" % source_file )
159         result= bs_request.DownloadFile( source_file, None, None,
160                                          1, 1, dest_file,
161                                          30, 14400)
162         if result:
163             log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
164             result= utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,untar_option), log )
165             log.write( "Done\n")
166             utils.removefile( dest_file )
167         else:
168             # the main tarball is required
169             if bootstrapfs_name == pldistro:
170                 raise BootManagerException, "Unable to download main tarball %s from server." % \
171                     source_file
172             else:
173                 log.write("tarball for %s-%s not found, scheduling a yum attempt\n"%(bootstrapfs_name,arch))
174                 yum_extensions.append(bootstrapfs_name)
175
176     # copy resolv.conf from the base system into our temp dir
177     # so DNS lookups work correctly while we are chrooted
178     log.write( "Copying resolv.conf to temp dir\n" )
179     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
180
181     # Copy the boot server certificate(s) and GPG public key to
182     # /usr/boot in the temp dir.
183     log.write( "Copying boot server certificates and public key\n" )
184
185     if os.path.exists("/usr/boot"):
186         utils.makedirs(SYSIMG_PATH + "/usr")
187         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
188     elif os.path.exists("/usr/bootme"):
189         utils.makedirs(SYSIMG_PATH + "/usr/boot")
190         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
191         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
192                     SYSIMG_PATH + "/usr/boot/cacert.pem")
193         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
194         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
195         
196     # For backward compatibility
197     if os.path.exists("/usr/bootme"):
198         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
199         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
200
201     # Import the GPG key into the RPM database so that RPMS can be verified
202     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
203     utils.sysexec("gpg --homedir=/root --export --armor" \
204                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
205                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
206     utils.sysexec("chroot %s rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % \
207                   SYSIMG_PATH)
208
209     # the yum config has changed entirely; 
210     # in addition yum installs have more or less never worked - let's forget about this
211     # maybe NodeManager could profitably do the job instead
212     if yum_extensions:
213         log.write('WARNING : yum installs for node extensions are not supported anymore')
214
215     return 1