Last active 1708112317

Keep your Mastodon install up to date.

cyberes's Avatar cyberes revised this gist 1708112317. Go to revision

4 files changed, 66 insertions

mastodon-auto-update.py(file created)

@@ -0,0 +1,40 @@
1 + import subprocess
2 + import sys
3 +
4 + import requests
5 +
6 +
7 + def 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
23 + response = requests.get('https://api.github.com/repos/mastodon/mastodon/releases/latest')
24 + data = response.json()
25 + latest_release = data['tag_name']
26 +
27 + # Get the current tag of the local repository
28 + process = subprocess.Popen(['git', 'describe', '--tags'], stdout=subprocess.PIPE)
29 + out, err = process.communicate()
30 + current_tag = out.decode('utf-8').strip()
31 +
32 + # Compare the latest release with the current tag
33 + if latest_release == current_tag:
34 + print('Local repository is up to date.')
35 + else:
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(file created)

@@ -0,0 +1,7 @@
1 + [Unit]
2 + Description=Update Mastodon
3 +
4 + [Service]
5 + WorkingDirectory=/srv/mastodon
6 + ExecStart=/srv/mastodon/update.sh
7 + SyslogIdentifier=mastodon-update

mastodon-update.timer(file created)

@@ -0,0 +1,9 @@
1 + [Unit]
2 + Description=Run mastodon-update daily at 7 AM
3 +
4 + [Timer]
5 + OnCalendar=*-*-* 07:00:00
6 + Persistent=true
7 +
8 + [Install]
9 + WantedBy=timers.target

update.sh(file created)

@@ -0,0 +1,10 @@
1 + #!/bin/bash
2 +
3 + sudo -u mastodon python3 /srv/mastodon/mastodon-auto-update.py
4 +
5 +
6 + if [ $? -eq 100 ]; then
7 + # If the exit code is 100, restart the Mastodon services
8 + echo "Restarting Mastodon..."
9 + sudo systemctl restart 'mastodon-*'
10 + fi
Newer Older