-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud-shell.sh
executable file
·77 lines (51 loc) · 1.44 KB
/
cloud-shell.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
#!/bin/bash
# Fetch configuration
source cloud-shell.cfg
if [ $1 == "up" ]; then
# Check for an existing instance
if [ -e INSTANCE.cfg ]; then
echo "ERROR: Active instance found. (INSTANCE.cfg present)"
else
# Create new instance name
printf -v INSTANCEID '%x' $(printf '%.2s ' $((RANDOM%16))' '{00..8})
INSTANCE="cloud-shell-$INSTANCEID"
# Write to file
echo "INSTANCE=\"$INSTANCE\"" > INSTANCE.cfg
echo "INFO: Starting instance $INSTANCE"
gcloud beta compute instances create $INSTANCE \
--project $PROJECT_ID \
--zone $ZONE \
--machine-type $MACHINE_TYPE \
--boot-disk-size $BOOT_DISK_SIZE \
--image $IMAGE_NAME \
--scopes "cloud-platform" \
--service-account $SERVICE_ACCOUNT
fi
elif [ $1 == "ssh" ]; then
# If there is an active instance ssh to it
if [ -e INSTANCE.cfg ]; then
source INSTANCE.cfg
echo "INFO: SSH to $INSTANCE"
gcloud compute ssh $INSTANCE \
--project $PROJECT_ID \
-- -R 52698:localhost:52698
else
echo "ERROR: No instance found. (INSTANCE.cfg missing)"
fi
elif [ $1 == "down" ]; then
# If there is an active instance delete it
if [ -e INSTANCE.cfg ]; then
source INSTANCE.cfg
echo "INFO: Deleting instance $INSTANCE"
gcloud compute instances delete $INSTANCE \
--project $PROJECT_ID \
--quiet
if [ $? -eq 0 ]; then
rm INSTANCE.cfg
fi
else
echo "ERROR: No instance found. (INSTANCE.cfg missing)"
fi
else
echo "ERROR: Unrecogised command."
fi