#!/bin/bash

pip3 install paramiko
rm -f s5.py
wget http://az200.net/gcp/s5.py


billing_ID=`gcloud beta billing accounts list|grep -oP "ACCOUNT_ID: (\S+-\S+-\S+)"|grep -oE "\S+-\S+-\S+"`
Gmail=`gcloud auth list|grep -oP '\S+@\S+'`
#ACCOUNT_Name = `${Gmail/\.gmail\.com/}`

echo "$billing_ID"
echo "$Gmail"
echo ${Gmail%@*}

p=`gcloud projects list|grep "${Gmail%@*}"`
if [ "$p" = "" ] ; then
   p1=${Gmail%@*}"01"
   gcloud projects create $p1 --name=$p1
   gcloud beta billing projects link $p1 --billing-account $billing_ID
   gcloud services enable compute.googleapis.com
   p2=${Gmail%@*}"02"
   gcloud projects create $p2 --name=$p2
   gcloud beta billing projects link $p2 --billing-account $billing_ID
   gcloud services enable compute.googleapis.com
   p3=${Gmail%@*}"03"
   gcloud projects create $p3 --name=$p3
   gcloud beta billing projects link $p3 --billing-account $billing_ID
   gcloud services enable compute.googleapis.com
else
   echo "pass projects create"
fi

#gcloud config set project ${Gmail%@*}

for project in  $(gcloud projects list --format="value(projectId)")
do
    echo "ProjectId:  $project"
    gcloud config set project $project
    f=`gcloud compute firewall-rules list|grep "$project"`
    if [ "$f" = "" ] ; then
       gcloud compute firewall-rules create $project --allow=all --description=$project
    else
       echo "pass firewall-rules"
    fi

    #gcloud services enable cloudresourcemanager.googleapis.com  --project $project
    
    t01=`gcloud compute instances list|grep "hk-01"`
    if [ "$t01" = "" ] ; then
       gcloud compute instances create hk-01 --machine-type f1-micro --zone asia-east2-c --image-project=ubuntu-os-cloud --image-family=ubuntu-2004-lts \
	  --metadata=startup-script='''#!/bin/bash
		echo root:zCx5nZBeQIVS |sudo chpasswd root
		sudo sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config;
		sudo sed -i "s/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config;
		sudo service sshd restart'''
       sleep 10s
       gcloud compute instances describe hk-01 --zone=asia-east2-c|grep natIP|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|xargs python3 s5.py
    else
        echo "pass create hk-01"
    fi

    t02=`gcloud compute instances list|grep "hk-02"`
    if [ "$t02" = "" ] ; then
       gcloud compute instances create hk-02 --machine-type f1-micro --zone asia-east2-c --image-project=ubuntu-os-cloud --image-family=ubuntu-2004-lts \
	  --metadata=startup-script='''#!/bin/bash
		echo root:zCx5nZBeQIVS |sudo chpasswd root
		sudo sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config;
		sudo sed -i "s/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config;
		sudo service sshd restart'''
       sleep 10s
       gcloud compute instances describe hk-02 --zone=asia-east2-c|grep natIP|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|xargs python3 s5.py
    else
       echo "pass create hk-02"
    fi

    t03=`gcloud compute instances list|grep "hk-03"`
    if [ "$t03" = "" ] ; then
       gcloud compute instances create hk-03 --machine-type f1-micro --zone asia-east2-c --image-project=ubuntu-os-cloud --image-family=ubuntu-2004-lts \
	  --metadata=startup-script='''#!/bin/bash
		echo root:zCx5nZBeQIVS |sudo chpasswd root
		sudo sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config;
		sudo sed -i "s/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config;
		sudo service sshd restart'''
       sleep 10s
       gcloud compute instances describe hk-03 --zone=asia-east2-c|grep natIP|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|xargs python3 s5.py
    else
       echo "pass create hk-03"
    fi

    t04=`gcloud compute instances list|grep "hk-04"`
    if [ "$t04" = "" ] ; then
       gcloud compute instances create hk-04 --machine-type f1-micro --zone asia-east2-c --image-project=ubuntu-os-cloud --image-family=ubuntu-2004-lts \
	  --metadata=startup-script='''#!/bin/bash
		echo root:zCx5nZBeQIVS |sudo chpasswd root
		sudo sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config;
		sudo sed -i "s/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config;
		sudo service sshd restart'''
       sleep 10s
       gcloud compute instances describe hk-04 --zone=asia-east2-c|grep natIP|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|xargs python3 s5.py
    else
       echo "pass create hk-04"
    fi
done
