- look for PlanetLab-Bootstrap-<nodegroup>.tar.bz2 in the boot/
[bootmanager.git] / source / steps / InstallBootstrapRPM.py
1 #!/usr/bin/python2
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 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
14 from Exceptions import *
15 import utils
16 import BootServerRequest
17 import BootAPI
18
19
20 def Run( vars, log ):
21     """
22     Download enough files to run rpm and yum from a chroot in
23     the system image directory
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     SUPPORT_FILE_DIR     directory on the boot servers containing
30                          scripts and support files
31     NODE_ID              the id of this machine
32     
33     Sets the following variables:
34     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
35                          path
36     ROOT_MOUNTED         set to 1 when the the base logical volumes
37                          are mounted.
38     """
39
40     log.write( "\n\nStep: Install: Bootstrapping RPM.\n" )
41
42     # make sure we have the variables we need
43     try:
44         SYSIMG_PATH= vars["SYSIMG_PATH"]
45         if SYSIMG_PATH == "":
46             raise ValueError, "SYSIMG_PATH"
47
48         PARTITIONS= vars["PARTITIONS"]
49         if PARTITIONS == None:
50             raise ValueError, "PARTITIONS"
51
52         SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
53         if SUPPORT_FILE_DIR == None:
54             raise ValueError, "SUPPORT_FILE_DIR"
55
56         NODE_ID= vars["NODE_ID"]
57         if NODE_ID == "":
58             raise ValueError, "NODE_ID"
59
60     except KeyError, var:
61         raise BootManagerException, "Missing variable in vars: %s\n" % var
62     except ValueError, var:
63         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
64
65
66     try:
67         # make sure the required partitions exist
68         val= PARTITIONS["root"]
69         val= PARTITIONS["swap"]
70         val= PARTITIONS["vservers"]
71     except KeyError, part:
72         log.write( "Missing partition in PARTITIONS: %s\n" % part )
73         return 0   
74
75     bs_request= BootServerRequest.BootServerRequest()
76     
77     log.write( "turning on swap space\n" )
78     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
79
80     # make sure the sysimg dir is present
81     utils.makedirs( SYSIMG_PATH )
82
83     log.write( "mounting root file system\n" )
84     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
85
86     log.write( "mounting vserver partition in root file system\n" )
87     utils.makedirs( SYSIMG_PATH + "/vservers" )
88     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
89                                                      SYSIMG_PATH), log )
90
91     vars['ROOT_MOUNTED']= 1
92
93     # check which nodegroups we are part of (>=4.0)
94     tarballs = []
95     try:
96         nodes = BootAPI.call_api_function(vars, "GetNodes", ([NODE_ID], ['nodegroup_ids']))
97         node = nodes[0]
98         nodegroups = BootAPI.call_api_function(vars, "GetNodeGroups", (node['nodegroup_ids'], ['name']))
99         for nodegroup in nodegroups:
100             tarballs.append("PlanetLab-Bootstrap-%s.tar.bz2" % nodegroup['name'].lower())
101     except:
102         pass
103     tarballs += ["PlanetLab-Bootstrap.tar.bz2", "alpina-BootstrapRPM.tar.bz2"]
104
105     # download and extract support tarball for
106     # this step, which has everything
107     # we need to successfully run
108     for step_support_file in tarballs:
109         source_file= "%s/%s" % (SUPPORT_FILE_DIR,step_support_file)
110         dest_file= "%s/%s" % (SYSIMG_PATH, step_support_file)
111
112         # 30 is the connect timeout, 7200 is the max transfer time
113         # in seconds (2 hours)
114         log.write( "downloading %s\n" % step_support_file )
115         result= bs_request.DownloadFile( source_file, None, None,
116                                          1, 1, dest_file,
117                                          30, 7200)
118         if result:
119             # New bootstrap tarball contains everything necessary to
120             # boot, no need to bootstrap further.
121             vars['SKIP_INSTALL_BASE']= (step_support_file == "PlanetLab-Bootstrap.tar.bz2")
122             break
123
124     if not result:
125         raise BootManagerException, "Unable to download %s from server." % \
126               source_file
127
128     log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
129     result= utils.sysexec( "tar -C %s -xpjf %s" % (SYSIMG_PATH,dest_file), log )
130     utils.removefile( dest_file )
131
132     # copy resolv.conf from the base system into our temp dir
133     # so DNS lookups work correctly while we are chrooted
134     log.write( "Copying resolv.conf to temp dir\n" )
135     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
136
137     # Copy the boot server certificate(s) and GPG public key to
138     # /usr/boot in the temp dir.
139     log.write( "Copying boot server certificates and public key\n" )
140
141     if os.path.exists("/usr/boot"):
142         utils.makedirs(SYSIMG_PATH + "/usr")
143         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
144     elif os.path.exists("/usr/bootme"):
145         utils.makedirs(SYSIMG_PATH + "/usr/boot")
146         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
147         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
148                     SYSIMG_PATH + "/usr/boot/cacert.pem")
149         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
150         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
151         
152     # For backward compatibility
153     if os.path.exists("/usr/bootme"):
154         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
155         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
156
157     # Import the GPG key into the RPM database so that RPMS can be verified
158     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
159     utils.sysexec("gpg --homedir=/root --export --armor" \
160                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
161                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
162     utils.sysexec("chroot %s rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % \
163                   SYSIMG_PATH)
164
165     return 1