Skip to content

Commit 619d479

Browse files
committed
Add log analytics logs, add output messages, remove Django code from Flask app
1 parent 793d59e commit 619d479

File tree

7 files changed

+90
-25
lines changed

7 files changed

+90
-25
lines changed

.devcontainer/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Dev container
2+
3+
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).
4+
5+
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.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is a Python web app using the Flask framework and the Azure Database for Po
44

55
## Requirements
66

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

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

3030
Steps for running the server:
3131

32-
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.
32+
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.
3333

3434
2. Install the requirements:
3535

azure.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,26 @@ services:
88
project: .
99
language: py
1010
host: appservice
11+
hooks:
12+
postprovision:
13+
posix:
14+
shell: sh
15+
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"
16+
interactive: true
17+
continueOnError: true
18+
windows:
19+
shell: pwsh
20+
run: Write-Host "`n`nApp Service app has the following settings:`n" $WEB_APP_SETTINGS | ConvertFrom-Json | ForEach-Object { Write-Host "\t- $_" }
21+
interactive: true
22+
continueOnError: true
23+
postdeploy:
24+
posix:
25+
shell: sh
26+
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"
27+
interactive: true
28+
continueOnError: true
29+
windows:
30+
shell: pwsh
31+
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
32+
interactive: true
33+
continueOnError: true

azureproject/development.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import os
2-
from pathlib import Path
3-
4-
# Build paths inside the project like this: BASE_DIR / 'subdir'.
5-
BASE_DIR = Path(__file__).resolve().parent.parent
62

73
DATABASE_URI = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
84
dbuser=os.environ['DBUSER'],
95
dbpass=os.environ['DBPASS'],
106
dbhost=os.environ['DBHOST'],
117
dbname=os.environ['DBNAME']
128
)
13-
14-
TIME_ZONE = 'UTC'
15-
16-
STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
17-
STATIC_URL = 'static/'
18-

azureproject/production.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import os
22

3-
# SECURITY WARNING: keep the secret key used in production secret!
4-
SECRET_KEY = os.getenv('SECRET_KEY')
5-
6-
ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
7-
CSRF_TRUSTED_ORIGINS = ['https://' + os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
8-
93
# Configure Postgres database based on connection string of the libpq Keyword/Value form
104
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
115
conn_str = os.environ['AZURE_POSTGRESQL_CONNECTIONSTRING']

infra/main.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ param location string
1414
param databasePassword string
1515

1616
@secure()
17-
@description('Flask SECRET_KEY for securing signed data')
17+
@description('Django SECRET_KEY for securing signed data')
1818
param secretKey string
1919

2020
var resourceToken = toLower(uniqueString(subscription().id, name, location))
@@ -42,3 +42,7 @@ module resources 'resources.bicep' = {
4242
output AZURE_LOCATION string = location
4343
output APPLICATIONINSIGHTS_CONNECTION_STRING string = resources.outputs.APPLICATIONINSIGHTS_CONNECTION_STRING
4444
output WEB_URI string = resources.outputs.WEB_URI
45+
output WEB_APP_SETTINGS array = resources.outputs.WEB_APP_SETTINGS
46+
output WEB_APP_LOG_STREAM string = resources.outputs.WEB_APP_LOG_STREAM
47+
output WEB_APP_SSH string = resources.outputs.WEB_APP_SSH
48+
output WEB_APP_CONFIG string = resources.outputs.WEB_APP_CONFIG

infra/resources.bicep

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ resource web 'Microsoft.Web/sites@2022-03-01' = {
106106
name: 'appsettings'
107107
properties: {
108108
SCM_DO_BUILD_DURING_DEPLOYMENT: 'true'
109-
AZURE_POSTGRESQL_CONNECTIONSTRING: 'dbname=${flaskDatabase.name} host=${postgresServer.name}.postgres.database.azure.com port=5432 sslmode=require user=${postgresServer.properties.administratorLogin} password=${databasePassword}'
109+
AZURE_POSTGRESQL_CONNECTIONSTRING: 'dbname=${pythonAppDatabase.name} host=${postgresServer.name}.postgres.database.azure.com port=5432 sslmode=require user=${postgresServer.properties.administratorLogin} password=${databasePassword}'
110110
SECRET_KEY: secretKey
111111
FLASK_DEBUG: 'False'
112112
}
@@ -143,10 +143,50 @@ resource web 'Microsoft.Web/sites@2022-03-01' = {
143143
}
144144
}
145145

146-
dependsOn: [virtualNetwork]
146+
dependsOn: [ virtualNetwork ]
147147

148148
}
149149

150+
resource webdiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
151+
name: 'AllLogs'
152+
scope: web
153+
properties: {
154+
workspaceId: logAnalyticsWorkspace.id
155+
logs: [
156+
{
157+
category: 'AppServiceHTTPLogs'
158+
enabled: true
159+
}
160+
{
161+
category: 'AppServiceConsoleLogs'
162+
enabled: true
163+
}
164+
{
165+
category: 'AppServiceAppLogs'
166+
enabled: true
167+
}
168+
{
169+
category: 'AppServiceAuditLogs'
170+
enabled: true
171+
}
172+
{
173+
category: 'AppServiceIPSecAuditLogs'
174+
enabled: true
175+
}
176+
{
177+
category: 'AppServicePlatformLogs'
178+
enabled: true
179+
}
180+
]
181+
metrics: [
182+
{
183+
category: 'AllMetrics'
184+
enabled: true
185+
}
186+
]
187+
}
188+
}
189+
150190
resource appServicePlan 'Microsoft.Web/serverfarms@2021-03-01' = {
151191
name: '${prefix}-service-plan'
152192
location: location
@@ -184,7 +224,6 @@ module applicationInsightsResources 'appinsights.bicep' = {
184224
}
185225
}
186226

187-
188227
resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2022-01-20-preview' = {
189228
location: location
190229
tags: tags
@@ -224,11 +263,21 @@ resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2022-01-20-pr
224263
]
225264
}
226265

227-
228-
resource flaskDatabase 'Microsoft.DBforPostgreSQL/flexibleServers/databases@2022-01-20-preview' = {
266+
resource pythonAppDatabase 'Microsoft.DBforPostgreSQL/flexibleServers/databases@2022-01-20-preview' = {
229267
parent: postgresServer
230-
name: 'flask'
268+
name: 'pythonapp'
231269
}
232270

233271
output WEB_URI string = 'https://${web.properties.defaultHostName}'
234272
output APPLICATIONINSIGHTS_CONNECTION_STRING string = applicationInsightsResources.outputs.APPLICATIONINSIGHTS_CONNECTION_STRING
273+
274+
resource webAppSettings 'Microsoft.Web/sites/config@2022-03-01' existing = {
275+
name: web::appSettings.name
276+
parent: web
277+
}
278+
279+
var webAppSettingsKeys = map(items(webAppSettings.list().properties), setting => setting.key)
280+
output WEB_APP_SETTINGS array = webAppSettingsKeys
281+
output WEB_APP_LOG_STREAM string = format('https://portal.azure.com/#@/resource{0}/logStream', web.id)
282+
output WEB_APP_SSH string = format('https://{0}.scm.azurewebsites.net/webssh/host', web.name)
283+
output WEB_APP_CONFIG string = format('https://portal.azure.com/#@/resource{0}/configuration', web.id)

0 commit comments

Comments
 (0)