-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcloud.sh
executable file
·108 lines (91 loc) · 4.9 KB
/
gcloud.sh
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# ╔════════════════════════════════════════════════════════════════════════════════╗
# ║ Version: 0.1 ║
# ║ Date of Version: 02.01.2025 ║
# ║ Owner: SD ║
# ║ Classification: Internal ║
# ║ Distribution: All Staff ║
# ╚════════════════════════════════════════════════════════════════════════════════╝
# ╔════════════════════════════════════════════════════════════════════════════════╗
# ║ Description: Containerized use of gcloud ║
# ╚════════════════════════════════════════════════════════════════════════════════╝
set -eu -o pipefail
set -o posix
# set -o errexit # abort on nonzero exitstatus
# set -o nounset # abort on unbound variable
# set -o pipefail # don't hide errors within pipes
script_path=$(realpath "${BASH_SOURCE[0]}")
script_path="${script_path%/*}"
cd "${script_path}"
{
function usage(){
printf "Usage:\n"
printf "\t%s <subcommand>\n\n" ${0}
printf "Where subcommand is the extension in .env.<subcommand>, which represents an isolated containerized gcloud command and contains the settings fot it.\n"
printf "You might want to see an example at the .env.template file in this folder.\n"
}
# Login to the container and save the credentials to volume and subfolder
function sandbox_login(){
printf "# Use this to authenticate the container and save credentials to volume and subfolder: ${MY_CONTAINER_NAME}\n" $MY_CFG
printf "docker run -it \\
\t-e CLOUDSDK_CONFIG=%s \\
\t-v %s:%s \\
\t--rm \\
\t--name %s \\
\t%s \\
\t/bin/bash -c \\
\t'gcloud config set project %s && gcloud container clusters get-credentials --region europe-west3 sd-a01'\n\n" $CLOUDSDK_CONFIG_MOUNT_POINT $CLOUDSDK_CONFIG $CLOUDSDK_CONFIG_MOUNT_POINT $MY_CONTAINER_NAME ${MY_GCLOUD_IMAGE} $MY_PROJ
}
# Create the container using function with the logged-in session and adc token
function sandbox_use(){
#printf "# using config:%s\n\n" $MY_CFG
printf "# Source the following function for your convenient use - it makes use of the same login and token from above:\n"
printf "function $MY_CFG_docker_function(){ \\
\tdocker run -it \\
\t-e CLOUDSDK_CONFIG=%s \\
\t-v %s:%s \\
\t--rm \\
\t${MY_GCLOUD_IMAGE} gcloud --project %s \$@ \\
}\n" $CLOUDSDK_CONFIG_MOUNT_POINT $CLOUDSDK_CONFIG $CLOUDSDK_CONFIG_MOUNT_POINT $MY_PROJ
printf "# Usage: %s compute instances list \n" $MY_CFG_docker_function
echo
printf "# This container re-uses the same kubectl config file your workstation uses, so that k9s works similarly:\n"
printf "function $MY_CFG_docker_function_k9s(){ \\
\tdocker run -it \\
\t-e CLOUDSDK_CONFIG=%s \\
\t-v %s:%s\\
\t-v %s/.kube/config:/root/.kube/config \\
\t--rm \\
\t%s \\
\tk9s \$@ \\
}\n" $CLOUDSDK_CONFIG_MOUNT_POINT $CLOUDSDK_CONFIG $CLOUDSDK_CONFIG_MOUNT_POINT $HOME ${MY_GCLOUD_IMAGE}
printf "# Usage: %s\n" $MY_CFG_docker_function_k9s
}
# Generate the docker command to login to the container and to use it
function gen(){
local sandbox=${1-template}
[ ! -f ./.env.${sandbox} ] && echo "File not found: .env.${sandbox}" && usage && exit 1
source ./.env.${sandbox}
sandbox_login
sandbox_use
}
function main(){
[ -z "${1-}" ] && usage && exit 1
sandbox=${1}
case ${sandbox} in
--usage|--help|-h)
usage
;;
*)
gen ${sandbox}
;;
esac
# echo "************************************************************************"
# docker container ls -a | head -1
# docker container ls -a | grep "gcloud-config-"
}
}
{
main "${@}"
exit 0
}