sed -i -e "s/<client_id>/$(identity_id)/g" $CONFIG_PATH
sed -i -e "s/<subscription_id>/$(subscription_id)/g" $CONFIG_PATH
sed -i -e "s/<managed_image_name>/$(managed_image_name)/g" $CONFIG_PATH
sed -i -e "s/<resource_group>/$(resource_group)/g" $CONFIG_PATH
sed -i -e "s/<network_security_group>/$(network_security_group)/g" $CONFIG_PATH
sed -i -e "s/<gallery_name>/$(gallery_name)/g" $CONFIG_PATH
sed -i -e "s/<image_name>/$(image_definition_name)/g" $CONFIG_PATH
sed -i -e "s/<image_version>/${VERSION}/g" $CONFIG_PATH
sed -i -e "s/<ip_address>/${IP_ADDRESS}/g" $CONFIG_PATH
cat $CONFIG_PATH
echo "##vso[task.logissue type=warning]During packer build, please avoid cancelling this task. Otherwise, created resources might need manual cleanup."
displayName:Prepare configuration
# Microsoft has a security group for VM created under their subscriptions, that,
# based on my observations (though I had no clearance to see it myself):
# 1. A low priority deny all that denies all unintended incoming traffic.
# 2. A medium-high priority denial for all traffic coming from small ports (lower than 8000 probably).
# 3. A high priority allowance for traffics from Microsoft-internal IPs.
#
# We can only insert new rules below medium. Therefore,
# 1. For Linux, we change the ssh port to 10022. This is done at provisioning by injecting user / custom data.
# 2. For Windows, they can't execute the user data script: https://stackoverflow.com/questions/62888359/custom-data-with-azure-windows-vm-run-powersell-script
# We can't use custom script extensions either because it's not supported in packer.
# We also can't use shell-local provisioner to invoke command, because when the VM is ready, packer always try to connect to WinRM.
# The workaround here is to use a monitor to detect the machine ready signal and change its WinRM port.
New-NetFirewallRule-Name'Custom-WinRM'-DisplayName'Custom WinRM Port Rule'-EnabledTrue-DirectionInbound-ProtocolTCP-ActionAllow-ProgramSystem-LocalPort15986