def _write_template_to(self, tmpl_file, dest_file, template_dict):
        """Loads the the given template, writing it to the dest_file

            Note: the template will be written {dest_dir}/{tmpl_file},
            directories will be created if the do not yet exist.
        """
        template = self.env.get_template(tmpl_file)
        mkdir_p(os.path.dirname(dest_file))
        logging.info("Writing: %s -> %s with %s", tmpl_file, dest_file,
                     template_dict)
        with open(dest_file, "w") as dfile:
            dfile.write(template.render(template_dict))
Example #2
0
    def create_docker_file(self, extra="", metrics=False, by_copying_zip_files=False):
        logging.info("Emulator zip: %s", self.emulator)
        logging.info("Sysimg zip: %s", self.sysimg)
        logging.info("Docker src dir: %s", self.dest)

        date = datetime.datetime.utcnow().isoformat("T") + "Z"

        # Make sure the destination directory is empty.
        if os.path.exists(self.dest):
            shutil.rmtree(self.dest)
        mkdir_p(self.dest)

        self.bin_place_emulator_files(by_copying_zip_files)
        self._copy_adb_to(self.dest)
        self.writer.write_template("avd/Pixel2.ini", {"api": self.sysimg.api()})
        self.writer.write_template(
            "avd/Pixel2.avd/config.ini",
            {
                "playstore": self.sysimg.tag() == "google_apis_playstore",
                "abi": self.sysimg.abi(),
                "cpu": self.sysimg.cpu(),
                "tag": self.sysimg.tag(),
            },
        )

        # Only version 29.3.1 >= can collect metrics.
        if metrics and version.parse(self.emulator.revision()) >= version.parse("29.3.1"):
            extra += " -metrics-collection"

        extra += " {}".format(self.sysimg.logger_flags())
        self.writer.write_template("launch-emulator.sh", {"extra": extra, "version": emu.__version__})
        self.writer.write_template("default.pa", {})

        src_template = "Dockerfile.from_zip" if by_copying_zip_files else "Dockerfile"
        self.writer.write_template(
            src_template,
            {
                "user": "******".format(os.environ.get("USER", "unknown"), socket.gethostname()),
                "tag": self.sysimg.tag(),
                "api": self.sysimg.api(),
                "abi": self.sysimg.abi(),
                "cpu": self.sysimg.cpu(),
                "gpu": self.sysimg.gpu(),
                "emu_zip": os.path.basename(self.emulator.fname),
                "emu_build_id": self.emulator.build_id(),
                "sysimg_zip": os.path.basename(self.sysimg.fname),
                "date": date,
                "from_base_img": self.base_img,
            },
            rename_as="Dockerfile",
        )
Example #3
0
 def clean(self, dest):
     if os.path.exists(dest):
         shutil.rmtree(dest)
     mkdir_p(dest)
Example #4
0
def cloud_build(args):
    """Prepares the cloud build yaml and all its dependencies.

    The cloud builder will generate a single cloudbuild.yaml and generates the build
    scripts for every individual container.

    It will construct the proper dependencies as needed.
    """
    accept_licenses(True)

    mkdir_p(args.dest)
    image_zip = [args.img]

    # Check if we are building a custom image from a zip file
    if not os.path.exists(image_zip[0]):
        # We are using a standard image, we likely won't need to download it.
        image_zip = emu_downloads_menu.find_image(image_zip[0])

    emulator_zip = [args.emuzip]
    if emulator_zip[0] in ["stable", "canary", "all"]:
        emulator_zip = [
            x.download()
            for x in emu_downloads_menu.find_emulator(emulator_zip[0])
        ]
    elif re.match(r"\d+", emulator_zip[0]):
        # We must be looking for a build id
        logging.warning("Treating %s as a build id", emulator_zip[0])
        emulator_zip = [emu_downloads_menu.download_build(emulator_zip[0])]

    steps = []
    images = []
    emulators = set()
    emulator_images = []

    for (img, emu) in itertools.product(image_zip, emulator_zip):
        logging.info("Processing %s, %s", img, emu)
        system_container = SystemImageContainer(img, args.repo)
        if args.sys:
            steps.append(create_build_step(system_container, args.dest))
        else:
            for metrics in [True, False]:
                emulator_container = EmulatorContainer(emu, system_container,
                                                       args.repo, metrics)
                emulators.add(emulator_container.props["emu_build_id"])
                steps.append(create_build_step(emulator_container, args.dest))
                images.append(emulator_container.full_name())
                images.append(emulator_container.latest_name())
                emulator_images.append(emulator_container.full_name())
                emulator_images.append(emulator_container.latest_name())

    cloudbuild = {"steps": steps, "images": images, "timeout": "21600s"}
    logging.info("Writing cloud yaml [%s] in %s", yaml, args.dest)
    with open(os.path.join(args.dest, "cloudbuild.yaml"), "w") as ymlfile:
        yaml.dump(cloudbuild, ymlfile)

    writer = TemplateWriter(args.dest)
    writer.write_template(
        "cloudbuild.README.MD",
        {
            "emu_version": ", ".join(emulators),
            "emu_images": "\n".join(
                ["* {}".format(x) for x in emulator_images])
        },
        rename_as="README.MD",
    )
    writer.write_template(
        "registry.README.MD",
        {
            "emu_version": ", ".join(emulators),
            "emu_images": "\n".join(["* {}".format(x) for x in images]),
            "first_image": next(iter(images), None),
        },
        rename_as="REGISTRY.MD",
    )

    if args.git:
        git_commit_and_push(args.dest)
