forked from leoleehit/sycgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
195 lines (168 loc) · 5.3 KB
/
install.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
#!/bin/bash
clear
CONTAINER_NAME="sycgram"
GITHUB_IMAGE_NAME="iwumingz/${CONTAINER_NAME}"
GITHUB_IMAGE_PATH="ghcr.io/${GITHUB_IMAGE_NAME}"
PROJECT_PATH="/opt/${CONTAINER_NAME}"
PROJECT_VERSION="v1.2.0"
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
pre_check() {
[[ $EUID -ne 0 ]] && echo -e "${red}错误: ${plain} 需要root权限\n" && exit 1
command -v git >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "正在安装Git..."
apt install git -y >/dev/null 2>&1
echo -e "${green}Git${plain} 安装成功"
fi
command -v curl >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "正在安装curl..."
apt install curl -y >/dev/null 2>&1
echo -e "${green}curl${plain} 安装成功"
fi
command -v docker >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "正在安装Docker..."
bash <(curl -fsL https://get.docker.com) >/dev/null 2>&1
echo -e "${green}Docker${plain} 安装成功"
fi
command -v tar >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo -e "正在安装tar..."
apt install tar -y >/dev/null 2>&1
echo -e "${green}tar${plain} 安装成功"
fi
}
delete_old_image_and_container(){
# 获取最新指令说明
# 远程file
echo -e "${red}警告!警告!警告!${plain}"
echo -e "${red}警告:通过脚本更新sycgram可能会导致您的command.yml被远程配置覆盖${plain}"
echo -e "${yellow}提示:如果您的command.yml相对不重要,您可以直接更新${plain}"
echo -e "${yellow}建议:通过sycgram指令更新${plain}"
read -p "是否继续,是则请输入yes:" opt
if [[ ${opt} != "yes" ]]; then
exit 0
fi
remote_file="https://raw.githubusercontent.com/iwumingz/sycgram/main/data/command.yml"
# 本地file
local_cmd_file="${PROJECT_PATH}/data/command.yml"
if [[ -f ${local_cmd_file} ]]; then
t=$(date "+%Y_%m_%d_%H_%M_%M")
mkdir -p "${PROJECT_PATH}/data/command" >/dev/null 2>&1
echo -e "${yello}正在备份${plain} >>> ${local_cmd_file}"
cp ${local_cmd_file} "${PROJECT_PATH}/data/command/command.yml.${t}"
fi
curl -fsL ${remote_file} > ${local_cmd_file}
echo -e "${yellow}正在删除旧版本容器...${plain}"
docker rm -f $(docker ps -a | grep ${CONTAINER_NAME} | awk '{print $1}')
echo -e "${yellow}正在删除旧版本镜像...${plain}"
docker image rm -f $(docker images | grep ${CONTAINER_NAME} | awk '{print $3}')
}
check_and_create_config(){
if [ ! -f ${PROJECT_PATH}/data/config.ini ]; then
mkdir -p "${PROJECT_PATH}/data" >/dev/null 2>&1
read -p "Please input your api_id:" api_id
read -p "Please input your api_hash:" api_hash
cat > ${PROJECT_PATH}/data/config.ini <<EOF
[pyrogram]
api_id=${api_id}
api_hash=${api_hash}
[plugins]
root=plugins
EOF
fi
}
stop_sycgram(){
res=$(docker stop $(docker ps -a | grep ${GITHUB_IMAGE_NAME} | awk '{print $1}'))
if [[ $res ]];then
echo -e "${yellow}已停止sycgram...${plain}"
else
echo -e "${red}无法停止sycgram...${plain}"
fi
}
restart_sycgram(){
res=$(docker restart $(docker ps -a | grep ${GITHUB_IMAGE_NAME} | awk '{print $1}'))
if [[ $res ]];then
echo -e "${yellow}已重启sycgram...${plain}"
else
echo -e "${red}无法重启sycgram...${plain}"
fi
}
view_docker_log(){
docker logs -f $(docker ps -a | grep ${GITHUB_IMAGE_NAME} | awk '{print $1}')
}
uninstall_sycgram(){
delete_old_image_and_container;
rm -rf ${project_path}
}
reinstall_sycgram(){
rm -rf ${PROJECT_PATH}
install_sycgram "-it"
}
install_sycgram(){
pre_check;
check_and_create_config;
delete_old_image_and_container;
echo -e "${yellow}正在拉取镜像...${plain}"
docker pull ${GITHUB_IMAGE_PATH}:latest
# 容器时区与宿主机同步,请自行修改宿主机时区
# 注意这里是docker的一些默认路径
# 如果你有过路径更改,请按需修改即可
echo -e "${yellow}正在启动容器...${plain}"
docker run $1 \
--name ${CONTAINER_NAME} \
--env TZ=$(cat /etc/timezone) \
--restart always \
--hostname ${CONTAINER_NAME} \
-v ${PROJECT_PATH}/data:/sycgram/data \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
${GITHUB_IMAGE_PATH}:latest
}
show_menu() {
echo -e "${green}Sycgram${plain} | ${green}管理脚本${plain} | ${red}${PROJECT_VERSION}${plain}"
echo -e " ${green}1.${plain} 安装"
echo -e " ${green}2.${plain} 更新"
echo -e " ${green}3.${plain} 停止"
echo -e " ${green}4.${plain} 重启"
echo -e " ${green}5.${plain} 查看日志"
echo -e " ${green}6.${plain} 重新安装"
echo -e " ${green}7.${plain} 卸载"
echo -e " ${green}0.${plain} 退出脚本"
read -ep "请输入选择 [0-7]: " option
case "${option}" in
0)
exit 0
;;
1)
install_sycgram "-it"
;;
2)
install_sycgram "-itd"
;;
3)
stop_sycgram
;;
4)
restart_sycgram
;;
5)
view_docker_log
;;
6)
reinstall_sycgram
;;
7)
uninstall_sycgram
;;
*)
echo -e "${yellow}已退出脚本...${plain}"
exit
;;
esac
}
show_menu;