Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add external_links to all services to enable access to domains in the web container #22

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose.external_links.yaml
37 changes: 37 additions & 0 deletions commands/host/propagate-external-links
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/python

## Description: Generates .ddev/docker-compose.external_links.yaml and adds external_links to all services to enable access to domains in the web container.
## Usage: propagate-external-links
## Example: "ddev propagate-external-links"

import subprocess
import yaml

# Get processed docker compose config.
command = ("ddev", "debug", "compose-config")
result = subprocess.run(command, capture_output=True, text=True)
output = result.stdout.strip()
compose = yaml.safe_load(output)

# Get list of services, but 'web'.
services_list = compose['services'].keys() - ['web']

# Get 'web' service external links and transform to link to 'web' instead of 'ddev-router'.
# We prefer 'web' because it works equally and it is a self contained solution (the router
# may not be available).
web_external_links = compose['services']['web']['external_links']
web_external_links_to_web = [x.replace('ddev-router:', 'web:') for x in web_external_links]

# Generate a compose file that declares the above external_links for all services.
# We need to filter the list to avoid duplicate items.
data = dict()
data['services'] = dict()
for service in services_list:
service_current_external_links = compose['services'][service].get('external_links', [])
service_extra_external_links = [x for x in web_external_links_to_web if x not in service_current_external_links]

data['services'][service] = dict()
data['services'][service]['external_links'] = service_extra_external_links

with open('.ddev/docker-compose.external_links.yaml', 'w') as file:
yaml.dump(data, file)
12 changes: 9 additions & 3 deletions config.aljibe.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#ddev-generated
# Custom config to alter composer executable in case Drupal is defining a custom version.
# See https://github.com/ddev/ddev/issues/6602 for more details.
hooks:
fail_on_hook_fail: True
hooks:
pre-start:
- exec-host: |
if pip3 show PyYAML &>/dev/null; then
ddev propagate-external-links
else
echo "[ERROR] Aljibe requires python with yaml extension."
fi
Loading