1
0
Fork 1
mirror of https://example.com synced 2024-11-22 06:46:40 +09:00

dev: support Docker in the upgrading script

This commit is contained in:
naskya 2023-11-12 05:31:20 +09:00
parent 87d1b6a1e0
commit 5789fc2fae
Signed by: naskya
GPG key ID: 164DFF24E2D40139
8 changed files with 151 additions and 95 deletions

View file

@ -40,7 +40,5 @@ packages/backend/assets/instance.css
# dockerignore custom
.git
!.git/HEAD
!.git/refs
Dockerfile
docker-compose.yml

View file

@ -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

View file

@ -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 ユーザー

View file

@ -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:

18
neko/update/docker.sh Normal file
View file

@ -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!"

66
neko/update/native.sh Executable file
View file

@ -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"

17
neko/update/utils Normal file
View file

@ -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
}

View file

@ -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~"