#!/bin/bash log() { printf '%s\n' "$*"; } error() { log "ERROR: $*" >&2; } fatal() { error "$@"; exit 1; } run_symfony_app_container() { set -e trap "set +e" RETURN local symfony_version=${1} local php_version=${2} local image_tag=${3} cd "${SCRIPT_DIR}/.." # Create temporary directory local project_dir=$(mktemp -d) local run_id=$(date +%s) # Defer cleanup on exit trap_add "rm -rf ${project_dir}" EXIT # Create dummy Symfony project symfony new --dir="${project_dir}" --version="${symfony_version}" --php="${php_version}" --webapp 1>&2 cat > "${project_dir}/Dockerfile" < "${project_dir}/.dockerignore" <&2 local http_port=$(get_unused_port) local container_name="symfony-test-${image_tag}-${run_id}" # Start project image docker run \ -d \ --rm \ -p "${http_port}:8080" \ --name "${container_name}" \ "${image_name}" \ 1>&2 # Wait container is healthy with a timeout local duration=0 local last_tick=$(date +%s) local container_status=$(docker inspect -f '{{.State.Health.Status}}' ${container_name}) log "Waiting for container to become healthy..." 1>&2 until [ "${container_status}" == "healthy" ]; do sleep 1 local now=$(date +%s) duration=$(( ${duration} + ( ${now} - ${last_tick} ) )) if [ ${duration} -gt 60 ]; then fail "Container did not become healthy before timeout !" fi last_tick=${now} container_status=$(docker inspect -f '{{.State.Health.Status}}' ${container_name}) done echo "${container_name}" } get_unused_port() { set -e trap "set +e" RETURN local lport=32768; local uport=60999; while true; do local mport=$[${lport} + (${RANDOM} % $uport)]; (echo "" >/dev/tcp/127.0.0.1/${mport}) >/dev/null 2>&1 if [ $? -ne 0 ]; then echo ${mport}; return 0; fi done } # From https://stackoverflow.com/a/7287873 trap_add() { local trap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error" for trap_add_name in "$@"; do trap -- "$( # helper fn to get existing trap command from output # of trap -p extract_trap_cmd() { printf '%s\n' "$3"; } # print existing trap command with newline eval "extract_trap_cmd $(trap -p "${trap_add_name}")" # print the new trap command printf '%s\n' "${trap_add_cmd}" )" "${trap_add_name}" \ || fatal "unable to add to trap ${trap_add_name}" done } declare -f -t trap_add