From 170ea14bfa13896c7259e8261e782f91f6bc445b Mon Sep 17 00:00:00 2001
From: Michael Filz <michael.filz@zv-extern.fraunhofer.de>
Date: Wed, 18 May 2022 12:43:17 +0200
Subject: [PATCH] add quay.io check

---
 README.md          |  5 +++--
 test_all.py        |  5 +++++
 testconf/check.yml |  6 ++++++
 vcheck.py          | 12 ++++++++++++
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 335a35c..ee2b7b4 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
 version-checker
 ===============
 
-Tool for checking if software versions are out of date. Supports (for now) checking against github releases, dockerhub images, debian packages and arbitrary web sites (using regexp). Will start a webserver that exposes the version status as prometheus metrics under the path '/metrics' and show a status page for all other paths.
+Tool for checking if software versions are out of date. Supports (for now) checking against github releases, dockerhub images, quay.io images, debian packages and arbitrary web sites (using regexp). Will start a webserver that exposes the version status as prometheus metrics under the path '/metrics' and show a status page for all other paths.
 
 Installation:
 -------------
@@ -51,12 +51,13 @@ The common keys are as follows:
 * check: check to use; currently supported:
   * github
   * dockerhub
+  * quay
   * debian
   * website
 * version: version string of the currently deployed version, should be in the format as found on the source sites
 
 Further keys depend on the check used:
-* github / dockerhub
+* github / dockerhub / quay
   * id: repo or image name (e.g. jboss/keycloak, ansible/ansible)
   * pattern: regex for the version, used to filter wanted versions (e.g. ^[0-9.]+-alpine$, ^v10\.[0-9.]+$)
 * debian
diff --git a/test_all.py b/test_all.py
index bbe453c..4ca511b 100644
--- a/test_all.py
+++ b/test_all.py
@@ -41,6 +41,11 @@ class TestVC(TestCase):
         self.assertIsNone(res)
         self.assertNotEqual(err, '')
 
+    def test_quay_error(self):
+        (res, err) = vcheck.quay('non existing image', 'v1234')
+        self.assertIsNone(res)
+        self.assertNotEqual(err, '')
+
     def test_run(self):
         self.server = threading.Thread(target = vcheck.run, args = [8080, './testconf', 2])
         self.server.start()
diff --git a/testconf/check.yml b/testconf/check.yml
index 9d81e6c..257e483 100644
--- a/testconf/check.yml
+++ b/testconf/check.yml
@@ -17,3 +17,9 @@
   check: debian
   id: radsecproxy
   release: bullseye
+
+- name: keycloak_quay
+  version: 17.0.0
+  check: quay
+  id: keycloak/keycloak
+  pattern: ^[0-9.]+$
diff --git a/vcheck.py b/vcheck.py
index 2d82085..d528818 100755
--- a/vcheck.py
+++ b/vcheck.py
@@ -200,6 +200,14 @@ def dockerhub(image, pattern):
         return None, err
 
 
+def quay(image, pattern):
+    (j, err) = rest(f'https://quay.io/v1/repositories/{image}/tags')
+    if j:
+        return latest(j.keys(), pattern)
+    else:
+        return None, err
+
+
 def github(repo, pattern):
     (j, err) = rest(f'https://api.github.com/repos/{repo}/tags')
     if j:
@@ -265,6 +273,10 @@ def check(confd, v):
             error = missing(c, 'id', 'pattern')
             if not error:
                 (current, error) = dockerhub(c['id'], c['pattern'])
+        elif c['check'] == 'quay':
+            error = missing(c, 'id', 'pattern')
+            if not error:
+                (current, error) = quay(c['id'], c['pattern'])
         elif c['check'] == 'github':
             error = missing(c, 'id', 'pattern')
             if not error:
-- 
GitLab