Skip to content

Commit

Permalink
fix: remote
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Nov 1, 2024
1 parent 0d68884 commit f7ae9ff
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ deploy/*_data
deploy/docker-images
scripts/download-image.sh
deploy/preset.zip
node_modules
node_modules
bin
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ RUN apt-get update && apt-get install -y \
#^3
ARG IS_PROD
ARG FLAVOR
ARG BUILD_ON_HOST
ARG BUILD_ON_LOCAL_HOST
ARG BUILD_ON_REMOTE_HOST
ARG BUILD_ON_DOCKER
ARG PRESET_ENCRYPTED_URI
ARG PRESET_ENCRYPTED_PASSWORD
#!3

#3
Expand All @@ -24,6 +27,7 @@ RUN find scripts/ -type f \( -name "*.sh" -o -name "*.js" \) -exec sed -i 's/\r$
RUN ./scripts/download_scripts/download_data.sh $PRESET_ENCRYPTED_URI $PRESET_ENCRYPTED_PASSWORD

ENV IS_LOCAL_HOST=false
ENV IS_REMOTE_HOST=false
ENV IS_DOCKER=true

RUN --mount=type=cache,target=/root/.gradle \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.patched
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ARG PRESET_SCRIPT
ARG PRESET_ENCRYPTED_URI
ARG PRESET_ENCRYPTED_PASSWORD
#override
ENV FLAVOR=generated-${FLAVOR}
ENV FLAVOR="(${FLAVOR})"

WORKDIR /app

Expand Down
4 changes: 2 additions & 2 deletions deploy/Dockerfile.koyeb.patched
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ARG PRESET_SCRIPT
ARG PRESET_ENCRYPTED_URI
ARG PRESET_ENCRYPTED_PASSWORD
#override
ENV FLAVOR=generated-${FLAVOR}
ENV FLAVOR="(${FLAVOR})"

WORKDIR /app

Expand Down Expand Up @@ -175,7 +175,7 @@ ARG PRESET_SCRIPT
ARG PRESET_ENCRYPTED_URI
ARG PRESET_ENCRYPTED_PASSWORD
#override
ENV FLAVOR=generated-${FLAVOR}
ENV FLAVOR="(${FLAVOR})"

WORKDIR /app

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ kotlin.mpp.enableCInteropCommonization=true

kotlin.native.ignoreDisabledTargets=true

server.prod=false
buildkonfig.flavor=koyeb
server.prod=true
buildkonfig.flavor=mini

# If set to true, enables Gradle Worker API for Detekt tasks. `false` by default.
# See the doc for the Worker API at https://docs.gradle.org/8.1/userguide/worker_api.html
Expand Down
13 changes: 10 additions & 3 deletions scripts/build_scripts/build-server-on-condition.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
set -e

if [ -z "$IS_LOCAL_HOST" ] || [ -z "$IS_DOCKER" ] || [ -z "$BUILD_ON_LOCAL_HOST" ] || [ -z "$BUILD_ON_DOCKER" ]; then
echo "Error: IS_LOCAL_HOST/IS_DOCKER and BUILD_ON_LOCAL_HOST/BUILD_ON_DOCKER must be set."
if [ -z "$IS_LOCAL_HOST" ] || [ -z "$IS_DOCKER" ] || [ -z "$BUILD_ON_LOCAL_HOST" ] || [ -z "$BUILD_ON_DOCKER" ] || [ -z "$IS_REMOTE_HOST" ] || [ -z "$BUILD_ON_REMOTE_HOST" ] ; then
echo "Error: IS_LOCAL_HOST/IS_DOCKER/IS_REMOTE_HOST and BUILD_ON_LOCAL_HOST/BUILD_ON_DOCKER/BUILD_ON_REMOTE_HOST must be set."
exit 1
fi

Expand All @@ -20,8 +20,15 @@ elif [ "$BUILD_ON_DOCKER" = "true" ]; then
echo "Error: BUILD_ON_DOCKER is true but IS_DOCKER is not true. Cannot proceed with build."
exit 0
fi
elif [ "$BUILD_ON_REMOTE_HOST" = "true" ]; then
if [ "$IS_REMOTE_HOST" = "true" ]; then
echo "Both BUILD_ON_REMOTE_HOST and IS_REMOTE_HOST are true, proceeding with build..."
else
echo "Error: BUILD_ON_REMOTE_HOST is true but IS_REMOTE_HOST is not true. Cannot proceed with build."
exit 0
fi
else
echo "Skip build."
echo "Never build."
exit 0
fi

Expand Down
4 changes: 2 additions & 2 deletions scripts/build_scripts/build-service-on-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ done < $TEMP_FILE

./scripts/tool_scripts/save-env.sh

./scripts/tool_scripts/modify-flavor.sh "saved-$FLAVOR" true
./scripts/tool_scripts/modify-flavor.sh "[$FLAVOR]" true

sh gradlew composeApp:build

mkdir -p "build/outputs/apk/release"

mv composeApp/build/outputs/apk/release/*.apk "build/outputs/apk/release/$FLAVOR.apk"
mv composeApp/build/outputs/apk/release/*.apk "build/outputs/apk/release/[$FLAVOR].apk"
26 changes: 0 additions & 26 deletions scripts/service_scripts/build-mini-service.sh

This file was deleted.

37 changes: 37 additions & 0 deletions scripts/service_scripts/build-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -e

if [ -z "$IS_LOCAL_HOST" ] || [ -z "$IS_REMOTE_HOST" ]; then
echo "IS_LOCAL_HOST/IS_REMOTE_HOST must be set."
exit 1
fi

FLAVOR=$1

if [ -z "$FLAVOR" ]; then
echo "FLAVOR must be set"
exit 1
fi

PUSH_TO_REMOTE_URI=$2
REMOTE_CERT_FILE=$3
REMOTE_COMMAND=$4

while IFS= read -r line; do
# Ignore empty lines and comments
[[ -z "$line" || "$line" =~ ^# ]] && continue
IFS='=' read -r key value <<<"$line"
export "$key"="$value"
done <"$FLAVOR.env"

if [ -z "$PUSH_TO_REMOTE_URI" ] || [ -z "$REMOTE_CERT_FILE" ] || [ -z "$REMOTE_COMMAND" ]; then
IS_DOCKER=false \
./scripts/build_scripts/build-all-in-flavor.sh "$FLAVOR" true
"./scripts/service_scripts/start-$FLAVOR-compose.sh"
else
IS_DOCKER=false \
./scripts/build_scripts/build-all-in-flavor.sh "$FLAVOR" true
args=$(grep -v '^#' "$FLAVOR".env | grep -v '^$' | awk -F '=' '{print "--build-arg " $1 "=\"" $2 "\""}' ORS=' ')
./scripts/tool_scripts/exec-until-success.sh docker build "$args" -t "a-server:latest" .
./scripts/service_scripts/start-service-on-remote.sh "$PUSH_TO_REMOTE_URI" "$REMOTE_CERT_FILE" "$REMOTE_COMMAND $FLAVOR"
fi
6 changes: 3 additions & 3 deletions scripts/service_scripts/mock-koyeb-compose-build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash
set -e
./scripts/tool_scripts/patch-koyeb-env.sh
args=$(grep -v '^#' ./mini.env | grep -v '^$' | awk -F '=' '{print "--build-arg " $1 "=\"" $2 "\""}' ORS=' ')
args=$(grep -v '^#' ./koyeb.env | grep -v '^$' | awk -F '=' '{print "--build-arg " $1 "=\"" $2 "\""}' ORS=' ')
eval docker build "$args" \
-f deploy/Dockerfile.koyeb.patched \
-t local-koyeb .
-t local-koyeb-compose:latest .
docker run \
--privileged \
-d \
-p 8811:8811 \
-p 5432:5432 \
-p 8812:8812 \
local-koyeb
local-koyeb-compose:latest
4 changes: 2 additions & 2 deletions scripts/service_scripts/mock-koyeb-single-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ set -e
args=$(grep -v '^#' ./koyeb.env | grep -v '^$' | awk -F '=' '{print "--build-arg " $1 "=\"" $2 "\""}' ORS=' ')
eval docker build "$args" \
-f ./Dockerfile.patched \
-t local-koyeb .
-t local-koyeb:latest .
docker run \
--privileged \
-d \
-p 8811:8811 \
-p 5432:5432 \
-p 8812:8812 \
local-koyeb
local-koyeb:latest
6 changes: 6 additions & 0 deletions scripts/service_scripts/start-mini-compose.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

docker compose --env-file ../mini.env \
-f docker-compose.yml \
-f docker-compose.d.yml \
up -d --build
20 changes: 17 additions & 3 deletions scripts/service_scripts/start-service-on-remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ if [ -z "$PUSH_TO_REMOTE_URI" ] || [ -z "$REMOTE_CERT_FILE" ]; then
echo "PUSH_TO_REMOTE_URI and REMOTE_CERT_FILE must be set"
exit 1
fi
ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "mkdir -p a-server"

echo "put $FILE ./a-server/image.tar" | sftp -i "$REMOTE_CERT_FILE" "$PUSH_TO_REMOTE_URI"
# 定义要保存的文件名
FILE="build/a-server.tar"
IMAGE_NAME="a-server:latest"

ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "$REMOTE_COMMAND"
# 检查文件是否存在
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist. Saving Docker image..."
docker save -o "$FILE" "$IMAGE_NAME"
echo "Docker image saved to $FILE."
else
echo "$FILE already exists. Skipping docker save."
fi

./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "mkdir -p a-server"

./scripts/tool_scripts/exec-until-success.sh echo "put $FILE ./a-server/image.tar" | sftp -i "$REMOTE_CERT_FILE" -P 422 "$PUSH_TO_REMOTE_URI"

./scripts/tool_scripts/exec-until-success.sh ssh -i "$REMOTE_CERT_FILE" -p 422 "$PUSH_TO_REMOTE_URI" "$REMOTE_COMMAND"
2 changes: 1 addition & 1 deletion scripts/tool_scripts/patch-koyeb-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ done < "$env_filter_file"
replace_string_2+="EOF\n"

replace_string_3+="#override${newline}"
replace_string_3+="ENV FLAVOR=generated-\${FLAVOR}"
replace_string_3+="ENV FLAVOR=(\${FLAVOR})"

# 使用 sed 替换 dockerfile 模板中的 #1 和 #2
sed -e "s|#1|$replace_string_1|" -e "s|#2|$replace_string_2|" -e "s|#3|$replace_string_3|" "$dockerfile_template" > "$dockerfile_output"
Expand Down
2 changes: 1 addition & 1 deletion scripts/tool_scripts/save-env.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
export FLAVOR=saved-$FLAVOR
export FLAVOR="[$FLAVOR]"
OUTPUT_FILE="$FLAVOR.env"

# 定义 env-filter 文件名
Expand Down

0 comments on commit f7ae9ff

Please sign in to comment.