002 SyncConfig.sh 2.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#!/bin/bash
set -e
IFS=$'\n\n'

declare appName=($(grep -oP '(?<=AppName>)[^<]+' "devops/app.props"))

echo "Begin creating ${appName}'s settings to the configmap of k8s..."

declare noPublishable=($(grep -oP '(?<=NoPublishable>)[^<]+' "/tmp/cicd.props"))
if [ "${noPublishable}" == "1" ] ;
then
    echo ""
    echo "Tips: No services need to be synced config."
else
	function create()
	{	
		if [ "$6" == "1" ]; 
		then
			declare createUrl="$3/api/v1/namespaces/$2/configmaps?pretty=true"
			echo "[environment: $1, namespace: $2]"

			function send()
			{
				set +e

				declare deleteUrl="$3/api/v1/namespaces/$2/configmaps/$5"

				curl -X DELETE $deleteUrl -k \
				--connect-timeout $2 --max-time $3 --retry $4 \
				-H 'Authorization: Bearer '${AccessToken}'' 

				set -e

				declare configInfo=$(cat $1 | jq tostring)

				curl -X POST $createUrl -k \
				--connect-timeout $2 --max-time $3 --retry $4 \
				-H 'Content-Type: application/json' \
				-H 'cache-control: no-cache' \
				-H 'Authorization: Bearer '${AccessToken}'' \
				-d '{
		  "kind": "ConfigMap",
		  "apiVersion": "v1",
		  "metadata": {
			"name": "'$5'",
			"namespace": "'${namespace}'"
		  },
		  "data": {
			"'$6'":'"$configInfo"'
		  }
		}'
			}

			declare maxTime=30 
			declare maxConnectTime=20
			declare retryCount=5
	
			send "$4/appsettings.json" $maxConnectTime $maxTime $retryCount "$5.appsettings.json" "appsettings.json"
			send "$4/appsettings.$1.json" $maxConnectTime $maxTime $retryCount "$(echo $5.appsettings.$1.json | tr 'A-Z' 'a-z')" "appsettings.$1.json"
		else
			echo ""
			echo "Tips: $5 will not be synced config!!!"
		fi 
	}

	declare major=($(grep -oP '(?<=VersionMajor>)[^<]+' "devops/version.props"))
	declare namespace=($(grep -oP '(?<=Namespace>)[^<]+' "devops/app.props"))
	declare namespaceOfK8s=$(echo "${namespace}-v${major}" | tr 'A-Z' 'a-z')
	declare k8sApiServer=($(grep -oP '(?<=K8sApiServer>)[^<]+' "devops/deploy.props"))

	declare services=$(ls -l src/services | awk 'NR>1')
	declare servicePrefix=""
	for service in ${services}
	do
	  servicePrefix=($(echo ${service} | awk '{print $9}')) 
	  declare isPublishable=($(grep -oP "(?<=${servicePrefix}Publishable>)[^<]+" "/tmp/cicd.props"))
	  Create ${Environment} ${namespaceOfK8s} ${k8sApiServer} "./src/${servicePrefix}.API" "${servicePrefix}" ${isPublishable}
	done
fi

echo ""
echo "End creating app settings to the configmap of k8s..."