DPZC-Hetzner/webservice-balancer/Azure/deploy.sh
2023-01-31 12:41:09 +01:00

215 lines
6.0 KiB
Bash
Executable File

#!/bin/bash
set -e
USERNAME="s478841"
RESOURCE_GROUP_NAME="webservice-$USERNAME"
VNET_NAME="webservice-vnet-$USERNAME"
SUBNET_NAME="webservice-subnet-$USERNAME"
LOADBALANCER_NAME="webservice-loadbalancer-$USERNAME"
LOAD_BALANCER_PUBLIC_IP="webservice-loadbalancer-publicip-$USERNAME"
LOCATION="uksouth"
CI_WEBSERVICE="#cloud-config
runcmd:
- cd /home/$USERNAME
- git clone https://git.wmi.amu.edu.pl/s478841/DPZC-Hetzner.git
- cd DPZC-Hetzner/webservice-balancer
- chmod +x ./webservice
- ./webservice
"
# Create resource group
echo "Creating RESOURCE GROUP..."
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
echo "Creating VNET..."
az network vnet create \
--resource-group $RESOURCE_GROUP_NAME \
--location $LOCATION \
--name $VNET_NAME \
--address-prefixes 10.1.0.0/16 \
--subnet-name $SUBNET_NAME \
--subnet-prefixes 10.1.0.0/24
echo "Creating PUBLIC IP..."
lbPublicIp=$(az network public-ip create \
--resource-group $RESOURCE_GROUP_NAME \
--name $LOAD_BALANCER_PUBLIC_IP \
--sku Standard)
lbPublicIpAddr=$(echo $lbPublicIp | jq .publicIp.ipAddress)
echo "Creating LOAD BALANCER..."
az network lb create \
--resource-group $RESOURCE_GROUP_NAME \
--name $LOADBALANCER_NAME \
--sku Standard \
--public-ip-address $LOAD_BALANCER_PUBLIC_IP \
--frontend-ip-name "$USERNAME"FrontEnd \
--backend-pool-name "$USERNAME"BackEndPool
echo "Creating LOAD BALANCER HEALTH PROBE..."
az network lb probe create \
--resource-group $RESOURCE_GROUP_NAME \
--lb-name $LOADBALANCER_NAME \
--name "$USERNAME"HealthProbe \
--protocol http \
--port 8080 \
--path /factors/10 \
--interval 300
echo "Creating LOAD BALANCER RULE..."
az network lb rule create \
--resource-group $RESOURCE_GROUP_NAME \
--lb-name $LOADBALANCER_NAME \
--name "$USERNAME"HTTPRule \
--protocol tcp \
--frontend-port 8080 \
--backend-port 8080 \
--frontend-ip-name "$USERNAME"FrontEnd \
--backend-pool-name "$USERNAME"BackEndPool \
--probe-name "$USERNAME"HealthProbe \
--idle-timeout 4
echo "Creating NETWORK SECURITY GROUP..."
az network nsg create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"NSG
echo "Creating NETWORK SECURITY GROUP RULES..."
az network nsg rule create \
--resource-group $RESOURCE_GROUP_NAME \
--nsg-name "$USERNAME"NSG \
--name "$USERNAME"NSGRuleHTTP \
--protocol '*' \
--direction inbound \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 80 \
--access allow \
--priority 200
az network nsg rule create \
--resource-group $RESOURCE_GROUP_NAME \
--nsg-name "$USERNAME"NSG \
--name "$USERNAME"NSGRuleHTTP \
--protocol '*' \
--direction inbound \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 8080 \
--access allow \
--priority 205
az network nsg rule create \
--resource-group $RESOURCE_GROUP_NAME \
--nsg-name "$USERNAME"NSG \
--name allowSSH \
--protocol '*' \
--direction inbound \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 22 \
--access allow \
--priority 210
array=("$USERNAME"NicVM1 "$USERNAME"NicVM2 "$USERNAME"NicVM3 "$USERNAME"NicVM4 "$USERNAME"NicVM5)
for vmnic in "${array[@]}"; do
echo "Creating NIC $vmnic..."
az network nic create \
--resource-group $RESOURCE_GROUP_NAME \
--name $vmnic \
--vnet-name $VNET_NAME \
--subnet $SUBNET_NAME \
--network-security-group "$USERNAME"NSG
done
echo "Creating SSH key..."
az sshkey create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"Key
echo "Creating VM1..."
az vm create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"VM1 \
--nics "$USERNAME"NicVM1 \
--image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \
--admin-username $USERNAME \
--size Standard_B1s \
--custom-data "$CI_WEBSERVICE" \
--ssh-key-name "$USERNAME"Key
echo " 2"
az vm create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"VM2 \
--nics "$USERNAME"NicVM2 \
--image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \
--admin-username $USERNAME \
--size Standard_B1s \
--custom-data "$CI_WEBSERVICE" \
--ssh-key-name "$USERNAME"Key
echo "Creating VM3..."
az vm create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"VM3 \
--nics "$USERNAME"NicVM3 \
--image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \
--admin-username $USERNAME \
--size Standard_B1s \
--custom-data "$CI_WEBSERVICE" \
--ssh-key-name "$USERNAME"Key
echo "Creating VM4..."
az vm create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"VM4 \
--nics "$USERNAME"NicVM4 \
--image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \
--admin-username $USERNAME \
--size Standard_B1s \
--custom-data "$CI_WEBSERVICE" \
--ssh-key-name "$USERNAME"Key
echo "Creating VM5..."
az vm create \
--resource-group $RESOURCE_GROUP_NAME \
--name "$USERNAME"VM5 \
--nics "$USERNAME"NicVM5 \
--image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest \
--admin-username $USERNAME \
--size Standard_DS1_v2 \
--custom-data "$CI_WEBSERVICE" \
--ssh-key-name "$USERNAME"Key
array=("$USERNAME"NicVM1 "$USERNAME"NicVM2 "$USERNAME"NicVM3 "$USERNAME"NicVM4 "$USERNAME"NicVM5)
for vmnic in "${array[@]}"; do
az network nic ip-config address-pool add \
--address-pool "$USERNAME"BackendPool \
--ip-config-name ipconfig1 \
--nic-name $vmnic \
--resource-group $RESOURCE_GROUP_NAME \
--lb-name $LOADBALANCER_NAME
done
echo "They probably need to get up... Let's give'm a few seconds..."
sleep 15
echo "Heya! I'm done! Here are some details:"
echo " LoadBalancer IP: $lbPublicIpAddr"
echo ""
echo "Have fun, Mate!"