pulumi-django-azure 1.0.33__tar.gz → 1.0.35__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pulumi-django-azure might be problematic. Click here for more details.

Files changed (20) hide show
  1. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/PKG-INFO +11 -10
  2. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/pyproject.toml +26 -24
  3. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/azure_helper.py +37 -7
  4. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/django_deployment.py +20 -115
  5. pulumi_django_azure-1.0.35/src/pulumi_django_azure/management/commands/test_redis.py +248 -0
  6. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/middleware.py +16 -33
  7. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/settings.py +7 -22
  8. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure.egg-info/PKG-INFO +11 -10
  9. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure.egg-info/SOURCES.txt +2 -1
  10. pulumi_django_azure-1.0.35/src/pulumi_django_azure.egg-info/requires.txt +14 -0
  11. pulumi_django_azure-1.0.33/src/pulumi_django_azure.egg-info/requires.txt +0 -13
  12. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/README.md +0 -0
  13. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/setup.cfg +0 -0
  14. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/__init__.py +0 -0
  15. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/context_processors.py +0 -0
  16. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/management/commands/__init__.py +0 -0
  17. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/management/commands/purge_cache.py +0 -0
  18. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure/management/commands/purge_cdn.py +0 -0
  19. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure.egg-info/dependency_links.txt +0 -0
  20. {pulumi_django_azure-1.0.33 → pulumi_django_azure-1.0.35}/src/pulumi_django_azure.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pulumi-django-azure
3
- Version: 1.0.33
3
+ Version: 1.0.35
4
4
  Summary: Simply deployment of Django on Azure with Pulumi
5
5
  Author-email: Maarten Ureel <maarten@youreal.eu>
6
6
  License-Expression: MIT
@@ -10,19 +10,20 @@ Classifier: Programming Language :: Python
10
10
  Classifier: Programming Language :: Python :: 3
11
11
  Requires-Python: <3.14,>=3.11
12
12
  Description-Content-Type: text/markdown
13
- Requires-Dist: azure-identity<2.0.0,>=1.21.0
14
- Requires-Dist: azure-keyvault-secrets<5.0.0,>=4.9.0
13
+ Requires-Dist: azure-identity<2.0.0,>=1.24.0
14
+ Requires-Dist: azure-keyvault-secrets<5.0.0,>=4.10.0
15
15
  Requires-Dist: azure-mgmt-cdn<14.0.0,>=13.1.1
16
- Requires-Dist: azure-mgmt-resource<24.0.0,>=23.3.0
17
- Requires-Dist: django<6.0,>=5.2
16
+ Requires-Dist: azure-mgmt-resource<25.0.0,>=24.0.0
17
+ Requires-Dist: django<6.0.0,>=5.2.5
18
18
  Requires-Dist: django-azure-communication-email<2.0.0,>=1.3.2
19
19
  Requires-Dist: django-environ<0.13.0,>=0.12.0
20
- Requires-Dist: django-redis<6.0.0,>=5.4.0
20
+ Requires-Dist: django-redis<7.0.0,>=6.0.0
21
21
  Requires-Dist: django-storages[azure]<2.0.0,>=1.14.6
22
- Requires-Dist: pulumi>=3.166.0
23
- Requires-Dist: pulumi-azure-native>=3.2.0
24
- Requires-Dist: pulumi-random>=4.18.2
25
- Requires-Dist: redis[hiredis]<7.0.0,>=6.0.0
22
+ Requires-Dist: pulumi>=3.189.0
23
+ Requires-Dist: pulumi-azure-native>=3.7.1
24
+ Requires-Dist: pulumi-random>=4.18.3
25
+ Requires-Dist: redis[hiredis]<7.0.0,>=6.4.0
26
+ Requires-Dist: tenacity<10.0.0,>=9.1.2
26
27
 
27
28
  # Pulumi Django Deployment
28
29
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pulumi-django-azure"
7
- version = "1.0.33"
7
+ version = "1.0.35"
8
8
  description = "Simply deployment of Django on Azure with Pulumi"
9
9
  readme = "README.md"
10
10
  authors = [{ name = "Maarten Ureel", email = "maarten@youreal.eu" }]
@@ -15,19 +15,20 @@ classifiers = [
15
15
  ]
16
16
  keywords = ["django", "pulumi", "azure"]
17
17
  dependencies = [
18
- "azure-identity (>=1.21.0,<2.0.0)",
19
- "azure-keyvault-secrets (>=4.9.0,<5.0.0)",
18
+ "azure-identity (>=1.24.0,<2.0.0)",
19
+ "azure-keyvault-secrets (>=4.10.0,<5.0.0)",
20
20
  "azure-mgmt-cdn (>=13.1.1,<14.0.0)",
21
- "azure-mgmt-resource (>=23.3.0,<24.0.0)",
22
- "django (>=5.2,<6.0)",
21
+ "azure-mgmt-resource (>=24.0.0,<25.0.0)",
22
+ "django (>=5.2.5,<6.0.0)",
23
23
  "django-azure-communication-email (>=1.3.2,<2.0.0)",
24
24
  "django-environ (>=0.12.0,<0.13.0)",
25
- "django-redis (>=5.4.0,<6.0.0)",
25
+ "django-redis (>=6.0.0,<7.0.0)",
26
26
  "django-storages[azure] (>=1.14.6,<2.0.0)",
27
- "pulumi (>=3.166.0)",
28
- "pulumi-azure-native (>=3.2.0)",
29
- "pulumi-random (>=4.18.2)",
30
- "redis[hiredis] (>=6.0.0,<7.0.0)"
27
+ "pulumi (>=3.189.0)",
28
+ "pulumi-azure-native (>=3.7.1)",
29
+ "pulumi-random (>=4.18.3)",
30
+ "redis[hiredis] (>=6.4.0,<7.0.0)",
31
+ "tenacity (>=9.1.2,<10.0.0)"
31
32
  ]
