From 3408cfd03a684d8830aee894df5204fed474a1ba Mon Sep 17 00:00:00 2001 From: echolib Date: Wed, 18 Sep 2024 11:04:27 +0200 Subject: [PATCH] Add tools/has_update + README --- tools/README.md | 39 +++++++++++++++++ tools/has_update | 111 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 tools/README.md create mode 100644 tools/has_update diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 0000000..1170193 --- /dev/null +++ b/tools/README.md @@ -0,0 +1,39 @@ +# Tools + +## has_update +This tool is used to check - with a set registred project - for: +- available online latest version +- current installed version + +This tool, writtent in python and under GPLv3 Licence assumes project is +installed using git in an admin project name owner on the system. + +It is configured here for "mastodon" project, but you can copy and use it, +by changing the settings inside the script + +``` +owner = "mastodon" # Also used for sudo -u {owner} +name = "mastodon" +repo = f"https://api.github.com/repos/{owner}/{name}/releases/latest" + +``` + +### Installation +``` +cd /srv +git clone https://forge.a-lec.org/cominfra/toot.git +chmod +x toot/tools/has_update +``` + +### Usage +``` +Usage: has_update [argument] + -h : This help ! + -v : Show both versions (online, and installed) +``` + +This tool is intended to inform administrator about available update of a +project. You can use it with an external script in a crontab that checks the +ouput of *has_update*. Do not forget, in that case, to not give argument. With +no argument, *has_update* ouputs a message ONLY if an update is available. + diff --git a/tools/has_update b/tools/has_update new file mode 100644 index 0000000..486a44f --- /dev/null +++ b/tools/has_update @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +# version: 0.1 +# File: /srv/toot/has_update + +# Copyright (C) 2023 Cyrille Louarn + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see .. + +#---------------------------------------------------------------------- +# XMPP: echolib (echolib@a-lec.org) +# +# Description: +# Search for release update using git +# ! Assume Project is insalled from git +# +# You may use with script (if you do not give argument to has_update) +# No output = no update +# Output update = v[latest] > v[current] +# +# Convenience -v argument (do not use with script): show both versions +# +# RC errors : +# 1 = Online Capture problem +# 2 = Installed Capture problem +#---------------------------------------------------------------------- + +import requests, subprocess, sys + + +# Set Project +#------------ +owner = "mastodon" # Also used for sudo -u {owner} +name = "mastodon" + + +helps = """Usage: has_update [argument] + -h : This help ! + -v : Show both versions (online, and installed) +""" + +#=====================================================# +# Check available version using Git API with requests # +#-----------------------------------------------------# +def get_available_release(): + url = f"https://api.github.com/repos/{owner}/{name}/releases/latest" + response = requests.get(url) + if response.status_code == 200: + latest_release = response.json() + return latest_release['tag_name'] + else: + print(f"Failed to get online lates release: {response.status_code}") + sys.exit(1) + + +#===========================================# +# Check installed version using git command # +# Assume Project is insalled from git # +#-------------------------------------------# +def get_installed_release(): + try: + result = subprocess.run( + ['sudo', '-u', owner, 'git', 'describe', '--tags', '--exact-match'], + stdout=subprocess.PIPE, # Get output + stderr=subprocess.PIPE, # Get error (if any) + text=True, # Set stdout + stderr + check=True # exception if failure + ) + + # Output captured successfully + return result.stdout.strip() + + except subprocess.CalledProcessError as e: + # Handle the case where the command fails + print("Failed to get installed release:", e.stderr.strip()) + sys.exit(2) + + +#======# +# Main # +#------# + +# With -h argument, show both versions +if '-h' in sys.argv: + print(helps) + sys.exit(0) + +# Get versions (online and local) +available_version = get_available_release() +installed_version = get_installed_release() + + +# With -v argument, show both versions +if '-v' in sys.argv: + print(f"{owner}:") + print(f"- Available: {available_version}\n-Installed: {installed_version}") + +# A new version is available +elif available_version != installed_version: + print(f"{owner}: {installed_version} -> {available_version}") +