websoft9/systemd/send_credentials.sh

64 lines
2.0 KiB
Bash
Raw Normal View History

2023-09-19 15:40:14 +08:00
#!/bin/bash
set -e
trap "sleep 1; continue" ERR
2023-09-22 10:16:52 +08:00
try_times=30
2023-09-21 18:06:35 +08:00
counter=1
2023-09-22 10:16:52 +08:00
2023-09-23 10:23:41 +08:00
copy_credential() {
source_container=$1
source_path=$2
destination_container=$3
destination_path=$4
2023-09-21 17:49:14 +08:00
2023-09-23 10:23:41 +08:00
if docker exec "$destination_container" [ -f "$destination_path" ]; then
printf "%s already exists\n" "$destination_path"
2023-09-21 17:49:14 +08:00
else
2023-09-23 10:23:41 +08:00
temp_file=$(mktemp)
docker cp "$source_container:$source_path" "$temp_file"
2023-09-21 17:49:14 +08:00
2023-09-23 10:23:41 +08:00
# 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\":\"$username\",\"password\":\"$content\"}"
echo "$json" > "$temp_file"
docker cp "$temp_file" "$destination_container:$destination_path"
fi
rm -f "$temp_file"
2023-09-21 17:49:14 +08:00
fi
2023-09-23 10:23:41 +08:00
}
2023-09-21 17:49:14 +08:00
2023-09-23 10:23:41 +08:00
while true; do
set +e
printf "Try to get credentials for %d times\n" "$counter"
copy_credential "websoft9-git" "/var/websoft9/credential" "websoft9-apphub" "/websoft9/credentials/credential_git"
copy_credential "websoft9-deployment" "/var/websoft9/credential" "websoft9-apphub" "/websoft9/credentials/credential_deployment"
copy_credential "websoft9-proxy" "/var/websoft9/credential" "websoft9-apphub" "/websoft9/credentials/credential_proxy"
if docker exec "websoft9-apphub" [ -f "/websoft9/credentials/credential_git" ] && \
docker exec "websoft9-apphub" [ -f "/websoft9/credentials/credential_deployment" ] && \
docker exec "websoft9-apphub" [ -f "/websoft9/credentials/credential_proxy" ]; then
2023-09-21 18:06:35 +08:00
break
2023-09-21 17:49:14 +08:00
else
2023-09-22 10:16:52 +08:00
if [ $counter -gt $try_times ]; then
2023-09-23 10:23:41 +08:00
printf "Systemd cannot get all credentials after executing %d times\n" "$try_times"
2023-09-21 18:06:35 +08:00
break
2023-09-21 17:49:14 +08:00
fi
fi
2023-09-23 10:23:41 +08:00
2023-09-20 14:44:46 +08:00
sleep 3
2023-09-22 10:16:52 +08:00
set -e
2023-09-23 10:23:41 +08:00
counter=$((counter + 1))
2023-09-21 18:06:35 +08:00
done
2023-09-23 10:23:41 +08:00
2023-09-21 18:06:35 +08:00
tail -f /dev/null