Last active 1708112317

Keep your Mastodon install up to date.

mastodon-auto-update.py Raw
1import subprocess
2import sys
3
4import requests
5
6
7def run_command(command):
8 print(command)
9 process = subprocess.Popen(['bash', '-c', '. /home/mastodon/.bashrc; . /home/mastodon/.profile; ' + command], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
10 out, err = process.communicate()
11 if process.returncode != 0:
12 print(f'Error executing command: {command}')
13 if err:
14 print(f'Error message: {err.decode("utf-8")}')
15 quit(1)
16 else:
17 if out:
18 print(f'{out.decode("utf-8")}')
19 return True
20
21
22# Get the latest release from the Mastodon repository
23response = requests.get('https://api.github.com/repos/mastodon/mastodon/releases/latest')
24data = response.json()
25latest_release = data['tag_name']
26
27# Get the current tag of the local repository
28process = subprocess.Popen(['git', 'describe', '--tags'], stdout=subprocess.PIPE)
29out, err = process.communicate()
30current_tag = out.decode('utf-8').strip()
31
32# Compare the latest release with the current tag
33if latest_release == current_tag:
34 print('Local repository is up to date.')
35else:
36 print(f'Local repository is not up to date. We have {current_tag}, latest release is {latest_release}.')
37 # Fetch the latest changes and checkout to the latest release
38 if run_command(f'git fetch && git checkout {latest_release}'):
39 if run_command('/home/mastodon/.rbenv/shims/bundle install') and run_command('yarn install --frozen-lockfile'):
40 sys.exit(100)
mastodon-update.service Raw
1[Unit]
2Description=Update Mastodon
3
4[Service]
5WorkingDirectory=/srv/mastodon
6ExecStart=/srv/mastodon/update.sh
7SyslogIdentifier=mastodon-update
mastodon-update.timer Raw
1[Unit]
2Description=Run mastodon-update daily at 7 AM
3
4[Timer]
5OnCalendar=*-*-* 07:00:00
6Persistent=true
7
8[Install]
9WantedBy=timers.target
10
update.sh Raw
1#!/bin/bash
2
3sudo -u mastodon python3 /srv/mastodon/mastodon-auto-update.py
4
5
6if [ $? -eq 100 ]; then
7 # If the exit code is 100, restart the Mastodon services
8 echo "Restarting Mastodon..."
9 sudo systemctl restart 'mastodon-*'
10fi
11