From d23f8163da7ab77ef52d73fe284b165609eb4eef Mon Sep 17 00:00:00 2001 From: Randy Fay Date: Sat, 20 Aug 2022 15:23:17 -0600 Subject: [PATCH] Use ddev debug get-volume-db-version to get existing volume, for #24 (#25) * Use ddev debug get-volume-db-version to get existing volume, for #24 * Use newer techniques for comparing db versions, remove control chars * Make hooks multiline * Special handling for a couple of php extensions --- install.yaml | 80 +++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/install.yaml b/install.yaml index eb9d4e9..44fdc8a 100644 --- a/install.yaml +++ b/install.yaml @@ -75,32 +75,21 @@ pre_install_actions: # We don't want to allow new config if they're changing database types from what is currently there. - | #ddev-nodisplay - #todo: This simplistic approach doesn't work for postgres; volume name is different - #and the database type file is different too - if docker volume ls | grep ${DDEV_PROJECT}-mariadb && docker run --rm -w /db -v ${DDEV_PROJECT}-mariadb:/db busybox cat db_mariadb_version.txt; then - export current_db_version="$(docker run --rm -w /db -v ${DDEV_PROJECT}-mariadb:/db busybox cat db_mariadb_version.txt)" - if [ -z "${current_db_version}" ]; then - echo "something went wrong, current_db_version is empty" && false - fi - {{ $dbheader := index (split ":" .platformapp.relationships.database) "_0" }} - {{ $dbtype := replace "postgresql" "postgres" (get (get .services $dbheader) "type") }} - # echo "dbheader={{$dbheader}} dbtype={{$dbtype}} " - export upstream_db="{{ regexReplaceAll "oracle-" (regexReplaceAll ":.*$" $dbtype "") "" }}:{{ regexReplaceAll "^.*:" $dbtype "" }}" - echo "Current db is ${current_db_version/_/:} (${current_db_version})" - echo "New db is ${upstream_db}" - if [ "${current_db_version/_/:}" != "${upstream_db}" ]; then - printf "There is an existing database in this project that doesn\'t match the upstream database type.\nPlease back up this database and then \"ddev delete\" so the new database can be created.\n" - false - else - echo "database versions match: ${current_db_version/_/:}=${upstream_db}" - fi + {{ $dbheader := index (split ":" .platformapp.relationships.database) "_0" }} + {{ $dbtype := replace "postgresql" "postgres" (get (get .services $dbheader) "type") }} + export upstream_db="{{ regexReplaceAll "oracle-" (regexReplaceAll ":.*$" $dbtype "") "" }}:{{ regexReplaceAll "^.*:" $dbtype "" }}" + # Unfortunate sed to remove color escape sequences from ddev debug output + current_db_version="$(ddev debug get-volume-db-version | sed -r 's/\x1b\[[0-9;]*m?//g')" + if [ "${current_db_version}" != "" ] && [ ">${upstream_db}<" != ">${current_db_version}<" ]; then + printf "There is an existing database in this project that doesn\'t match the upstream database type.\n Please use 'ddev delete' to delete the existing database and retry, or try 'ddev debug migrate-database ${upstream_db}' to migrate the database.\n" + false fi # Write a config.platformsh.yaml based on calculated values, php version, database, docroot - | #ddev-nodisplay # set -x - platform_routes=$(cat <<-ENDROUTES + platform_routes=$(cat <<-"ENDROUTES" { "${DDEV_PRIMARY_URL}": { "primary": true, @@ -113,6 +102,9 @@ pre_install_actions: } ENDROUTES ) + PLATFORM_ROUTES="$( if base64 --version >/dev/null 2>&1; then echo -n ${platform_routes} | base64 -w0; else echo -n ${platform_routes} | base64; fi)" + PLATFORM_PROJECT_ENTROPY="$(echo $RANDOM | shasum -a 256 | awk '{print $1}')" + if [ -f .ddev/config.platformsh.yaml ] && ! grep '#ddev-generated' .ddev/config.platformsh.yaml; then echo "Existing .ddev/config.platformsh.yaml does not have #ddev-generated, so can't be updated" exit 2 @@ -134,22 +126,34 @@ pre_install_actions: # TODO: Review which of these matters and which can be dummied up - "PLATFORM_MOUNTS={{ range $key, $value := .platformapp.mounts }}{{ $key }} {{ end }}" - "PLATFORM_APP_DIR=/var/www/html" - - PLATFORM_PROJECT_ENTROPY=$(echo $RANDOM | shasum -a 256 | awk '{print $1}') + - "PLATFORM_PROJECT_ENTROPY=${PLATFORM_PROJECT_ENTROPY}" # Consider commit hash for PLATFORM_TREE_ID - "PLATFORM_TREE_ID=2dc356f2fea13ef683f9adc5fc5bd28e05ad992a" - "PLATFORM_DIR=/var/www/html" - - "PLATFORM_ROUTES=$( if base64 --version >/dev/null 2>&1; then echo -n ${platform_routes} | base64 -w0; else echo -n ${platform_routes} | base64; fi)" - - PLATFORM_VARIABLES=e30= - - PATH=$PATH:/var/www/html/.global/bin + - "PLATFORM_ROUTES=${PLATFORM_ROUTES}" + - "PLATFORM_VARIABLES=e30=" + - "PATH=$PATH:/var/www/html/.global/bin" + + # Provide all PHP extensions but blackfire, which has different pattern (blackfire-php) and is already installed + # and pdo_pgsql, which is already installed + {{ $phpversion := trimPrefix "php:" .platformapp.type }} + {{ $phpextensions := without .platformapp.runtime.extensions "blackfire" "pdo_pgsql" "sodium" }} + webimage_extra_packages:{{range $extension := $phpextensions }} + - php{{$phpversion}}-{{$extension}}{{end}} + {{ if has "sodium" .platformapp.runtime.extensions }} + - php-sodium + {{end}} - # Provide all extensions but blackfire, which has different pattern (blackfire-php) and is already installed - webimage_extra_packages:{{range $extension := .platformapp.runtime.extensions}}{{if ne $extension "blackfire"}} - - php-{{$extension}}{{end}}{{end}} # Add pip only if we have python3 dependencies {{ if .platformapp.dependencies.python3 }} - python3-pip {{ end }} - + EOF + + # Because "ENDOFHOOKS" is quoted here, no variable expansion occurs + # so everything is left alone. + cat <<-"ENDOFHOOKS" >>.ddev/config.platformsh.yaml + hooks: post-start: {{ if eq .platformapp.build.flavor "composer" }} @@ -158,24 +162,30 @@ pre_install_actions: {{ if .platformapp.hooks.build }} # platformsh build hooks - - exec: '{{ trimAll "\n" .platformapp.hooks.build | replace "\n\n" "\n" | splitList "\n" | join ` && ` }}' + {{ $noblanks := regexReplaceAll "\n\n*" .platformapp.hooks.build "\n" }} + - exec: | + {{ indent 6 $noblanks }} {{ end }} - + {{ if .platformapp.hooks.deploy }} # platformsh deploy hooks - - exec: '{{ trimAll "\n" .platformapp.hooks.deploy | replace "\n\n" "\n" | splitList "\n" | join ` && ` }}' + {{ $noblanks := regexReplaceAll "\n\n*" .platformapp.hooks.deploy "\n" }} + - exec: | + {{ indent 6 $noblanks }} {{ end }} - + {{ if .platformapp.hooks.post_deploy }} # platformsh post_deploy hooks - - exec: '{{ trimAll "\n" .platformapp.hooks.post_deploy | replace "\n\n" "\n" | splitList "\n" | join ` && ` }}' + {{ $noblanks := regexReplaceAll "\n\n*" .platformapp.hooks.post_deploy "\n" }} + - exec: | + {{ indent 6 $noblanks }} {{ end }} - # Enable blackfire php extension if listed in platform.app.yaml runtime + {{ if has "blackfire" .platformapp.runtime.extensions }} - exec: phpenmod blackfire {{ end }} - EOF + ENDOFHOOKS project_files: - web-build/Dockerfile.platformsh