32
33
  requires-python = ">=3.11,<3.14"
33
34
 
@@ -36,31 +37,32 @@ Homepage = "https://gitlab.com/MaartenUreel/pulumi-django-azure"
36
37
 
37
38
  [tool.poetry]
38
39
  name = "pulumi-django-azure"
39
- version = "1.0.33"
40
+ version = "1.0.35"
40
41
  description = "Simplify deployment of Django websites on Azure with Pulumi"
41
42
  authors = ["Maarten Ureel <maarten@youreal.eu>"]
42
43
 
43
44
  [tool.poetry.dependencies]
44
45
  python = "^3.11,<3.14"
45
- azure-identity = "^1.21.0"
46
- azure-keyvault-secrets = "^4.9.0"
46
+ azure-identity = "^1.24.0"
47
+ azure-keyvault-secrets = "^4.10.0"
47
48
  azure-mgmt-cdn = "^13.1.1"
48
- azure-mgmt-resource = "^23.3.0"
49
- django = "^5.2"
49
+ azure-mgmt-resource = "^24.0.0"
50
+ django = "^5.2.5"
50
51
  django-azure-communication-email = "^1.3.2"
51
52
  django-environ = "^0.12.0"
52
- django-redis = "^5.4.0"
53
+ django-redis = "^6.0.0"
53
54
  django-storages = {extras = ["azure"], version = "^1.14.6"}
54
- pulumi = ">=3.166.0"
55
- pulumi-azure-native = ">=3.2.0"
56
- pulumi-random = ">=4.18.2"
57
- redis = {extras = ["hiredis"], version = "^6.0.0"}
55
+ pulumi = ">=3.189.0"
56
+ pulumi-azure-native = ">=3.7.1"
57
+ pulumi-random = ">=4.18.3"
58
+ redis = {extras = ["hiredis"], version = "^6.4.0"}
59
+ tenacity = "^9.1.2"
58
60
 
59
61
  [tool.poetry.group.dev.dependencies]
60
- build = "^1.2.2.post1"
61
- mkdocs-material = "^9.6.12"
62
- pre-commit = "^4.2.0"
63
- ruff = "^0.11.7"
62
+ build = "^1.3.0"
63
+ mkdocs-material = "^9.6.16"
64
+ pre-commit = "^4.3.0"
65
+ ruff = "^0.12.8"
64
66
  twine = "^6.1.0"
65
67
 
66
68
  [tool.ruff]
@@ -2,7 +2,9 @@ import base64
2
2
  import json
3
3
  import logging
4
4
  import os
5
+ import time
5
6
  from dataclasses import dataclass
7
+ from enum import Enum
6
8
  from subprocess import check_output
7
9
 
8
10
  from azure.identity import DefaultAzureCredential
@@ -15,6 +17,9 @@ logger = logging.getLogger("pulumi_django_azure.azure_helper")
15
17
  # Azure credentials
16
18
  AZURE_CREDENTIAL = DefaultAzureCredential()
17
19
 
20
+ # Buffer for token expiration (5 minutes)
21
+ TOKEN_EXPIRATION_BUFFER = 300
22
+
18
23
  # Get the local IP addresses of the machine (only when runnig on Azure)
19
24
  if os.environ.get("IS_AZURE_ENVIRONMENT"):
20
25
  LOCAL_IP_ADDRESSES = check_output(["hostname", "--all-ip-addresses"]).decode("utf-8").strip().split(" ")
@@ -22,17 +27,44 @@ else:
22
27
  LOCAL_IP_ADDRESSES = []
23
28
 
24
29
 
25
- def get_db_password() -> str:
30
+ class TokenType(Enum):
31
+ DATABASE = "https://ossrdbms-aad.database.windows.net/.default"
32
+ REDIS = "https://redis.azure.com/.default"
33
+
34
+
35
+ def _get_azure_token(type: TokenType) -> str:
26
36
  """
27
- Get a valid password for the database.
37
+ Get a valid token for the given scope.
28
38
  """
29
- token = AZURE_CREDENTIAL.get_token("https://ossrdbms-aad.database.windows.net/.default")
39
+ global AZURE_CREDENTIAL
40
+
41
+ token = AZURE_CREDENTIAL.get_token(type.value)
42
+
43
+ if token.expires_on < time.time() + TOKEN_EXPIRATION_BUFFER:
44
+ # We received an expired or nearly expired token from the API. Force a new token by creating a new instance of the credential.
45
+ logger.debug(
46
+ "Received an expired or nearly expired %s token (current time: %s, token expiration: %s)."
47
+ "Creating a new instance of the credential.",
48
+ type.name,
49
+ time.time(),
50
+ token.expires_on,
51
+ )
52
+
53
+ AZURE_CREDENTIAL = DefaultAzureCredential()
54
+ token = AZURE_CREDENTIAL.get_token(type.value)
30
55
 
