def try_download(self, json_spec: Dict) -> Optional[BotJsonMeta]: bot_spec = BotPlayer.parse_meta(json_spec) base_dir = f'{self.bot_dir}/{bot_spec.name}' try: os.makedirs(base_dir, exist_ok=False) # use http because local network on CTU has broken records # and it should work everywhere... download_extract_zip(bot_spec.botBinary.replace("https", "http"), f'{base_dir}/AI') download_file(bot_spec.bwapiDLL.replace("https", "http"), f'{base_dir}/BWAPI.dll') os.makedirs(f'{base_dir}/read', exist_ok=False) os.makedirs(f'{base_dir}/write', exist_ok=False) with open(f'{base_dir}/bot.json', 'w') as f: json.dump(json_spec, f) return bot_spec except Exception as e: logger.exception(f"Failed to process bot {bot_spec.name}") logger.exception(e) logger.info(f"Cleaning up dir {base_dir}") shutil.rmtree(base_dir) return None
def ensure_local_image( local_image: str, parent_image: str = SC_PARENT_IMAGE, java_image: str = SC_JAVA_IMAGE, starcraft_base_dir: str = SCBW_BASE_DIR, starcraft_binary_link: str = SC_BINARY_LINK, ) -> None: """ Check if `local_image` is present locally. If it is not, pull parent images and build. This includes pulling starcraft binary. :raises docker.errors.ImageNotFound :raises docker.errors.APIError """ logger.info(f"checking if there is local image {local_image}") docker_images = docker_client.images.list(local_image) if len(docker_images) and docker_images[0].short_id is not None: logger.info(f"image {local_image} found locally.") return logger.info("image not found locally, creating...") pkg_docker_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "local_docker") base_dir = os.path.join(starcraft_base_dir, "docker") logger.info(f"copying files from {pkg_docker_dir} to {base_dir}.") distutils.dir_util.copy_tree(pkg_docker_dir, base_dir) starcraft_zip_file = f"{base_dir}/starcraft.zip" if not os.path.exists(starcraft_zip_file): logger.info(f"downloading starcraft.zip to {starcraft_zip_file}") download_file(starcraft_binary_link, starcraft_zip_file) logger.info(f"pulling image {parent_image}, this may take a while...") pulled_image = docker_client.images.pull(parent_image) pulled_image.tag(java_image) logger.info( f"building local image {local_image}, this may take a while...") docker_client.images.build(path=base_dir, dockerfile="game.dockerfile", tag=local_image) logger.info(f"successfully built image {local_image}")