From e5211b74f4596fae54a84077694dd4f7c78e1966 Mon Sep 17 00:00:00 2001 From: Kumaran Rajendhiran Date: Mon, 2 Dec 2024 23:26:30 +0530 Subject: [PATCH] WIP: Expose services' ports --- .pre-commit-config.yaml | 3 +- hooks/post_gen_project.py | 1 + {{cookiecutter.project_slug}}/azure.yml | 20 ++++++++ .../scripts/deploy_to_azure.sh | 51 +++++++++++-------- 4 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/azure.yml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e56339f..bf8ae29 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,8 @@ repos: ^{{cookiecutter.project_slug}}/.github/workflows/test.yml| ^{{cookiecutter.project_slug}}/.github/workflows/deploy_to_fly_io.yml| ^{{cookiecutter.project_slug}}/.github/workflows/deploy_to_azure.yml| - ^{{cookiecutter.project_slug}}/.pre-commit-config.yaml + ^{{cookiecutter.project_slug}}/.pre-commit-config.yaml| + ^{{cookiecutter.project_slug}}/azure.yml ) - id: check-added-large-files diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index f84324f..09f8497 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -15,6 +15,7 @@ {% if cookiecutter.deployment != 'fly.io' %}".github/workflows/deploy_to_fly_io.yml",{% endif %} {% if cookiecutter.deployment != 'azure' %}"scripts/deploy_to_azure.sh",{% endif %} {% if cookiecutter.deployment != 'azure' %}".github/workflows/deploy_to_azure.yml",{% endif %} + {% if cookiecutter.deployment != 'azure' %}"azure.yml",{% endif %} ] for path in REMOVE_PATHS: diff --git a/{{cookiecutter.project_slug}}/azure.yml b/{{cookiecutter.project_slug}}/azure.yml new file mode 100644 index 0000000..caf1e26 --- /dev/null +++ b/{{cookiecutter.project_slug}}/azure.yml @@ -0,0 +1,20 @@ +properties: + configuration: + activeRevisionsMode: Single + ingress: + additionalPortMappings: + {% if "nats" in cookiecutter.app_type %} + - targetPort: 8000 + external: true + # exposedPort: 8000{%- endif %}{% if "fastapi" in cookiecutter.app_type and cookiecutter.app_type != "fastapi" %} + - targetPort: 8008 + external: true + # exposedPort: 8008{%- endif %} + allowInsecure: false + external: true + targetPort: {% if cookiecutter.app_type == 'fastapi' %}8008{% else %}8888{% endif %} + traffic: + - latestRevision: true + weight: 100 + transport: http +type: Microsoft.App/containerApps diff --git a/{{cookiecutter.project_slug}}/scripts/deploy_to_azure.sh b/{{cookiecutter.project_slug}}/scripts/deploy_to_azure.sh index 43244b4..edba32a 100755 --- a/{{cookiecutter.project_slug}}/scripts/deploy_to_azure.sh +++ b/{{cookiecutter.project_slug}}/scripts/deploy_to_azure.sh @@ -3,10 +3,11 @@ # Script to deploy to Azure Container Apps using Azure CLI # Variables -RESOURCE_GROUP="{{ cookiecutter.project_slug.replace("_", "-") }}-rg" -CONTAINER_APP_NAME="{{ cookiecutter.project_slug.replace("_", "-") }}" -LOCATION="westeurope" -ACR_NAME="{{ cookiecutter.project_slug.replace("_", "") }}-acr" +export RESOURCE_GROUP="{{ cookiecutter.project_slug.replace("_", "-") }}-rg" +export CONTAINER_APP_NAME="{{ cookiecutter.project_slug.replace("_", "-") }}" +export LOCATION="westeurope" +export ACR_NAME="{{ cookiecutter.project_slug.replace("_", "") }}-acr" +export VNET_NAME="{{ cookiecutter.project_slug.replace("_", "-") }}-vnet" echo -e "\033[0;32mChecking if already logged into Azure\033[0m" @@ -31,15 +32,33 @@ docker push $ACR_NAME.azurecr.io/${CONTAINER_APP_NAME}:latest echo -e "\033[0;32mChecking if container app environment exists\033[0m" if ! az containerapp env show --name "$CONTAINER_APP_NAME-env" --resource-group $RESOURCE_GROUP > /dev/null 2>&1; then + + echo -e "\033[0;32mCreating vnet for container app environment\033[0m" + az network vnet create --resource-group $RESOURCE_GROUP \ + --name $VNET_NAME --location $LOCATION --address-prefix 10.0.0.0/16 + az network vnet subnet create --resource-group $RESOURCE_GROUP \ + --vnet-name $VNET_NAME --name infrastructure-subnet \ + --address-prefixes 10.0.0.0/21 + az network vnet subnet update --resource-group $RESOURCE_GROUP \ + --vnet-name $VNET_NAME --name infrastructure-subnet \ + --delegations Microsoft.App/environments + + INFRASTRUCTURE_SUBNET=`az network vnet subnet show --resource-group ${RESOURCE_GROUP} --vnet-name $VNET_NAME --name infrastructure-subnet --query "id" -o tsv | tr -d '[:space:]'` + echo -e "\033[0;32mCreating container app environment\033[0m" az containerapp env create \ --name "$CONTAINER_APP_NAME-env" \ --resource-group $RESOURCE_GROUP \ - --location $LOCATION + --location $LOCATION \ + --infrastructure-subnet-resource-id $INFRASTRUCTURE_SUBNET else echo -e "\033[0;32mContainer app environment already exists\033[0m" fi +# echo -e "\033[0;32mUpdating azure.yml file\033[0m" +# export SUBSCRIPTION_ID=$(az account show --query id --output tsv) +# envsubst < azure.yml > azure.yml.tmp && mv azure.yml.tmp azure.yml + echo -e "\033[0;32mCreating container app\033[0m" az containerapp create \ --name $CONTAINER_APP_NAME \ @@ -55,22 +74,12 @@ az containerapp create \ --min-replicas 0 \ --max-replicas 2 \ --env-vars OPENAI_API_KEY=$OPENAI_API_KEY -{% if "nats" in cookiecutter.app_type %} -# echo -e "\033[0;32mUpdating nats port in container app\033[0m" -# az containerapp update \ -# --name $CONTAINER_APP_NAME \ -# --resource-group $RESOURCE_GROUP \ -# --add-ports 8000{%- endif %}{% if "fastapi" in cookiecutter.app_type %} -# echo -e "\033[0;32mUpdating fastapi port in container app\033[0m" -# az containerapp update \ -# --name $CONTAINER_APP_NAME \ -# --resource-group $RESOURCE_GROUP \ -# --add-ports 8008{%- endif %}{% if "mesop" in cookiecutter.app_type %} -# echo -e "\033[0;32mUpdating fastapi port in container app\033[0m" -# az containerapp update \ -# --name $CONTAINER_APP_NAME \ -# --resource-group $RESOURCE_GROUP \ -# --add-ports 8888{%- endif %} + +echo -e "\033[0;32mUpdating container app to expose all the service ports\033[0m" +az containerapp update \ + --name $CONTAINER_APP_NAME \ + --resource-group $RESOURCE_GROUP \ + --yaml azure.yml echo -e "\033[0;32mSetting up session affinity\033[0m" az containerapp ingress sticky-sessions set \