Skip to content
This repository
tree: 85b9310105
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 337 lines (276 sloc) 12.471 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
import os, sys, types
from glob import glob

from catalyst_support import MetroError, spawn, spawn_bash, ismount

CMDS = {
    "bash": "/bin/bash",
    "chroot": "/usr/bin/chroot",
    "install": "/usr/bin/install",
    "kill": "/bin/kill",
    "linux32": "/usr/bin/linux32",
    "mount": "/bin/mount",
    "rm": "/bin/rm",
}

class target:
    def run(self):
        self.clean_path(recreate=True)

        self.run_script("steps/run")
        self.run_script("trigger/ok/run", optional=True)

        self.clean_path()

    def run_script(self, key, chroot=None, optional=False):
        if not self.settings.has_key(key):
            if optional:
                return
            raise MetroError, "run_script: key '%s' not found." % (key,)

        if type(self.settings[key]) != types.ListType:
            raise MetroError, "run_script: key '%s' is not a multi-line element." % (key, )

        print "run_script: running %s..." % key

        os.environ["PATH"] = self.env["PATH"]

        if chroot:
            chrootfile = "/tmp/"+key+".metro"
            outfile = chroot+chrootfile
        else:
            outfile = self.settings["path/tmp"]+"/pid/"+repr(os.getpid())

        outdir = os.path.dirname(outfile)

        if not os.path.exists(outdir):
            os.makedirs(outdir)
        outfd = open(outfile,"w")

        for line in self.settings[key]:
            outfd.write(line + "\n")

        outfd.close()
        os.chmod(outfile, 0755)

        cmds = []
        if chroot:
            if self.settings["target/arch"] == "x86" and os.uname()[4] == "x86_64":
                cmds = [CMDS["linux32"], CMDS["chroot"]]
            else:
                cmds = [CMDS["chroot"]]
            cmds.append(chroot)
            cmds.append(chrootfile)
        else:
            cmds.append(outfile)

        retval = spawn(cmds, env=self.env )
        if retval != 0:
            raise MetroError, "Command failure (key %s, return value %s) : %s" % ( key, repr(retval), " ".join(cmds))

        # it could have been cleaned by our outscript, so if it exists:
        if os.path.exists(outfile):
            os.unlink(outfile)

    def target_exists(self, key):
        if self.settings.has_key("metro/options") and "replace" in self.settings["metro/options"].split():
            if os.path.exists(self.settings[key]):
                print "Removing existing file %s..." % self.settings[key]
                self.cmd(CMDS["rm"] + " -f " + self.settings[key])
            return False
        elif os.path.exists(self.settings[key]):
            print "File %s already exists - skipping..." % self.settings[key]
            return True
        else:
            return False

    def __init__(self, settings):
        self.settings = settings
        self.env = {}
        self.env["PATH"] = "/bin:/sbin:/usr/bin:/usr/sbin"

    def clean_path(self, path=None, recreate=False):
        if path == None:
            path = self.settings["path/work"]
        if os.path.exists(path):
            print "Cleaning up %s..." % path
        self.cmd(CMDS["rm"]+" -rf "+path)
        if recreate:
            # This line ensures that the root /var/tmp/metro path has proper 0700 perms:
            self.cmd(CMDS["install"]+" -d -m 0700 -g root -o root " + self.settings["path/tmp"])
            # This creates the directory we want.
            self.cmd(CMDS["install"]+" -d -m 0700 -g root -o root "+path)
            # The 0700 perms prevent Metro-generated /tmp directories from being abused by others -
            # because they are world-writeable, they could be used by malicious local users to
            # inject arbitrary data/executables into a Metro build.

    def cmd(self, mycmd, myexc="", badval=None):
        print "Executing \""+mycmd+"\"..."
        #print "Executing \""+mycmd.split(" ")[0]+"\"..."
        try:
            sys.stdout.flush()
            retval = spawn_bash(mycmd, self.env)
            if badval:
                # This code is here because tar has a retval of 1 for non-fatal warnings
                if retval == badval:
                    raise MetroError, myexc
            else:
                if retval != 0:
                    raise MetroError, myexc
        except:
            raise

