websoft9/systemd/script/send_credentials.sh

96 lines
2.6 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-09-19 15:40:14 +08:00
set -e
trap "sleep 1; continue" ERR
2023-09-23 11:29:08 +08:00
try_times=100
2023-09-21 18:06:35 +08:00
counter=1
2023-10-09 10:49:18 +08:00
2023-09-23 11:14:00 +08:00
portainer_username="admin"
2023-09-27 11:51:16 +08:00
credential_path="/var/websoft9/credential"
apphub_container_name="websoft9-apphub"
2023-09-22 10:16:52 +08:00
2023-09-23 10:23:41 +08:00
copy_credential() {
2023-09-27 11:51:16 +08:00
# 设置参数的默认值
2023-10-09 10:49:18 +08:00
source_container=
2023-09-27 11:51:16 +08:00
source_path="/var/websoft9/credential"
destination_container="websoft9-apphub"
destination_path="/websoft9/credentials/credential_git"
# 获取参数值
while [[ $# -gt 0 ]]; do
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
2023-09-23 14:43:41 +08:00
2023-09-27 11:51:16 +08:00
echo "Your installation parameters are as follows: "
echo "--sc: $source_container"
echo "--sp: $source_path"
echo "--dc: $destination_container"
echo "--dp: $destination_path"
2023-09-21 17:49:14 +08:00
2023-09-23 14:43:41 +08:00
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"
2023-09-21 17:49:14 +08:00
else
2023-09-23 14:43:41 +08:00
# 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"
2023-09-21 17:49:14 +08:00
fi
2023-09-23 14:43:41 +08:00
rm -f "$temp_file"
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
2023-09-23 16:56:38 +08:00
2023-09-23 11:29:08 +08:00
sleep 3
2023-09-23 16:56:38 +08:00
set +e
echo "Try to get credentials for %d times\n" "$counter" >> /tmp/copy
2023-09-23 10:23:41 +08:00
2023-09-27 11:51:16 +08:00
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"
2023-09-23 10:23:41 +08:00
2023-09-23 16:56:38 +08:00
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
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-22 10:16:52 +08:00
set -e
2023-09-23 10:23:41 +08:00
counter=$((counter + 1))
2023-09-27 11:51:16 +08:00
done