diff --git a/README.md b/README.md index f7e01a5..b9cc0ef 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,21 @@ Random opinionated helper scripts & front-end customizations for my [personal Ma ```sh git clone https://github.com/jakejarvis/mastodon-scripts.git /home/mastodon/scripts -git config --global --add safe.directory /home/mastodon/scripts +# apply vanilla patches: cd /home/mastodon/live -git apply --allow-binary-replacement --whitespace=warn /home/mastodon/scripts/patches/*.patch /home/mastodon/scripts/glitch/*.patch || true -RAILS_ENV=production bundle exec rails assets:precompile +git apply --allow-binary-replacement /home/mastodon/scripts/patches/*.patch +# apply glitch-only patches: +if [ -d /home/mastodon/live/app/javascript/flavours/glitch ]; then + git apply --allow-binary-replacement /home/mastodon/scripts/patches/glitch/*.patch +fi + +# compile new assets: +RAILS_ENV=production bundle exec rails assets:precompile chown -R mastodon:mastodon /home/mastodon/{scripts,live} +# restart Mastodon: systemctl restart mastodon-* ``` diff --git a/scripts/apply_patches.sh b/scripts/apply_patches.sh index afebeb2..52eba93 100755 --- a/scripts/apply_patches.sh +++ b/scripts/apply_patches.sh @@ -5,21 +5,44 @@ set -euo pipefail # default paths MASTODON_ROOT=/home/mastodon +APP_ROOT="$MASTODON_ROOT/live" +SCRIPTS_ROOT="$MASTODON_ROOT/scripts" +RBENV_ROOT="$MASTODON_ROOT/.rbenv" -if [ ! -d "$MASTODON_ROOT/scripts" ] +# clone this repo if it doesn't exist in the proper location +if [ ! -d "$SCRIPTS_ROOT" ] then - # clone repo - sudo -u mastodon git clone https://github.com/jakejarvis/mastodon-scripts.git "$MASTODON_ROOT/scripts" + sudo -u mastodon git clone https://github.com/jakejarvis/mastodon-scripts.git "$SCRIPTS_ROOT" # fix permissions - sudo chown -R mastodon:mastodon "$MASTODON_ROOT/scripts" - sudo git config --global --add safe.directory "$MASTODON_ROOT/scripts" + sudo chown -R mastodon:mastodon "$SCRIPTS_ROOT" + sudo git config --global --add safe.directory "$SCRIPTS_ROOT" fi # pull latest patches -cd "$MASTODON_ROOT/scripts" -sudo -u mastodon git pull origin main +cd "$SCRIPTS_ROOT" +sudo -u mastodon git fetch origin --all +sudo -u mastodon git reset origin/main --hard # apply custom patches -cd "$MASTODON_ROOT/live" -sudo -u mastodon git apply --allow-binary-replacement --whitespace=warn "$MASTODON_ROOT"/scripts/patches/*.patch || true +cd "$APP_ROOT" +sudo -u mastodon git apply --allow-binary-replacement "$SCRIPTS_ROOT"/patches/*.patch +if [ -d "$APP_ROOT/app/javascript/flavours/glitch" ]; +then + # apply additional glitch-only patches: + sudo -u mastodon git apply --allow-binary-replacement "$SCRIPTS_ROOT"/patches/glitch/*.patch +fi + +# update dependencies +echo "Updating deps..." +sudo -u mastodon "$RBENV_ROOT/shims/bundle" install --jobs "$(getconf _NPROCESSORS_ONLN)" +sudo -u mastodon yarn install --pure-lockfile --network-timeout 100000 + +# compile new assets +echo "Compiling new assets..." +sudo -u mastodon RAILS_ENV=production "$RBENV_ROOT/shims/bundle" exec rails assets:precompile +sudo chown -R mastodon:mastodon "$APP_ROOT" + +# restart frontend +echo "Restarting mastodon-web..." +sudo systemctl restart mastodon-web diff --git a/scripts/backup.sh b/scripts/backup.sh index 50a2f83..6670777 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -5,6 +5,8 @@ set -euo pipefail # default paths MASTODON_ROOT=/home/mastodon +APP_ROOT="$MASTODON_ROOT/live" +BACKUPS_ROOT="$MASTODON_ROOT/backups" if [ "$(systemctl is-active mastodon-web.service)" = "active" ] then @@ -14,10 +16,10 @@ then echo "" fi -if [ ! -d "$MASTODON_ROOT/backups" ] +if [ ! -d "$BACKUPS_ROOT" ] then - sudo mkdir -p "$MASTODON_ROOT/backups" - sudo chown -R mastodon:mastodon "$MASTODON_ROOT/backups" + sudo mkdir -p "$BACKUPS_ROOT" + sudo chown -R mastodon:mastodon "$BACKUPS_ROOT" fi TEMP_DIR=$(sudo -u mastodon mktemp -d) @@ -32,7 +34,7 @@ echo "Backing up secrets..." sudo cp "$MASTODON_ROOT/live/.env.production" "$TEMP_DIR/env.production" echo "Compressing..." -ARCHIVE_DEST="$MASTODON_ROOT/backups/$(date "+%Y.%m.%d-%H.%M.%S").tar.gz" +ARCHIVE_DEST="$BACKUPS_ROOT/$(date "+%Y.%m.%d-%H.%M.%S").tar.gz" sudo tar --owner=0 --group=0 -czvf "$ARCHIVE_DEST" -C "$TEMP_DIR" . sudo chown mastodon:mastodon "$ARCHIVE_DEST" diff --git a/scripts/tootctl_shim.sh b/scripts/tootctl_shim.sh index ae49c62..48f3baa 100755 --- a/scripts/tootctl_shim.sh +++ b/scripts/tootctl_shim.sh @@ -3,7 +3,9 @@ set -e MASTODON_ROOT=/home/mastodon +APP_ROOT="$MASTODON_ROOT/live" +RBENV_ROOT="$MASTODON_ROOT/.rbenv" tootctl() { - ( cd "$MASTODON_ROOT/live" && sudo -u mastodon RAILS_ENV=production "$MASTODON_ROOT/.rbenv/shims/ruby" "$MASTODON_ROOT/live/bin/tootctl" "$@" ) + ( cd "$APP_ROOT" && sudo -u mastodon RAILS_ENV=production "$RBENV_ROOT/shims/ruby" "$APP_ROOT/bin/tootctl" "$@" ) } diff --git a/scripts/upgrade_glitch.sh b/scripts/upgrade.sh similarity index 51% rename from scripts/upgrade_glitch.sh rename to scripts/upgrade.sh index 278c362..09c88af 100755 --- a/scripts/upgrade_glitch.sh +++ b/scripts/upgrade.sh @@ -5,50 +5,53 @@ set -euo pipefail # default paths MASTODON_ROOT=/home/mastodon +APP_ROOT="$MASTODON_ROOT/live" RBENV_ROOT="$MASTODON_ROOT/.rbenv" # check for existing installation -if [ ! -d "$MASTODON_ROOT/live" ] -then - echo "$MASTODON_ROOT/live doesn't exist, are you sure Mastodon is installed?" +if [ ! -d "$APP_ROOT" ]; then + echo "$APP_ROOT doesn't exist, are you sure Mastodon is installed?" exit 255 fi # pull latest mastodon source -cd "$MASTODON_ROOT/live" +cd "$APP_ROOT" sudo -u mastodon git fetch --all -sudo -u mastodon git checkout glitch-soc/main -sudo -u mastodon git pull glitch-soc main +if [ -d "$APP_ROOT/app/javascript/flavours/glitch" ]; then + # glitch-soc (uses latest commits) + echo "Pulling latest glitch-soc commits..." + sudo -u mastodon git checkout glitch-soc/main +else + # vanilla (uses latest release) + echo "Pulling latest Mastodon release..." + sudo -u mastodon git checkout "$(sudo -u mastodon git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)" +fi # pull & apply latest patches -. $(dirname "$0")/apply_patches.sh +. "$(dirname "$0")/apply_patches.sh" -# update dependencies -echo "Updating deps..." -sudo -u mastodon "$RBENV_ROOT/shims/bundle" install --jobs "$(getconf _NPROCESSORS_ONLN)" -sudo -u mastodon yarn install --pure-lockfile --network-timeout 100000 +# set new ruby version +RUBY_VERSION="$(sudo -u mastodon cat $APP_ROOT/.ruby-version)" +sudo -u mastodon RUBY_CONFIGURE_OPTS=--with-jemalloc "$RBENV_ROOT/bin/rbenv" install "$RUBY_VERSION" || true +sudo -u mastodon "$RBENV_ROOT/bin/rbenv" global "$RUBY_VERSION" # run migrations: # https://docs.joinmastodon.org/admin/upgrading/ echo "Running pre-deploy database migrations..." sudo -u mastodon SKIP_POST_DEPLOYMENT_MIGRATIONS=true RAILS_ENV=production "$RBENV_ROOT/shims/bundle" exec rails db:migrate -echo "Compiling new assets..." -sudo -u mastodon RAILS_ENV=production "$RBENV_ROOT/shims/bundle" exec rails assets:precompile # restart mastodon echo "Restarting services (round 1/2)..." -sudo systemctl reload mastodon-web sudo systemctl restart mastodon-sidekiq mastodon-streaming # clear caches & run post-deployment db migration echo "Clearing cache..." -sudo -u mastodon RAILS_ENV=production "$RBENV_ROOT/shims/ruby" "$MASTODON_ROOT/live/bin/tootctl" cache clear +sudo -u mastodon RAILS_ENV=production "$RBENV_ROOT/shims/ruby" "$APP_ROOT/bin/tootctl" cache clear echo "Running post-deploy database migrations..." sudo -u mastodon RAILS_ENV=production "$RBENV_ROOT/shims/bundle" exec rails db:migrate # restart mastodon again echo "Restarting services (round 2/2)..." -sudo systemctl reload mastodon-web -sudo systemctl restart mastodon-sidekiq mastodon-streaming +sudo systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming echo "🎉 done!" diff --git a/scripts/version.sh b/scripts/version.sh index 552818e..1ecdaa6 100755 --- a/scripts/version.sh +++ b/scripts/version.sh @@ -2,6 +2,6 @@ set -e -. $(dirname "$0")/tootctl_shim.sh +. "$(dirname "$0")/tootctl_shim.sh" tootctl version diff --git a/scripts/weekly_cleanup.sh b/scripts/weekly_cleanup.sh index 0702a54..beb266e 100755 --- a/scripts/weekly_cleanup.sh +++ b/scripts/weekly_cleanup.sh @@ -6,7 +6,7 @@ set -e -. $(dirname "$0")/toot_shim.sh +. "$(dirname "$0")/toot_shim.sh" tootctl media remove --days 7 tootctl preview_cards remove --days 90