31
- logger.debug("New database token: %s", token)
56
+ logger.debug("New %s token (try 2): %s", type.name, token)
32
57
 
33
58
  return token.token
34
59
 
35
60
 
61
+ def get_db_password() -> str:
62
+ """
63
+ Get a valid password for the database.
64
+ """
65
+ return _get_azure_token(TokenType.DATABASE)
66
+
67
+
36
68
  @dataclass
37
69
  class RedisCredentials:
38
70
  username: str
@@ -43,9 +75,7 @@ def get_redis_credentials() -> RedisCredentials:
43
75
  """
44
76
  Get valid credentials for the Redis cache.
45
77
  """
46
- token = AZURE_CREDENTIAL.get_token("https://redis.azure.com/.default")
47
-
48
- logger.debug("New Redis token: %s", token)
78
+ token = _get_azure_token(TokenType.REDIS)
49
79
 
50
80
  return RedisCredentials(_extract_username_from_token(token), token)
51
81
 
@@ -4,6 +4,8 @@ import pulumi
4
4
  import pulumi_azure_native as azure
5
5
  import pulumi_random
6
6
 
7
+ REDIS_IMAGE = "redis:8.2"
8
+
7
9
 
8
10
  class HostDefinition:
9
11
  """
@@ -71,8 +73,6 @@ class DjangoDeployment(pulumi.ComponentResource):
71
73
  pgsql_parameters: dict[str, str] | None = None,
72
74
  pgadmin_access_ip: Sequence[str] | None = None,
73
75
  pgadmin_dns_zone: azure.dns.Zone | None = None,
74
- cache_ip_prefix: str | None = None,
75
- cache_sku: azure.redis.SkuArgs | None = None,
76
76
  cdn_host: HostDefinition | None = None,
77
77
  opts=None,
78
78
  ):
@@ -92,8 +92,6 @@ class DjangoDeployment(pulumi.ComponentResource):
92
92
  :param pgsql_parameters: The parameters to set on the PostgreSQL server. (optional)
93
93
  :param pgadmin_access_ip: The IP addresses to allow access to pgAdmin. If empty, all IP addresses are allowed.
94
94
  :param pgadmin_dns_zone: The Azure DNS zone to a pgadmin DNS record in. (optional)
95
- :param cache_ip_prefix: The IP prefix for the cache subnet. (optional)
96
- :param cache_sku: The SKU for the cache. (optional)
97
95
  :param cdn_host: A custom CDN host name. (optional)
98
96
  :param opts: The resource options
99
97
  """
@@ -115,12 +113,6 @@ class DjangoDeployment(pulumi.ComponentResource):
115
113
  # PostgreSQL resources
116
114
  self._create_database(sku=pgsql_sku, ip_prefix=pgsql_ip_prefix, parameters=pgsql_parameters)
117
115
 
118
- # Cache resources
119
- if cache_ip_prefix and cache_sku:
120
- self._create_cache(sku=cache_sku, ip_prefix=cache_ip_prefix)
121
- else:
122
- self._cache = None
123
-
124
116
  # Subnet for the apps
