This commit is contained in:
qiaofeng1227 2024-10-23 12:04:38 +08:00
parent e128a6ec20
commit 7ccbcf09ed
2 changed files with 49 additions and 86 deletions

View file

@ -3,5 +3,6 @@
"log-opts": { "log-opts": {
"max-size": "10m", "max-size": "10m",
"max-file": "5" "max-file": "5"
} },
"registry-mirrors": ["https://dockerhub.websoft9.com"]
} }

View file

@ -276,9 +276,7 @@ install_tools(){
fi fi
} }
download_source_and_checkimage() {
download_source() {
echo_prefix_source=$'\n[Download Source] - ' echo_prefix_source=$'\n[Download Source] - '
echo "$echo_prefix_source Download Websoft9 source code from $artifact_url/$source_zip" echo "$echo_prefix_source Download Websoft9 source code from $artifact_url/$source_zip"
@ -290,6 +288,48 @@ download_source() {
exit 1 exit 1
fi fi
## unzip and check image
rm -rf /tmp/$source_unzip
sudo unzip -o "$source_zip" -d /tmp > /dev/null
if [ $? -ne 0 ]; then
echo "Failed to unzip source package."
exit 1
fi
cd /tmp/$source_unzip/docker
docker-compose pull
if [ $? -ne 0 ]; then
echo "Can not pull images from docker hub, set mirrors...."
if [ -f "/etc/docker/daemon.json" ]; then
if grep -q "registry-mirrors" "/etc/docker/daemon.json"; then
mv /etc/docker/daemon.json /etc/docker/daemon.json.bak
cp daemon.json /etc/docker/daemon.json
else
rm -f /etc/docker/daemon.json
cp daemon.json /etc/docker/daemon.json
fi
else
cp daemon.json /etc/docker/daemon.json
fi
sudo systemctl daemon-reload
sudo systemctl restart docker
# pull image by new mirrors
docker compose pull
if [ $? -ne 0 ]; then
echo "image pull failed again, exit install"
exit 1
else
echo "image pull success by new mirrors"
fi
else
echo "image pull success"
fi
rm -rf /tmp/$source_unzip
sudo unzip -o "$source_zip" -d "$install_path" > /dev/null sudo unzip -o "$source_zip" -d "$install_path" > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Failed to unzip source package." echo "Failed to unzip source package."
@ -306,8 +346,6 @@ download_source() {
} }
check_ports() { check_ports() {
local ports=("$@") local ports=("$@")
@ -331,61 +369,8 @@ check_ports() {
echo "All ports are available" echo "All ports are available"
} }
merge_json_files() {
local target_path="/etc/docker/daemon.json"
python3 - <<EOF 2>/dev/null
import json
import urllib.request
import os
def merge_json_files(file1, file2):
print("Merge from local file... ")
with open(file1, 'r') as f1, open(file2, 'r') as f2:
data1 = json.load(f1)
data2 = json.load(f2)
merged_data = {**data1, **data2}
with open(file1, 'w') as f:
json.dump(merged_data, f, indent=4)
def download_and_merge(url, file_path):
print("Download daemon.json from url and merge... ")
with urllib.request.urlopen(url) as response:
data = json.loads(response.read().decode())
with open(file_path, 'r') as f:
local_data = json.load(f)
merged_data = {**local_data, **data}
with open(file_path, 'w') as f:
json.dump(merged_data, f, indent=4)
# Create target file if it does not exist
if not os.path.exists("${target_path}"):
os.makedirs(os.path.dirname("${target_path}"), exist_ok=True)
with open("${target_path}", 'w') as f:
json.dump({}, f)
if os.path.exists("${install_path}/docker/daemon.json"):
merge_json_files("${target_path}", "${install_path}/docker/daemon.json")
elif urllib.request.urlopen("${source_github_pages}/docker/daemon.json").getcode() == 200:
download_and_merge("${source_github_pages}/docker/daemon.json", "${target_path}")
else:
print("No target daemon.json file need to merged")
EOF
if [ $? -ne 0 ]; then
echo "merge daemon.json failed, but install continue running"
fi
}
set_docker(){ set_docker(){
echo "Set Docker for Websoft9 backend service..." echo "Set Docker for Websoft9 backend service..."
merge_json_files
if ! systemctl is-active --quiet firewalld; then if ! systemctl is-active --quiet firewalld; then
echo "firewalld is not running" echo "firewalld is not running"
else else
@ -445,30 +430,7 @@ install_backends() {
echo "No containers to delete." echo "No containers to delete."
fi fi
DOCKER_CONFIG_FILE="/etc/docker/daemon.json" sudo docker compose -p websoft9 -f $composefile up -d
MIRROR_ADDRESS=$mirrors
timeout 10s sudo docker compose -f $composefile pull
if [ $? -eq 0 ]; then
echo "Docker Compose pull succeeded"
else
echo "Can not pull images from docker hub, set mirrors..."
if [ ! -f "$DOCKER_CONFIG_FILE" ]; then
echo "{}" | sudo tee "$DOCKER_CONFIG_FILE" > /dev/null
fi
if command -v jq >/dev/null 2>&1; then
MIRROR_ARRAY=$(echo $MIRROR_ADDRESS | sed 's/,/","/g' | sed 's/^/["/' | sed 's/$/"]/')
#jq --arg mirrors "$MIRROR_ARRAY" '.["registry-mirrors"] = ($mirrors | fromjson)' "$DOCKER_CONFIG_FILE" > "$DOCKER_CONFIG_FILE.tmp" && sudo mv "$DOCKER_CONFIG_FILE.tmp" "$DOCKER_CONFIG_FILE"
jq ".\"registry-mirrors\" = $MIRROR_ARRAY" "$DOCKER_CONFIG_FILE" > "$DOCKER_CONFIG_FILE.tmp" && sudo mv "$DOCKER_CONFIG_FILE.tmp" "$DOCKER_CONFIG_FILE"
else
echo "jq not installed!"
exit 1
fi
sudo systemctl daemon-reload
sudo systemctl restart docker
fi
sudo docker compose -p websoft9 -f $composefile up -d --build
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Failed to start docker services." echo "Failed to start docker services."
exit 1 exit 1
@ -517,12 +479,10 @@ install_systemd() {
fi fi
} }
#--------------- main----------------------------------------- #--------------- main-----------------------------------------
log_path="$install_path/install.log" log_path="$install_path/install.log"
check_ports $http_port $https_port $port | tee -a $log_path check_ports $http_port $https_port $port | tee -a $log_path
install_tools | tee -a $log_path install_tools | tee -a $log_path
download_source | tee -a $log_path
bash $install_path/install/install_docker.sh | tee -a $log_path bash $install_path/install/install_docker.sh | tee -a $log_path
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -530,6 +490,8 @@ if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
download_source_and_checkimage | tee -a $log_path
install_backends | tee -a $log_path install_backends | tee -a $log_path
install_systemd | tee -a $log_path install_systemd | tee -a $log_path