-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add README.md and fix some problems (#20)
* feat: add README.md and fix some problems * fix: fix the spelling error * modify readme.md * add: doc/deploy.md * Update Makefile * Update deploy.md * Update README.md --------- Co-authored-by: ozlinex <[email protected]>
- Loading branch information
1 parent
74afdaf
commit 5a31317
Showing
11 changed files
with
255 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Deploy | ||
target 请通过`make help`来显示可用的服务列表,后续的target指代我们的服务,例如api | ||
## 本地部署 | ||
修改`./config/config.yaml`的配置,将数据库等配置的ip修改为localhost(如果没有请新增这个文件) | ||
### 启动环境 | ||
#### 清理本地环境(optional) | ||
```shell | ||
make clean-all | ||
``` | ||
#### 启动环境 | ||
```shell | ||
make env-up | ||
``` | ||
### 启动服务 | ||
#### 启动所有服务 | ||
> 可以使用"ctrl+b s"来切换终端 | ||
```shell | ||
make local | ||
``` | ||
#### 启动特定服务 | ||
```shell | ||
make target #e.g. make api | ||
``` | ||
使用make help获取更多信息 | ||
## 云服务部署 | ||
> 请保证已经使用docker login | ||
### 构建镜像 | ||
```shell | ||
make push-target | ||
``` | ||
### 云服务器端 | ||
|
||
#### 环境搭建 | ||
```shell | ||
docker compose up -d | ||
``` | ||
#### 部署服务 | ||
```shell | ||
sh image-refresh.sh target #更新镜像 | ||
sh docker-run.sh target #运行容器 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright 2024 The west2-online Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
#!/bin/bash | ||
|
||
# 这个脚本用于管理一组服务的Docker容器。 | ||
# 它允许使用单个Docker镜像启动特定服务或所有服务。 | ||
|
||
CONFIG_PATH="../config/config.yaml" # related to project folder | ||
|
||
get_port() { | ||
local server_name="$1" | ||
|
||
port=$(awk -v name="$server_name" ' | ||
$0 ~ "name: " name {found=1} | ||
found && $0 ~ "port:" {print $2; exit} | ||
$0 ~ "services:" {found=0} | ||
' "$CONFIG_PATH") | ||
|
||
|
||
echo "$port" | ||
} | ||
|
||
# Docker容器的镜像名称 | ||
IMAGE_NAME="registry.cn-hangzhou.aliyuncs.com/west2-online/fzuhelper-server" | ||
|
||
# 要启动的服务,默认为 "all" 如果没有提供参数 | ||
SERVICE_TO_START=${1:-all} | ||
|
||
# 脚本所在的目录 | ||
DIR=$(cd "$(dirname "$0")" && pwd) | ||
|
||
# 可用服务的列表。在真实场景中,这应该是服务名称的数组。 | ||
# 例如:SERVICES=("service1" "service2" "service3") | ||
SERVICES=("api classroom user") | ||
# 1. 编译镜像时将多个服务打包为单一镜像(当然不建议这么做,不过镜像小的且不需要频繁更新这么做很方便) | ||
# 2. 启动时根据SERVICE_TO_START在SERVICES中查找对应服务名 | ||
# 3. 如果查找的到,则启动容器,查找不到则抛出错误 | ||
|
||
# 删除Docker容器的函数 | ||
remove_container() { | ||
local container_name="$1" | ||
local container_status=$(docker inspect -f '{{.State.Status}}' "$container_name" 2>/dev/null) | ||
echo "remove container $container_name" | ||
|
||
if [ "$container_status" == "running" ]; then | ||
docker stop "$container_name" | ||
elif [ "$container_status" == "paused" ]; then | ||
docker unpause "$container_name" | ||
docker stop "$container_name" | ||
fi | ||
|
||
if [ -n "$container_status" ]; then | ||
docker rm "$container_name" | ||
fi | ||
} | ||
|
||
# 启动新Docker容器的函数 | ||
start_container() { | ||
local service_name="$1" | ||
local server_port=$(get_port "$service_name") | ||
local image="$IMAGE_NAME:$service_name" | ||
|
||
echo "port is $server_port" | ||
echo "start container $service_name" | ||
docker run -d --name $service_name \ | ||
--network fzu-helper \ | ||
-p $server_port:$server_port \ | ||
--restart always \ | ||
$image | ||
} | ||
|
||
# 停止当前运行的容器,匹配名称模式 | ||
containers_to_stop=$(docker ps -aq | grep -v "fzu-helper") | ||
if [ "$SERVICE_TO_START" == "all" ]; then | ||
for container_id in $containers_to_stop; do | ||
remove_container "$container_id" | ||
done | ||
else | ||
remove_container "$SERVICE_TO_START" | ||
fi | ||
|
||
# 启动新容器 | ||
if [ "$SERVICE_TO_START" == "all" ]; then | ||
for service in "${SERVICES[@]}"; do | ||
start_container "$service" | ||
done | ||
else | ||
start_container "$SERVICE_TO_START" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2024 The west2-online Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
#!/bin/bash | ||
# 使用脚本前请保证docker login | ||
|
||
# 镜像名称和标签,请根据实际情况进行替换 | ||
IMAGE_NAME="registry.cn-hangzhou.aliyuncs.com/west2-online/fzuhelper-server" | ||
IMAGE_TAG="$1" | ||
FULL_IMAGE_NAME="${IMAGE_NAME}:${IMAGE_TAG}" | ||
|
||
# 检查jq是否安装 | ||
if ! type jq > /dev/null; then | ||
echo "错误:需要jq工具来解析JSON。请安装jq。 e.g: yum install jq" | ||
exit 1 | ||
fi | ||
|
||
# 获取远程镜像的摘要信息 | ||
get_remote_image_digest() { | ||
docker manifest inspect "$FULL_IMAGE_NAME" 2>/dev/null | jq -r '.config.digest' | ||
} | ||
|
||
# 获取本地镜像的摘要信息 | ||
get_local_image_digest() { | ||
docker image inspect --format='{{.Id}}' "$FULL_IMAGE_NAME" 2>/dev/null | ||
} | ||
|
||
# 拉取最新的Docker镜像 | ||
pull_new_image() { | ||
echo "正在拉取最新镜像: $FULL_IMAGE_NAME..." | ||
if docker pull "$FULL_IMAGE_NAME"; then | ||
echo "镜像更新成功。" | ||
else | ||
echo "错误:无法拉取镜像。" | ||
exit 1 | ||
fi | ||
} | ||
|
||
# 主流程 | ||
main() { | ||
local remote_digest=$(get_remote_image_digest) | ||
if [ -z "$remote_digest" ]; then | ||
echo "错误:无法获取远程镜像的摘要信息。" | ||
exit 1 | ||
fi | ||
|
||
local local_digest=$(get_local_image_digest) | ||
if [ -z "$local_digest" ]; then | ||
echo "警告:未找到本地镜像 $FULL_IMAGE_NAME 或无法获取摘要。" | ||
echo "尝试直接拉取远程镜像..." | ||
pull_new_image | ||
exit 0 | ||
fi | ||
|
||
echo "本地镜像摘要: $local_digest" | ||
echo "远程镜像摘要: $remote_digest" | ||
|
||
if [ "$local_digest" != "$remote_digest" ]; then | ||
echo "本地镜像与远程镜像不一致,需要更新。" | ||
pull_new_image | ||
else | ||
echo "本地镜像已是最新。" | ||
fi | ||
} | ||
|
||
# 执行主流程 | ||
main | ||
|
File renamed without changes.
Oops, something went wrong.