125
117
  self._app_subnet = self._create_subnet(
126
118
  name="app-service",
@@ -325,91 +317,6 @@ class DjangoDeployment(pulumi.ComponentResource):
325
317
 
326
318
  pulumi.export("pgsql_host", self._pgsql.fully_qualified_domain_name)
327
319
 
328
- def _create_cache(self, sku: azure.redis.SkuArgs, ip_prefix: str):
329
- # Create a Redis cache
330
- self._cache = azure.redis.Redis(
331
- f"cache-{self._name}",
332
- resource_group_name=self._rg,
333
- sku=sku,
334
- enable_non_ssl_port=False,
335
- public_network_access=azure.redis.PublicNetworkAccess.DISABLED,
336
- # Disable access key authentication...
337
- disable_access_key_authentication=True,
338
- # ... and enable Entra ID authentication.
339
- redis_configuration=azure.redis.RedisCommonPropertiesRedisConfigurationArgs(
340
- aad_enabled="true",
341
- ),
342
- )
343
-
344
- # Create an access policy that gives us access to the cache
345
- self._cache_access_policy = azure.redis.AccessPolicy(
346
- f"cache-access-policy-{self._name}",
347
- resource_group_name=self._rg,
348
- cache_name=self._cache.name,
349
- # Same as the built in Data Contributor policy + flushdb permissions
350
- permissions="+@all -@dangerous +flushdb +cluster|info +cluster|nodes +cluster|slots allkeys",
351
- )
352
-
353
- # Allocate a subnet for the cache
354
- subnet = self._create_subnet(
355
- name="cache",
356
- prefix=ip_prefix,
357
- )
358
-
359
- # Create a private DNS zone for the cache
360
- dns = azure.privatedns.PrivateZone(
361
- f"dns-cache-{self._name}",
362
- resource_group_name=self._rg,
363
- location="global",
364
- private_zone_name="privatelink.redis.cache.windows.net",
365
- )
366
-
367
- # Link the private DNS zone to the VNet in order to make resolving work
368
- azure.privatedns.VirtualNetworkLink(
369
- f"vnet-link-cache-{self._name}",
370
- resource_group_name=self._rg,
371
- location="global",
372
- private_zone_name=dns.name,
373
- virtual_network=azure.network.SubResourceArgs(id=self._vnet.id),
374
- registration_enabled=True,
375
- )
376
-
377
- # Create a private endpoint for the cache
378
- endpoint = azure.network.PrivateEndpoint(
379
- f"private-endpoint-cache-{self._name}",
380
- resource_group_name=self._rg,
381
- subnet=azure.network.SubnetArgs(id=subnet.id),
382
- custom_network_interface_name=f"nic-cache-{self._name}",
383
- private_link_service_connections=[
384
- azure.network.PrivateLinkServiceConnectionArgs(
385
- name=f"pls-cache-{self._name}",
386
- private_link_service_id=self._cache.id,
387
- group_ids=["redisCache"],
388
- )
389
- ],
390
- )
391
-
392
- # Get the private IP address of the endpoint NIC
393
- ip = endpoint.network_interfaces.apply(
394
- lambda nics: azure.network.get_network_interface(
395
- network_interface_name=nics[0].id.split("/")[-1],
396
- resource_group_name=self._rg,
397
- )
398
- .ip_configurations[0]
399
- .private_ip_address
400
- )
401
-
402
- # Create a DNS record for the cache
403
- azure.privatedns.PrivateRecordSet(
404
- f"dns-a-cache-{self._name}",
405
- resource_group_name=self._rg,
406
- private_zone_name=dns.name,
407
- relative_record_set_name=self._cache.name,
408
- record_type="A",
409
- ttl=300,
410
- a_records=[azure.privatedns.ARecordArgs(ipv4_address=ip)],
411
- )
412
-
413
320
  def _create_subnet(
414
321
  self,
415
322
  name,
@@ -866,7 +773,7 @@ class DjangoDeployment(pulumi.ComponentResource):
866
773
  comms_domains: list[HostDefinition] | None = None,
867
774
  dedicated_app_service_sku: azure.web.SkuDescriptionArgs | None = None,
868
775
  vault_administrators: list[str] | None = None,
869
- cache_db: int | None = None,
776
+ redis_cache: bool = True,
870
777
  startup_timeout: int = 300,
871
778
  ) -> azure.web.WebApp:
872
779
  """
@@ -887,7 +794,7 @@ class DjangoDeployment(pulumi.ComponentResource):
887
794
  :param comms_domains: The list of custom domains for the E-mail Communication Services (optional).
888
795
  :param dedicated_app_service_sku: The SKU for the dedicated App Service Plan (optional).
889
796
  :param vault_administrators: The principal IDs of the vault administrators (optional).
890
- :param cache_db: The index of the cache database to use (optional).
797
+ :param redis_cache: Whether to create a Redis sidecar container.
891
798
  :param startup_timeout: The startup timeout for the App Service (default is 300 seconds).
892
799
  """
893
800
 
@@ -917,6 +824,10 @@ class DjangoDeployment(pulumi.ComponentResource):
917
824
  container_name=f"{name}-static",
918
825
  )
919
826
 
827
+ # Redis cache environment variable
828
+ if redis_cache:
829
+ environment_variables["REDIS_SIDECAR"] = "true"
830
+
920
831
  # Communication Services (optional)
921
832
  if comms_data_location:
922
833
  if not comms_domains:
@@ -937,12 +848,6 @@ class DjangoDeployment(pulumi.ComponentResource):
937
848
  s = self._add_webapp_secret(vault, env_name, config_name, f"{name}-{self._name}")
938
849
  environment_variables[f"{env_name}_SECRET_NAME"] = s.name
939
850
 
940
- # Cache (we need to check explicitly if it is not None because the db could also be 0)
941
- if self._cache and cache_db is not None:
942
- environment_variables["REDIS_CACHE_HOST"] = self._cache.host_name
943
- environment_variables["REDIS_CACHE_PORT"] = self._cache.ssl_port.apply(lambda port: str(port))
944
- environment_variables["REDIS_CACHE_DB"] = str(cache_db)
945
-
946
851
  # Create a Django Secret Key (random)
947
852
  secret_key = pulumi_random.RandomString(f"django-secret-{name}-{self._name}", length=50)
948
853
 
@@ -1023,6 +928,18 @@ class DjangoDeployment(pulumi.ComponentResource):
1023
928
  ),
1024
929
  )
1025
930
 
931
+ # Redis cache
932
+ if redis_cache:
933
+ azure.web.WebAppSiteContainer(
934
+ f"redis-cache-{name}-{self._name}",
935
+ resource_group_name=self._rg,
936
+ name=app.name,
937
+ is_main=False,
938
+ container_name="redis-cache",
939
+ image=REDIS_IMAGE,
940
+ target_port="6379",
941
+ )
942
+
1026
943
  # We need this to create the database role and grant permissions
1027
944
  principal_id = app.identity.apply(lambda identity: identity.principal_id)
1028
945
  pulumi.export(f"{name}_site_principal_id", principal_id)
@@ -1148,18 +1065,6 @@ class DjangoDeployment(pulumi.ComponentResource):
1148
1065
  scope=self._storage_account.id,
1149
1066
  )
1150
1067
 
1151
- # Grant the app access to the cache if needed.
1152
- # We need to check explicitly if it is not None because the db could also be 0.
1153
- if self._cache and cache_db is not None:
1154
- azure.redis.AccessPolicyAssignment(
1155
- f"ra-{name}-cache",
1156
- resource_group_name=self._rg,
1157
- cache_name=self._cache.name,
1158
- object_id=principal_id,
1159
- object_id_alias=f"app-{name}-managed-identity",
1160
- access_policy_name=self._cache_access_policy.name,
1161
- )
1162
-
1163
1068
  # Grant the app to send e-mails
