diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..ff22a3a2106a97a8d2ed28fe0d0c68c12ee7f4f7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 M33 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..183da8518a329e2d5a549a308ec2a18fb42289a5 --- /dev/null +++ b/README.md @@ -0,0 +1,83 @@ +# FoundKey-admin-scripts-n-tips + +A collection of scripts and tips to make a FoundKey admin's life a little bit easyer. + +It is a complement to the [official documentation](https://FoundKey-hub.net/en/docs/install/docker.html). + +Abstract: a VPS with arm64 processor, Docker, a FoundKey instance, what could save you some time ? + +--- +--- + +# Tips list: + +## Separate code and data + +The default FoundKey docker install guide will place FoundKey code and your instance data in the same directory. Is something goes wrong on a later time, you may accidentally trash your datas (docker volumes mounted for the postgres database, redis cache...). + +Separate the FoundKey code (git clone...) from you container's volumes, something like this should do: +``` +/usr/local/foundkey/code/FoundKey +/usr/local/foundkey/data +``` + +You will need a custom yaml file for docker-compose, but it's worth it (see sample yaml file). + +Manage your FoundKey instance containers with docker-compose and the right path for code and datas: +``` +docker-compose --project-directory=/usr/local/FoundKey/data -f /usr/local/FoundKey/data/<your custom docker-compose>.yml +``` + +## Quickly adjust FoundKey defaults settings + +This particular setting in your instance configuration yaml in the `.config/default.yml` file will raises an error when federating with some (secured) mastodon instances, it's not trivial to find out, activate it to avoid such errors: +``` +signToActivityPubGet: true +``` + +## Arm64 specific + +Deploying your FoundKey instance on arm64 using docker is pretty straightforward, you will need to use some generic flavor of docker images instead of specific/pined version from the original docker-compose.yml file (see the exemple yaml file in this repository). + +--- +--- + +# Scripts list: + +## start_FoundKey.sh stop_FoundKey.sh +In a small VPS, or a limited computer like the rapsberry, or just because systemd, you may want to start and stop FoundKey with simple scripts like this. +For automatic start on boot, you may add this to your crontab: `@reboot /usr/local/bin/start_FoundKey.sh 1>>/var/log/syslog 2>&1` + +## check_update_FoundKey_git.sh +This script tries to determine if an update is needed, without pulling all source code from FoundKey's github depot. +Return 1 if an update is detected. + +## update_FoundKey.sh +Handles the grunt work for a git update, and containers rebuild. +It takes care of code base backup, you really should take care of database (and maybe data files) backup before upgrading. To this day FoundKey updates have not always been a smooth road. +It may automatically restart your instance to apply the update. + +## backup_postgres.sh +A daily backup for your docker postgresql database, run it daily +It will cycle with the day of week (1..7) +Add it to your crontab, when your instances has its lowest activity, ex: +`0 5 * * * /usr/local/bin/backup_postgres.sh` + +## cleanup_docker.sh +Prune all orphaned images, and stopped containers + +## get_container_id.sh +Get container ID from name (used in other scripts) + +--- +--- + +# Ressources list: + +## FoundKey-arm64.yml: + +A docker-compose yaml file, for an arm64 (raspberry pi, VPS arm instances) + +## Yolk__bad-blood__custom-emojis.zip: + +Yolk emojis, from [Bad-blood on Devianart](https://www.deviantart.com/bad-blood/art/Y-o-l-k-s-78118255). This zip file is ready to be imported on FoundKey (files with names and tags). diff --git a/Yolk__bad-blood__custom-emojis.zip b/Yolk__bad-blood__custom-emojis.zip new file mode 100644 index 0000000000000000000000000000000000000000..db2db9425aeaa08c216f49fa3d141e9fbfe3f407 Binary files /dev/null and b/Yolk__bad-blood__custom-emojis.zip differ diff --git a/backup_postgres.sh b/backup_postgres.sh new file mode 100755 index 0000000000000000000000000000000000000000..f043c5bd5c4e038861927cc59d6d7d045d054903 --- /dev/null +++ b/backup_postgres.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# A daily backup for your docker postgresql database, run it daily +# It will cycle with the day of week (1..7) +# +DT=`date +%w` + +### You will need to adjust DST, DOCKER_ENV and Paths ### +# Backup destination directory +DST=/usr/local/backup +# foundkey data (container volumes) directory +DATA=/usr/local/foundkey/data +# Docker env file from foundkey +DOCKER_ENV=$DATA/.config/docker.env +### + +source $DOCKER_ENV || exit 1 + +mkdir -p $DST +cd $DST || exit 1 + +# Get the posgres container ID +pgcont=`/usr/local/bin/get_container_id.sh postgres` || exit 1 + +# Dump quick, compress later +docker exec -i $pgcont pg_dump -U $POSTGRES_USER $POSTGRES_DB > backup-$POSTGRES_DB.$DT.pgsql + +nice gzip --force backup-$POSTGRES_DB.$DT.pgsql + +exit $? diff --git a/build_foundkey.sh b/build_foundkey.sh new file mode 100755 index 0000000000000000000000000000000000000000..a4eda6253300d89d2c74341cc5feed8c45b74faa --- /dev/null +++ b/build_foundkey.sh @@ -0,0 +1,107 @@ +#!/bin/bash +# +# semi automatic foundkey container build +# This script may trash your instance, use with caution, make backups... +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/foundkey.log +# foundkey (git) code directory +CODE=/usr/local/foundkey/code/FoundKey +# foundkey (containers volumes) data directory +DATA=/usr/local/foundkey/data +# Docker compose file name +COMPOSE=docker-compose.yml +# Backup destination directory +DST=/usr/local/backup +# Automatically stop, update and start the running instance +AUTOUPDATE=1 +TMP=/tmp/.update_foundkey.$$ +### + +# Foundkey settings +BRANCH=main +DEPOT="https://akkoma.dev/FoundKeyGang/FoundKey.git" +DEPOT_PATH=FoundKey + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 1 +fi + +echo "Retreiving codebase with git..." +cd $CODE +git clone -b $BRANCH $DEPOT +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +cd $DEPOT_PATH + +git checkout $BRANCH +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +# Compare version before/after pulling files +currentVersion=`grep version package.json | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` + +git pull | tee -a $TMP +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +grep -q "Already up to date." $TMP +if [ $? -eq 0 ]; then + echo "Your foundkey code is already up to date, version:$currentVersion." + rm -f $TMP + exit 0 +fi +rm -f $TMP + +latestVersion=`grep version package.json | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` +echo "Upgrade needed, current:$currentVersion latest:$latestVersion." + +git submodule update --init +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +git stash pop +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +echo "Building new images... (it will take some time)" +docker-compose --project-directory=$DATA --file $DATA/$COMPOSE build + +#sudo docker-compose stop && sudo docker-compose up -d +if [ $AUTOUPDATE -eq 0 ]; then + echo "Stop and restart the instance using:" + echo "stop_foundkey.sh" + echo "start_foundkey.sh" +else + echo "Automatically updating the instance, fingers crossed" + echo -n "Stopping..." + stop_foundkey.sh + if [ $? -ne 0 ]; then + echo "ERROR: something went wrong when stopping the instance" + exit 1 + fi + echo "done." + echo -n "Starting..." + start_foundkey.sh + if [ $? -ne 0 ]; then + echo "ERROR: something went wrong when starting the instance" + exit 1 + fi + echo "done." +fi + +exit 0 diff --git a/check_update_foundkey_git.sh b/check_update_foundkey_git.sh new file mode 100755 index 0000000000000000000000000000000000000000..a42ad09ab7d1b8be00ee6ad29cfe3278fb051782 --- /dev/null +++ b/check_update_foundkey_git.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# +# Try to determine quickly if an update is due +# This script may trash your instance, use with caution, make backups... +# +# Return 0 if an upgrade is needed +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/foundkey.log +# foundkey (git) code directory +CODE=/usr/local/foundkey/code/FoundKey +# foundkey (containers volumes) data directory +DATA=/usr/local/foundkey/data +# Docker compose file name +COMPOSE=docker-compose.yml +COMPOSE=foundkey-arm64.yml +# Backup destination directory +DST=/usr/local/backup +# Automatically stop, update and start the running instance +AUTOUPDATE=1 +TMP=/tmp/.update_foundkey.$$ +### + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 100 +fi + +cd $CODE +mv package.json package.json.$$ + +# Compare version before/after pulling files +currentVersion=`grep version package.json.$$ | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` + +git fetch --all +git checkout origin/main -- package.json +if [ $? -ne 0 ]; then + echo "Error, bailing out" + mv package.json.$$ package.json + exit 100 +fi + +latestVersion=`grep version package.json | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` +rm -f package.json +mv package.json.$$ package.json + +echo -n "Current:$currentVersion Latest:$latestVersion... " +if [ "$currentVersion" == "$latestVersion" ]; then + echo "Your foundkey code is already up to date." + exit 1 +fi + +echo "Upgrade needed." + +exit 0 diff --git a/check_update_misskey_git.sh b/check_update_misskey_git.sh new file mode 100755 index 0000000000000000000000000000000000000000..9a089128c6b36a93a3026812fb625cf49a451309 --- /dev/null +++ b/check_update_misskey_git.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# +# Try to determine quickly if an update is due +# This script may trash your instance, use with caution, make backups... +# +# Return 0 if an upgrade is needed +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/misskey.log +# Misskey (git) code directory +CODE=/usr/local/misskey/code +# Misskey (containers volumes) data directory +DATA=/usr/local/misskey/data +# Docker compose file name +COMPOSE=docker-compose.yml +COMPOSE=misskey-arm64.yml +# Backup destination directory +DST=/usr/local/backup +# Automatically stop, update and start the running instance +AUTOUPDATE=1 +TMP=/tmp/.update_misskey.$$ +### + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 100 +fi + +cd $CODE +mv package.json package.json.$$ + +# Compare version before/after pulling files +currentVersion=`grep version package.json.$$ | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` + +git fetch --all +git checkout origin/master -- package.json +if [ $? -ne 0 ]; then + echo "Error, bailing out" + mv package.json.$$ package.json + exit 100 +fi + +latestVersion=`grep version package.json | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` +rm -f package.json +mv package.json.$$ package.json + +echo -n "Current:$currentVersion Latest:$latestVersion... " +if [ "$currentVersion" == "$latestVersion" ]; then + echo "Your Misskey code is already up to date." + exit 1 +fi + +echo "Upgrade needed." + +exit 0 diff --git a/cleanup_docker.sh b/cleanup_docker.sh new file mode 100755 index 0000000000000000000000000000000000000000..efa8a1c7a0cc4494c96a2cb46b2fb4a6e0bcb527 --- /dev/null +++ b/cleanup_docker.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# +# Prune all orphaned images, and stopped containers +# +docker container prune --force +docker image prune --all --force +exit $? diff --git a/foundkey-arm64.yml b/foundkey-arm64.yml new file mode 100644 index 0000000000000000000000000000000000000000..2a8f089b22342498344b087ddcc79bb27dc0d1c8 --- /dev/null +++ b/foundkey-arm64.yml @@ -0,0 +1,44 @@ +version: "3" + +services: + web: + build: + context: /usr/local/foundkey/code/FoundKey + dockerfile: /usr/local/foundkey/data/Dockerfile-foundkey-arm + restart: unless-stopped + links: + - db + - redis + ports: + - "127.0.0.1:3000:3000" + networks: + - internal_network + - external_network + volumes: + - /usr/local/foundkey/data/files:/foundkey/files + - /usr/local/foundkey/data/.config:/foundkey/.config:ro + + redis: + restart: unless-stopped + image: redis:7.0-alpine + command: redis-server /usr/local/etc/redis/redis.conf + networks: + - internal_network + volumes: + - /usr/local/foundkey/data/redis:/data + - /usr/local/foundkey/data/.config/redis.conf:/usr/local/etc/redis/redis.conf + + db: + restart: unless-stopped + image: postgres:alpine + networks: + - internal_network + env_file: + - .config/docker.env + volumes: + - /usr/local/foundkey/data/db:/var/lib/postgresql/data + +networks: + internal_network: + internal: true + external_network: diff --git a/get_container_id.sh b/get_container_id.sh new file mode 100755 index 0000000000000000000000000000000000000000..f36be05be117a75f67ea2cd2eea3b117dae92192 --- /dev/null +++ b/get_container_id.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# +# Get container ID from name +# +docker container ls|grep "$1"|awk '{print $1}'|grep -v CONTAINER +exit $? diff --git a/start_foundkey.sh b/start_foundkey.sh new file mode 100755 index 0000000000000000000000000000000000000000..56f4fe5127f2ef597abfcf04535132e098a282e8 --- /dev/null +++ b/start_foundkey.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# +# Start foundkey instance +# Rotate logs +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker +# foundkey (git) code directory +CODE=/usr/local/foundkey/code/FoundKey +# foundkey (containers volumes) data directory +DATA=/usr/local/foundkey/data +# Docker compose file name +COMPOSE=docker-compose.yml +COMPOSE=foundkey-arm64.yml +### + +DT=`date +%s` +mkdir -p $LOG || exit 1 + +# Rotate logs if needed on each (re)start, cleanup with tmpreaper or something +if [ -f $LOG/foundkey.log ]; then + mv $LOG/foundkey.log $LOG/foundkey.log.$DT + nice gzip $LOG/foundkey.log.$DT & 1>/dev/null 2>&1 +fi + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 1 +fi + +echo "[start_foundkey] by $USER..." 1>$LOG/foundkey.log +nohup docker-compose --project-directory=$DATA --file $DATA/$COMPOSE up 1>>$LOG/foundkey.log 2>&1 & +exit $? diff --git a/stop_foundkey.sh b/stop_foundkey.sh new file mode 100755 index 0000000000000000000000000000000000000000..39b81c99c6f34159322cb63391edd3b7d8261e77 --- /dev/null +++ b/stop_foundkey.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# +# stop foundkey instance using docker commands +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/foundkey.log +# foundkey (git) code directory +CODE=/usr/local/foundkey/code +# foundkey (containers volumes) data directory +DATA=/usr/local/foundkey/data +# Docker compose file name +COMPOSE=docker-compose.yml +COMPOSE=foundkey-arm64.yml +### + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 1 +fi + +echo "[stop_foundkey] by $USER started..." >> $LOG +docker-compose --project-directory=$DATA -f $DATA/$COMPOSE stop 1>>$LOG 2>&1 +RV=$? +echo "[stop_foundkey] done ($RV)" >> $LOG +exit $RV diff --git a/stop_misskey.sh b/stop_misskey.sh new file mode 100755 index 0000000000000000000000000000000000000000..8e20abd6d167085b5aa0a786644373bff0172ef3 --- /dev/null +++ b/stop_misskey.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# +# stop misskey instance using docker commands +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/misskey.log +# Misskey (git) code directory +CODE=/usr/local/misskey/code +# Misskey (containers volumes) data directory +DATA=/usr/local/misskey/data +# Docker compose file name +COMPOSE=docker-compose.yml +COMPOSE=misskey-arm64.yml +COMPOSE=foundkey-arm64.yml +### + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 1 +fi + +echo "[stop_misskey] by $USER started..." >> $LOG +docker-compose --project-directory=$DATA -f $DATA/$COMPOSE stop 1>>$LOG 2>&1 +RV=$? +echo "[stop_misskey] done ($RV)" >> $LOG +exit $RV diff --git a/stop_misskey.sh.org b/stop_misskey.sh.org new file mode 100755 index 0000000000000000000000000000000000000000..eccd5cd7aa71a1b59c303388ef446f7d93e0ede1 --- /dev/null +++ b/stop_misskey.sh.org @@ -0,0 +1,27 @@ +#!/bin/bash +# +# stop misskey instance using docker commands +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/misskey.log +# Misskey (git) code directory +CODE=/usr/local/misskey/code +# Misskey (containers volumes) data directory +DATA=/usr/local/misskey/data +# Docker compose file name +COMPOSE=docker-compose.yml +COMPOSE=misskey-arm64.yml +### + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 1 +fi + +echo "[stop_misskey] by $USER started..." >> $LOG +docker-compose --project-directory=$DATA -f $DATA/$COMPOSE stop 1>>$LOG 2>&1 +RV=$? +echo "[stop_misskey] done ($RV)" >> $LOG +exit $RV diff --git a/update_foundkey_git.sh b/update_foundkey_git.sh new file mode 100755 index 0000000000000000000000000000000000000000..9067835db643100943ad76f25497f6f5622c0c9c --- /dev/null +++ b/update_foundkey_git.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# +# semi automatic foundkey updates +# This script may trash your instance, use with caution, make backups... +# + +### You will need to adjust Paths ### +# Store containers log +LOG=/var/log/docker/foundkey.log +# foundkey (git) code directory +CODE=/usr/local/foundkey/code/FoundKey +# foundkey (containers volumes) data directory +DATA=/usr/local/foundkey/data +# Docker compose file name +COMPOSE=docker-compose.yml +# Backup destination directory +DST=/usr/local/backup +# Automatically stop, update and start the running instance +AUTOUPDATE=1 +TMP=/tmp/.update_foundkey.$$ +### + +if [ ! -d $CODE ] || [ ! -d $DATA ]; then + echo "Error, you may need to adjust path in this script" + exit 1 +fi + +# Take some care before upgrading +echo "Backing up current code... (you really should have done yourself a data backup before upgrading, something like backup_postgres.sh)" +echo "$CODE will be archived in $DST/backup-code.tgz" +tar -czf $DST/backup-foundkey-code.tgz $CODE/* +if [ $? -ne 0 ]; then + echo "ERROR: something went wrong during backup..." + exit 1 +fi + +echo "Updating codebase with git..." +cd $CODE + +git stash +#if [ $? -ne 0 ]; then +# echo "Error, bailing out" +# exit 1 +#fi + +git checkout main +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +# Compare version before/after pulling files +currentVersion=`grep version package.json | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` + +git pull | tee -a $TMP +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +grep -q "Already up to date." $TMP +if [ $? -eq 0 ]; then + echo "Your Foundkey code is already up to date, version:$currentVersion." + rm -f $TMP + exit 0 +fi +rm -f $TMP + +latestVersion=`grep version package.json | awk '{ print $2 }' | sed 's/\"//g' | sed 's/,//'` +echo "Upgrade needed, current:$currentVersion latest:$latestVersion." + +git submodule update --init +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +git stash pop +if [ $? -ne 0 ]; then + echo "Error, bailing out" + exit 1 +fi + +echo "Building new images... (it will take some time)" +docker-compose --project-directory=$DATA --file $DATA/$COMPOSE build + +#sudo docker-compose stop && sudo docker-compose up -d +if [ $AUTOUPDATE -eq 0 ]; then + echo "Stop and restart the instance using:" + echo "stop_foundkey.sh" + echo "start_foundkey.sh" +else + echo "Automatically updating the instance, fingers crossed" + echo -n "Stopping..." + stop_foundkey.sh + if [ $? -ne 0 ]; then + echo "ERROR: something went wrong when stopping the instance" + exit 1 + fi + echo "done." + sleep 3 + echo -n "Starting..." + start_foundkey.sh + if [ $? -ne 0 ]; then + echo "ERROR: something went wrong when starting the instance" + exit 1 + fi + echo "done." +fi + +exit 0