-
Notifications
You must be signed in to change notification settings - Fork 4
/
gcloud_vm
executable file
·220 lines (173 loc) · 5.95 KB
/
gcloud_vm
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# Setup a VM on Google Cloud using our guided system
# Constants ########
readonly WAITLONG=3
readonly WAITSMALL=1
####################
# Main function
main(){
# Install Applications
echo ">>> This walk-through will help you easily setup a VM on Google Cloud"
echo ""
sleep $WAITLONG
echo "Requirement(s):"
echo "1) You must have attached billing to your project of choice. For more information, visit: https://cloud.google.com/billing/docs/how-to/create-billing-account#create-new-billing-account"
echo "Have you met the requirements above? Y/n"
read REQ
if [ "${REQ,,}" != "y" ]; then
echo "Aborting..."
exit 1
fi
if [ -d $HOME/.config/gcloud ]; then
CLOUDSDK_CONFIG=$HOME/.config/gcloud
else
mkdir $HOME/.config/gcloud
CLOUDSDK_CONFIG=$HOME/.config/gcloud
fi
# Check if there is a default zone, if not, Google init
zone=$(gcloud config get-value compute/zone)
while [ -z "$zone" ]
do
# Initialize Google Cloud
echo "We cannot detect a Zone in your configuration, so let us help you with configuration. Please remember to configure your zone as us-west, us-central, or us-east especially if you need the Google free VM"
read -p "Press <Enter> to proceed..."
gcloud init
zone=$(gcloud config get-value compute/zone)
done
# Name your machine with a unique name
echo ""
echo "Firstly, type the unique name you would like to call this machine, do not add any spaces when typing the name:"
read VMNAME
VMNAME=${VMNAME// /}$RANDOM
sleep $WAITLONG
# Choose machine
echo ""
echo "Secondly, what type of machine do you want?"
echo "1. The Free Instance VM"
echo "2. To setup a custom machine"
echo "Please indicate 1 or 2 below"
read TYPE
if [ "${TYPE,,}" = "1" ]; then
free_vm $VMNAME $zone
else
vm $VMNAME $zone
fi
}
free_vm(){
# Get a US Zone except us-south
usZones=$(gcloud compute zones list --filter="us- AND -us-south" --format="value(NAME)")
zonesArray+=($usZones)
zoneElem=$((RANDOM%${#zonesArray[@]}))
ZONE=${zonesArray[$zoneElem]}
# Use the default zone is it is the right zone
for usZone in $usZones
do
if [ "$usZone" == "$2" ]
then
ZONE=$2
break
fi
done
echo ""
echo "The machine you just selected is:"
gcloud compute machine-types describe e2-micro --zone=$ZONE
echo ""
echo "Below is the code to create your free VM:"
echo "gcloud compute instances create $1 --image-family=debian-10 --image-project=debian-cloud --machine-type=e2-micro --zone=$ZONE"
## Create a system for user to choose the debian version later
while [ -z "$FREEINSTANCE" ]
do
echo "Would you like to run it? Y/N"
read FREEINSTANCE
if [ "${FREEINSTANCE,,}" = "y" ]; then
gcloud compute instances create $1 --image-family=debian-10 --image-project=debian-cloud --machine-type=e2-micro --provisioning-model=STANDARD --boot-disk-size= --zone=$ZONE
echo "Free VM Created!"
elif [ "${FREEINSTANCE,,}" = "n" ]; then
echo "Free VM Creation aborted!"
else
unset FREEINSTANCE
fi
done
}
vm(){ # Expecting VMNAME and zone
echo ""
echo "To create a custom VM, kindly answer these few questions:"
# Select Operating System
echo "# Which Operating System do you want to use?"
echo "1. Debian (default)"
echo "2. Ubuntu"
echo "3. Rocky Linux"
read -p "Select (1-3): " OS
if [ "${OS,,}" = "2" ]; then
echo "You chose Ubuntu!"
OpSys="ubuntu"
elif [ "${OS,,}" = "3" ]; then
echo "You chose Rocky!"
OpSys="rocky"
else
echo "You chose Debian!"
OpSys="debian"
fi
# Select RAM
echo ""
echo "# How many GB RAM do you need for your Machine?"
echo "1GB"
echo "2GB"
echo "4GB"
echo "8GB"
echo "16GB"
echo "32GB"
echo "64GB"
echo "128GB"
while [ -z "$RAM" ]
do
read -p "Please input a number (GB): " RAM
if [[ -n ${RAM//[0-9]/} ]]; then
unset RAM
fi
done
# Get machines with selected RAM
ramMB=$(($RAM*1024))
machines=$(gcloud compute machine-types list --filter="zone:( $2 ) AND memoryMb:( $ramMB )" --format="value(NAME)")
machinesArray+=($machines)
echo ""
echo "Below are the machines that matches $RAM GB RAM. Note that the number at the end is the number of CPU."
i=0
for machine in $machines
do
((i++))
echo "$i: $machine"
done
while [ -z "$machineNum" ]
do
read -p "Please select a machine number: " machineNum
if [[ -n ${machineNum//[0-9]/} ]]; then
unset machineNum
fi
done
machineNum=$(($machineNum-1))
echo ""
echo "The machine you just selected is:"
# gcloud compute machine-types list --filter="zone:( $2 ) AND name=${machinesArray[$machineNum]}"
gcloud compute machine-types describe ${machinesArray[$machineNum]} --zone=$2
# Select Disk Space
# echo ""
# echo "# How many GB hard drive do you need?"
# while [ -z "$HDD" ]
# do
# read -p "Input a number (GB): " HDD
# if [[ -n ${HDD//[0-9]/} ]]; then
# unset HDD
# fi
# done
HDD=200 # Use this default for now
IMGFAM=$(gcloud compute images list --format="value(FAMILY)" --filter="$OpSys" --limit=1)
IMGPRO=$(gcloud compute images list --format="value(PROJECT)" --filter="$OpSys" --limit=1)
echo ""
echo "We about to deploy your machine with the following command"
echo "gcloud compute instances create $1 --image-family=$IMGFAM --image-project=$IMGPRO --machine-type=${machinesArray[$machineNum]} --boot-disk-size=$HDD"
read -p "Please press <Enter> to continue"
gcloud compute instances create $1 --image-family=$IMGFAM --image-project=$IMGPRO --machine-type=${machinesArray[$machineNum]} --boot-disk-size=$HDD
}
# Call Main function
main