Skip to content

Commit 77e097b

Browse files
authored
Merge pull request #100 from Metaswitch/md/renovate-batch
Test Renovate batch and remove Docker as a dependency
2 parents 7884504 + acec0de commit 77e097b

9 files changed

Lines changed: 228 additions & 426 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
python-version: ["3.11"]
11+
python-version: ["3.13"]
1212
steps:
1313
- uses: actions/checkout@v7
1414
- name: Set up Python ${{ matrix.python-version }}

‎README.md‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ an Azure Function App to keep it up to date. For use with
1818
- Azure CLI
1919
- Installation instructions available at https://learn.microsoft.com/en-us/cli/azure/install-azure-cli
2020

21-
- Docker (when not using shared key access from the function app to the storage container)
22-
- Installation instructions available at https://docs.docker.com/engine/install/
23-
2421
## Basic usage
2522

2623
To create a new Debian package repository with an Azure Function App, run
@@ -50,9 +47,10 @@ poetry run create-resources --location uksouth <resource_group_name>
5047
## No shared-key access / Managed Identities
5148

5249
By default, the storage container that is created has shared-key access enabled.
53-
You can instead create a deployment that uses Managed Identities, but this
54-
requires Docker (as the function application and its dependencies must be
55-
compiled and packed appropriately).
50+
You can instead create a deployment that uses Managed Identities. In this mode
51+
the function code is published with an Azure AD token via the "One Deploy"
52+
endpoint (shared-key and SCM basic-auth publishing are both disabled), so only
53+
the Azure CLI is required — no Docker or Azure Functions Core Tools.
5654

5755
To create a new Debian package repository which uses Managed Identities, run
5856

‎poetry.lock‎

Lines changed: 99 additions & 353 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ license = "MIT"
77
version = "1.0.0"
88
readme = "README.md"
99
authors = [{name = "Max Dymond", email = "max.dymond@alianza.com"}]
10-
requires-python = '>=3.9.2,<4.0.0'
10+
requires-python = '>=3.13,<4.0.0'
1111
dependencies = [
12-
'azure-functions (>=1.21.3,<2.0.0)',
12+
'azure-functions (>=2.2.0,<2.3.0)',
1313
'azure-identity (>=1.19.0,<2.0.0)',
1414
'azure-storage-blob (>=12.23.1,<13.0.0)',
1515
'pydpkg (>=1.9.3,<2.0.0)',
@@ -29,7 +29,7 @@ requires-poetry = '>=2.0'
2929
poetry-plugin-export = ">=1.8"
3030

3131
[tool.poetry.group.dev.dependencies]
32-
mypy = "^1"
32+
mypy = "^2.0.0"
3333
ruff = "^0.15.0"
3434

3535
[build-system]

‎rg.bicep‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var common_storage_properties = {
5252
var storage_properties = use_shared_keys ? common_storage_properties : union(common_storage_properties, {
5353
allowSharedKeyAccess: false
5454
})
55-
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-06-01' = {
55+
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-08-01' = {
5656
name: storage_account_name
5757
location: location
5858
kind: 'StorageV2'
@@ -63,17 +63,17 @@ resource storageAccount 'Microsoft.Storage/storageAccounts@2025-06-01' = {
6363
}
6464

6565
// Create a container for the packages
66-
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2025-06-01' = {
66+
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2025-08-01' = {
6767
parent: storageAccount
6868
name: 'default'
6969
}
70-
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-06-01' = {
70+
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-08-01' = {
7171
parent: defBlobServices
7272
name: package_container_name
7373
properties: {
7474
}
7575
}
76-
resource pythonContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-06-01' = {
76+
resource pythonContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-08-01' = {
7777
parent: defBlobServices
7878
name: python_container_name
7979
properties: {

‎rg_add_eventgrid.bicep‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ param storage_account_name string = 'debianrepo${suffix}'
2020
var package_container_name = 'packages'
2121

2222
// Define all existing resources
23-
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-06-01' existing = {
23+
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-08-01' existing = {
2424
name: storage_account_name
2525
}
26-
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2025-06-01' existing = {
26+
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2025-08-01' existing = {
2727
parent: storageAccount
2828
name: 'default'
2929
}
30-
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-06-01' existing = {
30+
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-08-01' existing = {
3131
parent: defBlobServices
3232
name: package_container_name
3333
}

‎rg_funcapp.bicep‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var python_container_name = 'python'
3939
var storage_connection_string = 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
4040

4141
// The version of Python to run with
42-
var python_version = '3.11'
42+
var python_version = '3.13'
4343

