install_app.sh

This commit is contained in:
qiaofeng1227 2023-12-08 17:19:29 +08:00
parent 4b68059cdc
commit 671e4972e7

View File

@ -96,26 +96,63 @@ while [[ $# -gt 0 ]]; do
esac esac
done done
check_appname() {
if [ -z "$appname" ]; then
echo "appname cannot be empty"
exit 1
fi
if [ ! -d "/tmp/library/apps/$appname" ]; then
echo "websoft9 can not support to install this app"
exit 1
fi
}
echo "Start to get ip from script" get_public_ip() {
get_ip_path=$(find / -name get_ip.sh 2>/dev/null) get_ip_path=$(find / -name get_ip.sh 2>/dev/null)
public_ip=$(bash "$get_ip_path") public_ip=$(bash "$get_ip_path")
ip_regex="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" echo "$public_ip"
}
get_domain_names() {
local domain_names="$1"
local public_ip="$2"
local ip_regex="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
if [ -z "$domain_names" ]; then if [ -z "$domain_names" ]; then
domain_names="$public_ip" domain_names="$public_ip"
proxy_enabled=false
elif [[ $domain_names =~ $ip_regex ]]; then elif [[ $domain_names =~ $ip_regex ]]; then
domain_names="$domain_names"
fi
echo "$domain_names"
}
get_proxy_enabled() {
local domain_names="$1"
local ip_regex="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
local proxy_enabled=true
if [ -z "$domain_names" ] || [[ $domain_names =~ $ip_regex ]]; then
proxy_enabled=false proxy_enabled=false
fi fi
echo "$proxy_enabled"
}
rm -rf /tmp/library && sudo docker cp websoft9-apphub:/websoft9/library /tmp get_settings() {
filename="/tmp/library/apps/${appname}/.env" local filename="$1"
settings=$(grep "^W9_.*_SET=" "$filename" | awk -F '=' '{print $1, $2}' | \ local settings=$(grep "^W9_.*_SET=" "$filename" | awk -F '=' '{print $1, $2}' | \
while read -r key value; do while read -r key value; do
jq -n --arg key "$key" --arg value "$value" '{($key): $value}' jq -n --arg key "$key" --arg value "$value" '{($key): $value}'
done | jq -s add | jq -c .) done | jq -s add | jq -c .)
echo "$settings"
}
echo "Start to install $appname" install_app(){
check_appname
public_ip=$(get_public_ip)
domain_names=$(get_domain_names "$domain_names" "$public_ip")
proxy_enabled=$(get_proxy_enabled "$domain_names")
rm -rf /tmp/library && sudo docker cp websoft9-apphub:/websoft9/library /tmp
filename="/tmp/library/apps/${appname}/.env"
settings=$(get_settings "${filename}")
api_url="localhost/api/apps/install" api_url="localhost/api/apps/install"
api_key=$(sudo docker exec -i websoft9-apphub apphub getconfig --section api_key --key key) api_key=$(sudo docker exec -i websoft9-apphub apphub getconfig --section api_key --key key)
request_param=$(jq -n \ request_param=$(jq -n \
@ -123,7 +160,7 @@ request_param=$(jq -n \
--arg dist "$dist" \ --arg dist "$dist" \
--arg version "$version" \ --arg version "$version" \
--arg app_id "$appid" \ --arg app_id "$appid" \
--argjson proxy_enabled "$proxy_enabled" \ --arg proxy_enabled "$proxy_enabled" \
--arg domain_names "$domain_names" \ --arg domain_names "$domain_names" \
--argjson settings "$settings" \ --argjson settings "$settings" \
'{ '{
@ -138,16 +175,30 @@ request_param=$(jq -n \
"settings": $settings "settings": $settings
}') }')
echo $request_param
response=$(curl -s -w "\n%{http_code}" -X POST "$api_url" \ response=$(curl -s -w "\n%{http_code}" -X POST "$api_url" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "x-api-key: $api_key" \ -H "x-api-key: $api_key" \
-d "$request_param") -d "$request_param")
echo "$response"
}
app_list(){
api_url="localhost/api/apps"
api_key=$(sudo docker exec -i websoft9-apphub apphub getconfig --section api_key --key key)
response=$(curl -s -w "\n%{http_code}" -X GET "$api_url" \
-H "Content-Type: application/json" \
-H "x-api-key: $api_key")
echo "$response"
}
echo "-----------------Start to install ${appname}---------------------"
response=$(install_app)
http_code=$(echo "$response" | tail -n1) http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | head -n -1) response_body=$(echo "$response" | head -n -1)
echo "HTTP Code: $http_code" echo "Install HTTP Code: $http_code"
echo "Response Body: $response_body" echo "Response Body: $response_body"
if [ "$http_code" -eq 200 ]; then if [ "$http_code" -eq 200 ]; then
@ -155,15 +206,17 @@ if [ "$http_code" -eq 200 ]; then
max_attempts=50 max_attempts=50
for (( i=1; i<=$max_attempts; i++ )) for (( i=1; i<=$max_attempts; i++ ))
do do
result=$(sudo docker ps -a | grep "$appid") result=$(app_list)
if [[ -n "$result" ]]; then result_body=$(echo "$result" | head -n -1)
echo "Found appid in docker processes." echo $result_body
docker ps -a | grep "$appid" if echo "$result_body" | jq -e --arg app_id "$appid" '.[] | select((.app_id | startswith($app_id)) and .status != 3)' >/dev/null; then
echo "$appname install success."
echo "---------check $appname status for docker---------"
sudo docker ps -a |grep $appid
break break
else else
echo "Appid not found, waiting for 5 seconds..." echo "App is installing, waiting for 5 seconds..."
sleep 5 sleep 5
# TODO max_attempts=50, 调用 /api/apps/{appid}
fi fi
done done
else else