1164
1069
  if comms:
1165
1070
  comms_role = comms.id.apply(
@@ -0,0 +1,248 @@
1
+ """
2
+ Management command to test Redis connectivity and functionality.
3
+ """
4
+
5
+ import time
6
+
7
+ from django.conf import settings
8
+ from django.core.cache import cache
9
+ from django.core.management.base import BaseCommand, CommandError
10
+
11
+
12
+ class Command(BaseCommand):
13
+ help = "Test Redis connectivity and basic functionality"
14
+
15
+ def add_arguments(self, parser):
16
+ parser.add_argument(
17
+ "--detailed",
18
+ action="store_true",
19
+ help="Show detailed Redis information and run comprehensive tests",
20
+ )
21
+ parser.add_argument(
22
+ "--quiet",
23
+ action="store_true",
24
+ help="Only show errors and final status",
25
+ )
26
+
27
+ def handle(self, *args, **options):
28
+ verbosity = 0 if options["quiet"] else (2 if options["detailed"] else 1)
29
+
30
+ if verbosity >= 1:
31
+ self.stdout.write("Testing Redis connectivity...")
32
+ self.stdout.write("-" * 50)
33
+
34
+ try:
35
+ # Test basic connectivity
36
+ self._test_basic_connectivity(verbosity)
37
+
38
+ # Test basic cache operations
39
+ self._test_cache_operations(verbosity)
40
+
41
+ # If detailed mode, run additional tests
42
+ if options["detailed"]:
43
+ self._test_detailed_operations(verbosity)
44
+ self._show_cache_info(verbosity)
45
+
46
+ if verbosity >= 1:
47
+ self.stdout.write(self.style.SUCCESS("✓ All Redis tests passed successfully!"))
48
+
49
+ except Exception as e:
50
+ if verbosity >= 1:
51
+ self.stdout.write(self.style.ERROR(f"✗ Redis test failed: {str(e)}"))
52
+ raise CommandError(f"Redis connectivity test failed: {str(e)}") from e
53
+
54
+ def _test_basic_connectivity(self, verbosity):
55
+ """Test basic Redis connectivity."""
56
+ if verbosity >= 2:
57
+ self.stdout.write("Testing basic connectivity...")
58
+
59
+ try:
60
+ # Try to set and get a simple value
61
+ cache.set("redis_test_key", "test_value", 30)
62
+ value = cache.get("redis_test_key")
63
+
64
+ if value != "test_value":
65
+ raise Exception("Failed to retrieve test value from cache")
66
+
67
+ # Clean up
68
+ cache.delete("redis_test_key")
69
+
70
+ if verbosity >= 2:
71
+ self.stdout.write(self.style.SUCCESS(" ✓ Basic connectivity test passed"))
72
+
73
+ except Exception as e:
74
+ raise Exception(f"Basic connectivity test failed: {str(e)}") from e
75
+
76
+ def _test_cache_operations(self, verbosity):
77
+ """Test various cache operations."""
78
+ if verbosity >= 2:
79
+ self.stdout.write("Testing cache operations...")
80
+
81
+ test_data = {
82
+ "string_test": "Hello Redis!",
83
+ "number_test": 42,
84
+ "dict_test": {"key": "value", "nested": {"data": True}},
85
+ "list_test": [1, 2, 3, "four", 5.0],
86
+ }
87
+
88
+ try:
89
+ # Test different data types
90
+ for key, value in test_data.items():
91
+ cache.set(f"test_{key}", value, 60)
92
+ retrieved_value = cache.get(f"test_{key}")
93
+
94
+ if retrieved_value != value:
95
+ raise Exception(f"Data mismatch for {key}: expected {value}, got {retrieved_value}")
96
+
97
+ if verbosity >= 2:
98
+ self.stdout.write(f" ✓ {key}: {type(value).__name__} data test passed")
99
+
100
+ # Test cache.get_or_set
101
+ default_value = "default_from_function"
102
+ result = cache.get_or_set("test_get_or_set", default_value, 60)
103
+ if result != default_value:
104
+ raise Exception("get_or_set test failed")
105
+
106
+ if verbosity >= 2:
107
+ self.stdout.write(" ✓ get_or_set operation test passed")
108
+
109
+ # Test cache.add (should fail on existing key)
110
+ if cache.add("test_string_test", "should_not_override"):
111
+ raise Exception("add() should have failed on existing key")
112
+
113
+ if verbosity >= 2:
114
+ self.stdout.write(" ✓ add operation test passed")
115
+
116
+ # Test cache expiration
117
+ cache.set("test_expiration", "will_expire", 1)
118
+ time.sleep(1.1) # Wait for expiration
119
+ expired_value = cache.get("test_expiration")
120
+ if expired_value is not None:
121
+ raise Exception("Cache expiration test failed - value should have expired")
122
+
123
+ if verbosity >= 2:
124
+ self.stdout.write(" ✓ Cache expiration test passed")
125
+
126
+ # Clean up test keys
127
+ for key in test_data:
128
+ cache.delete(f"test_{key}")
129
+ cache.delete("test_get_or_set")
130
+
131
+ if verbosity >= 2:
132
+ self.stdout.write(self.style.SUCCESS(" ✓ All cache operations tests passed"))
133
+
134
+ except Exception as e:
135
+ raise Exception(f"Cache operations test failed: {str(e)}") from e
136
+
137
+ def _test_detailed_operations(self, verbosity):
138
+ """Run detailed Redis operations tests."""
139
+ if verbosity >= 2:
140
+ self.stdout.write("Running detailed operations...")
141
+
142
+ try:
143
+ # Test cache.get_many and set_many
144
+ test_data = {
145
+ "bulk_key_1": "value_1",
146
+ "bulk_key_2": "value_2",
147
+ "bulk_key_3": "value_3",
148
+ }
149
+
150
+ cache.set_many(test_data, 60)
151
+ retrieved_data = cache.get_many(test_data.keys())
152
+
153
+ for key, expected_value in test_data.items():
154
+ if retrieved_data.get(key) != expected_value:
155
+ raise Exception(f"Bulk operation failed for key {key}")
156
+
157
+ if verbosity >= 2:
158
+ self.stdout.write(" ✓ Bulk operations (set_many/get_many) test passed")
159
+
160
+ # Test cache.delete_many
161
+ cache.delete_many(test_data.keys())
162
+ remaining_data = cache.get_many(test_data.keys())
163
+ if any(remaining_data.values()):
164
+ raise Exception("delete_many operation failed")
165
+
166
+ if verbosity >= 2:
167
+ self.stdout.write(" ✓ Bulk delete (delete_many) test passed")
168
+
169
+ # Test cache versioning if supported
170
+ try:
171
+ cache.set("version_test", "version_1", 60, version=1)
172
+ cache.set("version_test", "version_2", 60, version=2)
173
+
174
+ v1_value = cache.get("version_test", version=1)
175
+ v2_value = cache.get("version_test", version=2)
176
+
177
+ if v1_value == "version_1" and v2_value == "version_2":
178
+ if verbosity >= 2:
179
+ self.stdout.write(" ✓ Cache versioning test passed")
180
+ else:
181
+ if verbosity >= 2:
182
+ self.stdout.write(" ! Cache versioning not supported or failed")
183
+
184
+ cache.delete("version_test", version=1)
185
+ cache.delete("version_test", version=2)
186
+
187
+ except Exception:
188
+ if verbosity >= 2:
189
+ self.stdout.write(" ! Cache versioning not supported")
190
+
191
+ except Exception as e:
192
+ raise Exception(f"Detailed operations test failed: {str(e)}") from e
193
+
194
+ def _show_cache_info(self, verbosity):
195
+ """Show information about the cache configuration."""
196
+ if verbosity >= 2:
197
+ self.stdout.write("\nCache Configuration Information:")
198
+ self.stdout.write("-" * 35)
199
+
200
+ # Show cache backend information
201
+ cache_config = getattr(settings, "CACHES", {}).get("default", {})
202
+ backend = cache_config.get("BACKEND", "Unknown")
203
+ location = cache_config.get("LOCATION", "Not specified")
204
+
205
+ self.stdout.write(f"Backend: {backend}")
206
+ self.stdout.write(f"Location: {location}")
207
+
208
+ # Show cache options if any
209
+ options = cache_config.get("OPTIONS", {})
210
+ if options:
211
+ self.stdout.write("Options:")
212
+ for key, value in options.items():
213
+ # Don't show sensitive information like passwords
214
+ if "password" in key.lower() or "secret" in key.lower():
215
+ value = "***hidden***"
216
+ self.stdout.write(f" {key}: {value}")
217
+
218
+ # Try to get cache stats if available
219
+ try:
220
+ if hasattr(cache, "_cache") and hasattr(cache._cache, "get_stats"):
221
+ stats = cache._cache.get_stats()
222
+ if stats:
223
+ self.stdout.write("\nCache Statistics:")
224
+ for stat_key, stat_value in stats.items():
225
+ self.stdout.write(f" {stat_key}: {stat_value}")
226
+ except Exception:
227
+ pass # Stats not available
228
+
229
+ # Test if we can determine Redis info
230
+ try:
231
+ # Try to access Redis client directly if using django-redis
232
+ if hasattr(cache, "_cache") and hasattr(cache._cache, "_client"):
233
+ client = cache._cache._client
234
+ if hasattr(client, "connection_pool"):
235
+ pool = client.connection_pool
236
+ self.stdout.write("\nConnection Pool Info:")
237
+ self.stdout.write(f" Max connections: {getattr(pool, 'max_connections', 'Unknown')}")
238
+ self.stdout.write(f" Connection class: {getattr(pool, 'connection_class', 'Unknown')}")
239
+
240
+ connection_kwargs = getattr(pool, "connection_kwargs", {})
241
+ if connection_kwargs:
242
+ self.stdout.write(" Connection parameters:")
243
+ for key, value in connection_kwargs.items():
244
+ if "password" in key.lower():
245
+ value = "***hidden***"
246
+ self.stdout.write(f" {key}: {value}")
247
+ except Exception:
248
+ pass # Redis-specific info not available
@@ -1,14 +1,12 @@
1
1
  import logging
2
2
  import os
3
+ import signal
3
4
 
4
5
  from django.conf import settings
5
- from django.core.cache import cache
6
6
  from django.db import connection
7
- from django.db.utils import OperationalError
8
7
  from django.http import HttpResponse
9
- from django_redis import get_redis_connection
10
8
 
11
- from .azure_helper import get_db_password, get_redis_credentials
9
+ from .azure_helper import get_db_password
12
10
 
13
11
  logger = logging.getLogger("pulumi_django_azure.health_check")
14
12
 
@@ -18,9 +16,20 @@ class HealthCheckMiddleware:
18
16
  self.get_response = get_response
19
17
 
20
18
  def _self_heal(self):
21
- # Send HUP signal to gunicorn main thread,
22
- # which will trigger new workers to start.
23
- os.kill(os.getppid(), 1)
19
+ logger.warning("Self-healing by gracefully restarting Gunicorn.")
20
+
21
+ master_pid = os.getppid()
22
+
23
+ logger.debug("Master PID: %d", master_pid)
24
+
25
+ # https://docs.gunicorn.org/en/latest/signals.html
26
+
27
+ # Reload a new master with new workers,
28
+ # since the application is preloaded this is the only safe way for now.
29
+ os.kill(master_pid, signal.SIGUSR2)
30
+
31
+ # Gracefully shutdown the current workers
32
+ os.kill(master_pid, signal.SIGTERM)
24
33
 
25
34
  def __call__(self, request):
26
35
  if request.path == settings.HEALTH_CHECK_PATH:
@@ -43,41 +52,15 @@ class HealthCheckMiddleware:
43
52
  self._self_heal()
44
53
  return HttpResponse(status=503)
45
54
 
46
- # Update the Redis credentials if needed
47
- if settings.AZURE_REDIS_CREDENTIALS:
48
- try:
49
- current_redis_password = settings.CACHES["default"]["OPTIONS"]["PASSWORD"]
50
- redis_credentials = get_redis_credentials()
51
-
52
- if redis_credentials.password != current_redis_password:
53
- logger.debug("Redis password has changed, updating credentials")
54
-
55
- # Re-authenticate the Redis connection
56
- redis_connection = get_redis_connection("default")
57
- redis_connection.execute_command("AUTH", redis_credentials.username, redis_credentials.password)
58
-
59
- settings.CACHES["default"]["OPTIONS"]["PASSWORD"] = redis_credentials.password
60
- else:
61
- logger.debug("Redis password unchanged, keeping existing credentials")
62
- except Exception as e:
63
- logger.error("Failed to update Redis credentials: %s", str(e))
64
- self._self_heal()
65
- return HttpResponse(status=503)
66
-
67
55
  try:
68
56
  # Test the database connection
69
57
  connection.ensure_connection()
70
58
  logger.debug("Database connection check passed")
71
59
 
72
- # Test the Redis connection
73
- cache.set("health_check", "test")
74
- logger.debug("Redis connection check passed")
75
-
76
60
  return HttpResponse("OK")
77
61
 
78
62
  except Exception as e:
79
63
  logger.error("Health check failed with unexpected error: %s", str(e))
80
- logger.warning("Self-healing by gracefully restarting workers.")
81
64
  self._self_heal()
82
65
  return HttpResponse(status=503)
83
66
 
@@ -1,7 +1,7 @@
1
1
  import environ
2
2
  from azure.keyvault.secrets import SecretClient
3
3
 
4
- from .azure_helper import AZURE_CREDENTIAL, LOCAL_IP_ADDRESSES, get_db_password, get_redis_credentials, get_subscription
4
+ from .azure_helper import AZURE_CREDENTIAL, LOCAL_IP_ADDRESSES, get_db_password, get_subscription
5
5
 
6
6
  env = environ.Env()
7
7
 
@@ -104,7 +104,7 @@ if IS_AZURE_ENVIRONMENT:
104
104
  "disable_existing_loggers": False,
105
105
  "formatters": {
106
106
  "timestamped": {
107
- "format": "{asctime} {levelname} {message}",
107
+ "format": "{asctime} {levelname} [p:{process:d}] [t:{thread:d}] {message}",
108
108
  "style": "{",
109
109
  },
110
110
  },
@@ -127,42 +127,27 @@ if IS_AZURE_ENVIRONMENT:
127
127
  },
