Debian Codegraph @

examples/disaster-recovery/failover.sh

raw
1 # Copyright 2022 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # [START cloud_sql_proxy_secret_manager_failover]
16 #!/bin/bash
17
18 SECRET_ID="my-secret-id" # TODO(developer): replace this value
19 REFRESH_INTERVAL=5
20 PORT=5432 # TODO(developer): change this port as needed
21
22 # Get the latest version of the secret and start the proxy
23 INSTANCE=$(gcloud secrets versions access "latest" --secret="$SECRET_ID")
24 cloud_sql_proxy -instances="$INSTANCE"=tcp:"$PORT" &
25 PID=$!
26
27 # Every 5s, get the latest version of the secret. If it's changed, restart the
28 # proxy with the new value.
29 while true; do
30 sleep $REFRESH_INTERVAL
31 NEW=$(gcloud secrets versions access "latest" --secret="$SECRET_ID")
32 if [ "$INSTANCE" != "$NEW" ]; then
33 INSTANCE=$NEW
34 kill $PID
35 wait $PID
36 cloud_sql_proxy -instances="$INSTANCE"=tcp:"$PORT" &
37 PID=$!
38 fi
39 done
40 # [END cloud_sql_proxy_secret_manager_failover]
41