class chroot(target):

    def get_chroot_pids(self):
        cdir = self.settings["path/work"]
        pids = []
        for pid in os.listdir("/proc"):
            if not os.path.isdir("/proc/"+pid):
                continue
            try:
                mylink = os.readlink("/proc/"+pid+"/exe")
            except OSError:
                # not a pid directory
                continue
            if mylink[0:len(cdir)] == cdir:
                pids.append([pid, mylink])
        return pids

    def kill_chroot_pids(self):
        for pid, mylink in self.get_chroot_pids():
            print "Killing process "+pid+" ("+mylink+")"
            self.cmd(CMDS["kill"]+" -9 "+pid)

    def run_script_in_chroot(self, key, chroot=None, optional=False):
        if chroot == None:
            return self.run_script(key, chroot=self.settings["path/work"], optional=optional)
        else:
            return self.run_script(key, chroot=chroot, optional=optional)

    def __init__(self, settings):
        target.__init__(self, settings)

        # DEFINE GENERAL LINUX CHROOT MOUNTS

        self.mounts = ["/proc"]
        self.mountmap = {"/proc":"/proc" }

        # CCACHE SUPPORT FOR CHROOTS

        if not self.settings.has_key("target/class"):
            return

        skey = "metro/options/"+self.settings["target/class"]

        # enable ccache and pkgcache support - all we do in python is bind-mount the right directory to the right place.

        for key, name, dest in [
                [ "path/cache/compiler", "cache/compiler", "/var/tmp/cache/compiler" ] ,
                [ "path/cache/package", "cache/package", "/var/tmp/cache/package" ] ,
                [ "path/cache/kernel", "cache/kernel", "/var/tmp/cache/kernel" ] ,
                [ "path/cache/probe", "probe", "/var/tmp/cache/probe" ] ]:
            if self.settings.has_key(skey) and name in self.settings[skey].split():
                if not self.settings.has_key(key):
                    raise MetroError, "Required setting %s not found (for %s option support)" % ( key, name )
                if not os.path.exists(self.settings[key]):
                    os.makedirs(self.settings[key])
                self.mounts.append(dest)
                self.mountmap[dest] = self.settings[key]

    def bind(self):
        """ Perform bind mounts """
        for mount in self.mounts:
            if not os.path.exists(self.settings["path/work"]+mount):
                os.makedirs(self.settings["path/work"]+mount, 0755)

            if not os.path.exists(self.mountmap[mount]):
                os.makedirs(self.mountmap[mount], 0755)

            src = self.mountmap[mount]
            print "Mounting %s to %s..." % (src, mount)
            if os.system(CMDS["mount"]+" --bind "+src+" "+self.settings["path/work"]+mount) != 0:
                self.unbind()
                raise MetroError, "Couldn't bind mount "+src

    def unbind(self, attempt=0):
        mounts = self.get_active_mounts()
        while len(mounts) != 0:
            # now, go through our dictionary and try to unmound
            progress = 0
            mpos = 0
            while mpos < len(mounts):
                self.cmd("umount "+mounts[mpos], badval=10)
                if not ismount(mounts[mpos]):
                    del mounts[mpos]
                    progress += 1
                else:
                    mpos += 1
            if progress == 0:
                break

        mounts = self.get_active_mounts()
        if len(mounts):
            if attempt >= 3:
                mstring = ""
                for mount in mounts:
                    mstring += mount+"\n"
                raise MetroError, "The following bind mounts could not be unmounted: \n"+mstring
            else:
                attempt += 1
                self.kill_chroot_pids()
                self.unbind(attempt=attempt)

    def get_active_mounts(self):
        # os.path.realpath should ensure that we are comparing the right thing, if something in the path
        # is a symlink - like /var/tmp -> /foo. Because /proc/mounts will store the resolved path (ie.
        # /foo/metro) not the regular one (ie. /var/tmp/metro)
        prefix = os.path.realpath(self.settings["path/work"])
        # this used to have a "os.popen("mount")" which is not as accurate as the kernel list /proc/mounts.
        # The "mount" command relies on /etc/mtab which is not necessarily correct.
        myf = open("/proc/mounts","r")
        mylines = myf.readlines()
        myf.close()
        outlist = []
        for line in mylines:
            mypath = line.split()[1]
            if mypath[0:len(prefix)] == prefix:
                outlist.append(mypath)
        return outlist

    def check_mounts(self):
        mymounts = self.get_active_mounts()
        if len(mymounts) == 0:
            return
        else:
            self.unbind()

    def run(self, required_files=None):
        if self.target_exists("path/mirror/target"):
            self.run_script("trigger/ok/run", optional=True)
            return

        # look for required files
        for loc in [ "path/mirror/source" ] + (required_files or []):
            try:
                matches = glob(self.settings[loc])
            except:
                raise MetroError, "Setting %s is set to %s; glob failed." % ( loc, repr(self.settings[loc]) )
            if len(matches) == 0:
                raise MetroError, "Required file "+self.settings[loc]+" not found. Aborting."
            elif len(matches) > 1:
                raise MetroError, "Multiple matches found for required file pattern defined in '%s'; Aborting." % loc

        # BEFORE WE CLEAN UP - MAKE SURE WE ARE UNMOUNTED
        self.kill_chroot_pids()
        self.check_mounts()

        # BEFORE WE START - CLEAN UP ANY MESSES
        self.clean_path(recreate=True)
        try:
            self.check_mounts()
            self.run_script("steps/unpack")
            self.run_script("steps/unpack/post", optional=True)

            self.bind()

            self.run_script_in_chroot("steps/chroot/prerun", optional=True)
            self.run_script_in_chroot("steps/chroot/run")
            self.run_script_in_chroot("steps/chroot/postrun", optional=True)

            self.unbind()

            self.run_script_in_chroot("steps/chroot/clean", optional=True)
            self.run_script_in_chroot("steps/chroot/test", optional=True)
            self.run_script_in_chroot("steps/chroot/postclean", optional=True)
        except:
            self.kill_chroot_pids()
            self.check_mounts()
            raise

        self.run_script("steps/capture")
        self.run_script("trigger/ok/run", optional=True)

        self.kill_chroot_pids()
        self.check_mounts()
        self.clean_path()

