websoft9/systemd/script/send_credentials.sh

78 lines
3 KiB
Bash
Raw Permalink Normal View History

2023-09-19 07:40:14 +00:00
#!/bin/bash
2023-10-09 02:49:18 +00:00
2024-01-11 02:37:33 +00:00
# Websoft9 microservices containers(git,deployment,proxy) have credential info inside the container
2024-01-11 02:38:08 +00:00
# send_credentials.sh: copy credential from these container, and save it into apphub container by apphub cli, it will retry 20 times in every 3 seconds interval
2024-01-11 02:39:16 +00:00
# If the account information is not set successfully after 20 retries, it will not be set again
2024-01-11 02:37:01 +00:00
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
2023-10-11 09:19:38 +00:00
deployment_username="admin"
credentials=("/data/gitea/credential" "/data/credential" "/data/credential")
2023-10-11 09:19:38 +00:00
containers=("websoft9-git" "websoft9-deployment" "websoft9-proxy")
2023-10-12 06:31:06 +00:00
sections=("gitea" "portainer" "nginx_proxy_manager")
2023-10-12 00:57:12 +00:00
max_retries=20
2023-10-11 09:19:38 +00:00
2023-10-12 00:57:12 +00:00
declare -A usernames passwords
2023-09-21 09:49:14 +00:00
2023-10-12 00:57:12 +00:00
set +e # Ignore errors
2023-09-23 06:43:41 +00:00
for i in ${!containers[@]}; do
container=${containers[$i]}
credential_path=${credentials[$i]}
2023-10-12 00:57:12 +00:00
echo "Processing $container"
success=false
counter=0
while [[ $success == false && $counter -lt $max_retries ]]; do
temp_file=$(mktemp)
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 09:49:14 +00:00
fi
2023-10-12 00:57:12 +00: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 09:49:14 +00:00
fi
2023-10-12 00:57:12 +00:00
done
2023-09-23 02:23:41 +00:00
2023-10-12 00:57:12 +00:00
set -e # Stop ignoring errors
2023-09-23 02:23:41 +00:00
2023-10-12 06:31:06 +00:00
length=${#containers[@]}
for ((i=0; i<$length; i++)); do
2023-10-24 04:14:30 +00:00
2023-10-12 06:31:06 +00:00
container=${containers[$i]}
section=${sections[$i]}
2023-10-24 04:14:30 +00:00
if [[ -n ${passwords[$container]} ]]; then
echo "$container start to set password"
2024-03-12 08:13:37 +00:00
docker exec -i websoft9-apphub apphub setconfig --section $section --key user_name --value ${usernames[$container]}
2023-10-24 04:14:30 +00:00
docker exec -i websoft9-apphub apphub setconfig --section $section --key user_pwd --value ${passwords[$container]}
else
echo "Password for $container is not set or empty. Skipping..."
fi
2024-01-11 02:37:01 +00:00
done