Owen W. Taylor 97338cf
From 0ef58ca01d680c74f9776bf593990e8e9cadb98d Mon Sep 17 00:00:00 2001
Owen W. Taylor 97338cf
From: "Owen W. Taylor" <otaylor@fishsoup.net>
Owen W. Taylor 97338cf
Date: Tue, 31 Jul 2018 09:14:35 -0400
Owen W. Taylor 97338cf
Subject: [PATCH 3/3] Move to using flatpak-module-tools as a library
Owen W. Taylor 97338cf
Owen W. Taylor 97338cf
https://github.com/projectatomic/atomic-reactor/pull/1052
Owen W. Taylor 97338cf
Owen W. Taylor 97338cf
Instead of sharing code cut-and-paste between atomic-reactor and
Owen W. Taylor 97338cf
https://pagure.io/flatpak-module-tools, make atomic-reactor use
Owen W. Taylor 97338cf
flatpak-module-tools as a library for the shared code. This will
Owen W. Taylor 97338cf
prevent the code getting out of sync, and make future maintenance
Owen W. Taylor 97338cf
easier.
Owen W. Taylor 97338cf
Owen W. Taylor 97338cf
The flatpak-module-tools version has major improvements to how
Owen W. Taylor 97338cf
the filesystem tree is postprocessed to make a Flatpak using configuration
Owen W. Taylor 97338cf
options from container.yaml, needing some small adjustments of the
Owen W. Taylor 97338cf
tests here.
Owen W. Taylor 97338cf
---
Owen W. Taylor 97338cf
 atomic_reactor/plugins/exit_koji_import.py         |   6 +-
Owen W. Taylor 97338cf
 atomic_reactor/plugins/exit_koji_promote.py        |   6 +-
Owen W. Taylor 97338cf
 .../plugins/pre_flatpak_create_dockerfile.py       | 152 ++-------
Owen W. Taylor 97338cf
 .../plugins/pre_resolve_module_compose.py          |  12 +-
Owen W. Taylor 97338cf
 .../plugins/prepub_flatpak_create_oci.py           | 368 +--------------------
Owen W. Taylor 97338cf
 images/dockerhost-builder/Dockerfile               |   2 +-
Owen W. Taylor 97338cf
 images/privileged-builder/Dockerfile               |   2 +-
Owen W. Taylor 97338cf
 requirements-flatpak.txt                           |   1 +
Owen W. Taylor 97338cf
 tests/flatpak.py                                   |  17 +-
Owen W. Taylor 97338cf
 9 files changed, 57 insertions(+), 509 deletions(-)
Owen W. Taylor 97338cf
Owen W. Taylor 97338cf
diff --git a/atomic_reactor/plugins/exit_koji_import.py b/atomic_reactor/plugins/exit_koji_import.py
Owen W. Taylor 97338cf
index 4b1e1c9..e59b557 100644
Owen W. Taylor 97338cf
--- a/atomic_reactor/plugins/exit_koji_import.py
Owen W. Taylor 97338cf
+++ b/atomic_reactor/plugins/exit_koji_import.py
Owen W. Taylor 97338cf
@@ -24,6 +24,7 @@ from atomic_reactor.plugins.pre_reactor_config import get_openshift_session
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 try:
Owen W. Taylor 97338cf
     from atomic_reactor.plugins.pre_flatpak_create_dockerfile import get_flatpak_source_info
Owen W. Taylor 97338cf
+    from atomic_reactor.plugins.pre_resolve_module_compose import get_compose_info
Owen W. Taylor 97338cf
 except ImportError:
Owen W. Taylor 97338cf
     # modulemd and/or pdc_client not available
Owen W. Taylor 97338cf
     def get_flatpak_source_info(_):
Owen W. Taylor 97338cf
@@ -353,7 +354,10 @@ class KojiImportPlugin(ExitPlugin):
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         flatpak_source_info = get_flatpak_source_info(self.workflow)
Owen W. Taylor 97338cf
         if flatpak_source_info is not None:
Owen W. Taylor 97338cf
-            extra['image'].update(flatpak_source_info.koji_metadata())
Owen W. Taylor 97338cf
+            compose_info = get_compose_info(self.workflow)
Owen W. Taylor 97338cf
+            koji_metadata = compose_info.koji_metadata()
Owen W. Taylor 97338cf
+            koji_metadata['flatpak'] = True
Owen W. Taylor 97338cf
+            extra['image'].update(koji_metadata)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         koji_task_owner = get_koji_task_owner(self.session, koji_task_id).get('name')
Owen W. Taylor 97338cf
         extra['submitter'] = self.session.getLoggedInUser()['name']
Owen W. Taylor 97338cf
diff --git a/atomic_reactor/plugins/exit_koji_promote.py b/atomic_reactor/plugins/exit_koji_promote.py
Owen W. Taylor 97338cf
index da33380..0fe1f47 100644
Owen W. Taylor 97338cf
--- a/atomic_reactor/plugins/exit_koji_promote.py
Owen W. Taylor 97338cf
+++ b/atomic_reactor/plugins/exit_koji_promote.py
Owen W. Taylor 97338cf
@@ -30,6 +30,7 @@ from atomic_reactor.util import get_parent_image_koji_data
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 try:
Owen W. Taylor 97338cf
     from atomic_reactor.plugins.pre_flatpak_create_dockerfile import get_flatpak_source_info
Owen W. Taylor 97338cf
+    from atomic_reactor.plugins.pre_resolve_module_compose import get_compose_info
Owen W. Taylor 97338cf
 except ImportError:
Owen W. Taylor 97338cf
     # modulemd and/or pdc_client not available
Owen W. Taylor 97338cf
     def get_flatpak_source_info(_):
Owen W. Taylor 97338cf
@@ -564,7 +565,10 @@ class KojiPromotePlugin(ExitPlugin):
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         flatpak_source_info = get_flatpak_source_info(self.workflow)
Owen W. Taylor 97338cf
         if flatpak_source_info is not None:
Owen W. Taylor 97338cf
-            extra['image'].update(flatpak_source_info.koji_metadata())
Owen W. Taylor 97338cf
+            compose_info = get_compose_info(self.workflow)
Owen W. Taylor 97338cf
+            koji_metadata = compose_info.koji_metadata()
Owen W. Taylor 97338cf
+            koji_metadata['flatpak'] = True
Owen W. Taylor 97338cf
+            extra['image'].update(koji_metadata)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         resolve_comp_result = self.workflow.prebuild_results.get(PLUGIN_RESOLVE_COMPOSES_KEY)
Owen W. Taylor 97338cf
         if resolve_comp_result:
