Skip to content

Instantly share code, notes, and snippets.

@kunimi53chi
Last active May 11, 2019 16:50
Show Gist options
  • Save kunimi53chi/b6bd2cb88750e060ad74835a2a2aebe2 to your computer and use it in GitHub Desktop.
Save kunimi53chi/b6bd2cb88750e060ad74835a2a2aebe2 to your computer and use it in GitHub Desktop.
Mastodon(on Docker) Automatic Update Script
#!/bin/bash
# =====================================================================
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
# =====================================================================
# ***** HOU TO USE *****
# 1. In docker-compose.yml, change image row: "tootsuite/mastodon:vX.X.X" -> tootsuite/mastodon:${VERSION}
# 2. Make new ".env" file in your mastodon directory
# 3. Add description to .env file: VERSION=
# 4. Edit this file of "SET YOUR CONFIG"
# 5. Set cron (ex:) `0 3 * * * bash /home/ubuntu/mastodon_etc/mastodonautoupdate.sh >/dev/null 2>>/home/ubuntu/mastodon_etc/autoupdatelog.txt`
# Do you need more information? see: https://gist.github.com/kunimi53chi/506a5598cec32f7c253d93dae650a449 (written in Japanese)
# CONTACT: @kunimi_komichi@mstdn.komittee.net | https://keybase.io/kunimi_komichi
set -eu
# === SET YOUR CONFIG ===
readonly GIT_REPOSITORY='/home/user/mastodon/'
readonly DOMAIN='its.your.domain'
readonly LOGFILEPATH='/home/user/mastodon_etc/autoupdatelog.txt'
readonly ACCESS_TOKEN='' # you can get it from: your.domain/settings/applications
# =================
# SUB ROUTINE
# =================
#
# attention toot for follower. Please change the statement that you want to toot.
function choiceToot () {
# Result Report
local readonly STATUS_SUCCESS='(Auto Toot) YEEEEAAAAAH. '"${DOMAIN}"' update, ALL IS DONE!!!'
local readonly STATUS_DONOTHING='(Auto Toot) OK, There are not any TAG changes from the last update.'
# Progress Report
local readonly STATUS_START='(Auto Toot) Its time. Automatic Updates, lets start.'
local readonly STATUS_GIT='(Auto Toot) Now is Git Operation...'
local readonly STATUS_BUILD='(Auto Toot) Docker Build...'
local readonly STATUS_DB_MIGRATE='(Auto Toot) db:migrate...'
local readonly STATUS_ASSETS_PRECOMPILE='(Auto Toot) assets:precompile...'
local readonly STATUS_UP='(Auto Toot) OK, Everyone is all here. Docker Up!'
# $1=progress,
case $1 in
'start')
post "$STATUS_START"
logstart;;
'git')
post "$STATUS_GIT";;
'build')
post "$STATUS_BUILD";;
'db:migrate')
post "$STATUS_DB_MIGRATE";;
'assets:precompile')
post "$STATUS_ASSETS_PRECOMPILE";;
'up')
post "$STATUS_UP";;
'success')
post "$STATUS_SUCCESS"
logend;;
'donothing')
post "$STATUS_DONOTHING"
logend;;
*)
catch
esac
}
function post () {
local POST_STATUS=$1
local API="https://${DOMAIN}/api/v1/statuses"
curl -X POST -Ss ${API} \
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
-d "status=${POST_STATUS}"
}
function logstart () {
echo -e "`date +'%Y/%m/%d %H:%M:%S'` UPDATE START." >> $LOGFILEPATH
}
function logend () {
echo -e "`date +'%Y/%m/%d %H:%M:%S'` ALL IS DONE." >> $LOGFILEPATH
}
function isDonAlive () {
local DOMAIN_WITH_HTTPS="https://${DOMAIN}/"
while true; do
local DONALIVE=$(curl -s -o /dev/null -LI -w "%{http_code}\n" ${DOMAIN_WITH_HTTPS})
if [ $DONALIVE -eq 200 ]; then
break
fi
# retry
sleep 5s
done
}
function catch () {
local readonly STATUS_FAILED='(Auto Toot) Oops! '"${DOMAIN}"' update FAILED. Im sorry for the inconvenience.'
echo -e "`date +'%Y/%m/%d %H:%M:%S'` *****FAILED***** Failure Point is $2, line $1." >> $LOGFILEPATH
post "$STATUS_FAILED"
}
trap 'catch $LINENO $BASH_COMMAND' ERR
# ==========================
# MAIN ROUTINE
# ==========================
#
cd ${GIT_REPOSITORY}
choiceToot 'start'
choiceToot 'git'
# If "insufficient permission for adding an object to repository database .git/objects" error occured,
# do command `$ sudo chmod -R 0777 .git/objects/` on your clone repository.
# see: https://blog.t5o.me/post/20121227/git-error-insufficient.html (written in Japanese)
# see also: https://stackoverflow.com/questions/6448242/git-push-error-insufficient-permission-for-adding-an-object-to-repository-datab
git fetch
# is update needed?
CURRENT='DEFAULT'
LATEST='DEFAULT'
CURRENT=$(git describe --tags)
LATEST=$(git describe --tags `git rev-list --tags --max-count=1`)
if [ ${CURRENT} = ${LATEST} ]; then
choiceToot 'donothing'
exit 0
fi
# ok, update mastodon to latest version.
git stash
git checkout ${LATEST}
git stash pop
ENV_PATH="${GIT_REPOSITORY}.env"
sudo sed -i -e "s/VERSION=.*/VERSION=${LATEST}/g" ${ENV_PATH}
choiceToot 'build'
sudo docker-compose build
choiceToot 'db:migrate'
sudo docker-compose run --rm web bundle exec rake db:migrate
choiceToot 'assets:precompile'
sudo docker-compose run --rm web bundle exec rake assets:precompile
choiceToot 'up'
sleep 10s # to intended toot before docker-compose up
sudo docker-compose up -d
isDonAlive
choiceToot 'success'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment