Skip to content

Commit

Permalink
custom project and zone function
Browse files Browse the repository at this point in the history
  • Loading branch information
themobileprof committed Nov 28, 2023
1 parent d3ceec1 commit cf3242d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/a

1. [The GCP CLI setup script](../blob/develop/gcp_cli_setup): This script installs Google cloud CLI for managing Google Cloud resources. To use this script in Termux, copy and paste the below snippet of code into your terminal:
```
curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/gcp_cli_setup && chmod +x ./setup && ./setup && exit
curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/gcp_cli_setup && chmod +x ./setup && ./setup
```

1. [GCloud VM Setup](../blob/develop/gcloud_vm): This scripts helps you provision a free VM or customize a more robust machine for your use. It works well on Termux or on the Google cloud shell, just copy and paste the code below into your terminal:
```
curl -Lo vm https://raw.githubusercontent.com/themobileprof/scripts/develop/gcloud_vm && chmod +x ./vm && ./vm && exit
curl -Lo vm https://raw.githubusercontent.com/themobileprof/scripts/develop/gcloud_vm && chmod +x ./vm && ./vm
```

### AWS Cloud

1. [The AWS CLI setup script](../blob/develop/aws_cli_setup): This script installs AWS CLI for managing Amazon Web Services. To use this script in Termux, copy and paste the below snippet of code into your terminal:
```
curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/aws_cli_setup && chmod +x ./setup && ./setup && exit
curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/aws_cli_setup && chmod +x ./setup && ./setup
```

### Azure Cloud

1. [The Azure CLI setup script](../blob/develop/azure_cli_setup): This script installs Azure CLI for managing Microsoft Azure Cloud resources. To use this script in Termux, copy and paste the below snippet of code into your terminal:
```
curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/azure_cli_setup && chmod +x ./setup && ./setup && exit
curl -Lo setup https://raw.githubusercontent.com/themobileprof/scripts/develop/azure_cli_setup && chmod +x ./setup && ./setup
```


Expand Down
122 changes: 103 additions & 19 deletions gcloud_vm
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ main(){
fi

# Check if there is a default zone, if not, Google init
zone=$(gcloud config get-value compute/zone)
if [ -z "$zone" ]
then
# 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
fi
zone=$(gcloud config get-value compute/zone)
# zone=$(gcloud config get-value compute/zone)
# if [ -z "$zone" ]
# then
# # 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
# fi
# zone=$(gcloud config get-value compute/zone)

# Setup project
set_project

# Setup zone
zone_setup

# Name your machine with a unique name
echo ""
Expand All @@ -61,6 +67,83 @@ main(){
}


set_project(){
echo "What project would we be working on?"
projectList=$(gcloud projects list --format="value(NAME)")

projArray+=($projectList)

i=0
for project in $projectList
do
((i++))
echo "$i: $project"
done

while [ -z "$PROJNUM" ]
do
read -p "Please select (1-$i): " PROJNUM
if [[ -n ${PROJNUM//[0-9]/} ]]; then
unset PROJNUM
fi
done

PROJECT=${projArray[(($PROJNUM-1))]}
echo "You are to set $PROJECT as your default project, press <Enter> to continue"
read

gcloud config set project $PROJECT
}

zone_setup(){
############################################################
## DO NOT ASSUME THE ZONES AND REGIONS - GET IT FROM GCLOUD
############################################################
local zone=$(gcloud config get-value compute/zone)
if [ -z "$zone" ]
then
echo "We cannot detect a Zone in your configuration"
echo "What Region do you want to set your resources to?"
echo "1. US-West"
echo "2. US-Central"
echo "3. US-East"

while [ -z "$zoneNum" ]
do
read -p "Please select (1, 2, or 3): " zoneNum
if [[ -n ${zoneNum//[0-9]/} ]]; then
unset zoneNum
fi
done

local letters=(a b c)
local a=$((RANDOM%3+1))
local b=$((RANDOM%3))


if [ "${zoneNum,,}" = "2" ]; then
zoneValue="us-central${a}-${letters[$b]}"
regionValue="us-central${a}"
elif [ "${zoneNum,,}" = "3" ]; then
zoneValue="us-east${a}-${letters[$b]}"
regionValue="us-east${a}"
else
zoneValue="us-west${a}-${letters[$b]}"
regionValue="us-west${a}"
fi

echo "You are about to set your Zone to $zoneValue, press <Enter> to continue"
read

gcloud compute project-info add-metadata --metadata google-compute-default-region=$regionValue,google-compute-default-zone=$zoneValue

# Initialize Gcloud
echo "We recommend that you setup a default zone as part of the setup below"
gcloud init
fi
}


free_vm(){

# Get a US Zone except us-south
Expand Down Expand Up @@ -179,16 +262,17 @@ vm(){ # Expecting VMNAME and zone
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
# 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)
Expand Down

0 comments on commit cf3242d

Please sign in to comment.