Skip to content

Commit

Permalink
Run tre start/stop operations in parallel (#2394)
Browse files Browse the repository at this point in the history
* run tre start/stop operations in parallel

* update changelog

* update changelog
  • Loading branch information
tamirkamara authored Aug 3, 2022
1 parent ee23882 commit 9335e30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ FEATURES:
ENHANCEMENTS:

* Guacamole logs are sent to Application Insights ([#2376](https://github.com/microsoft/AzureTRE/pull/2376))
* `make tre-start/stop` run in parallel which saves ~5 minutes ([#2394](https://github.com/microsoft/AzureTRE/pull/2394))

BUG FIXES:

Expand Down
34 changes: 21 additions & 13 deletions devops/scripts/control_tre.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,45 @@ if [[ $(az group list --output json --query "[?name=='${core_rg_name}'] | length
fi

az config set extension.use_dynamic_install=yes_without_prompt
az extension add --name azure-firewall
az --version

if [[ "$1" == *"start"* ]]; then
if [[ $(az network firewall list --output json --query "[?resourceGroup=='${core_rg_name}'&&name=='${fw_name}'] | length(@)") != 0 ]]; then
CURRENT_PUBLIC_IP=$(az network firewall ip-config list -f "${fw_name}" -g "${core_rg_name}" --query "[0].publicIpAddress" -o tsv)
if [ -z "$CURRENT_PUBLIC_IP" ]; then
echo "Starting Firewall - creating ip-config"
az network firewall ip-config create -f "${fw_name}" -g "${core_rg_name}" -n "fw-ip-configuration" --public-ip-address "pip-${fw_name}" --vnet-name "vnet-$TRE_ID" > /dev/null
az network firewall ip-config create -f "${fw_name}" -g "${core_rg_name}" -n "fw-ip-configuration" --public-ip-address "pip-${fw_name}" --vnet-name "vnet-$TRE_ID" > /dev/null &
else
echo "Firewall ip-config already exists"
fi
fi

if [[ $(az network application-gateway list --output json --query "[?resourceGroup=='${core_rg_name}'&&name=='${agw_name}'&&operationalState=='Stopped'] | length(@)") != 0 ]]; then
echo "Starting Application Gateway"
az network application-gateway start -g "${core_rg_name}" -n "${agw_name}"
az network application-gateway start -g "${core_rg_name}" -n "${agw_name}" &
else
echo "Application Gateway already running"
fi

az mysql server list --resource-group "${core_rg_name}" --query "[?userVisibleState=='Stopped'].name" -o tsv |
while read -r mysql_name; do
echo "Starting MySQL ${mysql_name}"
az mysql server start --resource-group "${core_rg_name}" --name "${mysql_name}"
az mysql server start --resource-group "${core_rg_name}" --name "${mysql_name}" &
done

az vmss list --resource-group "${core_rg_name}" --query "[].name" -o tsv |
while read -r vmss_name; do
if [[ "$(az vmss list-instances --resource-group "${core_rg_name}" --name "${vmss_name}" --expand instanceView | \
jq 'select(.[].instanceView.statuses[].code=="PowerState/deallocated") | length')" -gt 0 ]]; then
echo "Starting VMSS ${vmss_name}"
az vmss start --resource-group "${core_rg_name}" --name "${vmss_name}"
az vmss start --resource-group "${core_rg_name}" --name "${vmss_name}" &
fi
done

az vm list -d --resource-group "${core_rg_name}" --query "[?powerState!='VM running'].name" -o tsv |
while read -r vm_name; do
echo "Starting VM ${vm_name}"
az vm start --resource-group "${core_rg_name}" --name "${vm_name}"
az vm start --resource-group "${core_rg_name}" --name "${vm_name}" &
done

# We don't start workspace VMs despite maybe stopping them because we don't know if they need to be on.
Expand All @@ -71,46 +70,55 @@ elif [[ "$1" == *"stop"* ]]; then

if [ -n "$IPCONFIG_NAME" ]; then
echo "Deleting Firewall ip-config: $IPCONFIG_NAME"
az network firewall ip-config delete -f "${fw_name}" -n "$IPCONFIG_NAME" -g "${core_rg_name}"
az network firewall ip-config delete -f "${fw_name}" -n "$IPCONFIG_NAME" -g "${core_rg_name}" &
else
echo "No Firewall ip-config found"
fi
fi

if [[ $(az network application-gateway list --output json --query "[?resourceGroup=='${core_rg_name}'&&name=='${agw_name}'&&operationalState=='Running'] | length(@)") != 0 ]]; then
echo "Stopping Application Gateway"
az network application-gateway stop -g "${core_rg_name}" -n "${agw_name}"
az network application-gateway stop -g "${core_rg_name}" -n "${agw_name}" &
else
echo "Application Gateway already stopped"
fi

az mysql server list --resource-group "${core_rg_name}" --query "[?userVisibleState=='Ready'].name" -o tsv |
while read -r mysql_name; do
echo "Stopping MySQL ${mysql_name}"
az mysql server stop --resource-group "${core_rg_name}" --name "${mysql_name}"
az mysql server stop --resource-group "${core_rg_name}" --name "${mysql_name}" &
done

az vmss list --resource-group "${core_rg_name}" --query "[].name" -o tsv |
while read -r vmss_name; do
echo "Deallocating VMSS ${vmss_name}"
az vmss deallocate --resource-group "${core_rg_name}" --name "${vmss_name}"
if [[ "$(az vmss list-instances --resource-group "${core_rg_name}" --name "${vmss_name}" --expand instanceView | \
jq 'select(.[].instanceView.statuses[].code=="PowerState/running") | length')" -gt 0 ]]; then
echo "Deallocating VMSS ${vmss_name}"
az vmss deallocate --resource-group "${core_rg_name}" --name "${vmss_name}" &
fi
done

az vm list -d --resource-group "${core_rg_name}" --query "[?powerState=='VM running'].name" -o tsv |
while read -r vm_name; do
echo "Deallocating VM ${vm_name}"
az vm deallocate --resource-group "${core_rg_name}" --name "${vm_name}"
az vm deallocate --resource-group "${core_rg_name}" --name "${vm_name}" &
done

# deallocating all VMs in workspaces
# RG is in uppercase here (which is odd). Checking both cases for future compatability.
az vm list --query "[?(starts_with(resourceGroup,'${core_rg_name}-ws') || starts_with(resourceGroup,'${core_rg_name^^}-WS')) && powerState=='VM running'][name, resourceGroup]" -o tsv |
while read -r vm_name rg_name; do
echo "Deallocating VM ${vm_name} in ${rg_name}"
az vm deallocate --resource-group "${rg_name}" --name "${vm_name}"
az vm deallocate --resource-group "${rg_name}" --name "${vm_name}" &
done
fi

# for some reason the vm/vmss commands aren't considered as 'jobs', but this will still work in most cases
# since firewall/appgw will take much longer to complete their change.
echo "Waiting for all jobs to finish..."
jobs
wait

# Report final FW status
FW_STATE="Stopped"
if [[ $(az network firewall list --output json --query "[?resourceGroup=='${core_rg_name}'&&name=='${fw_name}'] | length(@)") != 0 ]]; then
Expand Down

0 comments on commit 9335e30

Please sign in to comment.