-
Notifications
You must be signed in to change notification settings - Fork 2
/
.bash_gcp
46 lines (41 loc) · 2.03 KB
/
.bash_gcp
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
#!/usr/bin/env bash
GCP_DOCKER_IMAGE=gcr.io/google.com/cloudsdktool/google-cloud-cli:467.0.0-alpine
OAUTH2L_DOCKER_IMAGE=gcr.io/oauth2l/oauth2l
exec_gcloud() {
COMMAND="$(which docker) run --rm -i -v $HOME/.config/gke:/root/.kube -v /tmp:/tmp \
-v $PWD:/app -w /app -v /tmp:/tmp -v \
$HOME/.config/gcloud:/root/.config/gcloud $GCP_DOCKER_IMAGE gcloud"
update_any_new_kubeconfigs_to_use_dockerized_gcloud() {
kubeconfig="$HOME/.config/gke/config"
if test -f "$kubeconfig" && grep -q "google-cloud-sdk" "$kubeconfig"
then
>&2 echo "===> Updating GKE Kubeconfig to use gcloud in Docker"
command_bin=$(gsed -E 's#^(\S+) .*#\1#' <<< "$COMMAND")
command_args=$(gsed -E 's#^\S+ (.*)#\1#' <<< "$COMMAND")
gsed -Ei "s#cmd-args: (.*)#cmd-args: $command_args \1#" "$kubeconfig"
gsed -Ei "s#cmd-path: .*#cmd-path: $command_bin#" "$kubeconfig"
fi
}
$COMMAND "$@"
update_any_new_kubeconfigs_to_use_dockerized_gcloud
}
gcp_ps1_hook_enabled() {
test -f "$HOME/.config/gcloud/active_config" &&
test -f "$HOME/.config/gcloud/credentials.db" &&
which sqlite3 &>/dev/null
}
gcp_ps1_hook() {
config_dir="$HOME/.config/gcloud"
active_config=$(cat "${config_dir}/active_config")
active_project="$(grep -hr 'project =' "${config_dir}/configurations/config_${active_config}" | sed -E 's/^project =//' | tr -d ' ')"
test -z "$active_project" && active_project="None"
active_user="$(sqlite3 -ascii "${config_dir}/credentials.db" 'SELECT * FROM credentials LIMIT 1' | head -1 | tr -d '{ ')"
printf "%s (project: %s)" "$active_user" "$active_project"
}
test -d "$HOME/.config/gke" || mkdir -p "$HOME/.config/gke"
alias gcloud=exec_gcloud
alias gsutil="docker run --rm -i -v /tmp:/tmp -v $PWD:/app -w /app -v /tmp:/tmp -v $HOME/.config/gcloud:/root/.config/gcloud $GCP_DOCKER_IMAGE gsutil"
alias oauth2l="docker run --rm -i -v /tmp:/tmp -v $PWD:/app -w /app $OAUTH2L_DOCKER_IMAGE"
alias gkubectl="kubectl --kubeconfig=$HOME/.config/gke/config"
complete -F _complete_alias gkubectl
complete -F _complete_alias gcloud