├── LICENSE.md ├── README.md └── gssh /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019-2021 Carlos Alexandro Becker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gssh 2 | 3 | Helps to SSH into gcloud instances: 4 | 5 | - project fuzzy filter 6 | - instance name fuzzy filter 7 | - 1 day local project cache 8 | - 1 hour local instance cache 9 | 10 | #### install 11 | 12 | ```sh 13 | brew install caarlos0/tap/gssh 14 | ``` 15 | 16 | You'll also need the `gcloud` cli available and set up, you can do so with: 17 | 18 | ```sh 19 | brew cask install google-cloud-sdk 20 | gcloud auth login 21 | ``` 22 | 23 | #### usage 24 | 25 | **Basic usage:** 26 | 27 | ```sh 28 | gssh 29 | ``` 30 | 31 | **Pre-filter project:** 32 | 33 | ```sh 34 | gssh my-proj 35 | ``` 36 | 37 | **Pre-filter both project and instance:** 38 | 39 | ```sh 40 | gssh my-proj instance-xyz 41 | ``` 42 | 43 | **Flush caches:** 44 | 45 | ```sh 46 | gssh --flush 47 | ``` 48 | 49 | **Port-forward instance port 9200 to localhost:19200:** 50 | 51 | ```sh 52 | GSSH_FLAGS='-L 19200:localhost:9200' gssh my-proj elasticsearch 53 | ``` 54 | -------------------------------------------------------------------------------- /gssh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | test -n "$DEBUG" && set -x 3 | set -eo pipefail 4 | 5 | test $# -ge 1 && { test "$1" == "--help" || test "$1" == "-h"; } && { 6 | echo " 7 | gssh: quickly ssh into gcloud instances. 8 | 9 | Usage: 10 | 11 | Basic usage: 12 | gssh 13 | 14 | Pre-filter project: 15 | gssh my-proj 16 | 17 | Pre-filter both project and instance: 18 | gssh my-proj instance-xyz 19 | 20 | Flush caches: 21 | gssh --flush 22 | 23 | Port-forward instance port 9200 to localhost:19200: 24 | GSSH_FLAGS='-L 19200:localhost:9200' gssh my-proj elasticsearch 25 | 26 | " >&2 27 | exit 0 28 | } 29 | 30 | CACHE="$HOME/Library/Caches/gssh" 31 | test "$(uname)" = "Linux" && CACHE="$HOME/.cache/gssh" 32 | mkdir -p "$CACHE" 33 | 34 | test $# -ge 1 && test "$1" == "--flush" && { 35 | echo "flushing caches..." >&2 36 | rm -rf "$CACHE" 37 | exit 0 38 | } 39 | 40 | pcache="$CACHE/projects.json" 41 | test "$(date +'%j')" != "$(stat -f '%Sm' -t '%j' "$pcache" 2>/dev/null)" && { 42 | echo "updating projects cache..." >&2 43 | trap 'rm $pcache' SIGINT 44 | gcloud projects list --format json >"$pcache" 45 | } 46 | 47 | export qproj="" 48 | export qinst="" 49 | 50 | test $# -ge 1 && qproj="$1" 51 | test $# -ge 2 && qinst="$2" 52 | 53 | project="$(jq -r '.[].projectId' "$pcache" | fzf -q "$qproj" -1)" 54 | 55 | icache="$CACHE/instances-$project.json" 56 | # shellcheck disable=SC2046 57 | if [[ $(date -v-180M +'%j%H%M') > $(stat -f '%Sm' -t '%j%H%M' "$icache" 2>/dev/null) ]]; then 58 | echo "updating $project instance cache..." >&2 59 | trap 'rm $icache' SIGINT 60 | gcloud compute instances list --project "$project" --format json >"$icache" 61 | fi 62 | 63 | name="$(jq -r '.[] | .name' "$icache" | fzf -q "$qinst" -1 | awk '{print $1}')" 64 | 65 | echo "ssh-ing into $name..." >&2 66 | # shellcheck disable=SC2086 67 | gcloud beta compute ssh "$name" --tunnel-through-iap --project "$project" -- $GSSH_FLAGS 68 | --------------------------------------------------------------------------------