Skip to content

Commit

Permalink
Add log analytics logs, add output messages, remove Django code from …
Browse files Browse the repository at this point in the history
…Flask app
  • Loading branch information
cephalin committed Oct 6, 2023
1 parent 793d59e commit 619d479
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dev container

This `.devcontainer` directory contains the configuration for a [dev container](https://docs.github.com/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers). It lets you open the repository in a [GitHub codespace](https://docs.github.com/codespaces/overview).

The dev container is configured to have the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview), so you can run `azd` commands directly.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a Python web app using the Flask framework and the Azure Database for Po

## Requirements

The [requirements.txt](./requirements.txt) has the following packages:
The [requirements.txt](./requirements.txt) has the following packages, all used by a typical data-driven Flask application:

| Package | Description |
| ------- | ----------- |
Expand All @@ -29,7 +29,7 @@ This project has Dev Container support, so you can open it in Github Codespaces

Steps for running the server:

1. (Optional) If you're unable to open the Dev Container, [create a Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate that.
1. (Optional) If you're unable to open the Dev Container, [create a Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate the virtual environment.

2. Install the requirements:

Expand Down
23 changes: 23 additions & 0 deletions azure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,26 @@ services:
project: .
language: py
host: appservice
hooks:
postprovision:
posix:
shell: sh
run: echo $'\n\nApp Service app has the following settings:\n' && echo "$WEB_APP_SETTINGS" | jq -r '.[]' | sed 's/\(.*\)/\t- \1/' && echo -e $"\nSee the settings in the portal:\033[1;36m $WEB_APP_CONFIG"
interactive: true
continueOnError: true
windows:
shell: pwsh
run: Write-Host "`n`nApp Service app has the following settings:`n" $WEB_APP_SETTINGS | ConvertFrom-Json | ForEach-Object { Write-Host "\t- $_" }
interactive: true
continueOnError: true
postdeploy:
posix:
shell: sh
run: echo -e $"\n\nOpen SSH session to App Service container at:\033[1;36m $WEB_APP_SSH\033[0m" && echo -e $"Stream App Service logs at:\033[1;36m $WEB_APP_LOG_STREAM"
interactive: true
continueOnError: true
windows:
shell: pwsh
run: Write-Host "`n`nOpen SSH session to App Service container at:`n" $WEB_APP_SSH; Write-Host "Stream App Service logs at:`n" $WEB_APP_LOG_STREAM
interactive: true
continueOnError: true
10 changes: 0 additions & 10 deletions azureproject/development.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

DATABASE_URI = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
dbuser=os.environ['DBUSER'],
dbpass=os.environ['DBPASS'],
dbhost=os.environ['DBHOST'],
dbname=os.environ['DBNAME']
)

TIME_ZONE = 'UTC'

STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
STATIC_URL = 'static/'

6 changes: 0 additions & 6 deletions azureproject/production.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import os

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY')

ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
CSRF_TRUSTED_ORIGINS = ['https://' + os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []

# Configure Postgres database based on connection string of the libpq Keyword/Value form
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
conn_str = os.environ['AZURE_POSTGRESQL_CONNECTIONSTRING']
Expand Down
6 changes: 5 additions & 1 deletion infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ param location string
param databasePassword string

@secure()
@description('Flask SECRET_KEY for securing signed data')
@description('Django SECRET_KEY for securing signed data')
param secretKey string

var resourceToken = toLower(uniqueString(subscription().id, name, location))
Expand Down Expand Up @@ -42,3 +42,7 @@ module resources 'resources.bicep' = {
output AZURE_LOCATION string = location
output APPLICATIONINSIGHTS_CONNECTION_STRING string = resources.outputs.APPLICATIONINSIGHTS_CONNECTION_STRING
output WEB_URI string = resources.outputs.WEB_URI
output WEB_APP_SETTINGS array = resources.outputs.WEB_APP_SETTINGS
output WEB_APP_LOG_STREAM string = resources.outputs.WEB_APP_LOG_STREAM
output WEB_APP_SSH string = resources.outputs.WEB_APP_SSH
output WEB_APP_CONFIG string = resources.outputs.WEB_APP_CONFIG
61 changes: 55 additions & 6 deletions infra/resources.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ resource web 'Microsoft.Web/sites@2022-03-01' = {
name: 'appsettings'
properties: {
SCM_DO_BUILD_DURING_DEPLOYMENT: 'true'
AZURE_POSTGRESQL_CONNECTIONSTRING: 'dbname=${flaskDatabase.name} host=${postgresServer.name}.postgres.database.azure.com port=5432 sslmode=require user=${postgresServer.properties.administratorLogin} password=${databasePassword}'
AZURE_POSTGRESQL_CONNECTIONSTRING: 'dbname=${pythonAppDatabase.name} host=${postgresServer.name}.postgres.database.azure.com port=5432 sslmode=require user=${postgresServer.properties.administratorLogin} password=${databasePassword}'
SECRET_KEY: secretKey
FLASK_DEBUG: 'False'
}
Expand Down Expand Up @@ -143,10 +143,50 @@ resource web 'Microsoft.Web/sites@2022-03-01' = {
}
}

dependsOn: [virtualNetwork]
dependsOn: [ virtualNetwork ]

}

resource webdiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
name: 'AllLogs'
scope: web
properties: {
workspaceId: logAnalyticsWorkspace.id
logs: [
{
category: 'AppServiceHTTPLogs'
enabled: true
}
{
category: 'AppServiceConsoleLogs'
enabled: true
}
{
category: 'AppServiceAppLogs'
enabled: true
}
{
category: 'AppServiceAuditLogs'
enabled: true
}
{
category: 'AppServiceIPSecAuditLogs'
enabled: true
}
{
category: 'AppServicePlatformLogs'
enabled: true
}
]
metrics: [
{
category: 'AllMetrics'
enabled: true
}
]
}
}

resource appServicePlan 'Microsoft.Web/serverfarms@2021-03-01' = {
name: '${prefix}-service-plan'
location: location
Expand Down Expand Up @@ -184,7 +224,6 @@ module applicationInsightsResources 'appinsights.bicep' = {
}
}


resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2022-01-20-preview' = {
location: location
tags: tags
Expand Down Expand Up @@ -224,11 +263,21 @@ resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2022-01-20-pr
]
}


resource flaskDatabase 'Microsoft.DBforPostgreSQL/flexibleServers/databases@2022-01-20-preview' = {
resource pythonAppDatabase 'Microsoft.DBforPostgreSQL/flexibleServers/databases@2022-01-20-preview' = {
parent: postgresServer
name: 'flask'
name: 'pythonapp'
}

output WEB_URI string = 'https://${web.properties.defaultHostName}'
output APPLICATIONINSIGHTS_CONNECTION_STRING string = applicationInsightsResources.outputs.APPLICATIONINSIGHTS_CONNECTION_STRING

resource webAppSettings 'Microsoft.Web/sites/config@2022-03-01' existing = {
name: web::appSettings.name
parent: web
}

var webAppSettingsKeys = map(items(webAppSettings.list().properties), setting => setting.key)
output WEB_APP_SETTINGS array = webAppSettingsKeys
output WEB_APP_LOG_STREAM string = format('https://portal.azure.com/#@/resource{0}/logStream', web.id)
output WEB_APP_SSH string = format('https://{0}.scm.azurewebsites.net/webssh/host', web.name)
output WEB_APP_CONFIG string = format('https://portal.azure.com/#@/resource{0}/configuration', web.id)

0 comments on commit 619d479

Please sign in to comment.