4444
// The name of the hosting plan, application insights, and function app
4545
var functionAppName = appName
@@ -50,18 +50,18 @@ var applicationInsightsName = appName
5050
resource uami 'Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30' existing = {
5151
name: 'uami${suffix}'
5252
}
53-
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-06-01' existing = {
53+
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-08-01' existing = {
5454
name: storage_account_name
5555
}
56-
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2025-06-01' existing = {
56+
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2025-08-01' existing = {
5757
parent: storageAccount
5858
name: 'default'
5959
}
60-
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-06-01' existing = {
60+
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-08-01' existing = {
6161
parent: defBlobServices
6262
name: package_container_name
6363
}
64-
resource pythonContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-06-01' existing = {
64+
resource pythonContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-08-01' existing = {
6565
parent: defBlobServices
6666
name: python_container_name
6767
}

‎ruff.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ exclude = [
1010
line-length = 88
1111
indent-width = 4
1212

13-
# Assume Python 3.9
14-
target-version = "py39"
13+
# Assume Python 3.13
14+
target-version = "py313"
1515

1616
[lint]
1717
extend-select = [

‎src/apt_package_function/func_app.py‎

Lines changed: 107 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import json
66
import logging
7-
import os
8-
import subprocess
97
import tempfile
108
import time
9+
import urllib.request
10+
from enum import IntEnum
1111
from pathlib import Path
1212
from subprocess import CalledProcessError
1313
from types import TracebackType
@@ -19,6 +19,23 @@
1919
log = logging.getLogger(__name__)
2020
log.addHandler(logging.NullHandler())
2121

22+
# The files that make up the deployable function app package.
23+
FUNCTION_APP_FILES = [
24+
Path("host.json"),
25+
Path("requirements.txt"),
26+
Path("function_app.py"),
27+
]
28+
29+
30+
class DeployStatus(IntEnum):
31+
"""Kudu deployment status codes (from /api/deployments/latest)."""
32+
33+
PENDING = 0
34+
BUILDING = 1
35+
DEPLOYING = 2
36+
FAILED = 3
37+
SUCCESS = 4
38+
2239

2340
class FuncApp:
2441
"""Basic class for managing function apps."""
@@ -36,6 +53,12 @@ def __init__(
3653
self.output_path = output_path
3754
self.subscription = subscription
3855

56+
def build_function_zip(self) -> None:
57+
"""Write the function app package to the output path."""
58+
with ZipFile(self.output_path, "w") as zipf:
59+
for path in FUNCTION_APP_FILES:
60+
zipf.write(path, path.name)
61+
3962
def wait_for_event_trigger(self) -> None:
4063
"""Wait until the function app has an eventGridTrigger function."""
4164
cmd = AzCmdJson(
@@ -103,16 +126,7 @@ def __init__(
103126
name, resource_group, Path(self.tempfile.name), subscription=subscription
104127
)
105128

106-
self.zip_paths = [
107-
Path("host.json"),
108-
Path("requirements.txt"),
109-
Path("function_app.py"),
110-
]
111-
112-
with ZipFile(self.tempfile, "w") as zipf:
113-
for path in self.zip_paths:
114-
zipf.write(path, path.name)
115-
129+
self.build_function_zip()
116130
self.tempfile.close()
117131

118132
def deploy(self) -> None:
@@ -141,50 +155,94 @@ def deploy(self) -> None:
141155

142156

143157
class FuncAppBundle(FuncApp):
144-
"""Publishes the function app using the core-tools tooling."""
158+
"""Publishes the function app via the Azure "One Deploy" endpoint.
159+
160+
Used when shared-key access is disabled. Both 'az functionapp deployment
161+
source config-zip' and 'az functionapp deploy' insist on fetching SCM basic
162+
publishing credentials, which are disabled on such apps, so they fail with
163+
HTTP 403. Instead we POST the package straight to the One Deploy endpoint
164+
with an AAD bearer token, which is accepted. This needs only 'az' (for the
165+
token); no Docker image or Azure Functions Core Tools are required.
166+
"""
167+
168+
# Poll the deployment for at most this long (remote build can be slow).
169+
_DEPLOY_TIMEOUT_S = 600
170+
_DEPLOY_POLL_INTERVAL_S = 15
145171

146172
def __init__(
147173
self, name: str, resource_group: str, subscription: Optional[str] = None
148174
) -> None:
149175
"""Create a FuncAppBundle object."""
176+
self.tempfile = tempfile.NamedTemporaryFile(suffix=".zip", delete=False)
150177
super().__init__(
151-
name, resource_group, Path("function_app.zip"), subscription=subscription
178+
name, resource_group, Path(self.tempfile.name), subscription=subscription
152179
)
180+
self.build_function_zip()
181+
self.tempfile.close()
182+
183+
def _access_token(self) -> str:
184+
"""Get an AAD access token for the deployment endpoint."""
185+
token: str = AzCmdJson(
186+
["az", "account", "get-access-token", "--query", "accessToken"],
187+
subscription=self.subscription,
188+
).run()
189+
return token
153190

154191
def deploy(self) -> None:
155-
"""Deploy the function application."""
156-
log.info("Deploying function app code")
157-
cwd = Path.cwd()
158-
159-
# Mint an access token on the host: the host 'az' can read its own
160-
# credential cache, whereas the (older) 'az' inside the core-tools image
161-
# cannot deserialise a cache written by a newer host 'az'. So instead of
162-
# mounting ~/.azure into the container, pass a token to 'func' directly.
163-
token_cmd = ["az", "account", "get-access-token", "--query", "accessToken"]
164-
token = AzCmdJson(token_cmd, subscription=self.subscription).run()
165-
166-
# Pass the token via the environment so it never appears in the logged
167-
# command line or the container's process arguments.
168-
func_cmd = f'func azure functionapp publish {self.name} --python --build remote --access-token "$FUNC_ACCESS_TOKEN"'
169-
if self.subscription:
170-
func_cmd += f" --subscription {self.subscription}"
171-
172-
# Publish the application using the core-tools tooling
173-
cmd = [
174-
"docker",
175-
"run",
176-
"--rm",
177-
"-e",
178-
"FUNC_ACCESS_TOKEN",
179-
"-v",
180-
f"{cwd}:/function_app",
181-
"-w",
182-
"/function_app",
183-
"mcr.microsoft.com/azure-functions/python:4-python3.11-core-tools",
184-
"bash",
185-
"-c",
186-
func_cmd,
187-
]
188-
log.debug("Running %s", cmd)
189-
subprocess.run(cmd, check=True, env={**os.environ, "FUNC_ACCESS_TOKEN": token})
192+
"""Deploy the function app via One Deploy with a bearer token."""
193+
log.info("Deploying function app code to %s", self.name)
194+
token = self._access_token()
195+
196+
data = self.output_path.read_bytes()
197+
# RemoteBuild=true runs the build on Azure (Oryx) at the app's own
198+
# runtime, so nothing local needs to match the target Python version.
199+
url = (
200+
f"https://{self.name}.scm.azurewebsites.net/api/publish"
201+
"?type=zip&RemoteBuild=true"
202+
)
203+
request = urllib.request.Request( # noqa: S310
204+
url,
205+
data=data,
206+
method="POST",
207+
headers={
208+
"Authorization": f"Bearer {token}",
209+
"Content-Type": "application/zip",
210+
},
211+
)
212+
with urllib.request.urlopen(request) as response: # noqa: S310
213+
log.info("Deployment accepted (HTTP %s), awaiting build", response.status)
214+
215+
self._wait_for_deployment(token)
190216
log.info("Function app code published to %s", self.name)
217+
218+
def _wait_for_deployment(self, token: str) -> None:
219+
"""Poll the latest deployment until it succeeds, or raise on failure."""
220+
url = f"https://{self.name}.scm.azurewebsites.net/api/deployments/latest"
221+
deadline = time.monotonic() + self._DEPLOY_TIMEOUT_S
222+
while time.monotonic() < deadline:
223+
request = urllib.request.Request( # noqa: S310
224+
url, headers={"Authorization": f"Bearer {token}"}
225+
)
226+
with urllib.request.urlopen(request) as response: # noqa: S310
227+
info = json.loads(response.read())
228+
229+
status = info.get("status")
230+
try:
231+
status_name = DeployStatus(status).name
232+
except ValueError:
233+
status_name = f"Unknown({status})"
234+
log.info(
235+
"Deployment status: %s %s", status_name, info.get("status_text", "")
236+
)
237+
if status == DeployStatus.SUCCESS:
238+
return
239+
if status == DeployStatus.FAILED:
240+
raise RuntimeError(
241+
f"Deployment of {self.name} failed: {info.get('status_text', '')}"
242+
)
243+
time.sleep(self._DEPLOY_POLL_INTERVAL_S)
244+
245+
raise TimeoutError(
246+
f"Deployment of {self.name} did not complete within "
247+
f"{self._DEPLOY_TIMEOUT_S}s"
248+
)

0 commit comments

Comments
 (0)