From ce5f1f40acb1377ebb65bfe75d95482656d36bae Mon Sep 17 00:00:00 2001 From: Benoit Date: Thu, 10 Oct 2024 22:36:29 +0900 Subject: [PATCH] Init Mastodon --- cfg/jobs/mastodon.run | 59 ++++++++++++++++++++++++ run/mastodon/workspace/setup.sh | 28 +++++++++++ run/mastodon/workspace/setup_mastodon.sh | 18 ++++++++ 3 files changed, 105 insertions(+) create mode 100644 cfg/jobs/mastodon.run create mode 100644 run/mastodon/workspace/setup.sh create mode 100644 run/mastodon/workspace/setup_mastodon.sh diff --git a/cfg/jobs/mastodon.run b/cfg/jobs/mastodon.run new file mode 100644 index 0000000..f43e2c4 --- /dev/null +++ b/cfg/jobs/mastodon.run @@ -0,0 +1,59 @@ +#!/bin/env bash +# Upstream doc: https://docs.joinmastodon.org/admin/install/ +set -euxo pipefail + +IMAGE="ubuntu/24.04" +DIST="${IMAGE%%/*}" +VER="${IMAGE#*/}" +VER="${VER%/*}" +VER="${VER//./-}" +UPSTREAM_VER="4.3.0" +UPSTREAM_VER_DASH="${UPSTREAM_VER//./-}" +UPSTREAM_NAME="mastodon" +SERIAL="1benoitjpnet" +CNAME="$UPSTREAM_NAME-$UPSTREAM_VER_DASH-$SERIAL-$DIST-$VER" +ALIAS="$UPSTREAM_NAME-$UPSTREAM_VER-$SERIAL" + +cd $WORKSPACE +incus launch images:$IMAGE $CNAME --quiet + +# Wait for network +timeout 30 bash -c 'until incus exec $CNAME -- ping -c1 google.com &>/dev/null; do sleep 1; done' + +# Mastodon +incus exec $CNAME -- bash -c ' + apt update && \ + apt upgrade -y && \ + apt install -y --no-install-recommends curl wget gnupg apt-transport-https lsb-release ca-certificates +' +incus file push $WORKSPACE/{setup.sh,setup_mastodon.sh} $CNAME/tmp/ +incus exec $CNAME -- bash /tmp/setup.sh +incus exec $CNAME --user 1001 --env HOME=/home/mastodon -- bash /tmp/setup_mastodon.sh +incus file push $WORKSPACE/postgresql.conf $CNAME/etc/postgresql/17/main/postgresql.conf + +# Clean +incus exec $CNAME -- \ + rm -rf \ + /etc/machine-id \ + /var/cache/apt \ + /var/log/journal + +# Publish +incus stop $CNAME +incus config metadata show $CNAME > $ARCHIVE/metadata.yaml +# Get the current Unix timestamp +current_timestamp=$(date +%s) +# Calculate expiry date as current timestamp + 1 week (604800 seconds) +expiry_date=$(($current_timestamp + 604800)) +# Update metadata values +sed -i \ + -e "s/creation_date: .*/creation_date: $current_timestamp/" \ + -e "s/expiry_date: .*/expiry_date: $expiry_date/" \ + -e "s#description: .*#description: $UPSTREAM_NAME v$UPSTREAM_VER on $IMAGE release $SERIAL#" \ + -e "s/name: .*/name: $CNAME/" \ + -e "s/serial: .*/serial: $SERIAL/" \ + $ARCHIVE/metadata.yaml + +incus config metadata edit $CNAME < $ARCHIVE/metadata.yaml +incus publish $CNAME --alias $ALIAS --public --quiet +incus rm $CNAME diff --git a/run/mastodon/workspace/setup.sh b/run/mastodon/workspace/setup.sh new file mode 100644 index 0000000..ef72086 --- /dev/null +++ b/run/mastodon/workspace/setup.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -euox pipefail + +# System repositories +apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates +# Node.js +curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg +echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list +# PostgreSQL +wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc +echo "deb [signed-by=/usr/share/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/postgresql.list +# System packages +apt update +apt install -y \ + imagemagick ffmpeg libvips-tools libpq-dev libxml2-dev libxslt1-dev file git-core \ + g++ libprotobuf-dev protobuf-compiler pkg-config gcc autoconf \ + bison build-essential libssl-dev libyaml-dev libreadline6-dev \ + zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev \ + nginx nodejs redis-server redis-tools postgresql postgresql-contrib \ + certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev +# Yarn +corepack enable +# Creating the mastodon user +useradd -m -s /bin/bash mastodon +passwd -d mastodon +# Postgresql +su - postgres -c "psql -c 'CREATE USER mastodon CREATEDB;'" +exit 0 diff --git a/run/mastodon/workspace/setup_mastodon.sh b/run/mastodon/workspace/setup_mastodon.sh new file mode 100644 index 0000000..4bd9687 --- /dev/null +++ b/run/mastodon/workspace/setup_mastodon.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euox pipefail + +cd /home/mastodon +git clone https://github.com/mastodon/mastodon.git live && cd live +git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1) +git clone https://github.com/rbenv/rbenv.git ~/.rbenv +{ + echo 'export PATH="$HOME/.rbenv/bin:$PATH"' + echo 'eval "$(rbenv init -)"' +} | tee -a ~/.bashrc ~/.profile +source ~/.profile +git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build +RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install +bundle config deployment 'true' +bundle config without 'development test' +bundle install -j$(getconf _NPROCESSORS_ONLN) +yarn install --yes