Owen W. Taylor 97338cf
diff --git a/atomic_reactor/plugins/pre_flatpak_create_dockerfile.py b/atomic_reactor/plugins/pre_flatpak_create_dockerfile.py
Owen W. Taylor 97338cf
index 148be70..a53e0da 100644
Owen W. Taylor 97338cf
--- a/atomic_reactor/plugins/pre_flatpak_create_dockerfile.py
Owen W. Taylor 97338cf
+++ b/atomic_reactor/plugins/pre_flatpak_create_dockerfile.py
Owen W. Taylor 97338cf
@@ -20,6 +20,8 @@ Example configuration:
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 import os
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
+from flatpak_module_tools.flatpak_builder import FlatpakSourceInfo, FlatpakBuilder
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
 from atomic_reactor.constants import DOCKERFILE_FILENAME, YUM_REPOS_DIR
Owen W. Taylor 97338cf
 from atomic_reactor.plugin import PreBuildPlugin
Owen W. Taylor 97338cf
 from atomic_reactor.plugins.pre_resolve_module_compose import get_compose_info
Owen W. Taylor 97338cf
@@ -52,67 +54,6 @@ RUN chroot /var/tmp/flatpak-build/ /bin/sh /tmp/cleanup.sh
Owen W. Taylor 97338cf
 '''
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-class FlatpakSourceInfo(object):
Owen W. Taylor 97338cf
-    def __init__(self, flatpak_yaml, compose):
Owen W. Taylor 97338cf
-        self.flatpak_yaml = flatpak_yaml
Owen W. Taylor 97338cf
-        self.compose = compose
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        mmd = compose.base_module.mmd
Owen W. Taylor 97338cf
-        # A runtime module must have a 'runtime' profile, but can have other
Owen W. Taylor 97338cf
-        # profiles for SDKs, minimal runtimes, etc.
Owen W. Taylor 97338cf
-        self.runtime = 'runtime' in mmd.peek_profiles()
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        module_spec = split_module_spec(compose.source_spec)
Owen W. Taylor 97338cf
-        if module_spec.profile:
Owen W. Taylor 97338cf
-            self.profile = module_spec.profile
Owen W. Taylor 97338cf
-        elif self.runtime:
Owen W. Taylor 97338cf
-            self.profile = 'runtime'
Owen W. Taylor 97338cf
-        else:
Owen W. Taylor 97338cf
-            self.profile = 'default'
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        assert self.profile in mmd.peek_profiles()
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    # The module for the Flatpak runtime that this app runs against
Owen W. Taylor 97338cf
-    @property
Owen W. Taylor 97338cf
-    def runtime_module(self):
Owen W. Taylor 97338cf
-        assert not self.runtime
Owen W. Taylor 97338cf
-        compose = self.compose
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        dependencies = compose.base_module.mmd.props.dependencies
Owen W. Taylor 97338cf
-        # A built module should have its dependencies already expanded
Owen W. Taylor 97338cf
-        assert len(dependencies) == 1
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        for key in dependencies[0].peek_buildrequires().keys():
Owen W. Taylor 97338cf
-            try:
Owen W. Taylor 97338cf
-                module = compose.modules[key]
Owen W. Taylor 97338cf
-                if 'runtime' in module.mmd.peek_profiles():
Owen W. Taylor 97338cf
-                    return module
Owen W. Taylor 97338cf
-            except KeyError:
Owen W. Taylor 97338cf
-                pass
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        raise RuntimeError("Failed to identify runtime module in the buildrequires for {}"
Owen W. Taylor 97338cf
-                           .format(compose.base_module.name))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    # All modules that were build against the Flatpak runtime,
Owen W. Taylor 97338cf
-    # and thus were built with prefix=/app. This is primarily the app module
Owen W. Taylor 97338cf
-    # but might contain modules shared between multiple flatpaks as well.
Owen W. Taylor 97338cf
-    @property
Owen W. Taylor 97338cf
-    def app_modules(self):
Owen W. Taylor 97338cf
-        runtime_module_name = self.runtime_module.mmd.props.name
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        def is_app_module(m):
Owen W. Taylor 97338cf
-            dependencies = m.mmd.props.dependencies
Owen W. Taylor 97338cf
-            return runtime_module_name in dependencies[0].peek_buildrequires()
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return [m for m in self.compose.modules.values() if is_app_module(m)]
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    def koji_metadata(self):
Owen W. Taylor 97338cf
-        metadata = self.compose.koji_metadata()
Owen W. Taylor 97338cf
-        metadata['flatpak'] = True
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return metadata
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
 WORKSPACE_SOURCE_KEY = 'source_info'
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
@@ -157,7 +98,12 @@ class FlatpakCreateDockerfilePlugin(PreBuildPlugin):
Owen W. Taylor 97338cf
             raise RuntimeError(
Owen W. Taylor 97338cf
                 "resolve_module_compose must be run before flatpak_create_dockerfile")
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        return FlatpakSourceInfo(flatpak_yaml, compose_info)
Owen W. Taylor 97338cf
+        module_spec = split_module_spec(compose_info.source_spec)
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
+        return FlatpakSourceInfo(flatpak_yaml,
Owen W. Taylor 97338cf
+                                 compose_info.modules,
Owen W. Taylor 97338cf
+                                 compose_info.base_module,
Owen W. Taylor 97338cf
+                                 module_spec.profile)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
     def run(self):
Owen W. Taylor 97338cf
         """
Owen W. Taylor 97338cf
@@ -168,38 +114,18 @@ class FlatpakCreateDockerfilePlugin(PreBuildPlugin):
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         set_flatpak_source_info(self.workflow, source)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        module_info = source.compose.base_module
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        # For a runtime, certain information is duplicated between the container.yaml
Owen W. Taylor 97338cf
-        # and the modulemd, check that it matches
Owen W. Taylor 97338cf
-        if source.runtime:
Owen W. Taylor 97338cf
-            flatpak_yaml = source.flatpak_yaml
Owen W. Taylor 97338cf
-            flatpak_xmd = module_info.mmd.props.xmd['flatpak']
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            def check(condition, what):
Owen W. Taylor 97338cf
-                if not condition:
Owen W. Taylor 97338cf
-                    raise RuntimeError(
Owen W. Taylor 97338cf
-                        "Mismatch for {} betweeen module xmd and container.yaml".format(what))
Owen W. Taylor 97338cf
+        builder = FlatpakBuilder(source, None, None)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-            check(flatpak_yaml['branch'] == flatpak_xmd['branch'], "'branch'")
Owen W. Taylor 97338cf
-            check(source.profile in flatpak_xmd['runtimes'], 'profile name')
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            profile_xmd = flatpak_xmd['runtimes'][source.profile]
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            check(flatpak_yaml['id'] == profile_xmd['id'], "'id'")
Owen W. Taylor 97338cf
-            check(flatpak_yaml.get('runtime', None) ==
Owen W. Taylor 97338cf
-                  profile_xmd.get('runtime', None), "'runtime'")
Owen W. Taylor 97338cf
-            check(flatpak_yaml.get('sdk', None) == profile_xmd.get('sdk', None), "'sdk'")
Owen W. Taylor 97338cf
+        builder.precheck()
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         # Create the dockerfile
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
+        module_info = source.base_module
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
         # We need to enable all the modules other than the platform pseudo-module
Owen W. Taylor 97338cf
-        modules_str = ' '.join(sorted(m.mmd.props.name + ':' + m.mmd.props.stream
Owen W. Taylor 97338cf
-                                      for m in source.compose.modules.values()
Owen W. Taylor 97338cf
-                                      if m.mmd.props.name != 'platform'))
Owen W. Taylor 97338cf
+        modules_str = ' '.join(builder.get_enable_modules())
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        install_packages = module_info.mmd.peek_profiles()[source.profile].props.rpms.get()
Owen W. Taylor 97338cf
-        install_packages_str = ' '.join(install_packages)
Owen W. Taylor 97338cf
+        install_packages_str = ' '.join(builder.get_install_packages())
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         df_path = os.path.join(self.workflow.builder.df_dir, DOCKERFILE_FILENAME)
Owen W. Taylor 97338cf
         with open(df_path, 'w') as fp:
Owen W. Taylor 97338cf
@@ -213,54 +139,16 @@ class FlatpakCreateDockerfilePlugin(PreBuildPlugin):
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         self.workflow.builder.set_df_path(df_path)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        # For a runtime, we want to make sure that the set of RPMs that is installed
Owen W. Taylor 97338cf
-        # into the filesystem is *exactly* the set that is listed in the runtime
Owen W. Taylor 97338cf
-        # profile. Requiring the full listed set of RPMs to be listed makes it
Owen W. Taylor 97338cf
-        # easier to catch unintentional changes in the package list that might break
Owen W. Taylor 97338cf
-        # applications depending on the runtime. It also simplifies the checking we
Owen W. Taylor 97338cf
-        # do for application flatpaks, since we can simply look at the runtime
Owen W. Taylor 97338cf
-        # modulemd to find out what packages are present in the runtime.
Owen W. Taylor 97338cf
-        #
Owen W. Taylor 97338cf
-        # For an application, we want to make sure that each RPM that is installed
Owen W. Taylor 97338cf
-        # into the filesystem is *either* an RPM that is part of the 'runtime'
Owen W. Taylor 97338cf
-        # profile of the base runtime, or from a module that was built with
Owen W. Taylor 97338cf
-        # flatpak-rpm-macros in the install root and, thus, prefix=/app.
Owen W. Taylor 97338cf
-        #
Owen W. Taylor 97338cf
-        # We achieve this by restricting the set of available packages in the dnf
Owen W. Taylor 97338cf
-        # configuration to just the ones that we want.
Owen W. Taylor 97338cf
-        #
Owen W. Taylor 97338cf
-        # The advantage of doing this upfront, rather than just checking after the
Owen W. Taylor 97338cf
-        # fact is that this makes sure that when a application is being installed,
Owen W. Taylor 97338cf
-        # we don't get a different package to satisfy a dependency than the one
Owen W. Taylor 97338cf
-        # in the runtime - e.g. aajohan-comfortaa-fonts to satisfy font(:lang=en)
Owen W. Taylor 97338cf
-        # because it's alphabetically first.
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        if not source.runtime:
Owen W. Taylor 97338cf
-            runtime_module = source.runtime_module
Owen W. Taylor 97338cf
-            runtime_profile = runtime_module.mmd.peek_profiles()['runtime']
Owen W. Taylor 97338cf
-            available_packages = sorted(runtime_profile.props.rpms.get())
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            for m in source.app_modules:
Owen W. Taylor 97338cf
-                # Strip off the '.rpm' suffix from the filename to get something
Owen W. Taylor 97338cf
-                # that DNF can parse.
Owen W. Taylor 97338cf
-                available_packages.extend(x[:-4] for x in m.rpms)
Owen W. Taylor 97338cf
-        else:
Owen W. Taylor 97338cf
-            base_module = source.compose.base_module
Owen W. Taylor 97338cf
-            runtime_profile = base_module.mmd.peek_profiles()['runtime']
Owen W. Taylor 97338cf
-            available_packages = sorted(runtime_profile.props.rpms.get())
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
+        includepkgs = builder.get_includepkgs()
Owen W. Taylor 97338cf
         includepkgs_path = os.path.join(self.workflow.builder.df_dir, 'atomic-reactor-includepkgs')
Owen W. Taylor 97338cf
         with open(includepkgs_path, 'w') as f:
Owen W. Taylor 97338cf
-            f.write('includepkgs = ' + ','.join(available_packages) + '\n')
Owen W. Taylor 97338cf
+            f.write('includepkgs = ' + ','.join(includepkgs) + '\n')
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         # Create the cleanup script
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         cleanupscript = os.path.join(self.workflow.builder.df_dir, "cleanup.sh")
Owen W. Taylor 97338cf
         with open(cleanupscript, 'w') as f:
Owen W. Taylor 97338cf
-            cleanup_commands = source.flatpak_yaml.get('cleanup-commands')
Owen W. Taylor 97338cf
-            if cleanup_commands is not None:
Owen W. Taylor 97338cf
-                f.write(cleanup_commands.rstrip())
Owen W. Taylor 97338cf
-                f.write("\n")
Owen W. Taylor 97338cf
+            f.write(builder.get_cleanup_script())
Owen W. Taylor 97338cf
         os.chmod(cleanupscript, 0o0755)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         # Add a yum-repository pointing to the compose
Owen W. Taylor 97338cf
@@ -270,9 +158,11 @@ class FlatpakCreateDockerfilePlugin(PreBuildPlugin):
Owen W. Taylor 97338cf
             stream=module_info.stream,
Owen W. Taylor 97338cf
             version=module_info.version)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
+        compose_info = get_compose_info(self.workflow)
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
         repo = {
Owen W. Taylor 97338cf
             'name': repo_name,
Owen W. Taylor 97338cf
-            'baseurl': source.compose.repo_url,
Owen W. Taylor 97338cf
+            'baseurl': compose_info.repo_url,
Owen W. Taylor 97338cf
             'enabled': 1,
Owen W. Taylor 97338cf
             'gpgcheck': 0,
Owen W. Taylor 97338cf
         }
Owen W. Taylor 97338cf
@@ -280,4 +170,4 @@ class FlatpakCreateDockerfilePlugin(PreBuildPlugin):
Owen W. Taylor 97338cf
         path = os.path.join(YUM_REPOS_DIR, repo_name + '.repo')
Owen W. Taylor 97338cf
         self.workflow.files[path] = render_yum_repo(repo, escape_dollars=False)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        override_build_kwarg(self.workflow, 'module_compose_id', source.compose.compose_id)
Owen W. Taylor 97338cf
+        override_build_kwarg(self.workflow, 'module_compose_id', compose_info.compose_id)
Owen W. Taylor 97338cf
diff --git a/atomic_reactor/plugins/pre_resolve_module_compose.py b/atomic_reactor/plugins/pre_resolve_module_compose.py
Owen W. Taylor 97338cf
index 5ce6e29..df5f040 100644
Owen W. Taylor 97338cf
--- a/atomic_reactor/plugins/pre_resolve_module_compose.py
Owen W. Taylor 97338cf
+++ b/atomic_reactor/plugins/pre_resolve_module_compose.py
Owen W. Taylor 97338cf
@@ -23,6 +23,9 @@ Example configuration:
Owen W. Taylor 97338cf
 }
Owen W. Taylor 97338cf
 """
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
+from flatpak_module_tools.flatpak_builder import ModuleInfo
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
 import gi
Owen W. Taylor 97338cf
 try:
Owen W. Taylor 97338cf
     gi.require_version('Modulemd', '1.0')
Owen W. Taylor 97338cf
@@ -37,15 +40,6 @@ from atomic_reactor.plugins.pre_reactor_config import (get_pdc_session, get_odcs
Owen W. Taylor 97338cf
                                                        get_pdc, get_odcs)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-class ModuleInfo(object):
Owen W. Taylor 97338cf
-    def __init__(self, name, stream, version, mmd, rpms):
Owen W. Taylor 97338cf
-        self.name = name
Owen W. Taylor 97338cf
-        self.stream = stream
Owen W. Taylor 97338cf
-        self.version = version
Owen W. Taylor 97338cf
-        self.mmd = mmd
Owen W. Taylor 97338cf
-        self.rpms = rpms
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
 class ComposeInfo(object):
Owen W. Taylor 97338cf
     def __init__(self, source_spec, compose_id, base_module, modules, repo_url):
Owen W. Taylor 97338cf
         self.source_spec = source_spec
Owen W. Taylor 97338cf
diff --git a/atomic_reactor/plugins/prepub_flatpak_create_oci.py b/atomic_reactor/plugins/prepub_flatpak_create_oci.py
Owen W. Taylor 97338cf
index b86e792..d1b6463 100644
Owen W. Taylor 97338cf
--- a/atomic_reactor/plugins/prepub_flatpak_create_oci.py
Owen W. Taylor 97338cf
+++ b/atomic_reactor/plugins/prepub_flatpak_create_oci.py
Owen W. Taylor 97338cf
@@ -10,14 +10,7 @@ pre_flatpak_create_dockerfile, extracts the tree at /var/tmp/flatpak-build
Owen W. Taylor 97338cf
 and turns it into a Flatpak application or runtime.
Owen W. Taylor 97338cf
 """
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-import os
Owen W. Taylor 97338cf
-from six.moves import configparser
Owen W. Taylor 97338cf
-import re
Owen W. Taylor 97338cf
-import shlex
Owen W. Taylor 97338cf
-import shutil
Owen W. Taylor 97338cf
-import subprocess
Owen W. Taylor 97338cf
-import tarfile
Owen W. Taylor 97338cf
-from textwrap import dedent
Owen W. Taylor 97338cf
+from flatpak_module_tools.flatpak_builder import FlatpakBuilder
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 from atomic_reactor.constants import IMAGE_TYPE_OCI, IMAGE_TYPE_OCI_TAR
Owen W. Taylor 97338cf
 from atomic_reactor.plugin import PrePublishPlugin
Owen W. Taylor 97338cf
@@ -26,99 +19,6 @@ from atomic_reactor.rpm_util import parse_rpm_output
Owen W. Taylor 97338cf
 from atomic_reactor.util import get_exported_image_metadata
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-# Returns flatpak's name for the current arch
Owen W. Taylor 97338cf
-def get_arch():
Owen W. Taylor 97338cf
-    return subprocess.check_output(['flatpak', '--default-arch'],
Owen W. Taylor 97338cf
-                                   universal_newlines=True).strip()
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-# flatpak build-init requires the sdk and runtime to be installed on the
Owen W. Taylor 97338cf
-# build system (so that subsequent build steps can execute things with
Owen W. Taylor 97338cf
-# the SDK). While it isn't impossible to download the runtime image and
Owen W. Taylor 97338cf
-# install the flatpak, that would be a lot of unnecessary complexity
Owen W. Taylor 97338cf
-# since our build step is just unpacking the filesystem we've already
Owen W. Taylor 97338cf
-# created. This is a stub implementation of 'flatpak build-init' that
Owen W. Taylor 97338cf
-# doesn't check for the SDK or use it to set up the build filesystem.
Owen W. Taylor 97338cf
-def build_init(directory, appname, sdk, runtime, runtime_branch, tags=[]):
Owen W. Taylor 97338cf
-    if not os.path.isdir(directory):
Owen W. Taylor 97338cf
-        os.mkdir(directory)
Owen W. Taylor 97338cf
-    with open(os.path.join(directory, "metadata"), "w") as f:
Owen W. Taylor 97338cf
-        f.write(dedent("""\
Owen W. Taylor 97338cf
-                       [Application]
Owen W. Taylor 97338cf
-                       name={appname}
Owen W. Taylor 97338cf
-                       runtime={runtime}/{arch}/{runtime_branch}
Owen W. Taylor 97338cf
-                       sdk={sdk}/{arch}/{runtime_branch}
Owen W. Taylor 97338cf
-                       """.format(appname=appname,
Owen W. Taylor 97338cf
-                                  sdk=sdk,
Owen W. Taylor 97338cf
-                                  runtime=runtime,
Owen W. Taylor 97338cf
-                                  runtime_branch=runtime_branch,
Owen W. Taylor 97338cf
-                                  arch=get_arch())))
Owen W. Taylor 97338cf
-        if tags:
Owen W. Taylor 97338cf
-            f.write("tags=" + ";".join(tags) + "\n")
Owen W. Taylor 97338cf
-    os.mkdir(os.path.join(directory, "files"))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-# add_app_prefix('org.gimp', 'gimp, 'gimp.desktop') => org.gimp.desktop
Owen W. Taylor 97338cf
-# add_app_prefix('org.gnome', 'eog, 'eog.desktop') => org.gnome.eog.desktop
Owen W. Taylor 97338cf
-def add_app_prefix(app_id, root, full):
Owen W. Taylor 97338cf
-    prefix = app_id
Owen W. Taylor 97338cf
-    if prefix.endswith('.' + root):
Owen W. Taylor 97338cf
-        prefix = prefix[:-(1 + len(root))]
Owen W. Taylor 97338cf
-    return prefix + '.' + full
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-def find_desktop_files(builddir):
Owen W. Taylor 97338cf
-    desktopdir = os.path.join(builddir, 'files/share/applications')
Owen W. Taylor 97338cf
-    for (dirpath, dirnames, filenames) in os.walk(desktopdir):
Owen W. Taylor 97338cf
-        for filename in filenames:
Owen W. Taylor 97338cf
-            if filename.endswith('.desktop'):
Owen W. Taylor 97338cf
-                yield os.path.join(dirpath, filename)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-def find_icons(builddir, name):
Owen W. Taylor 97338cf
-    icondir = os.path.join(builddir, 'files/share/icons/hicolor')
Owen W. Taylor 97338cf
-    for (dirpath, dirnames, filenames) in os.walk(icondir):
Owen W. Taylor 97338cf
-        for filename in filenames:
Owen W. Taylor 97338cf
-            if filename.startswith(name + '.'):
Owen W. Taylor 97338cf
-                yield os.path.join(dirpath, filename)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-def update_desktop_files(app_id, builddir):
Owen W. Taylor 97338cf
-    for full_path in find_desktop_files(builddir):
Owen W. Taylor 97338cf
-        cp = configparser.RawConfigParser()
Owen W. Taylor 97338cf
-        cp.read([full_path])
Owen W. Taylor 97338cf
-        try:
Owen W. Taylor 97338cf
-            icon = cp.get('Desktop Entry', 'Icon')
Owen W. Taylor 97338cf
-        except configparser.NoOptionError:
Owen W. Taylor 97338cf
-            icon = None
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        # Does it have an icon?
Owen W. Taylor 97338cf
-        if icon and not icon.startswith(app_id):
Owen W. Taylor 97338cf
-            found_icon = False
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            # Rename any matching icons
Owen W. Taylor 97338cf
-            for icon_file in find_icons(builddir, icon):
Owen W. Taylor 97338cf
-                shutil.copy(icon_file,
Owen W. Taylor 97338cf
-                            os.path.join(os.path.dirname(icon_file),
Owen W. Taylor 97338cf
-                                         add_app_prefix(app_id, icon, os.path.basename(icon_file))))
Owen W. Taylor 97338cf
-                found_icon = True
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            # If we renamed the icon, change the desktop file
Owen W. Taylor 97338cf
-            if found_icon:
Owen W. Taylor 97338cf
-                subprocess.check_call(['desktop-file-edit',
Owen W. Taylor 97338cf
-                                       '--set-icon',
Owen W. Taylor 97338cf
-                                       add_app_prefix(app_id, icon, icon), full_path])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        # Is the desktop file not prefixed with the app id, then prefix it
Owen W. Taylor 97338cf
-        basename = os.path.basename(full_path)
Owen W. Taylor 97338cf
-        if not basename.startswith(app_id):
Owen W. Taylor 97338cf
-            shutil.move(full_path,
Owen W. Taylor 97338cf
-                        os.path.join(os.path.dirname(full_path),
Owen W. Taylor 97338cf
-                                     add_app_prefix(app_id,
Owen W. Taylor 97338cf
-                                                    basename[:-len('.desktop')],
Owen W. Taylor 97338cf
-                                                    basename)))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
 # This converts the generator provided by the export() operation to a file-like
Owen W. Taylor 97338cf
 # object with a read that we can pass to tarfile.
Owen W. Taylor 97338cf
 class StreamAdapter(object):
Owen W. Taylor 97338cf
@@ -165,133 +65,11 @@ class FlatpakCreateOciPlugin(PrePublishPlugin):
Owen W. Taylor 97338cf
         """
Owen W. Taylor 97338cf
         super(FlatpakCreateOciPlugin, self).__init__(tasker, workflow)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-    # Compiles a list of path mapping rules to a simple function that matches
Owen W. Taylor 97338cf
-    # against a list of fixed patterns, see below for rule syntax
Owen W. Taylor 97338cf
-    def _compile_target_rules(rules):
Owen W. Taylor 97338cf
-        ROOT = "var/tmp/flatpak-build"
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        patterns = []
Owen W. Taylor 97338cf
-        for source, target in rules:
Owen W. Taylor 97338cf
-            source = re.sub("^ROOT", ROOT, source)
Owen W. Taylor 97338cf
-            if source.endswith("/"):
Owen W. Taylor 97338cf
-                patterns.append((re.compile(source + "(.*)"), target, False))
Owen W. Taylor 97338cf
-                patterns.append((source[:-1], target, True))
Owen W. Taylor 97338cf
-            else:
Owen W. Taylor 97338cf
-                patterns.append((source, target, True))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        def get_target_func(self, path):
Owen W. Taylor 97338cf
-            for source, target, is_exact_match in patterns:
Owen W. Taylor 97338cf
-                if is_exact_match:
Owen W. Taylor 97338cf
-                    if source == path:
Owen W. Taylor 97338cf
-                        return target
Owen W. Taylor 97338cf
-                else:
Owen W. Taylor 97338cf
-                    m = source.match(path)
Owen W. Taylor 97338cf
-                    if m:
Owen W. Taylor 97338cf
-                        return os.path.join(target, m.group(1))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            return None
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return get_target_func
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    # Rules for mapping paths within the exported filesystem image to their
Owen W. Taylor 97338cf
-    # location in the final flatpak filesystem
Owen W. Taylor 97338cf
-    #
Owen W. Taylor 97338cf
-    # ROOT = /var/tmp/flatpak-build
Owen W. Taylor 97338cf
-    # No trailing slash - map a directory itself exactly
Owen W. Taylor 97338cf
-    # trailing slash - map a directory and everything inside of it
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    _get_target_path_runtime = _compile_target_rules([
Owen W. Taylor 97338cf
-        # We need to make sure that 'files' is created before 'files/etc',
Owen W. Taylor 97338cf
-        # which wouldn't happen if just relied on ROOT/usr/ => files.
Owen W. Taylor 97338cf
-        # Instead map ROOT => files and omit ROOT/usr
Owen W. Taylor 97338cf
-        ("ROOT", "files"),
Owen W. Taylor 97338cf
-        ("ROOT/usr", None),
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        # We map ROOT/usr => files and ROOT/etc => files/etc. This creates
Owen W. Taylor 97338cf
-        # A conflict between ROOT/usr/etc and /ROOT/etc. Just assume there
Owen W. Taylor 97338cf
-        # is nothing useful in /ROOT/usr/etc.
Owen W. Taylor 97338cf
-        ("ROOT/usr/etc/", None),
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        ("ROOT/usr/", "files"),
Owen W. Taylor 97338cf
-        ("ROOT/etc/", "files/etc")
Owen W. Taylor 97338cf
-    ])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    _get_target_path_app = _compile_target_rules([
Owen W. Taylor 97338cf
-        ("ROOT/app/", "files")
Owen W. Taylor 97338cf
-    ])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    def _get_target_path(self, export_path):
Owen W. Taylor 97338cf
-        if self.source.runtime:
Owen W. Taylor 97338cf
-            return self._get_target_path_runtime(export_path)
Owen W. Taylor 97338cf
-        else:
Owen W. Taylor 97338cf
-            return self._get_target_path_app(export_path)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
     def _export_container(self, container_id):
Owen W. Taylor 97338cf
-        outfile = os.path.join(self.workflow.source.workdir, 'filesystem.tar.gz')
Owen W. Taylor 97338cf
-        manifestfile = os.path.join(self.workflow.source.workdir, 'flatpak-build.rpm_qf')
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
         export_generator = self.tasker.d.export(container_id)
Owen W. Taylor 97338cf
         export_stream = StreamAdapter(export_generator)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        out_fileobj = open(outfile, "wb")
Owen W. Taylor 97338cf
-        compress_process = subprocess.Popen(['gzip', '-c'],
Owen W. Taylor 97338cf
-                                            stdin=subprocess.PIPE,
Owen W. Taylor 97338cf
-                                            stdout=out_fileobj)
Owen W. Taylor 97338cf
-        in_tf = tarfile.open(fileobj=export_stream, mode='r|')
Owen W. Taylor 97338cf
-        out_tf = tarfile.open(fileobj=compress_process.stdin, mode='w|')
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        for member in in_tf:
Owen W. Taylor 97338cf
-            if member.name == 'var/tmp/flatpak-build.rpm_qf':
Owen W. Taylor 97338cf
-                reader = in_tf.extractfile(member)
Owen W. Taylor 97338cf
-                with open(manifestfile, 'wb') as out:
Owen W. Taylor 97338cf
-                    out.write(reader.read())
Owen W. Taylor 97338cf
-                reader.close()
Owen W. Taylor 97338cf
-            target_name = self._get_target_path(member.name)
Owen W. Taylor 97338cf
-            if target_name is None:
Owen W. Taylor 97338cf
-                continue
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            # Match the ownership/permissions changes done by 'flatpak build-export'.
Owen W. Taylor 97338cf
-            # See commit_filter() in:
Owen W. Taylor 97338cf
-            #   https://github.com/flatpak/flatpak/blob/master/app/flatpak-builtins-build-export.c
Owen W. Taylor 97338cf
-            #
Owen W. Taylor 97338cf
-            # We'll run build-export anyways in the app case, but in the runtime case we skip
Owen W. Taylor 97338cf
-            # flatpak build-export and use ostree directly.
Owen W. Taylor 97338cf
-            member.uid = 0
Owen W. Taylor 97338cf
-            member.gid = 0
Owen W. Taylor 97338cf
-            member.uname = "root"
Owen W. Taylor 97338cf
-            member.gname = "root"
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            if member.isdir():
Owen W. Taylor 97338cf
-                member.mode = 0o0755
Owen W. Taylor 97338cf
-            elif member.mode & 0o0100:
Owen W. Taylor 97338cf
-                member.mode = 0o0755
Owen W. Taylor 97338cf
-            else:
Owen W. Taylor 97338cf
-                member.mode = 0o0644
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            member.name = target_name
Owen W. Taylor 97338cf
-            if member.islnk():
Owen W. Taylor 97338cf
-                # Hard links have full paths within the archive (no leading /)
Owen W. Taylor 97338cf
-                link_target = self._get_target_path(member.linkname)
Owen W. Taylor 97338cf
-                if link_target is None:
Owen W. Taylor 97338cf
-                    self.log.debug("Skipping %s, hard link to %s", target_name, link_target)
Owen W. Taylor 97338cf
-                    continue
Owen W. Taylor 97338cf
-                member.linkname = link_target
Owen W. Taylor 97338cf
-                out_tf.addfile(member)
Owen W. Taylor 97338cf
-            elif member.issym():
Owen W. Taylor 97338cf
-                # Symlinks have the literal link target, which will be
Owen W. Taylor 97338cf
-                # relative to the chroot and doesn't need rewriting
Owen W. Taylor 97338cf
-                out_tf.addfile(member)
Owen W. Taylor 97338cf
-            else:
Owen W. Taylor 97338cf
-                f = in_tf.extractfile(member)
Owen W. Taylor 97338cf
-                out_tf.addfile(member, fileobj=f)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        in_tf.close()
Owen W. Taylor 97338cf
-        out_tf.close()
Owen W. Taylor 97338cf
-        export_stream.close()
Owen W. Taylor 97338cf
-        compress_process.stdin.close()
Owen W. Taylor 97338cf
-        if compress_process.wait() != 0:
Owen W. Taylor 97338cf
-            raise RuntimeError("gzip failed")
Owen W. Taylor 97338cf
-        out_fileobj.close()
Owen W. Taylor 97338cf
+        outfile, manifestfile = self.builder._export_from_stream(export_stream)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         return outfile, manifestfile
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
@@ -310,148 +88,23 @@ class FlatpakCreateOciPlugin(PrePublishPlugin):
Owen W. Taylor 97338cf
             self.log.info("Cleaning up docker container")
Owen W. Taylor 97338cf
             self.tasker.d.remove_container(container_id)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-    def _get_components(self, manifest):
Owen W. Taylor 97338cf
-        with open(manifest, 'r') as f:
Owen W. Taylor 97338cf
-            lines = f.readlines()
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return parse_rpm_output(lines)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    def _filter_app_manifest(self, components):
Owen W. Taylor 97338cf
-        runtime_rpms = self.source.runtime_module.mmd.peek_profiles()['runtime'].props.rpms
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return [c for c in components if not runtime_rpms.contains(c['name'])]
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    def _create_runtime_oci(self, tarred_filesystem, outfile):
Owen W. Taylor 97338cf
-        info = self.source.flatpak_yaml
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        builddir = os.path.join(self.workflow.source.workdir, "build")
Owen W. Taylor 97338cf
-        os.mkdir(builddir)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        repo = os.path.join(self.workflow.source.workdir, "repo")
Owen W. Taylor 97338cf
-        subprocess.check_call(['ostree', 'init', '--mode=archive-z2', '--repo', repo])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        id_ = info['id']
Owen W. Taylor 97338cf
-        runtime_id = info.get('runtime', id_)
Owen W. Taylor 97338cf
-        sdk_id = info.get('sdk', id_)
Owen W. Taylor 97338cf
-        branch = info['branch']
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        args = {
Owen W. Taylor 97338cf
-            'id': id_,
Owen W. Taylor 97338cf
-            'runtime_id': runtime_id,
Owen W. Taylor 97338cf
-            'sdk_id': sdk_id,
Owen W. Taylor 97338cf
-            'arch': get_arch(),
Owen W. Taylor 97338cf
-            'branch': branch
Owen W. Taylor 97338cf
-        }
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        METADATA_TEMPLATE = dedent("""\
Owen W. Taylor 97338cf
-            [Runtime]
Owen W. Taylor 97338cf
-            name={id}
Owen W. Taylor 97338cf
-            runtime={runtime_id}/{arch}/{branch}
Owen W. Taylor 97338cf
-            sdk={sdk_id}/{arch}/{branch}
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-            [Environment]
Owen W. Taylor 97338cf
-            LD_LIBRARY_PATH=/app/lib64:/app/lib
Owen W. Taylor 97338cf
-            GI_TYPELIB_PATH=/app/lib64/girepository-1.0
Owen W. Taylor 97338cf
-            """)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        with open(os.path.join(builddir, 'metadata'), 'w') as f:
Owen W. Taylor 97338cf
-            f.write(METADATA_TEMPLATE.format(**args))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        runtime_ref = 'runtime/{id}/{arch}/{branch}'.format(**args)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        subprocess.check_call(['ostree', 'commit',
Owen W. Taylor 97338cf
-                               '--repo', repo, '--owner-uid=0',
Owen W. Taylor 97338cf
-                               '--owner-gid=0', '--no-xattrs',
Owen W. Taylor 97338cf
-                               '--branch', runtime_ref,
Owen W. Taylor 97338cf
-                               '-s', 'build of ' + runtime_ref,
Owen W. Taylor 97338cf
-                               '--tree=tar=' + tarred_filesystem,
Owen W. Taylor 97338cf
-                               '--tree=dir=' + builddir])
Owen W. Taylor 97338cf
-        subprocess.check_call(['ostree', 'summary', '-u', '--repo', repo])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        subprocess.check_call(['flatpak', 'build-bundle', repo,
Owen W. Taylor 97338cf
-                               '--oci', '--runtime',
Owen W. Taylor 97338cf
-                               outfile, id_, branch])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return runtime_ref
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    def _find_runtime_info(self):
Owen W. Taylor 97338cf
-        runtime_module = self.source.runtime_module
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        flatpak_xmd = runtime_module.mmd.props.xmd['flatpak']
Owen W. Taylor 97338cf
-        runtime_id = flatpak_xmd['runtimes']['runtime']['id']
Owen W. Taylor 97338cf
-        sdk_id = flatpak_xmd['runtimes']['runtime'].get('sdk', runtime_id)
Owen W. Taylor 97338cf
-        runtime_version = flatpak_xmd['branch']
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return runtime_id, sdk_id, runtime_version
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-    def _create_app_oci(self, tarred_filesystem, outfile):
Owen W. Taylor 97338cf
-        info = self.source.flatpak_yaml
Owen W. Taylor 97338cf
-        app_id = info['id']
Owen W. Taylor 97338cf
-        app_branch = info.get('branch', 'master')
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        builddir = os.path.join(self.workflow.source.workdir, "build")
Owen W. Taylor 97338cf
-        os.mkdir(builddir)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        repo = os.path.join(self.workflow.source.workdir, "repo")
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        runtime_id, sdk_id, runtime_version = self._find_runtime_info()
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        # See comment for build_init() for why we can't use 'flatpak build-init'
Owen W. Taylor 97338cf
-        # subprocess.check_call(['flatpak', 'build-init',
Owen W. Taylor 97338cf
-        #                        builddir, app_id, runtime_id, runtime_id, runtime_version])
Owen W. Taylor 97338cf
-        build_init(builddir, app_id, sdk_id, runtime_id, runtime_version, tags=info.get('tags', []))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        # with gzip'ed tarball, tar is several seconds faster than tarfile.extractall
Owen W. Taylor 97338cf
-        subprocess.check_call(['tar', 'xCfz', builddir, tarred_filesystem])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        update_desktop_files(app_id, builddir)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        finish_args = []
Owen W. Taylor 97338cf
-        if 'finish-args' in info:
Owen W. Taylor 97338cf
-            # shlex.split(None) reads from standard input, so avoid that
Owen W. Taylor 97338cf
-            finish_args = shlex.split(info['finish-args'] or '')
Owen W. Taylor 97338cf
-        if 'command' in info:
Owen W. Taylor 97338cf
-            finish_args = ['--command', info['command']] + finish_args
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        subprocess.check_call(['flatpak', 'build-finish'] + finish_args + [builddir])
Owen W. Taylor 97338cf
-        subprocess.check_call(['flatpak', 'build-export', repo, builddir, app_branch])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        subprocess.check_call(['flatpak', 'build-bundle', repo, '--oci',
Owen W. Taylor 97338cf
-                               outfile, app_id, app_branch])
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        app_ref = 'app/{app_id}/{arch}/{branch}'.format(app_id=app_id,
Owen W. Taylor 97338cf
-                                                        arch=get_arch(),
Owen W. Taylor 97338cf
-                                                        branch=app_branch)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        return app_ref
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
     def run(self):
Owen W. Taylor 97338cf
         self.source = get_flatpak_source_info(self.workflow)
Owen W. Taylor 97338cf
         if self.source is None:
Owen W. Taylor 97338cf
             raise RuntimeError("flatpak_create_dockerfile must be run before flatpak_create_oci")
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
+        self.builder = FlatpakBuilder(self.source, self.workflow.source.workdir,
Owen W. Taylor 97338cf
+                                      'var/tmp/flatpak-build',
Owen W. Taylor 97338cf
+                                      parse_manifest=parse_rpm_output)
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
         tarred_filesystem, manifest = self._export_filesystem()
Owen W. Taylor 97338cf
         self.log.info('filesystem tarfile written to %s', tarred_filesystem)
Owen W. Taylor 97338cf
         self.log.info('manifest written to %s', manifest)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        all_components = self._get_components(manifest)
Owen W. Taylor 97338cf
-        if self.source.runtime:
Owen W. Taylor 97338cf
-            image_components = all_components
Owen W. Taylor 97338cf
-        else:
Owen W. Taylor 97338cf
-            image_components = self._filter_app_manifest(all_components)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        self.log.info("Components:\n%s",
Owen W. Taylor 97338cf
-                      "\n".join("        {name}-{epoch}:{version}-{release}.{arch}.rpm"
Owen W. Taylor 97338cf
-                                .format(**c) for c in image_components))
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
+        image_components = self.builder.get_components(manifest)
Owen W. Taylor 97338cf
         self.workflow.image_components = image_components
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        outfile = os.path.join(self.workflow.source.workdir, 'flatpak-oci-image')
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
-        if self.source.runtime:
Owen W. Taylor 97338cf
-            ref_name = self._create_runtime_oci(tarred_filesystem, outfile)
Owen W. Taylor 97338cf
-        else:
Owen W. Taylor 97338cf
-            ref_name = self._create_app_oci(tarred_filesystem, outfile)
Owen W. Taylor 97338cf
+        ref_name, outfile, tarred_outfile = self.builder.build_container(tarred_filesystem)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         metadata = get_exported_image_metadata(outfile, IMAGE_TYPE_OCI)
Owen W. Taylor 97338cf
         metadata['ref_name'] = ref_name
Owen W. Taylor 97338cf
@@ -459,11 +112,6 @@ class FlatpakCreateOciPlugin(PrePublishPlugin):
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
         self.log.info('OCI image is available as %s', outfile)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-        tarred_outfile = outfile + '.tar'
Owen W. Taylor 97338cf
-        with tarfile.TarFile(tarred_outfile, "w") as tf:
Owen W. Taylor 97338cf
-            for f in os.listdir(outfile):
Owen W. Taylor 97338cf
-                tf.add(os.path.join(outfile, f), f)
Owen W. Taylor 97338cf
-
Owen W. Taylor 97338cf
         metadata = get_exported_image_metadata(tarred_outfile, IMAGE_TYPE_OCI_TAR)
Owen W. Taylor 97338cf
         metadata['ref_name'] = ref_name
Owen W. Taylor 97338cf
         self.workflow.exported_image_sequence.append(metadata)
Owen W. Taylor 97338cf
diff --git a/images/dockerhost-builder/Dockerfile b/images/dockerhost-builder/Dockerfile
Owen W. Taylor 97338cf
index 11368b9..7e5be3e 100644
Owen W. Taylor 97338cf
--- a/images/dockerhost-builder/Dockerfile
Owen W. Taylor 97338cf
+++ b/images/dockerhost-builder/Dockerfile
Owen W. Taylor 97338cf
@@ -1,5 +1,5 @@
Owen W. Taylor 97338cf
 FROM fedora:latest
Owen W. Taylor 97338cf
-RUN dnf -y install docker git python-docker-py python-setuptools desktop-file-utils e2fsprogs flatpak koji libmodulemd ostree python2-gobject-base python-backports-lzma osbs gssproxy && dnf clean all
Owen W. Taylor 97338cf
+RUN dnf -y install docker git python-docker-py python-setuptools desktop-file-utils e2fsprogs flatpak koji libmodulemd ostree python2-gobject-base python2-flatpak-module-tools python-backports-lzma osbs gssproxy && dnf clean all
Owen W. Taylor 97338cf
 ADD ./atomic-reactor.tar.gz /tmp/
Owen W. Taylor 97338cf
 RUN cd /tmp/atomic-reactor-*/ && python setup.py install
Owen W. Taylor 97338cf
 CMD ["atomic-reactor", "--verbose", "inside-build"]
Owen W. Taylor 97338cf
diff --git a/images/privileged-builder/Dockerfile b/images/privileged-builder/Dockerfile
Owen W. Taylor 97338cf
index a6fa7fd..75653bf 100644
Owen W. Taylor 97338cf
--- a/images/privileged-builder/Dockerfile
Owen W. Taylor 97338cf
+++ b/images/privileged-builder/Dockerfile
Owen W. Taylor 97338cf
@@ -1,5 +1,5 @@
Owen W. Taylor 97338cf
 FROM fedora:latest
Owen W. Taylor 97338cf
-RUN dnf -y install docker git python-docker-py python-setuptools desktop-file-utils e2fsprogs flatpak koji libmodulemd ostree python2-gobject-base python-backports-lzma osbs gssproxy && dnf clean all
Owen W. Taylor 97338cf
+RUN dnf -y install docker git python-docker-py python-setuptools desktop-file-utils e2fsprogs flatpak koji libmodulemd ostree python2-gobject-base python2-flatpak-module-tools python-backports-lzma osbs gssproxy && dnf clean all
Owen W. Taylor 97338cf
 ADD ./atomic-reactor.tar.gz /tmp/
Owen W. Taylor 97338cf
 RUN cd /tmp/atomic-reactor-*/ && python setup.py install
Owen W. Taylor 97338cf
 ADD ./docker.sh /tmp/docker.sh
Owen W. Taylor 97338cf
diff --git a/requirements-flatpak.txt b/requirements-flatpak.txt
Owen W. Taylor 97338cf
index a751b49..acb27ca 100644
Owen W. Taylor 97338cf
--- a/requirements-flatpak.txt
Owen W. Taylor 97338cf
+++ b/requirements-flatpak.txt
Owen W. Taylor 97338cf
@@ -1 +1,2 @@
Owen W. Taylor 97338cf
+flatpak-module-tools
Owen W. Taylor 97338cf
 pdc-client
Owen W. Taylor 97338cf
diff --git a/tests/flatpak.py b/tests/flatpak.py
Owen W. Taylor 97338cf
index 2ac42e9..c412284 100644
Owen W. Taylor 97338cf
--- a/tests/flatpak.py
Owen W. Taylor 97338cf
+++ b/tests/flatpak.py
Owen W. Taylor 97338cf
@@ -1,11 +1,12 @@
Owen W. Taylor 97338cf
 import yaml
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
+from atomic_reactor.util import split_module_spec
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
 try:
Owen W. Taylor 97338cf
-    from atomic_reactor.plugins.pre_resolve_module_compose import (ModuleInfo,
Owen W. Taylor 97338cf
-                                                                   ComposeInfo,
Owen W. Taylor 97338cf
+    from atomic_reactor.plugins.pre_resolve_module_compose import (ComposeInfo,
Owen W. Taylor 97338cf
                                                                    set_compose_info)
Owen W. Taylor 97338cf
-    from atomic_reactor.plugins.pre_flatpak_create_dockerfile import (FlatpakSourceInfo,
Owen W. Taylor 97338cf
-                                                                      set_flatpak_source_info)
Owen W. Taylor 97338cf
+    from atomic_reactor.plugins.pre_flatpak_create_dockerfile import set_flatpak_source_info
Owen W. Taylor 97338cf
+    from flatpak_module_tools.flatpak_builder import FlatpakSourceInfo, ModuleInfo
Owen W. Taylor 97338cf
     from gi.repository import Modulemd
Owen W. Taylor 97338cf
     MODULEMD_AVAILABLE = True
Owen W. Taylor 97338cf
 except ImportError:
Owen W. Taylor 97338cf
@@ -123,6 +124,9 @@ flatpak:
Owen W. Taylor 97338cf
     # Test overriding the automatic "first executable in /usr/bin'
Owen W. Taylor 97338cf
     command: eog2
Owen W. Taylor 97338cf
     tags: ["Viewer"]
Owen W. Taylor 97338cf
+    copy-icon: true
Owen W. Taylor 97338cf
+    rename-desktop-file: eog.desktop
Owen W. Taylor 97338cf
+    rename-icon: eog
Owen W. Taylor 97338cf
     finish-args: >
Owen W. Taylor 97338cf
 """ + "".join("        {}\n".format(a) for a in FLATPAK_APP_FINISH_ARGS)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
@@ -342,7 +346,10 @@ def setup_flatpak_source_info(workflow, config=APP_CONFIG):
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
     flatpak_yaml = yaml.safe_load(config['container_yaml'])['flatpak']
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
-    source = FlatpakSourceInfo(flatpak_yaml, compose)
Owen W. Taylor 97338cf
+    module_spec = split_module_spec(compose.source_spec)
Owen W. Taylor 97338cf
+
Owen W. Taylor 97338cf
+    source = FlatpakSourceInfo(flatpak_yaml, compose.modules, compose.base_module,
Owen W. Taylor 97338cf
+                               module_spec.profile)
Owen W. Taylor 97338cf
     set_flatpak_source_info(workflow, source)
Owen W. Taylor 97338cf
 
Owen W. Taylor 97338cf
     return source
Owen W. Taylor 97338cf
-- 
Owen W. Taylor 97338cf
2.14.3
Owen W. Taylor 97338cf