-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_ctrl.sh
executable file
·199 lines (177 loc) · 4.87 KB
/
dev_ctrl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
cd "$(cd "$(dirname "$0")" && pwd)"
project_name=$(basename "$(pwd)")
test .env && source .env
flyer_port=${flyer_port:-8080}
version=${fastflyer_version:-latest}
image=$project_name:dev
# 打印带时间前缀的消息
print_message() {
local level=$1
local message=$2
local color_code=''
local reset_color='\033[0m'
case $level in
INFO|info)
color_code='\033[32m' # 绿色
;;
WARN|warn)
color_code='\033[33m' # 黄色
;;
ERROR|error)
color_code='\033[31m' # 红色
;;
*)
color_code='\033[0m' # 默认色
;;
esac
echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] ${color_code}${message}${reset_color}"
}
# 更新镜像
update_images() {
print_message INFO "正在拉取镜像..."
docker pull ${image}
print_message INFO "镜像拉取完成"
}
# 查看日志
show_logs() {
count=${1:-100}
docker logs -f --tail $count "${project_name}"
}
# 不存在容器
is_not_exist() {
if ! docker ps -a | grep -wq "${project_name}"; then
print_message WARN "未找到开发容器:${project_name},请先运行 $0 start 命令启动" >&2
exit 1
fi
}
# 存在容器
is_exist() {
if docker ps -a | grep -wq "${project_name}"; then
print_message WARN "检测到已存在容器:${project_name},你可以执行 $0 reset | update 重建或更新容器" >&2
exit 1
fi
}
# 查看容器
show() {
is_not_exist
if [[ -z $1 ]];then
docker ps -a | grep -w "${project_name}"
else
docker inspect "${project_name}"
fi
}
# 更新开发容器插件
update() {
is_not_exist
print_message INFO "正在更新开发容器插件..."
docker exec -ti "${project_name}" bash -c "
pip3 install -r requirements.txt \
--index-url https://pypi.tuna.tsinghua.edu.cn/simple/
"
docker exec -ti "${project_name}" bash -c "
pip3 install --upgrade fastflyer fastkit \
--index-url https://pypi.tuna.tsinghua.edu.cn/simple/
"
# print_message INFO "正在更新本地 FastFlyer 插件,以便 IDE 读取最新代码 ..."
# pip3 uninstall -y fastflyer 2>&1
# pip3 install --upgrade fastflyer \
# --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ 2>&1
print_message INFO "更新完成!"
}
# 登录容器
login() {
is_not_exist
docker exec -ti "${project_name}" ${1:-bash} -c "echo -e '\033[32m已成功进入容器,现在你可以执行命令操作了...\033[0m';echo ; ${1:-bash}"
}
# 构建镜像
build() {
print_message INFO "正在构建开发镜像..."
docker build --pull --network host -f Dockerfile -t $image ./
print_message INFO "镜像构建完成"
}
# 启动开发容器
start() {
is_exist
if netstat -nutlp | grep ":${flyer_port} "; then
print_message WARN "检测到 ${flyer_port} 端口已被占用,你可以停止当前监听8080端口的进程或者代码根目录的 .env 文件中的端口配置参数" >&2
exit 1
fi
build
print_message INFO "正在启动开发容器..."
docker run -dti \
--name="${project_name}" \
--restart=always \
-p ${flyer_port}:${flyer_port} \
-v "$(pwd)":/fastflyer \
${image} ./start-reload.sh
print_message INFO "容器启动成功"
print_message INFO "开发环境启动成功!以下为执行日志,你可以按下 ctrl + c 退出日志查看"
show_logs "${project_name}"
}
# 重启开发容器
restart() {
is_not_exist
print_message WARN "正在重启开发容器..."
docker restart "${project_name}"
print_message INFO "容器重启成功!"
}
# 销毁开发容器
destroy() {
is_not_exist
print_message WARN "即将销毁开发容器:${project_name}..."
docker rm -f "${project_name}"
print_message WARN "容器销毁成功!"
}
# 重建开发容器
reset() {
destroy
start
}
# 打印帮助信息
print_help() {
echo
echo "欢迎使用 FastFlyer 开发环境辅助工具。"
echo
echo "用法: $0 <OPTION>"
echo "命令:"
echo " start 启动开发容器"
echo " show [D] 查看开发容器"
echo " login 进入开发容器"
echo " update 更新容器插件"
echo " restart 重启开发容器"
echo " reset 重建开发容器"
echo " destroy 销毁开发容器"
echo " log [COUNT] 查看容器日志"
echo
}
# 处理命令
case "$1" in
"update")
update
;;
"start")
start
;;
"show")
show $2
;;
"login")
login
;;
"restart")
restart
;;
"destroy")
destroy
;;
"reset")
reset
;;
"logs"|"log")
show_logs $2
;;
*)
print_help
;;
esac