-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate-gcloud-link-instance.sh
executable file
·71 lines (57 loc) · 1.58 KB
/
create-gcloud-link-instance.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
#!/bin/sh
# Default value for arguments
name=custom-node
zone=asia-southeast2-a
# Constants
FIREWALL_NAME=moonbeam-chainlink-service
INSTANCE_TAG=tag-chainlink-node
# Parsing arguments
## ----- Accepted arguments ------
## -n -> instance name
## -z -> instance zone to be created (see all availables zones with: `gcloud compute zones list`)
while getopts ":n:z" opt; do
case ${opt} in
n )
name=$OPTARG
;;
z )
zone=$OPTARG
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid Option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
echo "Creating gcloud instance: $name at zone $zone..."
# Create an instance
gcloud compute instances create "$name" \
--custom-cpu=2 \
--custom-memory=2 \
--zone=asia-southeast2-a \
--image-family=debian-10 \
--image-project=debian-cloud
# Adding tag to an instance to apply firewall rule
echo "Adding tag to $name instance..."
gcloud compute instances add-tags "$name" \
--zone=asia-southeast2-a \
--tags=$INSTANCE_TAG
# Create a firewall rule
echo "Creating firewall rules if needed..."
gcloud compute firewall-rules list | grep $FIREWALL_NAME &> /dev/null
if [ $? == 0 ]; then
echo "Firewall is already existed. Skipping firewall creation."
else
gcloud compute firewall-rules create $FIREWALL_NAME \
--allow=tcp:6688 \
--description="Allow incoming traffic on TCP port 6688" \
--target-tags=$INSTANCE_TAG
fi
# Wait for instance to be running
sleep 10s
# SSH to the instance
gcloud beta compute ssh "$name" --zone "$zone" 2> /dev/null