Skip to content

Commit

Permalink
WIP: Expose services' ports
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Dec 2, 2024
1 parent 93a3bb1 commit e5211b7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions {{cookiecutter.project_slug}}/azure.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 30 additions & 21 deletions {{cookiecutter.project_slug}}/scripts/deploy_to_azure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 \
Expand All @@ -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 \
Expand Down

0 comments on commit e5211b7

Please sign in to comment.