websoft9/systemd/script/send_credentials.sh

68 lines
2.4 KiB
Bash
Raw Normal View History

2023-09-19 15:40:14 +08:00
#!/bin/bash
2023-09-27 11:59:08 +08:00
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
2023-10-09 10:49:18 +08:00
2023-10-11 17:19:38 +08:00
deployment_username="admin"
2023-09-27 11:51:16 +08:00
credential_path="/var/websoft9/credential"
2023-10-11 17:19:38 +08:00
containers=("websoft9-git" "websoft9-deployment" "websoft9-proxy")
2023-10-12 14:31:06 +08:00
sections=("gitea" "portainer" "nginx_proxy_manager")
2023-10-12 08:57:12 +08:00
max_retries=20
2023-10-11 17:19:38 +08:00
2023-10-12 08:57:12 +08:00
declare -A usernames passwords
2023-09-21 17:49:14 +08:00
2023-10-12 08:57:12 +08:00
set +e # Ignore errors
2023-09-23 14:43:41 +08:00
2023-10-12 08:57:12 +08:00
for container in ${containers[@]}; do
echo "Processing $container"
success=false
counter=0
while [[ $success == false && $counter -lt $max_retries ]]; do
temp_file=$(mktemp)
echo "Attempt $((counter+1)) to copy $credential_path from $container to $temp_file"
if docker cp $container:$credential_path $temp_file; then
# 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
username=$(jq -r '.username' $temp_file)
password=$(jq -r '.password' $temp_file)
if [[ -n $username && -n $password ]]; then
usernames[$container]=$username
passwords[$container]=$password
success=true
fi
else
# If it is not JSON format, get the content and convert it to JSON
content=$(cat "$temp_file")
username="$deployment_username"
password="$content"
if [[ -n $username && -n $password ]]; then
usernames[$container]=$username
passwords[$container]=$password
success=true
fi
fi
fi
rm -f "$temp_file"
if [[ $success == false ]]; then
echo "Waiting for 3 seconds before next attempt..."
sleep 3
2023-09-21 17:49:14 +08:00
fi
2023-10-12 08:57:12 +08:00
((counter++))
done
if [[ $success == true ]]; then
echo "Successfully retrieved credentials for $container"
else
echo "Failed to retrieve credentials for $container after $max_retries attempts"
2023-09-21 17:49:14 +08:00
fi
2023-10-12 08:57:12 +08:00
done
2023-09-23 10:23:41 +08:00
2023-10-12 08:57:12 +08:00
set -e # Stop ignoring errors
2023-09-23 10:23:41 +08:00
2023-10-12 14:31:06 +08:00
length=${#containers[@]}
for ((i=0; i<$length; i++)); do
container=${containers[$i]}
section=${sections[$i]}
2023-10-12 08:57:12 +08:00
echo "$container:"
echo "Username: ${usernames[$container]}"
echo "Password: ${passwords[$container]}"
2023-10-12 14:31:06 +08:00
sudo docker exec -i websoft9-apphub apphub setconfig --section $section --key ${usernames[$container]} --value ${passwords[$container]}
2023-09-27 11:51:16 +08:00
done