From fb586b65a9fc9c4cbfe8d1903276e133ccc558ee Mon Sep 17 00:00:00 2001
From: Adam Miller <maxamillion@fedoraproject.org>
Date: Feb 16 2017 17:59:45 +0000
Subject: [PATCH 1/2] add container image release candidate module to library and example playbook


Signed-off-by: Adam Miller <maxamillion@fedoraproject.org>

---

diff --git a/ansible/containerrcdemo.yml b/ansible/containerrcdemo.yml
new file mode 100644
index 0000000..3d4cc75
--- /dev/null
+++ b/ansible/containerrcdemo.yml
@@ -0,0 +1,11 @@
+---
+- name: container_rc demo
+  hosts: automator
+  tasks:
+  - name: Gather list of releases
+    container_rc:
+      release: 25
+    register: query_data
+
+  - debug: var=query_data["rclist"]
+
diff --git a/ansible/library/container_rc.py b/ansible/library/container_rc.py
new file mode 100755
index 0000000..f790bf7
--- /dev/null
+++ b/ansible/library/container_rc.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# fedcontainer_rc.py - Ansible module to gather Fedora container image release
+#                      candidates
+#
+# Copyright (C) 2017 Red Hat, Inc.
+# SPDX-License-Identifier:      GPL-2.0+
+#
+# Authors:
+#     Adam Miller <maxamillion@fedoraproject.org>
+#
+
+DOCUMENTATION = '''
+---
+author:
+  - "Adam Miller <maxamillion@fedoraproject.org>"
+module: fedcontainer_rc
+short_description: Query Container Image Release Candidates
+description:
+  - Query Container Image Release Candidates, will return a list of current
+    candidate images.
+  - Returns dict entry 'rclist' in return JSON
+options:
+  release:
+    description:
+      - Fedora Release number
+    required: true
+
+'''
+
+EXAMPLES = '''
+# Generally meant to be used with other tasks as input, registry the output var
+- fedcontainer_rc:
+    release: 25
+  register: query_data
+
+- debug: var=query_data["rclist"]
+
+
+'''
+
+import requests
+
+def get_releasecandidates(release_num):
+    # type: (int) -> list
+    """
+    Logic to actually get the release candidate, this is likely to change over
+    time (PDC?) so we want it split out from the main control of the module
+    """
+
+    # NOTE: the following abbreviations are true for the vars below
+    #
+    #   cntr    == container
+    #   pkgdb   == package db
+    #   dg      == datagrepper
+
+    # The list we will return, initialized here so we can append to it
+    rclist = []
+
+    # pkgdb2 api vars
+    pkgdb_api_url = "https://admin.fedoraproject.org/pkgdb/api/packages/*"
+    pkgdb_req_params = {"namespace": "docker"}
+
+    # datagrepper api vars
+    dg_api_url = "https://apps.fedoraproject.org/datagrepper/raw"
+    dg_req_params = {
+        "topic": "org.fedoraproject.prod.buildsys.tag",
+        "user": "containerbuild",
+        "rows_per_page": 100
+    }
+    # FIXME - not sure which topic to be looking for
+
+    # First query pkgdb for all container images we currently have
+    cntr_request = requests.get(pkgdb_api_url, params=pkgdb_req_params)
+
+    # Start with page 1 response from pkgdb, grab names and then iterate
+    # through pagination
+    cntr_data = cntr_request.json()[u'packages']
+    cpage_range = range(2, cntr_request.json()[u'page_total']+1)
+
+    for cntr_page in cpage_range:
+        cntr_data += requests.get(
+            pkgdb_api_url,
+            params=dict(page=cntr_page, **pkgdb_req_params)
+        ).json()[u'packages']
+
+    # Query Datagrepper for latest container build
+    for cntr in cntr_data:
+        try:
+            # Query datagrepper, extract raw_messages, list comprehension
+            # filter, grab only the latest (index 0)
+            rclist.append([
+                "{}-{}-{}".format(
+                    cntr_msg[u'msg'][u'name'],
+                    cntr_msg[u'msg'][u'version'],
+                    cntr_msg[u'msg'][u'release']
+                ) for cntr_msg in requests.get(
+                                        dg_api_url,
+                                        params=dict(
+                                            package=cntr[u'name'],
+                                            **dg_req_params
+                                        )
+                                    ).json()[u'raw_messages']
+                if "f{}docker".format(release_num) in cntr_msg[u'msg'][u'release']
+            ][0])
+        except IndexError:
+            # This happens when we have a container branched in pkgdb but nobody
+            # has done a build in koji yet.
+            pass
+
+    return rclist
+
+
+def main():
+
+    module = AnsibleModule(
+        argument_spec=dict(
+            release=dict(required=True, type="int", default=None),
+        ),
+        supports_check_mode=False
+    )
+
+
+    # Technically this never changes anything on the system, so always mark
+    # changed=False
+    module.exit_json(
+        changed=False,
+        msg="Successfully gathered container release candidates, results in 'rclist'",
+        rclist=get_releasecandidates(module.params['release'])
+    )
+
+##################################################
+# import module snippets
+from ansible.module_utils.basic import *
+main()
+# vim: set expandtab sw=4 sts=4 ts=4

