mastodon-auto-update.py
原始文件
import subprocess
import sys
import requests
def run_command(command):
print(command)
process = subprocess.Popen(['bash', '-c', '. /home/mastodon/.bashrc; . /home/mastodon/.profile; ' + command], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode != 0:
print(f'Error executing command: {command}')
if err:
print(f'Error message: {err.decode("utf-8")}')
quit(1)
else:
if out:
print(f'{out.decode("utf-8")}')
return True
# Get the latest release from the Mastodon repository
response = requests.get('https://api.github.com/repos/mastodon/mastodon/releases/latest')
data = response.json()
latest_release = data['tag_name']
# Get the current tag of the local repository
process = subprocess.Popen(['git', 'describe', '--tags'], stdout=subprocess.PIPE)
out, err = process.communicate()
current_tag = out.decode('utf-8').strip()
# Compare the latest release with the current tag
if latest_release == current_tag:
print('Local repository is up to date.')
else:
print(f'Local repository is not up to date. We have {current_tag}, latest release is {latest_release}.')
# Fetch the latest changes and checkout to the latest release
if run_command(f'git fetch && git checkout {latest_release}'):
if run_command('/home/mastodon/.rbenv/shims/bundle install') and run_command('yarn install --frozen-lockfile'):
sys.exit(100)
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
原始文件
[Unit]
Description=Update Mastodon
[Service]
WorkingDirectory=/srv/mastodon
ExecStart=/srv/mastodon/update.sh
SyslogIdentifier=mastodon-update
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
原始文件
[Unit]
Description=Run mastodon-update daily at 7 AM
[Timer]
OnCalendar=*-*-* 07:00:00
Persistent=true
[Install]
WantedBy=timers.target
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 |
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 |
11 |