128
128
  "pulumi_django_azure": {
129
129
  "handlers": ["file"],
130
- "level": "INFO",
130
+ "level": "DEBUG",
131
131
  "propagate": True,
132
132
  },
133
133
  },
134
134
  }
135
135
 
136
136
  # Redis, if enabled
137
- redis_cache_host = env("REDIS_CACHE_HOST", default=None)
138
- redis_cache_port = env("REDIS_CACHE_PORT", default=None)
139
- redis_cache_db = env("REDIS_CACHE_DB", default=None)
140
-
141
- if redis_cache_host and redis_cache_port and redis_cache_db:
142
- # This will enable the health check to update the Redis credentials
143
- AZURE_REDIS_CREDENTIALS = True
137
+ REDIS_SIDECAR = env("REDIS_SIDECAR", default=False)
144
138
 
145
- # This will prevent the website from failing if Redis is not available
139
+ if REDIS_SIDECAR:
140
+ # This will prevent the website from failing if Redis is not available.
146
141
  DJANGO_REDIS_IGNORE_EXCEPTIONS = True
147
142
  DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
148
143
 
149
- REDIS_CACHE_HOST = redis_cache_host
150
- REDIS_CACHE_PORT = redis_cache_port
151
- REDIS_CACHE_DB = redis_cache_db
152
- redis_credentials = get_redis_credentials()
153
- REDIS_USERNAME = redis_credentials.username
154
- REDIS_PASSWORD = redis_credentials.password
155
-
156
144
  CACHES = {
157
145
  "default": {
158
146
  "BACKEND": "django_redis.cache.RedisCache",
159
- "LOCATION": f"rediss://{REDIS_USERNAME}@{REDIS_CACHE_HOST}:{REDIS_CACHE_PORT}/{REDIS_CACHE_DB}",
147
+ "LOCATION": "redis://localhost:6379/0",
160
148
  "OPTIONS": {
161
149
  "CLIENT_CLASS": "django_redis.client.DefaultClient",
162
150
  "PARSER_CLASS": "redis.connection._HiredisParser",
163
- "PASSWORD": REDIS_PASSWORD,
164
151
  },
165
152
  },
166
153
  }
