-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssh.sh
executable file
·73 lines (56 loc) · 1.64 KB
/
ssh.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
#!/usr/bin/env bash
set -eu
set -o pipefail
readonly PROGDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
function main() {
local vm_name
local gcp_project
local service_account_json
while [[ "${#}" != 0 ]]; do
case "${1}" in
--help|-h)
usage
exit 0
;;
--name|-n)
vm_name="${2}"
shift 2
;;
--service-account-json|-s)
service_account_json="${2}"
shift 2
;;
*)
usage
echo "unknown argument \"${1}\""
exit 1
esac
done
tfstate::download "${vm_name}" "${service_account_json}"
workstation::ssh "${vm_name}"
}
function usage() {
cat <<-USAGE
ssh.sh [OPTIONS]
OPTIONS
--help, -h prints the command usage
--name, -n <name of vm> specify the name of this workstation
--service-account-json, -s <path/to/service/account/json> path to gcp service account json to authenticate with
USAGE
}
function tfstate::download(){
local vm_name
local service_account_json
vm_name="${1}"
service_account_json="${2}"
gcloud auth activate-service-account --key-file="${service_account_json}"
gsutil cp "gs://cf-buildpacks-workstations/${vm_name}/default.tfstate" "/tmp/${vm_name}.tfstate"
}
function workstation::ssh() {
local vm_name
vm_name="${1}"
terraform output -state "/tmp/${vm_name}.tfstate" ssh_private_key > /tmp/key
chmod 600 /tmp/key
ssh -i /tmp/key ubuntu@"$(terraform output -state "/tmp/${vm_name}.tfstate" vm_ip)"
}
main "${@:-}"