From 6f2234587f9d3fe21fabadd6ad8f0ba241356a21 Mon Sep 17 00:00:00 2001
From: Lubos Kocman <lkocman@redhat.com>
Date: Aug 02 2016 14:58:56 +0000
Subject: Port pungi-trigger to execute pungi-signed repo rather than pungi-createrepo


---

diff --git a/fedmsg.d/fed-mod-config.py b/fedmsg.d/fed-mod-config.py
deleted file mode 100644
index 9c2f4c0..0000000
--- a/fedmsg.d/fed-mod-config.py
+++ /dev/null
@@ -1,12 +0,0 @@
-config = {
-    # XXX Only for development.
-    'validate_signatures': False,
-
-    'endpoints': {
-        'modularity-test-server': [
-            # This assumes that we fedmsg-relay running with its relay_outbound
-            # endpoint set at port 4001 and that that port is opened in os1.
-            'tcp://fed-mod.org:4001',
-        ],
-    },
-}
diff --git a/pungi-trigger.py b/pungi-trigger.py
index e0e01cc..74ffaf1 100755
--- a/pungi-trigger.py
+++ b/pungi-trigger.py
@@ -5,7 +5,6 @@ kicks off pungi runs in response to module metadata changes.
 """
 from __future__ import print_function
 
-import json
 import os
 import sys
 import subprocess as sp
@@ -16,10 +15,14 @@ import fedmsg.config
 import fedmsg.meta
 import pdc_client
 
+import tempfile
+import glob
+import rida.scm # XXX: replace
+import shutil
+
 config = fedmsg.config.load_config()
 fedmsg.meta.make_processors(**config)
-
-target = 'org.fedoraproject.dev.rida.module.state.change'
+target_topic = 'org.fedoraproject.dev.rida.module.state.change'
 pdcurl = os.environ['PDCURL']
 repodir = os.environ['REPODIR']
 
@@ -59,54 +62,64 @@ def get_built_repo_location(lines):
             return line.split(telltale)[0].strip().split()[-1]
     raise ValueError("Could not find %r in output." % telltale)
 
-def get_tag_from_pdc(pdc_proxy, variant_name, variant_version):
-    tv = pdc_proxy.treevariant(page_size=-1, variant_id=variant_name, variant_version=variant_version)
-    log ("Pdc returned %s" % (str(tv)))
+def get_tag_from_pdc(pdc_proxy, name, version, release):
+    tv = pdc_proxy.unreleasedvariants(page_size=-1, variant_id=name, variant_version=version, variant_release=release)
     if not tv:
         return None
-    if len(tv) != 1:
-        log("Received multiple tree variants=%s, expected exactly 1"  % tv)
-        return None
     return tv[0]['koji_tag']
 
+def checkout_modulemd(scmurl):
+    scm_obj = rida.scm.SCM(url=scmurl)
+
+    src_dir = scm_obj.checkout(tempfile.mkdtemp(prefix="pungi-trigger"))
+    mmds = glob.glob("%s/*.yaml" % src_dir)
+    assert len(mmds) == 1, "Expected exactly 1 modulemd file in git checkout. Got: %s" % mmds
+    return mmds[0]
+
 def main():
+    log("* Connecting to PDC %s" % pdcurl)
     pdc_proxy = pdc_client.PDCClient(pdcurl, insecure=True, develop=True) 
 
-    log("* Listening for messages on topic %r" % target)
-    fedmsg.crypto.init(**config)
-    for _, _, topic, msg in fedmsg.tail_messages(topic=target, **config):
-        print ("Handling msg %s" % fedmsg.encoding.pretty_dumps(msg))
-        idx = msg['msg_id']
-        log("Saw a message", topic, idx)
-        try:
-            module = json.loads(msg['msg'][msg['msg'].keys()[0]]) # get msg body
-        except ValueError, e:
-            log(e)
-            continue
-        if module['state'] != "ready":
-            continue
-
-        # Run signed repo
-        log("Working with", topic, idx)
-        sigkeys = " ".join(['unsigned', ]) # XXX: fix this
-        tag = get_tag_from_pdc(pdc_proxy, variant_name=module['module'], variant_version=module['version'])
-        if not tag:
-            log("Couldn't get tag for module %s-%s" % (module['module'], module['version']))
-            continue
-
+    log("* Listening for messages on topic %r" % target_topic)
+    for _, _, topic, msg in fedmsg.tail_messages(topic=target_topic, **config):
         try:
-            log("    Running signed-repo for tag=%s", tag)
-            cmd = [ 
+            print(msg)
+            idx = msg['msg_id']
+            log("Saw a message", topic, idx)
+            if msg['msg']['state_name'] != "ready":
+                continue
+
+            # Run signed repo
+            log("Working with", topic, idx)
+            sigkeys = ['unsigned', ] # XXX: fix this
+            name = msg['msg']['name']
+            version = msg['msg']['version']
+            release = msg['msg']['release']
+            tag = get_tag_from_pdc(pdc_proxy, name, version, release)
+            if not tag:
+                log("ERROR: Could get tag from PDC for module %s-%s-%s. Skipping repo creation." % (name, version, release))
+                continue
+            # Should be exception safe
+            modulemd_path = checkout_modulemd(msg['msg']['scmurl'])
+            cmd = [
                 'pungi-signed-repo-prototype',
                 '--target-dir', os.environ['REPODIR'],
                 '--koji-profile', os.environ['KOJI_PROFILE'],
+                '--notification-script', os.environ['NOTIFICATION_SCRIPT'],
+                '--link-type', os.environ['LINK_TYPE'],
+                '--modulemd', modulemd_path,
                 tag,
-                sigkeys,
             ]
+            cmd.extend(sigkeys) # multiple
+            print(cmd)
+            log("Attemping to execute %s" % " ".join(cmd))
             lines = run(cmd)
-            log("$ %s returned '%s'" % (" ".join(cmd), "\n".join( lines)))
-        except OSError:
-            log("Execution of $ %s failed" %  " ".join(cmd))
+
+            # Remove checkout of module definition
+            shutil.rmtree(os.path.dirname(modulemd_path), ignore_errors=True)
+        except Exception, e:
+            log("ERROR: %s" % e)
+            pass
 
 if __name__ == '__main__':
     sanity_check()
diff --git a/systemd/pungi-modularity.env b/systemd/pungi-modularity.env
index 2fdbddf..af5d1a3 100644
--- a/systemd/pungi-modularity.env
+++ b/systemd/pungi-modularity.env
@@ -1,9 +1,13 @@
 # Just an example env file.  In prod/stg, we'll use some real values here.
 # See https://pagure.io/pungi-modularity/
 
-export PYTHONPATH="/home/fedora/lkocman/Workspace/pungi-prototype:/home/fedora/lkocman/Workspace/libhif/build/python/hawkey:/home/fedora/lkocman/Workspace/dnf:/home/fedora/lkocman/Workspace/modulemd:/home/fedora/lkocman/Workspace/PyYAML-3.11/lib:/home/fedora/lkocman/Workspace/pdc-client:/home/fedora/lkocman/Workspace/beanbag"
-export LD_LIBRARY_PATH="/home/fedora/lkocman/Workspace/libhif/build/libhif"
-export PATH=$MODULARITY_DIR/pungi-gather/bin:$MODULARITY_DIR/pungi/bin:$PATH
-export REPODIR=/home/fedora/lkocman
-export PDCURL=http://fed-mod.org:8000/rest_api/v1/
-export KOJI_PROFILE="koji"
+MODULARITY_DIR="/srv/modularity/pungi-trigger-repos"
+# Libhif is required by pungi-signed-repo-prototype
+export PYTHONPATH="$MODULARITY_DIR/pungi-prototype:$MODULARITY_DIR/libhif/build/python/hawkey:$MODULARITY_DIR/dnf:$MODULARITY_DIR/modulemd"
+export LD_LIBRARY_PATH="$MODULARITY_DIR/libhif/build/libhif"
+export PATH=$MODULARITY_DIR/pungi-prototype/bin:$PATH
+export REPODIR=/var/www/html/modularity/repos
+export PDCURL=http://modularity.fedorainfracloud.org:8080/rest_api/v1/
+export KOJI_PROFILE=koji
+export NOTIFICATION_SCRIPT="pungi-signed-repo-fedmsg-notification"
+export LINK_TYPE="download"

