Skip to content

Instantly share code, notes, and snippets.

@elpatron68
Last active September 9, 2023 08:50
Show Gist options
  • Save elpatron68/944623c91374b56d24eef8b97321cc5c to your computer and use it in GitHub Desktop.
Save elpatron68/944623c91374b56d24eef8b97321cc5c to your computer and use it in GitHub Desktop.
Checks latest Mattermost version, compares against installed version from .env, stops, updates container, finally deletes orphaned container(s)
#!/bin/python3
# © 2023 Markus Busche, [email protected]
# - Checks latest Mattermost version
# - Compares against installed version from .env
# - Stops, updates container
# - Finally deletes orphaned container(s)
import re
from urllib import response
import requests
from os.path import exists
from requests_html import HTMLSession
from packaging import version
import subprocess
WEBHOOKURL = '<your Mattermost webhook url>'
CHANNEL = 'Town Square'
def getLatestVersion():
downloadUrl = ""
session = HTMLSession()
r = session.get('https://mattermost.com/deploy/')
htmlPageText=r.text
regex = r'https:\/\/releases\.mattermost\.com\/\d+\.\d+\.\d+\/mattermost-\d+\.\d+\.\d+-linux-amd64\.tar\.gz'
downloadUrl = ''
version = ''
try:
downloadUrl = re.findall(regex, htmlPageText)[0]
except:
pass
try:
version = re.findall(r'\d+\.\d+\.\d+', downloadUrl)[0]
except:
pass
return downloadUrl, version
def readInstalledVersion():
with open('.env', 'r') as file:
text = file.read().rstrip()
line = re.findall('MATTERMOST_IMAGE_TAG=\d\.\d\.\d', text)[0]
result = line.split('=')[1]
return result
def updateEnv(version):
with open('.env', 'r') as file:
content = file.read()
content_new = re.sub('MATTERMOST_IMAGE_TAG=\d\.\d\.\d', 'MATTERMOST_IMAGE_TAG=' + version, content, flags = re.M)
with open('.env', 'w') as file:
file.write(content_new)
def isNewer(latestVersion, installedVersion):
return installedversion.parse(latestVersion) > installedversion.parse(installedVersion)
def sendMM(text):
headers = {'Content-Type': 'application/json',}
values = '{ "channel": "' + CHANNEL + '", "text": "' + text + '"}'
response = requests.post(WEBHOOKURL, headers=headers, data=values)
return response.status_code
def updateContainer():
print('Stop running container')
# docker compose -f docker-compose.yml -f docker-compose.nginx.yml down
args = 'compose -f docker-compose.yml -f docker-compose.nginx.yml down'
result = subprocess.run(['docker', 'args'], capture_output=True, text=True)
print('Update container')
# docker compose -f docker-compose.yml -f docker-compose.nginx.yml up -d
args = 'compose -f docker-compose.yml -f docker-compose.nginx.yml up -d'
result = subprocess.run(['docker', 'args'], capture_output=True, text=True)
print('Prune orphaned container(s)')
# docker image prune -a --force
args = 'image prune -a --force'
result = subprocess.run(['docker', 'args'], capture_output=True, text=True)
if __name__ == "__main__":
installedversion = readInstalledVersion()
print('Installed version: ' + installedversion)
url, latestversion = getLatestVersion()
if (isNewer(latestversion, installedversion)):
print('New Mattermost version found, information updated:')
print('Installed version: ' + installedversion)
print('Latest version: ' + latestversion)
print('Updating .env')
updateEnv(latestversion)
updateContainer()
text = 'New Mattermost version found!\nLatest version: ' + latestversion + \
'\nFormer version: ' + installedversion + \
'\n[Release notes](https://docs.mattermost.com/install/self-managed-changelog.html)'
result = sendMM(text=text)
print('Message sent: ' + str(result))
else:
print('Nothing to do (you are up-to-date).')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment