This commit is contained in:
qiaofeng1227 2023-10-12 08:57:12 +08:00
parent 0e75c0bfa3
commit b55461b7e5

View File

@ -1,114 +1,65 @@
#!/bin/bash #!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
set -e
trap "sleep 1; continue" ERR
try_times=20
counter=1
deployment_username="admin" deployment_username="admin"
credential_path="/var/websoft9/credential" credential_path="/var/websoft9/credential"
containers=("websoft9-git" "websoft9-deployment" "websoft9-proxy") containers=("websoft9-git" "websoft9-deployment" "websoft9-proxy")
max_retries=20
declare -A usernames passwords
set +e # Ignore errors
for container in ${containers[@]}; do for container in ${containers[@]}; do
temp_file=$(mktemp) echo "Processing $container"
echo "Copying /var/websoft9/credential from $container to $temp_file" success=false
docker cp $container:$credential_path $temp_file counter=0
# Check if temp_file is JSON format while [[ $success == false && $counter -lt $max_retries ]]; do
if jq -e . >/dev/null 2>&1 <<< "$(cat "$temp_file")"; then temp_file=$(mktemp)
# If it is JSON format, use it directly echo "Attempt $((counter+1)) to copy $credential_path from $container to $temp_file"
username=$(jq -r '.username' $temp_file) if docker cp $container:$credential_path $temp_file; then
password=$(jq -r '.password' $temp_file) # Check if temp_file is JSON format
else if jq -e . >/dev/null 2>&1 <<< "$(cat "$temp_file")"; then
# If it is not JSON format, get the content and convert it to JSON # If it is JSON format, use it directly
content=$(cat "$temp_file") username=$(jq -r '.username' $temp_file)
username="$deployment_username" password=$(jq -r '.password' $temp_file)
password="$content" if [[ -n $username && -n $password ]]; then
fi usernames[$container]=$username
passwords[$container]=$password
rm -f "$temp_file" success=true
done fi
else
get_credential() { # If it is not JSON format, get the content and convert it to JSON
content=$(cat "$temp_file")
# 设置参数的默认值 username="$deployment_username"
source_container= password="$content"
source_path="/var/websoft9/credential" if [[ -n $username && -n $password ]]; then
destination_container="websoft9-apphub" usernames[$container]=$username
destination_path="/websoft9/credentials/credential_git" passwords[$container]=$password
success=true
# 获取参数值 fi
while [[ $# -gt 0 ]]; do fi
case $1 in
--sc)
source_container="$2"
shift 2
;;
--sp)
source_path="$2"
shift 2
;;
--dc)
destination_container="$2"
shift 2
;;
--dp)
destination_path="$2"
shift 2
;;
*)
shift
;;
esac
done
echo "Your installation parameters are as follows: "
echo "--sc: $source_container"
echo "--sp: $source_path"
echo "--dc: $destination_container"
echo "--dp: $destination_path"
temp_file=$(mktemp)
docker cp "$source_container:$source_path" "$temp_file"
# Check if temp_file is JSON format
if jq -e . >/dev/null 2>&1 <<< "$(cat "$temp_file")"; then
# If it is JSON format, use it directly
docker cp "$temp_file" "$destination_container:$destination_path"
else
# If it is not JSON format, get the content and convert it to JSON
content=$(cat "$temp_file")
json="{\"username\":\"$portainer_username\",\"password\":\"$content\"}"
echo "$json" > "$temp_file"
docker cp "$temp_file" "$destination_container:$destination_path"
fi
rm -f "$temp_file"
}
while true; do
sleep 3
set +e
echo "Try to get credentials for %d times\n" "$counter" >> /tmp/copy
copy_credential --sc "websoft9-git" --sp $credential_path --dc $apphub_container_name --dp "/websoft9/credentials/credential_git"
copy_credential --sc "websoft9-deployment" --sp $credential_path --dc $apphub_container_name --dp "/websoft9/credentials/credential_deployment"
copy_credential --sc "websoft9-proxy" --sp $credential_path --dc $apphub_container_name --dp "/websoft9/credentials/credential_proxy"
if docker exec "websoft9-apphub" [ -s "/websoft9/credentials/credential_git" ] && \
docker exec "websoft9-apphub" [ -s "/websoft9/credentials/credential_deployment" ] && \
docker exec "websoft9-apphub" [ -s "/websoft9/credentials/credential_proxy" ]; then
break
else
if [ $counter -gt $try_times ]; then
printf "Systemd cannot get all credentials after executing %d times\n" "$try_times"
break
fi fi
rm -f "$temp_file"
if [[ $success == false ]]; then
echo "Waiting for 3 seconds before next attempt..."
sleep 3
fi
((counter++))
done
if [[ $success == true ]]; then
echo "Successfully retrieved credentials for $container"
else
echo "Failed to retrieve credentials for $container after $max_retries attempts"
fi fi
done
set -e
set -e # Stop ignoring errors
counter=$((counter + 1))
# Set credentials to config.ini
for container in ${containers[@]}; do
echo "$container:"
echo "Username: ${usernames[$container]}"
echo "Password: ${passwords[$container]}"
docker exec -it websoft9-apphub apphub getconfig cockpit port
done done