mirror of
https://github.com/Websoft9/websoft9.git
synced 2025-02-02 17:08:38 +08:00
install_app.sh
This commit is contained in:
parent
4b68059cdc
commit
671e4972e7
@ -96,58 +96,109 @@ 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"
|
||||||
if [ -z "$domain_names" ]; then
|
}
|
||||||
domain_names="$public_ip"
|
|
||||||
proxy_enabled=false
|
|
||||||
elif [[ $domain_names =~ $ip_regex ]]; then
|
|
||||||
proxy_enabled=false
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm -rf /tmp/library && sudo docker cp websoft9-apphub:/websoft9/library /tmp
|
get_domain_names() {
|
||||||
filename="/tmp/library/apps/${appname}/.env"
|
local domain_names="$1"
|
||||||
settings=$(grep "^W9_.*_SET=" "$filename" | awk -F '=' '{print $1, $2}' | \
|
local public_ip="$2"
|
||||||
while read -r key value; do
|
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]?)$"
|
||||||
jq -n --arg key "$key" --arg value "$value" '{($key): $value}'
|
if [ -z "$domain_names" ]; then
|
||||||
done | jq -s add | jq -c .)
|
domain_names="$public_ip"
|
||||||
|
elif [[ $domain_names =~ $ip_regex ]]; then
|
||||||
|
domain_names="$domain_names"
|
||||||
|
fi
|
||||||
|
echo "$domain_names"
|
||||||
|
}
|
||||||
|
|
||||||
echo "Start to install $appname"
|
get_proxy_enabled() {
|
||||||
api_url="localhost/api/apps/install"
|
local domain_names="$1"
|
||||||
api_key=$(sudo docker exec -i websoft9-apphub apphub getconfig --section api_key --key key)
|
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]?)$"
|
||||||
request_param=$(jq -n \
|
local proxy_enabled=true
|
||||||
--arg app_name "$appname" \
|
if [ -z "$domain_names" ] || [[ $domain_names =~ $ip_regex ]]; then
|
||||||
--arg dist "$dist" \
|
proxy_enabled=false
|
||||||
--arg version "$version" \
|
fi
|
||||||
--arg app_id "$appid" \
|
echo "$proxy_enabled"
|
||||||
--argjson proxy_enabled "$proxy_enabled" \
|
}
|
||||||
--arg domain_names "$domain_names" \
|
|
||||||
--argjson settings "$settings" \
|
|
||||||
'{
|
|
||||||
"app_name": $app_name,
|
|
||||||
"edition": {
|
|
||||||
"dist": $dist,
|
|
||||||
"version": $version
|
|
||||||
},
|
|
||||||
"app_id": $app_id,
|
|
||||||
"proxy_enabled": $proxy_enabled,
|
|
||||||
"domain_names": [$domain_names],
|
|
||||||
"settings": $settings
|
|
||||||
}')
|
|
||||||
|
|
||||||
echo $request_param
|
get_settings() {
|
||||||
response=$(curl -s -w "\n%{http_code}" -X POST "$api_url" \
|
local filename="$1"
|
||||||
-H "Content-Type: application/json" \
|
local settings=$(grep "^W9_.*_SET=" "$filename" | awk -F '=' '{print $1, $2}' | \
|
||||||
-H "x-api-key: $api_key" \
|
while read -r key value; do
|
||||||
-d "$request_param")
|
jq -n --arg key "$key" --arg value "$value" '{($key): $value}'
|
||||||
|
done | jq -s add | jq -c .)
|
||||||
|
echo "$settings"
|
||||||
|
}
|
||||||
|
|
||||||
|
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_key=$(sudo docker exec -i websoft9-apphub apphub getconfig --section api_key --key key)
|
||||||
|
request_param=$(jq -n \
|
||||||
|
--arg app_name "$appname" \
|
||||||
|
--arg dist "$dist" \
|
||||||
|
--arg version "$version" \
|
||||||
|
--arg app_id "$appid" \
|
||||||
|
--arg proxy_enabled "$proxy_enabled" \
|
||||||
|
--arg domain_names "$domain_names" \
|
||||||
|
--argjson settings "$settings" \
|
||||||
|
'{
|
||||||
|
"app_name": $app_name,
|
||||||
|
"edition": {
|
||||||
|
"dist": $dist,
|
||||||
|
"version": $version
|
||||||
|
},
|
||||||
|
"app_id": $app_id,
|
||||||
|
"proxy_enabled": $proxy_enabled,
|
||||||
|
"domain_names": [$domain_names],
|
||||||
|
"settings": $settings
|
||||||
|
}')
|
||||||
|
|
||||||
|
response=$(curl -s -w "\n%{http_code}" -X POST "$api_url" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "x-api-key: $api_key" \
|
||||||
|
-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
|
||||||
|
Loading…
Reference in New Issue
Block a user