Skip to content

Commit

Permalink
Improve $PLATFORM_ROUTES to include at least route id and multiple ro…
Browse files Browse the repository at this point in the history
…utes, for #95, followup to #47 (#103)
  • Loading branch information
rfay authored Aug 25, 2023
1 parent 143a967 commit 85cc00d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ If you change your `.platform.app.yaml` or something in your `.platform` directo
* Oracle MySQL
* Postgresql
* Redis
* Redis-persistent
* Memcached
* ElasticSearch
* Provides the following [Platform.sh-provided environmental variables](https://docs.platform.sh/development/variables/use-variables.html#use-platformsh-provided-variables):
Expand Down
46 changes: 28 additions & 18 deletions install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ project_files:
- platformsh/generate_elasticsearch_relationship.sh
- platformsh/generate_memcached_relationship.sh
- platformsh/generate_redis_relationship.sh
- platformsh/generate_redis-persistent_relationship.sh
- platformsh/generate_route.sh

global_files:
- commands/web/platform
Expand All @@ -17,7 +19,7 @@ pre_install_actions:
- |
#ddev-nodisplay
#ddev-description:Checking DDEV version
(ddev debug capabilities | grep web-start-hooks >/dev/null) || (echo "Please upgrade DDEV to v1.21.5+ for appropriate capabilities" && false)
(ddev debug capabilities | grep multiple-upload-dirs >/dev/null) || (echo "Please upgrade DDEV to v1.22+ for appropriate capabilities" && false)
- |
#ddev-nodisplay
Expand Down Expand Up @@ -140,25 +142,32 @@ post_install_actions:
#ddev-nodisplay
#ddev-description:Installing dependencies and generating needed environment variables
# set -x
platform_routes=$(cat <<-ENDROUTES
{
"${DDEV_PRIMARY_URL}/": {
"primary": true,
"id": null,
"production_url": "${DDEV_PRIMARY_URL}/",
"attributes": {},
"upstream": "drupal",
"type": "upstream",
"original_url": "https://{default}/" }
}
ENDROUTES
)
#set -x
BASE64_ENCODE="docker run -i --rm ddev/ddev-utilities base64 -w 0"
BASE64_DECODE="docker run -i --rm ddev/ddev-utilities base64 -d"
PLATFORM_ROUTES="$(echo -n ${platform_routes} | ${BASE64_ENCODE})"
PLATFORM_PROJECT_ENTROPY="$(echo $RANDOM | docker run -i --rm ddev/ddev-utilities shasum -a 256 | awk '{print $1}')"
routes=()
#set -x
#echo {{ .routes }}
{{ range $k, $v := .routes }}
#echo key={{ $k }} val={{ $v }}
# Get a proper id value
{{ $id := "" }}
{{ if $v.id }}
{{ $id = $v.id }}
{{ end }}
r=$(./platformsh/generate_route.sh "${DDEV_PRIMARY_URL}/" '{{ $id }}' '{{ $v.production_url }}' '{{ $v.upstream }}' '{{ $v.type }}' '{{ $v.original_url }}' | ${BASE64_ENCODE})
#echo "r=$r"
routes+=(${r})
{{ end }}
set +x
#echo routes=$routes
#echo "Number of routes=${#routes[@]}"
#set -x
PLATFORM_ROUTES="$((echo '{'; for r in ${routes[@]::${#routes[@]}-1}; do echo $r | ${BASE64_DECODE}; printf ', \n'; done; echo ${routes[@]: -1:1} | ${BASE64_DECODE}; echo ' }') | ${BASE64_ENCODE})"
{{/* Handling services relationships */}}
export relationships=()
{{ range $relationship_name, $relationship_def := .platformapp.relationships }}
Expand All @@ -176,7 +185,7 @@ post_install_actions:
{{ $supported_db_types := list "postgres" "mysql" "mariadb" }}
{{/* For supported services, key is the service name and value is the corresponding add-on to install if applicable (nil if not needed) */}}
{{ $supported_services := dict "redis" "ddev/ddev-redis" "memcached" "ddev/ddev-memcached" "elasticsearch" "ddev/ddev-elasticsearch" }}
{{ $supported_services := dict "redis" "ddev/ddev-redis" "redis-persistent" "ddev/ddev-redis" "memcached" "ddev/ddev-memcached" "elasticsearch" "ddev/ddev-elasticsearch" }}
{{ if $supported_db_types | has $service_def.type }}
{{/* Databases */}}
Expand Down Expand Up @@ -301,4 +310,5 @@ post_install_actions:
yaml_read_files:
platformapp: .platform.app.yaml
services: .platform/services.yaml
routes: .platform/routes.yaml

30 changes: 30 additions & 0 deletions platformsh/generate_redis-persistent_relationship.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

#ddev-generated

export relationshipname=$1

read -r -d '' redis_stanza <<REDIS_EOF
"${relationshipname}": [
{
"username": null,
"scheme": "redis",
"service": "cache",
"fragment": null,
"ip": "255.255.255.255",
"hostname": "redis",
"public": false,
"cluster": "ddev-dummy-cluster",
"host": "redis",
"rel": "redis",
"query": {},
"path": null,
"password": null,
"type": "redis:6.0",
"port": 6379,
"host_mapped": false
}
]
REDIS_EOF

printf "$redis_stanza"
39 changes: 39 additions & 0 deletions platformsh/generate_route.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

#ddev-generated

# Create a stanza for $PLATFORM_ROUTES

# Args:
# route (like https://{default}/)
# id
# production_url
# upstream
# type
# original_url

export route=$1
export id=$2
export production_url=$3
export upstream=$4
export type=$5
export original_url=$6

idline='"id": null'
if [ ! -z ${id} ]; then
idline="\"id\": \"${id}\""
fi

read -r -d '' route_stanza <<ROUTE_EOF
"${route}": {
"primary": true,
${idline},
"production_url": "${production_url}",
"attributes": {},
"upstream": "${upstream}",
"type": "${type}",
"original_url": "${original_url}"
}
ROUTE_EOF

printf "${route_stanza}"
10 changes: 8 additions & 2 deletions tests/drupal9.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ teardown() {
for source in $PROJECT_SOURCE ddev/ddev-platformsh; do
per_test_setup

ddev exec drush cr

run curl -L -s http://${PROJNAME}.ddev.site/
assert_output --partial "this is a test of ddev-platformsh drupal9"

run ddev exec -s db 'echo ${DDEV_DATABASE}'
assert_output "mariadb:10.4"
run ddev exec "php --version | awk 'NR==1 { sub(/\.[0-9]+$/, \"\", \$2); print \$2 }'"
Expand All @@ -26,10 +31,11 @@ teardown() {
run jq -r .raw.docroot <describe.json
assert_output "web"

assert_equal $(ddev exec 'echo $PLATFORM_ROUTES | base64 -d | jq -r "keys[0]"') "https://${PROJNAME}.ddev.site/"
assert_equal $(ddev exec 'echo $PLATFORM_ROUTES | base64 -d | jq -r .[].production_url') "https://${PROJNAME}.ddev.site/"
assert_equal "$(ddev exec 'echo $PLATFORM_ROUTES | base64 -d | jq -r "keys[0]"')" "https://${PROJNAME}.ddev.site/"
ddev exec 'echo $PLATFORM_RELATIONSHIPS | base64 -d' >relationships.json
echo "# PLATFORM_RELATIONSHIPS=$(cat relationships.json)" >&3
ddev exec 'echo $PLATFORM_ROUTES | base64 -d' >routes.json
echo "# PLATFORM_ROUTES=$(cat routes.json)" >&3

assert_equal "$(jq -r .database[0].type <relationships.json)" "mariadb:10.4"
assert_equal "$(jq -r .database[0].username <relationships.json)" "db"
Expand Down
4 changes: 3 additions & 1 deletion tests/per_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ per_test_setup() {
rm -rf ${TESTDIR} && mkdir -p ${TESTDIR} && cd ${TESTDIR}
curl -sfL -o /tmp/testtemplate.tgz "https://github.com/platformsh-templates/${template}/tarball/master"
tar -zxf /tmp/testtemplate.tgz --strip-components=1
# If the template happens to have a .ddev directory, remove as it can break things
rm -rf .ddev
# Start with bogus settings so we know we got the right stuff when testing
ddev config --project-name=${PROJNAME} --php-version=5.6 --database=mariadb:10.1 --docroot=x --create-docroot --project-type=php --web-environment-add=PLATFORMSH_CLI_TOKEN=notokenrightnow,PLATFORM_PROJECT=notyet,PLATFORM_ENVIRONMENT=notyet
echo "# doing ddev get $source with template ${template} PROJNAME=${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ${PROJECT_SOURCE}
echo "# doing ddev restart with template ${template} PROJNAME=${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev restart >/dev/null
if [ -f ${PROJECT_SOURCE}/tests/testdata/${template}/db.sql.gz ]; then
ddev import-db --src=${PROJECT_SOURCE}/tests/testdata/${template}/db.sql.gz
ddev import-db --file=${PROJECT_SOURCE}/tests/testdata/${template}/db.sql.gz
fi
}

Expand Down
Binary file added tests/testdata/drupal9/db.sql.gz
Binary file not shown.

0 comments on commit 85cc00d

Please sign in to comment.