From ff2c560b81a7e6dcbf889fa944d9c10b3494a27d Mon Sep 17 00:00:00 2001
From: Julien 'Lta' BALLET <lta@fb.com>
Date: Fri, 17 Apr 2015 16:05:55 -0700
Subject: [PATCH] Add a plugin that calls command (from the host) on the
 produced rpms.

As it has been developped to auto-sign package after build the plugin
name is sign. It's disabled by default, but its default configuration
is to call rpm --addsign
---
 Makefile.am                  |  1 +
 etc/mock/site-defaults.cfg   |  7 +++++++
 py/mockbuild/plugin.py       |  1 +
 py/mockbuild/plugins/sign.py | 43 +++++++++++++++++++++++++++++++++++++++++++
 py/mockbuild/util.py         |  7 ++++++-
 5 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 py/mockbuild/plugins/sign.py

diff --git a/Makefile.am b/Makefile.am
index e28e0d1..8866fa5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -60,6 +60,7 @@ plugins_PYTHON = \
     py/mockbuild/plugins/mount.py \
     py/mockbuild/plugins/chroot_scan.py \
     py/mockbuild/plugins/lvm_root.py
+    py/mockbuild/plugins/sign.py
 
 mockbuilddir = $(pythondir)/mockbuild
 mockbuild_PYTHON = \
diff --git a/etc/mock/site-defaults.cfg b/etc/mock/site-defaults.cfg
index 26127e5..0f3dc9d 100644
--- a/etc/mock/site-defaults.cfg
+++ b/etc/mock/site-defaults.cfg
@@ -161,6 +161,13 @@
 # config_opts['plugin_conf']['compress_logs_opts'] = {}
 ### Command used to compress logs - e.g. "/usr/bin/xz -9"
 # config_opts['plugin_conf']['compress_logs_opts']['command'] = ""
+#
+# Configuration options for the sign plugin:
+# config_opts['plugin_conf']['sign_enable'] = False
+# config_opts['plugin_conf']['sign_opts']['cmd'] = 'rpm'
+# The options to pass to the signing command. %(rpms)s will be expanded to
+# the rpms in the results folder.
+# config_opts['plugin_conf']['sign_opts']['opts'] = '--addsign %(rpms)s -D "%%_gpg_name your_name" -D "%%_gpg_path /home/your_name/.gnupg'
 
 #############################################################################
 #
diff --git a/py/mockbuild/plugin.py b/py/mockbuild/plugin.py
index fa7d654..bcb2d9a 100644
--- a/py/mockbuild/plugin.py
+++ b/py/mockbuild/plugin.py
@@ -24,6 +24,7 @@ class Plugins(object):
                 self.plugin_conf[key]['cache_topdir'] = buildroot.cache_topdir
                 self.plugin_conf[key]['cachedir'] = buildroot.cachedir
                 self.plugin_conf[key]['root'] = buildroot.shared_root_name
+                self.plugin_conf[key]['resultdir'] = buildroot.resultdir
 
         self.state.start("init plugins")
         # Import plugins  (simplified copy of what yum does). Can add yum
diff --git a/py/mockbuild/plugins/sign.py b/py/mockbuild/plugins/sign.py
new file mode 100644
index 0000000..7eeadfe
--- /dev/null
+++ b/py/mockbuild/plugins/sign.py
@@ -0,0 +1,43 @@
+# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:textwidth=0:
+# License: GPL2 or later see COPYING
+# Written by Julien BALLET <lta@fb.com>
+# Copyright (C) 2014 Facebook
+
+# python library imports
+from mockbuild.trace_decorator import traceLog, getLog
+from mockbuild.util import do, doshell, run
+import glob
+import os
+import subprocess
+
+requires_api_version = "1.1"
+
+# plugin entry point
+@traceLog()
+def init(plugins, conf, buildroot):
+    Sign(plugins, conf, buildroot)
+
+
+class Sign(object):
+    """Automatically sign package after build"""
+
+    @traceLog()
+    def __init__(self, plugins, conf, buildroot):
+        self.plugins = plugins
+        self.conf = conf
+        self.buildroot = buildroot
+        self.plugins.add_hook('postbuild', self.sign_results)
+
+        getLog().info(conf)
+        getLog().info("enabled package signing")
+
+    def sign_results(self):
+        rpms = glob.glob('%s/*.rpm' % self.buildroot.resultdir)
+        if rpms:
+            getLog().info("Signing %s", ', '.join(rpms))
+            opts = self.conf['opts'] % {'rpms': ' '.join(rpms)}
+            cmd = "{0} {1}".format(self.conf['cmd'], opts)
+            getLog().info("Executing %s", cmd)
+            self.buildroot.uid_manager.dropPrivsTemp()
+            subprocess.call(cmd, shell=True, env=os.environ)
+            self.buildroot.uid_manager.restorePrivs()
diff --git a/py/mockbuild/util.py b/py/mockbuild/util.py
index f6d20cd..3ad2afe 100644
--- a/py/mockbuild/util.py
+++ b/py/mockbuild/util.py
@@ -72,7 +72,7 @@ personality_defs = {
 
 PLUGIN_LIST = ['tmpfs', 'root_cache', 'yum_cache', 'bind_mount',
                'ccache', 'selinux', 'package_state', 'chroot_scan',
-               'lvm_root', 'compress_logs']
+               'lvm_root', 'compress_logs', 'sign']
 
 # This is set to False on EL6 in build time
 USE_NSPAWN = False
@@ -712,6 +712,11 @@ def setup_default_config_opts(unprivUid, version, pkgpythondir):
                 "\\bcore(\\.\\d+)?$",
                 "\\.log$",
                 ]},
+            'sign_enable': False,
+            'sign_opts': {
+                'cmd' : 'rpm',
+                'opts' : '--addsign %(rpms)s',
+                },
             }
 
     config_opts['environment'] = {
-- 
1.8.5.6