167
- else:
168
- AZURE_REDIS_CREDENTIALS = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pulumi-django-azure
3
- Version: 1.0.33
3
+ Version: 1.0.35
4
4
  Summary: Simply deployment of Django on Azure with Pulumi
5
5
  Author-email: Maarten Ureel <maarten@youreal.eu>
6
6
  License-Expression: MIT
@@ -10,19 +10,20 @@ Classifier: Programming Language :: Python
10
10
  Classifier: Programming Language :: Python :: 3
11
11
  Requires-Python: <3.14,>=3.11
12
12
  Description-Content-Type: text/markdown
13
- Requires-Dist: azure-identity<2.0.0,>=1.21.0
14
- Requires-Dist: azure-keyvault-secrets<5.0.0,>=4.9.0
13
+ Requires-Dist: azure-identity<2.0.0,>=1.24.0
14
+ Requires-Dist: azure-keyvault-secrets<5.0.0,>=4.10.0
15
15
  Requires-Dist: azure-mgmt-cdn<14.0.0,>=13.1.1
16
- Requires-Dist: azure-mgmt-resource<24.0.0,>=23.3.0
17
- Requires-Dist: django<6.0,>=5.2
16
+ Requires-Dist: azure-mgmt-resource<25.0.0,>=24.0.0
17
+ Requires-Dist: django<6.0.0,>=5.2.5
18
18
  Requires-Dist: django-azure-communication-email<2.0.0,>=1.3.2
