remove svn keywords and use %{SCMURL} in spec file
[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 core + extensions bootstrapfs tarballs and install on the hard drive
24     
25     Expect the following variables from the store:
26     SYSIMG_PATH          the path where the system image will be mounted
27     PARTITIONS           dictionary of generic part. types (root/swap)
28                          and their associated devices.
29     NODE_ID              the id of this machine
30     
31     Sets the following variables:
32     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
33                          path
34     ROOT_MOUNTED         set to 1 when the the base logical volumes
35                          are mounted.
36     """
37
38     log.write( "\n\nStep: Install: bootstrapfs tarball.\n" )
39
40     # make sure we have the variables we need
41     try:
42         SYSIMG_PATH= vars["SYSIMG_PATH"]
43         if SYSIMG_PATH == "":
44             raise ValueError, "SYSIMG_PATH"
45
46         PARTITIONS= vars["PARTITIONS"]
47         if PARTITIONS == None:
48             raise ValueError, "PARTITIONS"
49
50         NODE_ID= vars["NODE_ID"]
51         if NODE_ID == "":
52             raise ValueError, "NODE_ID"
53
54     except KeyError, var:
55         raise BootManagerException, "Missing variable in vars: %s\n" % var
56     except ValueError, var:
57         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
58
59
60     try:
61         # make sure the required partitions exist
62         val= PARTITIONS["root"]
63         val= PARTITIONS["swap"]
64         val= PARTITIONS["vservers"]
65     except KeyError, part:
66         log.write( "Missing partition in PARTITIONS: %s\n" % part )
67         return 0   
68
69     bs_request= BootServerRequest.BootServerRequest(vars)
70     
71     log.write( "turning on swap space\n" )
72     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
73
74     # make sure the sysimg dir is present
75     utils.makedirs( SYSIMG_PATH )
76
77     log.write( "mounting root file system\n" )
78     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
79
80     log.write( "mounting vserver partition in root file system\n" )
81     utils.makedirs( SYSIMG_PATH + "/vservers" )
82     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
83                                                      SYSIMG_PATH), log )
84
85     vars['ROOT_MOUNTED']= 1
86
87     # call getNodeFlavour
88     try:
89         node_flavour = BootAPI.call_api_function(vars, "GetNodeFlavour", (NODE_ID,) )
90         nodefamily = node_flavour['nodefamily']
91         extensions = node_flavour['extensions']
92         plain = node_flavour['plain']
93     except:
94         raise BootManagerException ("Could not call GetNodeFlavour - need PLCAPI-5.0")
95     
96     # the 'plain' option is for tests mostly
97     if plain:
98         download_suffix=".tar"
99         uncompress_option=""
100         log.write("Using plain bootstrapfs images\n")
101     else:
102         download_suffix=".tar.bz2"
103         uncompress_option="-j"
104         log.write("Using compressed bootstrapfs images\n")
105
106     log.write ("Using nodefamily=%s\n"%(nodefamily))
107     if not extensions:
108         log.write("Installing only core software\n")
109     else:
110         log.write("Requested extensions %r" % extensions)
111     
112     bootstrapfs_names = [ nodefamily ] + extensions
113
114     for name in bootstrapfs_names:
115         tarball = "bootstrapfs-%s%s"%(name,download_suffix)
116         source_file= "/boot/%s" % (tarball)
117         dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
118
119         source_hash_file= "/boot/%s.sha1sum" % (tarball)
120         dest_hash_file= "%s/%s.sha1sum" % (SYSIMG_PATH, tarball)
121
122         # 30 is the connect timeout, 14400 is the max transfer time in
123         # seconds (4 hours)
124         log.write( "downloading %s\n" % source_file )
125         result = bs_request.DownloadFile( source_file, None, None,
126                                          1, 1, dest_file,
127                                          30, 14400)
128
129         if result:
130             # Download SHA1 checksum file
131             result = bs_request.DownloadFile( source_hash_file, None, None,
132                                          1, 1, dest_hash_file,
133                                          30, 14400)
134  
135             if not utils.check_file_hash(dest_file, dest_hash_file):
136                 raise BootManagerException, "FATAL: SHA1 checksum does not match between %s and %s" % (source_file, source_hash_file)
137                 
138             log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
139             result = utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,uncompress_option), log )
140             log.write( "Done\n")
141             utils.removefile( dest_file )
142         else:
143             # the main tarball is required
144             if name == nodefamily:
145                 raise BootManagerException, "FATAL: Unable to download main tarball %s from server." % \
146                     source_file
147             # for extensions, just print a warning
148             else:
149                 log.write("WARNING: tarball for extension %s not found\n"%(name))
150
151     # copy resolv.conf from the base system into our temp dir
152     # so DNS lookups work correctly while we are chrooted
153     log.write( "Copying resolv.conf to temp dir\n" )
154     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
155
156     # Copy the boot server certificate(s) and GPG public key to
157     # /usr/boot in the temp dir.
158     log.write( "Copying boot server certificates and public key\n" )
159
160     if os.path.exists("/usr/boot"):
161         utils.makedirs(SYSIMG_PATH + "/usr")
162         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
163     elif os.path.exists("/usr/bootme"):
164         utils.makedirs(SYSIMG_PATH + "/usr/boot")
165         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
166         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
167                     SYSIMG_PATH + "/usr/boot/cacert.pem")
168         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
169         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
170         
171     # For backward compatibility
172     if os.path.exists("/usr/bootme"):
173         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
174         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
175
176     # Import the GPG key into the RPM database so that RPMS can be verified
177     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
178     utils.sysexec("gpg --homedir=/root --export --armor" \
179                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
180                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
181     utils.sysexec_chroot(SYSIMG_PATH, "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab")
182
183     return 1