-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_appstore_sync_helper.sh
280 lines (229 loc) · 7.8 KB
/
local_appstore_sync_helper.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
# 1panel本地app的目录(如果不是默认安装,需修改该目录)
app_local_dir="/opt/1panel/resource/apps/local"
# AppStore的git仓库地址(必选)
# git_repo_url="https://github.com/xxxily/local-appstore-for-1Panel"
git_repo_url="https://github.com/okxlin/appstore"
# 访问git仓库的access token,访问私有仓库时用,优先级高于账密(可选)
# 建议使用access token,降低账密泄露的风险
git_access_token=""
# 访问git仓库的用户名,访问私有仓库时用(可选)
git_username=""
# 访问git仓库的密码,访问私有仓库时用(可选)
git_password=""
# 指定克隆的分支(可选)
git_branch=""
# 指定克隆的深度(可选)
git_depth=1
# 强制拉取最新代码(可防止因差异导致的拉取失败)
git_force_pull=true
# 拉取远程仓库前是否清空本地app目录(可选)
clean_local_app=false
# 拉取远程仓库前是否清空远程app缓存(可选)
clean_remote_app_cache=false
# 设置克隆或拉取远程仓库时使用的代理(可选)
proxyUrl=""
# 设置示例:
# proxyUrl="http://127.0.0.1:7890"
# proxyUrl="socks5://127.0.0.1:7890"
# proxyUrl="socks5://user:password@host:port"
# 将远程app store工程克隆到本地的工作目录
work_dir="/opt/1panel_hepler"
set -e
mkdir -p "$work_dir/logs"
log_file="$work_dir/logs/local_appstore_sync_helper_$(date +"%Y-%m-%d").log"
logs() {
local message="$1"
if [ -n "$log_file" ]; then
mkdir -p "$(dirname "$log_file")"
if [ $? -eq 0 ]; then
echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] $message"
echo "[$(date +"%Y-%m-%d %H:%M:%S")] $message" >>"$log_file"
return
fi
fi
echo -e "$message"
}
# 函数: url_encode
# 参数:
# - url: 需要进行编码的字符串
# 返回值:
# 经过URL编码后的字符串
function url_encode() {
local string=$1
local length="${#string}"
local url_encoded_string=""
local c
for ((i = 0; i < length; i++)); do
c=${string:i:1}
case "$c" in
[a-zA-Z0-9.~_-]) url_encoded_string+=$c ;;
*) url_encoded_string+=$(printf '%%%02X' "'$c") ;;
esac
done
echo "$url_encoded_string"
}
# 定义函数,接收一个URL参数和可选的替换字符串参数
replace_protocol() {
local url=$1
local replacement=$2
# 如果没有提供替换字符串,则删除"http://"或"https://"
if [[ -z $replacement ]]; then
local new_url=$(echo $url | sed "s/http:\/\///" | sed "s/https:\/\///")
else
local new_url=$(echo $url | sed "s/http:\/\//${replacement}/" | sed "s/https:\/\//${replacement}/")
fi
# 输出替换后的URL
echo $new_url
}
# 函数: clone_git_repo
# 参数:
# - url: Git仓库URL
# - username: 账号(可选)
# - password: 密码(可选)
# - access_token: 访问令牌(可选)
# - branch: 克隆分支(可选)
# - depth: 克隆深度(可选,默认为0,即克隆整个仓库)
function clone_git_repo() {
local url=$1
local username=$2
local password=$3
local access_token=$4
local branch=$5
local depth=$6
branch=${branch:+--branch $branch}
depth=${depth:+--depth $depth}
echo "branch: $branch, depth: $depth"
if [[ -n $access_token ]]; then
echo "use access_token to clone"
local fix_url=$(replace_protocol "$url")
git clone "https://oauth2:$access_token@$fix_url" $branch $depth
elif [[ -n $username && -n $password ]]; then
local encoded_username=$(url_encode "$username")
local encoded_password=$(url_encode "$password")
local fix_url=$(replace_protocol "$url")
# echo "use username and password to clone, encoded_username: $encoded_username, encoded_password: $encoded_password, fix_url: $fix_url"
echo "use username and password to clone"
git clone "https://$encoded_username:$encoded_password@$fix_url" $branch $depth
else
echo "use default clone"
git clone "$url" $branch $depth
fi
}
# 取消代理
function proxy_off() {
unset http_proxy
unset https_proxy
unset ALL_PROXY
unset no_proxy
logs "Proxy Off"
}
# 开启代理
function proxy_on() {
proxy_url="http://127.0.0.1:7890"
match_str="://"
if [ -n "$1" ]; then
if [[ $1 =~ $match_str ]]; then
proxy_url=$1
else
logs "Incorrect proxy_url, use defualt proxy_url"
return
fi
fi
export http_proxy=$proxy_url
export https_proxy=$proxy_url
export ALL_PROXY=$proxy_url
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
logs "Proxy On $proxy_url"
}
function scriptInfo() {
echo ""
logs "##################################################################"
logs "# Name: local appstore sync helper for 1Panel #"
logs "# Version: v1.0.0 #"
logs "# Author: xxxily #"
logs "# Github: https://github.com/xxxily/local-appstore-for-1Panel #"
logs "##################################################################"
echo ""
}
function main() {
scriptInfo
if [ ! -d "$app_local_dir" ]; then
logs "未检测到1panel的app目录,请检查1panel是否安装正确,或修改脚本中的app_local_dir变量"
exit 1
fi
# 检查地址结尾是否包含.git,如果不包含则自动补全
if [[ "$git_repo_url" != *".git" ]]; then
git_repo_url="${git_repo_url}.git"
fi
local repo_username=""
local repo_projectname=""
# 使用正则表达式匹配仓库地址中的用户名和项目名
if [[ $git_repo_url =~ .*\/(.*)\/(.*)\.git ]]; then
repo_username=${BASH_REMATCH[1]}
repo_projectname=${BASH_REMATCH[2]}
# logs "用户名: $repo_username"
# logs "项目名: $repo_projectname"
fi
if [ -z "$repo_username" ] || [ -z "$repo_projectname" ]; then
logs "无法提取用户名和项目名,请检查git_repo_url变量提供的地址是否正确"
exit 1
fi
mkdir -p "$work_dir/temp"
local repo_user_dir="$work_dir/temp/$repo_username"
local repo_dir="$repo_user_dir/$repo_projectname"
# 根据clean_remote_app_cache变量的值决定是否清空远程app的缓存数据
if [ "$clean_remote_app_cache" = true ] && [ -d "$repo_dir" ]; then
rm -rf "$repo_dir"
logs "已清空远程app的缓存数据"
fi
# 根据proxyUrl变量的值决定是否开启代理
if [ -n "$proxyUrl" ]; then
proxy_on "$proxyUrl"
fi
# clone或拉取远程仓库最新代码
logs "准备获取远程仓库最新代码:$git_repo_url"
if [ -d "$repo_dir" ]; then
logs "执行git pull操作"
cd "$repo_dir"
if [ "$git_force_pull" = true ]; then
# 强行拉取最新代码
git fetch --all 2>>"$log_file"
git reset --hard origin/$(git symbolic-ref --short -q HEAD) 2>>"$log_file"
logs "[git_force_pull][$(git symbolic-ref --short -q HEAD)] 已拉取最新代码"
else
git pull 2>>"$log_file"
logs "已拉取最新代码"
fi
else
logs "执行git clone操作"
mkdir -p "$repo_user_dir"
cd "$repo_user_dir"
clone_git_repo "$git_repo_url" "$git_username" "$git_password" "$git_access_token" "$git_branch" "$git_depth" 2>>"$log_file"
fi
logs "远程仓库最新代码获取完成"
if [ ! -d "$repo_dir/apps" ]; then
logs "未检测到apps目录,请检查远程仓库是否正确"
exit 1
fi
# 根据clean_local_app变量的值决定是否清空本地app目录
if [ "$clean_local_app" = true ]; then
rm -rf "$app_local_dir"/*
logs "已清空本地原有的app"
fi
# 将远程仓库的apps目录下的所有app复制到本地app_local_dir目录下
cd "$repo_dir"
cp -rf apps/* "$app_local_dir"
pwd
ls -lah
du -sh
# 根据clean_remote_app_cache变量的值决定是否清空远程app的缓存数据
if [ "$clean_remote_app_cache" = true ]; then
rm -rf "$repo_dir"
fi
if [ -n "$proxyUrl" ]; then
proxy_off
fi
logs "1panel本地app同步成功,enjoy it!"
}
main "$@"