From 5789fc2fae1268e26fc2c4fbb2a483b4d4a5db97 Mon Sep 17 00:00:00 2001 From: naskya Date: Sun, 12 Nov 2023 05:31:20 +0900 Subject: [PATCH] dev: support Docker in the upgrading script --- .dockerignore | 2 - Dockerfile | 14 +++---- README.md | 28 ++++++++++++- docker-compose.yml | 3 +- neko/update/docker.sh | 18 ++++++++ neko/update/native.sh | 66 +++++++++++++++++++++++++++++ neko/update/utils | 17 ++++++++ update.sh | 98 +++++++------------------------------------ 8 files changed, 151 insertions(+), 95 deletions(-) create mode 100644 neko/update/docker.sh create mode 100755 neko/update/native.sh create mode 100644 neko/update/utils diff --git a/.dockerignore b/.dockerignore index ac4ce35b..ad6ad169 100644 --- a/.dockerignore +++ b/.dockerignore @@ -40,7 +40,5 @@ packages/backend/assets/instance.css # dockerignore custom .git -!.git/HEAD -!.git/refs Dockerfile docker-compose.yml diff --git a/Dockerfile b/Dockerfile index ce27b7d2..b0d4de23 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,7 +32,7 @@ COPY packages/backend/native-utils/npm/linux-x64-musl/package.json packages/back COPY packages/backend/native-utils/npm/linux-arm64-musl/package.json packages/backend/native-utils/npm/linux-arm64-musl/package.json # Configure pnpm, and install dev mode dependencies for compilation -RUN corepack enable && corepack prepare pnpm@latest --activate && pnpm i +RUN corepack enable && corepack prepare pnpm@latest --activate && pnpm install # Copy in the rest of the native-utils rust files COPY packages/backend/native-utils packages/backend/native-utils/ @@ -42,15 +42,17 @@ RUN pnpm run --filter native-utils build # Copy in the rest of the files to compile COPY . ./ -# Create dummy .git/objects directory for `git rev-parse` -RUN mkdir .git/objects + # Write version info -RUN bash -c 'NEW_COMMIT=$(git rev-parse --short HEAD) && sed -i -r "s/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${NEW_COMMIT:0:7}\",/" package.json' +ARG COMMIT_HASH +ENV COMMIT_HASH=${COMMIT_HASH} +RUN sed -i -r "s/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${COMMIT_HASH}\",/" package.json + # Compile RUN env NODE_ENV=production sh -c "pnpm run --filter '!native-utils' build && pnpm run gulp" # Trim down the dependencies to only those for production -RUN pnpm i --prod +RUN pnpm install --prod ## Runtime container FROM node:20 @@ -60,8 +62,6 @@ WORKDIR /firefish RUN apt-get update && apt-get install -y libvips-dev zip unzip tini ffmpeg COPY . ./ -# Remove .git -RUN rm -rf .git COPY --from=build /firefish/packages/megalodon /firefish/packages/megalodon diff --git a/README.md b/README.md index d94d8f5c..633314ae 100644 --- a/README.md +++ b/README.md @@ -148,9 +148,33 @@ ## Docker ユーザー -ToDo ([#ndqEd](https://code.naskya.net/decks/4wJQ3/tickets/ndqEd)) +### インストール -- Docker image とアップデートスクリプトを提供することを考えています +ToDo ([#a3WPw](https://code.naskya.net/decks/4wJQ3/tickets/a3WPw)) + +- インストールスクリプトの提供を考えています +- 慣れている方向けの説明: [`docker.io/naskya/firefish`](https://hub.docker.com/r/naskya/firefish) を用いて Firefish をインストールし、以下のコマンドを用いて PGroonga の拡張機能を有効にしてください。 + + ```bash + docker-compose exec db psql --command="CREATE EXTENSION pgroonga;" --dbname=firefish + ``` + +### アップデート + +重要なお知らせがある場合にはアップデートスクリプトを通じてお伝えするので、必ず `update.sh` を用いてアップデートしてください。 + +1. サーバーのバックアップを取る +2. `update.sh` を実行し、表示される指示に従う + + ```bash + ./update.sh + ``` + +3. サーバーを起動して動作を確認する + + ```bash + docker-compose up --detach + ``` ## 非 Docker ユーザー diff --git a/docker-compose.yml b/docker-compose.yml index 70c6e8f5..5a9a88ec 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,7 @@ version: "3" services: web: - image: firefish - build: . + image: docker.io/naskya/firefish container_name: firefish_web restart: unless-stopped depends_on: diff --git a/neko/update/docker.sh b/neko/update/docker.sh new file mode 100644 index 00000000..7211b69f --- /dev/null +++ b/neko/update/docker.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -eu + +source neko/update/utils + +pull() { + say "Pulling the image..." + docker pull docker.io/naskya/firefish +} + +if ! pull; then + say "awawa, the image may not be compatible with your environment..." + say "Gonnya try building the image locally!" + COMMIT_HASH=$(git rev-parse --short HEAD) + docker build --tag docker.io/naskya/firefish --build-arg COMMIT="${COMMIT_HASH:0:7}" . +fi + +say "Done!" diff --git a/neko/update/native.sh b/neko/update/native.sh new file mode 100755 index 00000000..b02648c4 --- /dev/null +++ b/neko/update/native.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +set -eu + +source neko/update/utils + +# Confirm that the server is stopped +if [ $# != 1 ] || [ "$1" != "--skip-all-confirmations" ]; then + say "Did you stop your server?" + read -r -p "[Y/n] > " yn + case "${yn}" in + [Nn]|[Nn][Oo]) + say "You must stop your server first!" + exit 1 + ;; + *) + say "uwu~ erai erai!\n" + ;; + esac +fi + +# write version info +say "Writing version info to package.json..." + +COMMIT_HASH=$(git rev-parse --short HEAD) + +running "sed -i -r 's/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${COMMIT_HASH:0:7}\",/' package.json" +sed -i -r "s/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${COMMIT_HASH:0:7}\",/" package.json + +say "Done!\n" + +# install dependencies +say "Upgrading dependencies..." + +running "corepack prepare pnpm@latest --activate" +corepack prepare pnpm@latest --activate + +running "pnpm install" +pnpm install + +say "Done!\n" + +# build +say "Start building Firefish." +say "It takes some time! Why not brew a cup of cofe?" + +running "NODE_OPTIONS=\"--max_old_space_size=3072\" NODE_ENV=\"production\" pnpm run rebuild" +NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run rebuild + +say "Done! We're almost there.\n" + +# prevent migration errors +if [ ! -f packages/backend/native-utils/built/index.js ]; then + say "Something went wrong orz... Gonnya try fixing that." + running "cp neko/index.js packages/backend/native-utils/built/index.js" + cp neko/index.js packages/backend/native-utils/built/index.js +else + say "It's going well so far!\n" +fi + +# migrate +say "Database migration time!" + +running "NODE_OPTIONS=\"--max_old_space_size=3072\" NODE_ENV=\"production\" pnpm run migrate" +NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run migrate + +say "Done!\n" diff --git a/neko/update/utils b/neko/update/utils new file mode 100644 index 00000000..f6aa0def --- /dev/null +++ b/neko/update/utils @@ -0,0 +1,17 @@ +color() { + if [ -t 1 ]; then + tput setaf "${1:-7}" + fi +} + +say() { + color 5 # magenta + echo -e "( ^-^) < $1" + color +} + +running() { + color 3 # yellow + echo "[running] \$ $1" + color +} diff --git a/update.sh b/update.sh index e8bc4d0a..624b0777 100755 --- a/update.sh +++ b/update.sh @@ -1,45 +1,13 @@ #!/usr/bin/env bash set -eu -function color { - if [[ -t 1 ]]; then - tput setaf ${1:-7} - fi -} - -function say { - color 5 # magenta - echo -e "( ^-^) < $1" - color -} - -function running { - color 3 # yellow - echo "[running] \$ $1" - color -} +source neko/update/utils say "Start upgrading Firefish!" -# Confirm that the server is stopped -if [[ $# != 1 ]] || [[ $1 != "--skip-all-confirmations" ]]; then - say "Did you stop your server?" - read -r -p "[Y/n] > " yn - case "${yn}" in - [Nn]|[Nn][Oo]) - say "You must stop your server first!" - exit 1 - ;; - *) - say "uwu~ erai erai!\n" - ;; - esac -fi - # Pull changes ## git pull OLD_COMMIT=$(git rev-parse --short HEAD) -say "Currently we're on \`${OLD_COMMIT}\`" say "Pulling changes from the remote repo..." @@ -50,11 +18,11 @@ running "git pull --ff --no-edit --autostash --strategy-option theirs" git pull --ff --no-edit --autostash --strategy-option theirs NEW_COMMIT=$(git rev-parse --short HEAD) -say "Pulled successfully! Now we're on \`${NEW_COMMIT}\`\n" +say "Pulled successfully!\n" ## check if the update script itself is updated -say "Checking if the update script itself has been changed by this update..." -if [[ $(git diff ${OLD_COMMIT} ${NEW_COMMIT} update.sh) != "" ]]; then +say "Checking if the update script itself has been modified by this update..." +if [ $(git diff "${OLD_COMMIT}" "${NEW_COMMIT}" update.sh) != "" ]; then say "Oh meow, that seems to be the case." say "I'm sorry to bother you, but please run this script again!" exit 1 @@ -65,8 +33,8 @@ fi ## show messages for message in neko/messages/*; do file=$(basename -- "${message}") - if [[ ! -f "neko/flags/${file}" ]]; then - if [[ $# != 1 ]] || [[ $1 != "--skip-all-confirmations" ]]; then + if [ ! -f "neko/flags/${file}" ]; then + if [ $# != 1 ] || [ "$1" != "--skip-all-confirmations" ]; then say "There is an important notice!" cat "${message}" say "Continue? (Are you ready for upgrading?)" @@ -88,51 +56,17 @@ for message in neko/messages/*; do fi done -## write version info -say "Writing version info to package.json..." +say "Do you use Docker?" +read -r -p "[y/N] > " yn -running "sed -i -r 's/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${NEW_COMMIT:0:7}\",/' package.json" -sed -i -r "s/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${NEW_COMMIT:0:7}\",/" package.json - -say "Done!\n" - -# Upgrade -## install dependencies -say "Upgrading dependencies..." - -running "corepack prepare pnpm@latest --activate" -corepack prepare pnpm@latest --activate - -running "pnpm install" -pnpm install - -say "Done!\n" - -## build -say "Start building Firefish." -say "It takes some time! Why not brew a cup of cofe?" - -running "NODE_OPTIONS=\"--max_old_space_size=3072\" NODE_ENV=\"production\" pnpm run rebuild" -NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run rebuild - -say "Done! We're almost there.\n" - -## prevent migration errors -if [[ ! -f packages/backend/native-utils/built/index.js ]]; then - say "Something went wrong orz... Gonnya try fixing that." - running "cp neko/index.js packages/backend/native-utils/built/index.js" - cp neko/index.js packages/backend/native-utils/built/index.js -else - say "It's going well so far!\n" -fi - -## migrate -say "Database migration time!" - -running "NODE_OPTIONS=\"--max_old_space_size=3072\" NODE_ENV=\"production\" pnpm run migrate" -NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run migrate - -say "Done!\n" +case "${yn}" in + [Yy]|[Yy][Ee][Ss]) + ./neko/update/docker.sh $@ + ;; + *) + ./neko/update/native.sh $@ + ;; +esac # Done say "Enjoy your sabakan life~"