From 37270da56ae887aedb729066e261d0d447af16d9 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Tue, 2 Nov 2021 12:20:49 +0100
Subject: [PATCH] move autodetection for os and mysql to plugin scripts

---
 check_eol                                   | 41 ++++++---------------
 check_eol-versiondetect/autodetect-mysqlany | 32 ++++++++++++++++
 check_eol-versiondetect/autodetect-os       | 26 +++++++++++++
 check_eol-versiondetect/detect-mariadb      |  2 +-
 4 files changed, 71 insertions(+), 30 deletions(-)
 create mode 100755 check_eol-versiondetect/autodetect-mysqlany
 create mode 100755 check_eol-versiondetect/autodetect-os

diff --git a/check_eol b/check_eol
index 40b8ddf..f917eea 100755
--- a/check_eol
+++ b/check_eol
@@ -60,6 +60,7 @@ bDebug=false
 
 
 myKey=
+myVersion=
 myEolEnd=
 typeset -i myDaysLeft=
 
@@ -70,40 +71,21 @@ typeset -i myDaysLeft=
 
 # auto detetct an os or a version based on cli output
 # used on param detect*
-# global string  myKey   name of product
+# global string  myKey      name of product
+# global string  myVersion  its version
 function autodetect(){
 
     myVersion=
 
-    case $myKey in
-        'os')
-            # ----- detect operating system and major version
-            # see inc_pluginfunctions
-            myKey=`ph.getOS`
-            myVersion=`ph.getOSMajor`
-            test "$myKey" = "centos" && grep -i stream /etc/centos-release >/dev/null 2>&1 && myVersion="${myVersion}-stream"
-            ;;
-        'mysqlany')            
-            local _tmpfile=/tmp/detect_mysql_$$
-            mysql --version >$_tmpfile 2>&1
-            if [ $? -ne 0 ]; then
-                rm -f $_tmpfile
-                ph.abort "UNKNOWN: mysql was not found. A Mysql version cannot be detected."
-            fi
-
-            # default: mysql
-            myKey=mysql
-
-            # scan for forks:
-            # todo: add ... percona ... memdb ... whatever
-            grep "MariaDB" $_tmpfile >/dev/null && myKey=mariadb
-
-            rm -f $_tmpfile
-            ;;
-        *)
-            # echo ERROR: detection for [$1] is not implemented
-    esac
+    # --- step 1: autodetection to find a key for os or software derivat
+    detectorPlugin="$0-versiondetect/autodetect-$myKey"
+    if [ -x "$detectorPlugin" ]; then
+        local mydata=$(.  "$detectorPlugin")
+        myKey=$(     echo $mydata | rev | cut -f 2 -d "|" | rev )
+        myVersion=$( echo $mydata | rev | cut -f 1 -d "|" | rev )
+    fi
 
+    # --- stop 2: find version number
     detectorPlugin="$0-versiondetect/detect-$myKey"
     if [ -x "$detectorPlugin" ]; then
         myVersion=`. "$detectorPlugin"`
@@ -232,6 +214,7 @@ fi
 myKey=$1
 myVersion=$2
 test $myVersion = "detect" && autodetect $myKey
+
 eol=$( findEolDate "$myKey" "$myVersion" )
 
 if [ -z "$eol" ]; then
diff --git a/check_eol-versiondetect/autodetect-mysqlany b/check_eol-versiondetect/autodetect-mysqlany
new file mode 100755
index 0000000..773950a
--- /dev/null
+++ b/check_eol-versiondetect/autodetect-mysqlany
@@ -0,0 +1,32 @@
+# ----------------------------------------------------------------------------
+# 
+# EOL - End of life detection - Warn before reaching end of life
+#
+# AUTODETECT MYSQL OR A FORK OF IT
+#
+# ----------------------------------------------------------------------------
+# 2021-11-02  v1.0  <axel.hahn@iml.unibe.ch>
+# ----------------------------------------------------------------------------
+
+_tmpfile=/tmp/detect_mysql_$$
+mysql --version >$_tmpfile 2>&1
+if [ $? -ne 0 ]; then
+    rm -f $_tmpfile
+    ph.abort "UNKNOWN: mysql was not found. A Mysql version cannot be detected."
+fi
+
+# ----------------------------------------------------------------------------
+
+# default: mysql
+myKey=mysql
+
+
+# scan for forks:
+# todo: add ... percona ... memdb ... whatever
+grep "MariaDB" $_tmpfile >/dev/null && myKey=mariadb
+
+rm -f $_tmpfile
+
+# ----------------------------------------------------------------------------
+
+echo "|$myKey|$myVersion"
diff --git a/check_eol-versiondetect/autodetect-os b/check_eol-versiondetect/autodetect-os
new file mode 100755
index 0000000..36978fc
--- /dev/null
+++ b/check_eol-versiondetect/autodetect-os
@@ -0,0 +1,26 @@
+# ----------------------------------------------------------------------------
+# 
+# EOL - End of life detection - Warn before reaching end of life
+#
+# AUTODETECT LINUX OS
+#
+# ----------------------------------------------------------------------------
+# 2021-11-02  v1.0  <axel.hahn@iml.unibe.ch>
+# ----------------------------------------------------------------------------
+
+
+myKey=`ph.getOS`
+myVersion=`ph.getOSMajor`
+
+case "$mykey" in
+    centos)
+        # detect centos stream
+        grep -i stream /etc/centos-release >/dev/null 2>&1 && myVersion="${myVersion}-stream"
+        ;;
+    *)
+        echo no special handling
+esac
+
+# ----------------------------------------------------------------------------
+
+echo "|$myKey|$myVersion"
diff --git a/check_eol-versiondetect/detect-mariadb b/check_eol-versiondetect/detect-mariadb
index ee81ea4..d815bf5 100755
--- a/check_eol-versiondetect/detect-mariadb
+++ b/check_eol-versiondetect/detect-mariadb
@@ -12,4 +12,4 @@
 #   mysql --version
 #   mysql  Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1
 
-mysql --version | grep MariaDB | awk '{ print $5 }' | cut -f 1,2 -d '.'
\ No newline at end of file
+mysql --version | grep MariaDB | awk '{ print $5 }' | cut -f 1,2 -d '.'
-- 
GitLab