19
19
  Requires-Dist: django-environ<0.13.0,>=0.12.0
20
- Requires-Dist: django-redis<6.0.0,>=5.4.0
20
+ Requires-Dist: django-redis<7.0.0,>=6.0.0
21
21
  Requires-Dist: django-storages[azure]<2.0.0,>=1.14.6
22
- Requires-Dist: pulumi>=3.166.0
23
- Requires-Dist: pulumi-azure-native>=3.2.0
24
- Requires-Dist: pulumi-random>=4.18.2
25
- Requires-Dist: redis[hiredis]<7.0.0,>=6.0.0
22
+ Requires-Dist: pulumi>=3.189.0
23
+ Requires-Dist: pulumi-azure-native>=3.7.1
24
+ Requires-Dist: pulumi-random>=4.18.3
25
+ Requires-Dist: redis[hiredis]<7.0.0,>=6.4.0
26
+ Requires-Dist: tenacity<10.0.0,>=9.1.2
26
27
 
27
28
  # Pulumi Django Deployment
28
29
 
@@ -14,4 +14,5 @@ src/pulumi_django_azure.egg-info/requires.txt
14
14
  src/pulumi_django_azure.egg-info/top_level.txt
15
15
  src/pulumi_django_azure/management/commands/__init__.py
16
16
  src/pulumi_django_azure/management/commands/purge_cache.py
17
- src/pulumi_django_azure/management/commands/purge_cdn.py
17
+ src/pulumi_django_azure/management/commands/purge_cdn.py
18
+ src/pulumi_django_azure/management/commands/test_redis.py
@@ -0,0 +1,14 @@
1
+ azure-identity<2.0.0,>=1.24.0
2
+ azure-keyvault-secrets<5.0.0,>=4.10.0
3
+ azure-mgmt-cdn<14.0.0,>=13.1.1
4
+ azure-mgmt-resource<25.0.0,>=24.0.0
5
+ django<6.0.0,>=5.2.5
6
+ django-azure-communication-email<2.0.0,>=1.3.2
7
+ django-environ<0.13.0,>=0.12.0
8
+ django-redis<7.0.0,>=6.0.0
9
+ django-storages[azure]<2.0.0,>=1.14.6
10
+ pulumi>=3.189.0
11
+ pulumi-azure-native>=3.7.1
12
+ pulumi-random>=4.18.3
13
+ redis[hiredis]<7.0.0,>=6.4.0
14
+ tenacity<10.0.0,>=9.1.2
@@ -1,13 +0,0 @@
1
- azure-identity<2.0.0,>=1.21.0
2
- azure-keyvault-secrets<5.0.0,>=4.9.0
3
- azure-mgmt-cdn<14.0.0,>=13.1.1
4
- azure-mgmt-resource<24.0.0,>=23.3.0
5
- django<6.0,>=5.2
6
- django-azure-communication-email<2.0.0,>=1.3.2
7
- django-environ<0.13.0,>=0.12.0
8
- django-redis<6.0.0,>=5.4.0
9
- django-storages[azure]<2.0.0,>=1.14.6
10
- pulumi>=3.166.0
11
- pulumi-azure-native>=3.2.0
12
- pulumi-random>=4.18.2
13
- redis[hiredis]<7.0.0,>=6.0.0