Add tools/has_update + README

This commit is contained in:
Cyrille L 2024-09-18 11:04:27 +02:00
parent 217db5e2ef
commit 3408cfd03a
2 changed files with 150 additions and 0 deletions

39
tools/README.md Normal file
View File

@ -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.

111
tools/has_update Normal file
View File

@ -0,0 +1,111 @@
#!/usr/bin/env python3
# version: 0.1
# File: /srv/toot/has_update
# Copyright (C) 2023 Cyrille Louarn <echolib+tools@a-lec.org>
# 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 <https://www.gnu.org/licenses/>..
#----------------------------------------------------------------------
# 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}")