class snapshot(target):
    def __init__(self, settings):
        target.__init__(self, settings)

    def run(self):
        if self.target_exists("path/mirror/snapshot"):
            self.run_script("trigger/ok/run", optional=True)
            return

        target.run(self)

class stage(chroot):

    def __init__(self, settings):
        chroot.__init__(self, settings)

        # DEFINE GENTOO MOUNTS
        if self.settings.has_key("path/distfiles"):
            self.mounts.append("/usr/portage/distfiles")
            self.mountmap["/usr/portage/distfiles"] = self.settings["path/distfiles"]

        # let's bind-mount our main system's device nodes in place
        if self.settings["portage/ROOT"] != "/":
            # this seems to be needed for libperl to build (x2p) during stage1 - so we'll mount it....
            self.mounts.append("/dev")
            self.mounts.append("/dev/pts")
            self.mountmap["/dev"] = "/dev"
            self.mountmap["/dev/pts"] = "/dev/pts"

    def run(self):
        chroot.run(self, ["path/mirror/snapshot"])

        # Now, we want to clean up our build-related caches, if configured to do so:
        if self.settings.has_key("metro/options"):
            if "clean/auto" in self.settings["metro/options"].split():
                if self.settings.has_key("path/cache/build"):
                    self.clean_path(self.settings["path/cache/build"])

# vim: ts=4 sw=4 et
Something went wrong with that request. Please try again.