Example #5
0
    def create_docker_file(self,
                           extra="",
                           metrics=False,
                           by_copying_zip_files=False,
                           screen_resolution=None):
        if screen_resolution is None:
            screen_resolution = DEFAULT_RESOLUTION
        if type(extra) is list:
            extra = " ".join(extra)
        logging.info("Emulator zip: %s", self.emulator)
        logging.info("Sysimg zip: %s", self.sysimg)
        logging.info("Docker src dir: %s", self.dest)

        date = datetime.datetime.utcnow().isoformat("T") + "Z"

        # Make sure the destination directory is empty.
        if os.path.exists(self.dest):
            shutil.rmtree(self.dest)
        mkdir_p(self.dest)

        self._copy_adb_to(self.dest)
        self.writer.write_template("avd/Pixel2.ini",
                                   {"api": self.sysimg.api()})
        self.writer.write_template(
            "avd/Pixel2.avd/config.ini",
            {
                "playstore": self.sysimg.tag() == "google_apis_playstore",
                "abi": self.sysimg.abi(),
                "cpu": self.sysimg.cpu(),
                "tag": self.sysimg.tag(),
                'width': screen_resolution['width'],
                'height': screen_resolution['height'],
                'density': screen_resolution['density']
            },
        )

        metrics_msg = NO_METRICS_MESSAGE
        # Only version 29.3.1 >= can collect metrics.
        if metrics and version.parse(
                self.emulator.revision()) >= version.parse("29.3.1"):
            extra += " -metrics-collection"
            metrics_msg = METRICS_MESSAGE

        # Include a README.MD message.
        self.writer.write_template(
            "emulator.README.MD",
            {
                "metrics": metrics_msg,
                "dessert": self.sysimg.codename(),
                "tag": self.sysimg.tag(),
                "container_id": self.tag,
                "emu_build_id": self.emulator.build_id(),
            },
            rename_as="README.MD",
        )

        extra += " {}".format(self.sysimg.logger_flags())
        self.writer.write_template("launch-emulator.sh", {
            "extra": extra,
            "version": emu.__version__
        })
        self.writer.write_template("default.pa", {})

        self.bin_place_emulator_files(by_copying_zip_files)
        src_template = "Dockerfile.from_zip" if by_copying_zip_files else "Dockerfile"
        self.writer.write_template(
            src_template,
            {
                "user":
                "******".format(os.environ.get("USER", "unknown"),
                               socket.gethostname()),
                "tag":
                self.sysimg.tag(),
                "api":
                self.sysimg.api(),
                "abi":
                self.sysimg.abi(),
                "cpu":
                self.sysimg.cpu(),
                "gpu":
                self.sysimg.gpu(),
                "emu_zip":
                os.path.basename(self.emulator.fname),
                "emu_build_id":
                self.emulator.build_id(),
                "sysimg_zip":
                os.path.basename(self.sysimg.fname),
                "date":
                date,
                "from_base_img":
                self.base_img,
            },
            rename_as="Dockerfile",
        )