From 7324e833ec9a5909449ebc03fbdd4201542b8784 Mon Sep 17 00:00:00 2001
From: Adam Miller <maxamillion@fedoraproject.org>
Date: Feb 16 2017 17:59:45 +0000
Subject: [PATCH 2/2] fedcontainer_rc: fix module naming, add FGC namespacing, add Name and Name-Version tags


Signed-off-by: Adam Miller <maxamillion@fedoraproject.org>

---

diff --git a/ansible/containerrcdemo.yml b/ansible/containerrcdemo.yml
deleted file mode 100644
index 3d4cc75..0000000
--- a/ansible/containerrcdemo.yml
+++ /dev/null
@@ -1,11 +0,0 @@
----
-- name: container_rc demo
-  hosts: automator
-  tasks:
-  - name: Gather list of releases
-    container_rc:
-      release: 25
-    register: query_data
-
-  - debug: var=query_data["rclist"]
-
diff --git a/ansible/fedcontainerrcdemo.yml b/ansible/fedcontainerrcdemo.yml
new file mode 100644
index 0000000..e722da8
--- /dev/null
+++ b/ansible/fedcontainerrcdemo.yml
@@ -0,0 +1,11 @@
+---
+- name: fedcontainer_rc demo
+  hosts: automator
+  tasks:
+  - name: Gather list of releases
+    fedcontainer_rc:
+      release: 25
+    register: query_data
+
+  - debug: var=query_data["rclist"]
+
diff --git a/ansible/library/container_rc.py b/ansible/library/container_rc.py
deleted file mode 100755
index f790bf7..0000000
--- a/ansible/library/container_rc.py
+++ /dev/null
@@ -1,137 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-#
-# fedcontainer_rc.py - Ansible module to gather Fedora container image release
-#                      candidates
-#
-# Copyright (C) 2017 Red Hat, Inc.
-# SPDX-License-Identifier:      GPL-2.0+
-#
-# Authors:
-#     Adam Miller <maxamillion@fedoraproject.org>
-#
-
-DOCUMENTATION = '''
----
-author:
-  - "Adam Miller <maxamillion@fedoraproject.org>"
-module: fedcontainer_rc
-short_description: Query Container Image Release Candidates
-description:
-  - Query Container Image Release Candidates, will return a list of current
-    candidate images.
-  - Returns dict entry 'rclist' in return JSON
-options:
-  release:
-    description:
-      - Fedora Release number
-    required: true
-
-'''
-
-EXAMPLES = '''
-# Generally meant to be used with other tasks as input, registry the output var
-- fedcontainer_rc:
-    release: 25
-  register: query_data
-
-- debug: var=query_data["rclist"]
-
-
-'''
-
-import requests
-
-def get_releasecandidates(release_num):
-    # type: (int) -> list
-    """
-    Logic to actually get the release candidate, this is likely to change over
-    time (PDC?) so we want it split out from the main control of the module
-    """
-
-    # NOTE: the following abbreviations are true for the vars below
-    #
-    #   cntr    == container
-    #   pkgdb   == package db
-    #   dg      == datagrepper
-
-    # The list we will return, initialized here so we can append to it
-    rclist = []
-
-    # pkgdb2 api vars
-    pkgdb_api_url = "https://admin.fedoraproject.org/pkgdb/api/packages/*"
-    pkgdb_req_params = {"namespace": "docker"}
-
-    # datagrepper api vars
-    dg_api_url = "https://apps.fedoraproject.org/datagrepper/raw"
-    dg_req_params = {
-        "topic": "org.fedoraproject.prod.buildsys.tag",
-        "user": "containerbuild",
-        "rows_per_page": 100
-    }
-    # FIXME - not sure which topic to be looking for
-
-    # First query pkgdb for all container images we currently have
-    cntr_request = requests.get(pkgdb_api_url, params=pkgdb_req_params)
-
-    # Start with page 1 response from pkgdb, grab names and then iterate
-    # through pagination
-    cntr_data = cntr_request.json()[u'packages']
-    cpage_range = range(2, cntr_request.json()[u'page_total']+1)
-
-    for cntr_page in cpage_range:
-        cntr_data += requests.get(
-            pkgdb_api_url,
-            params=dict(page=cntr_page, **pkgdb_req_params)
-        ).json()[u'packages']
-
-    # Query Datagrepper for latest container build
-    for cntr in cntr_data:
-        try:
-            # Query datagrepper, extract raw_messages, list comprehension
-            # filter, grab only the latest (index 0)
-            rclist.append([
-                "{}-{}-{}".format(
-                    cntr_msg[u'msg'][u'name'],
-                    cntr_msg[u'msg'][u'version'],
-                    cntr_msg[u'msg'][u'release']
-                ) for cntr_msg in requests.get(
-                                        dg_api_url,
-                                        params=dict(
-                                            package=cntr[u'name'],
-                                            **dg_req_params
-                                        )
-                                    ).json()[u'raw_messages']
-                if "f{}docker".format(release_num) in cntr_msg[u'msg'][u'release']
-            ][0])
-        except IndexError:
-            # This happens when we have a container branched in pkgdb but nobody
-            # has done a build in koji yet.
-            pass
-
-    return rclist
-
-
-def main():
-
-    module = AnsibleModule(
-        argument_spec=dict(
-            release=dict(required=True, type="int", default=None),
-        ),
-        supports_check_mode=False
-    )
-
-
-    # Technically this never changes anything on the system, so always mark
-    # changed=False
-    module.exit_json(
-        changed=False,
-        msg="Successfully gathered container release candidates, results in 'rclist'",
-        rclist=get_releasecandidates(module.params['release'])
-    )
-
-##################################################
-# import module snippets
-from ansible.module_utils.basic import *
-main()
-# vim: set expandtab sw=4 sts=4 ts=4
diff --git a/ansible/library/fedcontainer_rc.py b/ansible/library/fedcontainer_rc.py
new file mode 100755
index 0000000..894bd42
--- /dev/null
+++ b/ansible/library/fedcontainer_rc.py
@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# fedcontainer_rc.py - Ansible module to gather Fedora container image release
+#                      candidates
+#
+# Copyright (C) 2017 Red Hat, Inc.
+# SPDX-License-Identifier:      GPL-2.0+
+#
+# Authors:
+#     Adam Miller <maxamillion@fedoraproject.org>
+#
+
+DOCUMENTATION = '''
+---
+author:
+  - "Adam Miller <maxamillion@fedoraproject.org>"
+module: fedcontainer_rc
+short_description: Query Container Image Release Candidates
+description:
+  - Query Container Image Release Candidates, will return a list of current
+    candidate images.
+  - Returns dict entry 'rclist' in return JSON
+options:
+  release:
+    description:
+      - Fedora Release number
+    required: true
+
+'''
+
+EXAMPLES = '''
+# Generally meant to be used with other tasks as input, registry the output var
+- fedcontainer_rc:
+    release: 25
+  register: query_data
+
+- debug: var=query_data["rclist"]
+
+
+'''
+
+import requests
+
+def get_releasecandidates(release_num):
+    # type: (int) -> list
+    """
+    Logic to actually get the release candidate, this is likely to change over
+    time (PDC?) so we want it split out from the main control of the module
+    """
+
+    # NOTE: the following abbreviations are true for the vars below
+    #
+    #   cntr    == container
+    #   pkgdb   == package db
+    #   dg      == datagrepper
+    #   fgc     == Fedora Generational Core
+
+    # Set the FGC prefix as the "repo" inside the registry as per documented
+    # layered image naming guidelines:
+    #   https://fedoraproject.org/wiki/Container:Guidelines
+    fgc = "f{}".format(release_num)
+
+    # The list we will return, initialized here so we can append to it
+    rclist = []
+
+    # pkgdb2 api vars
+    pkgdb_api_url = "https://admin.fedoraproject.org/pkgdb/api/packages/*"
+    pkgdb_req_params = {"namespace": "docker"}
+
+    # datagrepper api vars
+    dg_api_url = "https://apps.fedoraproject.org/datagrepper/raw"
+    dg_req_params = {
+        "topic": "org.fedoraproject.prod.buildsys.tag",
+        "user": "containerbuild",
+        "rows_per_page": 100
+    }
+    # FIXME - not sure which topic to be looking for
+
+    # First query pkgdb for all container images we currently have
+    cntr_request = requests.get(pkgdb_api_url, params=pkgdb_req_params)
+
+    # Start with page 1 response from pkgdb, grab names and then iterate
+    # through pagination
+    cntr_data = cntr_request.json()[u'packages']
+    cpage_range = range(2, cntr_request.json()[u'page_total']+1)
+
+    for cntr_page in cpage_range:
+        cntr_data += requests.get(
+            pkgdb_api_url,
+            params=dict(page=cntr_page, **pkgdb_req_params)
+        ).json()[u'packages']
+
+    # Query Datagrepper for latest container build
+    for cntr in cntr_data:
+        try:
+            # Query datagrepper, extract raw_messages, list comprehension
+            # filter, grab only the latest (index 0)
+            full_nvr = [
+                "{}-{}-{}".format(
+                    cntr_msg[u'msg'][u'name'],
+                    cntr_msg[u'msg'][u'version'],
+                    cntr_msg[u'msg'][u'release']
+                ) for cntr_msg in requests.get(
+                                        dg_api_url,
+                                        params=dict(
+                                            package=cntr[u'name'],
+                                            **dg_req_params
+                                        )
+                                    ).json()[u'raw_messages']
+                if "f{}docker".format(release_num) in cntr_msg[u'msg'][u'release']
+            ][0]
+
+            # We want the full Name-Version-Release, but we also want the
+            # shorthand Name and Name-Version references as well
+            fnvr_split = full_nvr.split('-')
+            rclist.append("{}/{}:{}".format(fgc, '-'.join(fnvr_split[:-2]), '-'.join(fnvr_split[-2:])))
+            rclist.append("{}/{}".format(fgc, ':'.join(fnvr_split[:-1])))
+            rclist.append("{}/{}".format(fgc, ':'.join(fnvr_split[:-2])))
+        except IndexError:
+            # This happens when we have a container branched in pkgdb but nobody
+            # has done a build in koji yet.
+            pass
+
+    return rclist
+
+
+def main():
+
+    module = AnsibleModule(
+        argument_spec=dict(
+            release=dict(required=True, type="int", default=None),
+        ),
+        supports_check_mode=False
+    )
+
+
+    # Technically this never changes anything on the system, so always mark
+    # changed=False
+    module.exit_json(
+        changed=False,
+        msg="Successfully gathered container release candidates, results in 'rclist'",
+        rclist=get_releasecandidates(module.params['release'])
+    )
+
+##################################################
+# import module snippets
+from ansible.module_utils.basic import *
+main()
+# vim: set expandtab sw=4 sts=4 ts=4

