This function takes care of approximately half of the job that was up until now handled by _action_dockerfile(), namely taking a populated dictionary and turning it into a Dockerfile. Signed-off-by: Andrea Bolognani <abologna@xxxxxxxxxx> --- guests/lcitool | 192 +++++++++++++++++++++++++------------------------ 1 file changed, 100 insertions(+), 92 deletions(-) diff --git a/guests/lcitool b/guests/lcitool index 6da2206..f216910 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -642,102 +642,11 @@ class Application: with open(keyfile, "r") as r: return r.read().rstrip() - def _action_dockerfile(self, args): - mappings = self._projects.get_mappings() - pip_mappings = self._projects.get_pip_mappings() - - hosts = self._inventory.expand_pattern(args.hosts) - if len(hosts) > 1: - raise Exception("Can't generate Dockerfile for multiple hosts") - host = hosts[0] - - facts = self._inventory.get_facts(host) + def _dockerfile_format(self, facts, cross_arch, varmap): package_format = facts["package_format"] package_manager = facts["package_manager"] os_name = facts["os_name"] os_version = facts["os_version"] - os_full = os_name + os_version - cross_arch = args.cross_arch - - if package_format not in ["deb", "rpm"]: - raise Exception("Host {} doesn't support Dockerfiles".format(host)) - if cross_arch: - if os_name != "Debian": - raise Exception("Cannot cross compile on {}".format(os_name)) - if cross_arch == self._native_arch: - raise Exception("Cross arch {} should differ from native {}". - format(cross_arch, self._native_arch)) - - projects = self._projects.expand_pattern(args.projects) - for project in projects: - if project not in facts["projects"]: - raise Exception( - "Host {} doesn't support project {}".format( - host, - project, - ) - ) - - pkgs = {} - cross_pkgs = {} - pip_pkgs = {} - base_keys = ["default", package_format, os_name, os_full] - cross_keys = [] - if cross_arch: - keys = base_keys + [cross_arch + "-" + k for k in base_keys] - cross_keys = ["cross-policy-" + k for k in base_keys] - else: - keys = base_keys + [self._native_arch + "-" + k for k in base_keys] - - # We need to add the base project manually here: the standard - # machinery hides it because it's an implementation detail - for project in projects + ["base"]: - for package in self._projects.get_packages(project): - cross_policy = "native" - for key in cross_keys: - if key in mappings[package]: - cross_policy = mappings[package][key] - if cross_policy not in ["native", "foreign", "skip"]: - raise Exception( - "Unexpected cross arch policy {} for {}".format - (cross_policy, package)) - - for key in keys: - if key in mappings[package]: - pkgs[package] = mappings[package][key] - if package in pip_mappings and key in pip_mappings[package]: - pip_pkgs[package] = pip_mappings[package][key] - - if package not in pkgs: - continue - if package in pip_pkgs and pkgs[package] is not None: - del pip_pkgs[package] - if cross_policy == "foreign" and pkgs[package] is not None: - cross_pkgs[package] = pkgs[package] - if pkgs[package] is None or cross_policy in ["skip", "foreign"]: - del pkgs[package] - - pkg_align = " \\\n" + (" " * len("RUN " + package_manager + " ")) - pip_pkg_align = " \\\n" + (" " * len("RUN pip3 ")) - - varmap = {} - varmap["package_manager"] = package_manager - varmap["pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkgs.values()))) - - if pip_pkgs: - varmap["pip_pkgs"] = pip_pkg_align[1:] + pip_pkg_align.join(sorted(set(pip_pkgs.values()))) - - if package_format == "deb" and args.cross_arch: - deb_arch = Util.native_arch_to_deb_arch(args.cross_arch) - abi = Util.native_arch_to_abi(args.cross_arch) - lib = Util.native_arch_to_lib(args.cross_arch) - gcc = "gcc-" + abi - varmap["cross_arch"] = deb_arch - pkg_names = [p + ":" + deb_arch for p in cross_pkgs.values()] - pkg_names.append(gcc) - varmap["cross_pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkg_names))) - varmap["cross_abi"] = abi - varmap["cross_lib"] = lib print("FROM {}".format(facts["docker_base"])) @@ -842,6 +751,105 @@ class Application: ENV PKG_CONFIG_LIBDIR "/usr/lib/{cross_lib}/pkgconfig" """).format(**varmap)) + def _action_dockerfile(self, args): + mappings = self._projects.get_mappings() + pip_mappings = self._projects.get_pip_mappings() + + hosts = self._inventory.expand_pattern(args.hosts) + if len(hosts) > 1: + raise Exception("Can't generate Dockerfile for multiple hosts") + host = hosts[0] + + facts = self._inventory.get_facts(host) + package_format = facts["package_format"] + package_manager = facts["package_manager"] + os_name = facts["os_name"] + os_version = facts["os_version"] + os_full = os_name + os_version + cross_arch = args.cross_arch + + if package_format not in ["deb", "rpm"]: + raise Exception("Host {} doesn't support Dockerfiles".format(host)) + if cross_arch: + if os_name != "Debian": + raise Exception("Cannot cross compile on {}".format(os_name)) + if cross_arch == self._native_arch: + raise Exception("Cross arch {} should differ from native {}". + format(cross_arch, self._native_arch)) + + projects = self._projects.expand_pattern(args.projects) + for project in projects: + if project not in facts["projects"]: + raise Exception( + "Host {} doesn't support project {}".format( + host, + project, + ) + ) + + pkgs = {} + cross_pkgs = {} + pip_pkgs = {} + base_keys = ["default", package_format, os_name, os_full] + cross_keys = [] + if cross_arch: + keys = base_keys + [cross_arch + "-" + k for k in base_keys] + cross_keys = ["cross-policy-" + k for k in base_keys] + else: + keys = base_keys + [self._native_arch + "-" + k for k in base_keys] + + # We need to add the base project manually here: the standard + # machinery hides it because it's an implementation detail + for project in projects + ["base"]: + for package in self._projects.get_packages(project): + cross_policy = "native" + for key in cross_keys: + if key in mappings[package]: + cross_policy = mappings[package][key] + if cross_policy not in ["native", "foreign", "skip"]: + raise Exception( + "Unexpected cross arch policy {} for {}".format + (cross_policy, package)) + + for key in keys: + if key in mappings[package]: + pkgs[package] = mappings[package][key] + if package in pip_mappings and key in pip_mappings[package]: + pip_pkgs[package] = pip_mappings[package][key] + + if package not in pkgs: + continue + if package in pip_pkgs and pkgs[package] is not None: + del pip_pkgs[package] + if cross_policy == "foreign" and pkgs[package] is not None: + cross_pkgs[package] = pkgs[package] + if pkgs[package] is None or cross_policy in ["skip", "foreign"]: + del pkgs[package] + + pkg_align = " \\\n" + (" " * len("RUN " + package_manager + " ")) + pip_pkg_align = " \\\n" + (" " * len("RUN pip3 ")) + + varmap = {} + varmap["package_manager"] = package_manager + varmap["pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkgs.values()))) + + if package_format == "deb" and cross_arch: + deb_arch = Util.native_arch_to_deb_arch(cross_arch) + abi = Util.native_arch_to_abi(cross_arch) + lib = Util.native_arch_to_lib(cross_arch) + gcc = "gcc-" + abi + varmap["cross_arch"] = deb_arch + pkg_names = [p + ":" + deb_arch for p in cross_pkgs.values()] + pkg_names.append(gcc) + varmap["cross_pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkg_names))) + varmap["cross_abi"] = abi + varmap["cross_lib"] = lib + + if pip_pkgs: + varmap["pip_pkgs"] = pip_pkg_align[1:] + pip_pkg_align.join(sorted(set(pip_pkgs.values()))) + + self._dockerfile_format(facts, cross_arch, varmap) + def run(self): args = self._parser.parse_args() if args.debug: -- 2.24.1