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

Fix multiple argument run/passthru escaping and migrate most to use it #351

Merged
merged 12 commits into from
Aug 11, 2020
Merged
85 changes: 63 additions & 22 deletions src/_base/docker/image/console/root/lib/sidekick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,58 @@ TASKS="/lib/task"

task()
{
local task_file="${TASKS}/${1//:/\/}.sh"
local task_name="task_${1//:/_}"
local TASK_FILE="${TASKS}/${1//:/\/}.sh"
local TASK_NAME="task_${1//:/_}"

# shellcheck source=/dev/null
declare -F "$task_name" &>/dev/null || source "$task_file"
declare -F "${TASK_NAME}" &>/dev/null || source "${TASK_FILE}"

shift

$task_name "$@"
"${TASK_NAME}" "$@"
}

prompt()
{
if [ "${RUN_CWD}" != "$(pwd)" ]; then
RUN_CWD="$(pwd)"
echo -e "\\033[1m[\\033[0mdocker(console):$(pwd)\\033[1m]:\\033[0m"
echo -e "\\033[1m[\\033[0mdocker(console):$(pwd)\\033[1m]:\\033[0m" >&2
fi
}

run()
{
local COMMAND="$*"
local -r COMMAND_DEPRECATED="$*"
local -r COMMAND=("$@")
local DEPRECATED_MODE=no

if [[ "${COMMAND[0]}" = *" "* ]]; then
# echo "deprecated: support for passing multiple arguments in the following line will be removed in a future version" >&2
# echo "run '${COMMAND_DEPRECATED[*]}'" >&2
# echo "a future major version will only support:" >&2
# echo "run ${COMMAND_DEPRECATED[*]}" >&2
# echo >&2
DEPRECATED_MODE=yes
fi

if [ "$VERBOSE" = "no" ]; then
prompt

echo " > ${COMMAND[*]}"
setCommandIndicator "$INDICATOR_RUNNING"
prompt
if [ "${DEPRECATED_MODE}" = "yes" ]; then
echo " > ${COMMAND_DEPRECATED[*]}" >&2
setCommandIndicator "${INDICATOR_RUNNING}"
bash -c "${COMMAND_DEPRECATED[@]}" > /tmp/my127ws-stdout.txt 2> /tmp/my127ws-stderr.txt
else
echo " >$(printf ' %q' "${COMMAND[@]}")" >&2
setCommandIndicator "${INDICATOR_RUNNING}"
"${COMMAND[@]}" > /tmp/my127ws-stdout.txt 2> /tmp/my127ws-stderr.txt
fi

if ! bash -c "${COMMAND[@]}" > /tmp/my127ws-stdout.txt 2> /tmp/my127ws-stderr.txt; then
setCommandIndicator "$INDICATOR_ERROR"
# shellcheck disable=SC2181
if [ "$?" -gt 0 ]; then
setCommandIndicator "${INDICATOR_ERROR}"

if [ "$APP_BUILD" = "static" ]; then
if [ "${APP_BUILD}" = "static" ]; then
echo "Command failed. stdout:"
cat /tmp/my127ws-stdout.txt
echo
Expand All @@ -62,29 +82,50 @@ run()

exit 1
else
setCommandIndicator "$INDICATOR_SUCCESS"
setCommandIndicator "${INDICATOR_SUCCESS}"
fi
elif [ "${DEPRECATED_MODE}" = "yes" ]; then
passthru "${COMMAND_DEPRECATED[@]}"
else
passthru "${COMMAND[@]}"
fi
}

passthru()
{
prompt
local -r COMMAND_DEPRECATED="$*"
local -r COMMAND=("$@")
local DEPRECATED_MODE=no

if [[ "${COMMAND[0]}" = *" "* ]]; then
# echo "deprecated: support for passing multiple arguments in the following line will be removed in a future version" >&2
# echo "passthru '${COMMAND_DEPRECATED[*]}'" >&2
# echo "a future major version will only support:" >&2
# echo "passthru ${COMMAND_DEPRECATED[*]}" >&2
# echo >&2
DEPRECATED_MODE=yes
fi

echo -e "\\033[${INDICATOR_PASSTHRU}■\\033[0m > $*"
prompt

if ! bash -c "$@"; then
exit 1
if [ "${DEPRECATED_MODE}" = "yes" ]; then
echo -e "\\033[${INDICATOR_PASSTHRU}■\\033[0m > $*" >&2
if ! bash -e -c "${COMMAND_DEPRECATED[@]}"; then
exit 1
fi
else
echo -e "\\033[${INDICATOR_PASSTHRU}■\\033[0m >$(printf ' %q' "${COMMAND[@]}")" >&2
if ! "${COMMAND[@]}"; then
exit 1
fi
fi
}

setCommandIndicator()
{
echo -ne "\\033[1A";
echo -ne "\\033[$1"
echo -n "■"
echo -ne "\\033[0m"
echo -ne "\\033[1E";
echo -ne "\\033[1A" >&2
echo -ne "\\033[$1" >&2
echo -n "■" >&2
echo -ne "\\033[0m" >&2
echo -ne "\\033[1E" >&2
}
4 changes: 2 additions & 2 deletions src/_base/docker/image/console/root/lib/task/assets/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ function task_assets_apply()
if [ -f "$DATABASE_FILE" ]; then
passthru "pv --force $DATABASE_FILE | zcat - | $IMPORT_COMMAND"
else
task "install"
task install
fi
fi

for file in "/app/${ASSETS_DIR}/"*.files.{tgz,tar.gz}; do
[ -f "$file" ] || continue
run "tar -xvf ${file} -C /app"
run tar -xvf "${file}" -C /app
done
}
6 changes: 3 additions & 3 deletions src/_base/docker/image/console/root/lib/task/assets/dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ function task_assets_dump()
local ASSETS_DIR="${ASSETS_DIR:-tools/assets/development}"

if [ ! -d "/app/${ASSETS_DIR}" ]; then
run "mkdir -p /app/${ASSETS_DIR}"
run mkdir -p "/app/${ASSETS_DIR}"
fi

if [ "${DB_PLATFORM}" == "mysql" ]; then
run "mysqldump -h ${DB_HOST} -u ${DB_USER} -p${DB_PASS} ${DB_NAME} | gzip > /app/${ASSETS_DIR}/${DB_NAME}.sql.gz"
run "mysqldump -h '${DB_HOST}' -u '${DB_USER}' '-p${DB_PASS}' '${DB_NAME}' | gzip > '/app/${ASSETS_DIR}/${DB_NAME}.sql.gz'"
elif [ "${DB_PLATFORM}" == "postgres" ]; then
run "PGPASSWORD=$DB_PASS pg_dump -h ${DB_HOST} -U ${DB_USER} ${DB_NAME} | gzip > /app/${ASSETS_DIR}/${DB_NAME}.sql.gz"
PGPASSWORD="$DB_PASS" run "pg_dump -h '${DB_HOST}' -U '${DB_USER}' '${DB_NAME}' | gzip > '/app/${ASSETS_DIR}/${DB_NAME}.sql.gz'"
elif [ -n "${DB_PLATFORM}" ]; then
(>&2 echo "invalid database type")
exit 1
Expand Down
8 changes: 4 additions & 4 deletions src/_base/docker/image/console/root/lib/task/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
function task_build()
{
if [ ! -f /app/composer.json ]; then
task "skeleton:apply"
task skeleton:apply
fi

task "overlay:apply"
task overlay:apply

task "build:backend"
task "build:frontend"
task build:backend
task build:frontend
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

function task_composer_install()
{
passthru "composer install --no-interaction"
passthru composer install --no-interaction
}
6 changes: 3 additions & 3 deletions src/_base/docker/image/console/root/lib/task/init.sh.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

function task_init()
{
task "database:available"
task database:available

if ! db_hasSchema; then

task "assets:apply"
task assets:apply

{% for step in @('backend.init.steps') -%}
{{ step|raw }}
{% endfor %}

task "welcome"
task welcome

fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function task_install()
{
task "database:available"
task database:available

{% for step in @('backend.install.steps') -%}
{{ step|raw }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function task_migrate()
{
task "database:available"
task database:available

{% for step in @('backend.migrate.steps') -%}
{{ step|raw }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

function task_overlay_apply()
{
run "rsync --exclude='*.twig' --exclude='_twig' -a /home/build/application/overlay/ /app/"
run rsync --exclude='*.twig' --exclude='_twig' -a /home/build/application/overlay/ /app/
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function task_rabbitmq_vhosts() {
:
{% else %}
local rabbitmq_api_url="http://$RABBITMQ_HOST:$RABBITMQ_API_PORT/api"
task "http:wait" "$rabbitmq_api_url/index.html"
task http:wait "$rabbitmq_api_url/index.html"

{% for vhost in @('rabbitmq.vhosts') %}
curl -f -I -s -u "$RABBITMQ_USER:$RABBITMQ_PASSWORD" -X PUT "$rabbitmq_api_url/vhosts/{{ vhost }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

function task_skeleton_apply()
{
run "rsync --exclude='*.twig' --exclude='_twig' -a /home/build/application/skeleton/ /app/"
run rsync --exclude='*.twig' --exclude='_twig' -a /home/build/application/skeleton/ /app/
}
2 changes: 1 addition & 1 deletion src/_base/docker/image/console/root/lib/task/state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function task_state()
{
task "database:available"
task database:available

echo "Ready!"
}
12 changes: 6 additions & 6 deletions src/_base/harness/attributes/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ attributes.default:
build:
when: -f "composer.json"
steps:
- task "composer:install"
- task composer:install
install:
steps: []
init:
Expand All @@ -123,18 +123,18 @@ attributes.default:
steps:
- |
if [ "$APP_BUILD" == "static" ] && [ -f package-lock.json ]; then
run "npm clean-install"
run npm clean-install
else
run "npm install"
run npm install
fi
- |
if [ "$(jq ".scripts.build != null" < package.json)" != "false" ]; then
run "npm run build"
run npm run build
fi
- |
if [ "$APP_BUILD" == "static" ]; then
run "rm -rf node_modules/"
run "npm cache clean --force"
run rm -rf node_modules/
run npm cache clean --force
fi

git:
Expand Down
21 changes: 11 additions & 10 deletions src/_base/harness/config/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ command('db console'):
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru "docker-compose exec console bash -c 'mysql -h\"\$DB_HOST\" -u\"\$DB_USER\" -p\"\$DB_PASS\" \"\$DB_NAME\"'"
passthru docker-compose exec console mysql --host "${DB_HOST}" --user "${DB_USER}" "-p${DB_PASS}" "${DB_NAME}"

command('assets download'):
env:
AWS_ID: =@('aws.id')
AWS_KEY: =@('aws.key')
exec: |
#!bash(workspace:/)|@
passthru ws.aws s3 sync @('assets.remote') @('assets.local')
passthru ws.aws s3 sync '@('assets.remote')' '@('assets.local')'

command('assets upload'):
env:
AWS_ID: =@('aws.id')
AWS_KEY: =@('aws.key')
exec: |
#!bash(workspace:/)|@
passthru ws.aws s3 sync @('assets.local') @('assets.remote')
passthru ws.aws s3 sync '@('assets.local')' '@('assets.remote')'

command('feature docker-sync (on|off)'):
env:
Expand All @@ -135,37 +135,37 @@ command('frontend build'):
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru "docker-compose exec -u build console bash -i -c 'app build:frontend'"
passthru docker-compose exec -u build console app build:frontend

command('frontend watch'):
env:
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru "docker-compose exec -u build console bash -i -c 'cd @('frontend.path'); @('frontend.watch')'"
passthru docker-compose exec -u build --workdir '@('frontend.path')' console @('frontend.watch')

command('frontend console'):
env:
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru "docker-compose exec -u build console bash -i -c 'cd @('frontend.path'); bash'"
passthru docker-compose exec -u build console --workdir '@('frontend.path')' bash

command('port <service>'):
env:
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|=
passthru docker port $(docker-compose ps -q ={input.argument('service')})
passthru docker port "$(docker-compose ps -q ={input.argument('service')})"

command('service php-fpm restart'):
env:
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru ws install --step=prepare
passthru "docker-compose exec console bash -c 'cp -r /.my127ws/docker/image/console/root/usr/local/etc/php/conf.d/* /usr/local/etc/php/conf.d/'"
passthru "docker-compose exec php-fpm bash -c 'cp -r /.my127ws/docker/image/php-fpm/root/usr/local/etc/php/conf.d/* /usr/local/etc/php/conf.d/'"
passthru docker-compose exec console bash -c 'cp -r /.my127ws/docker/image/console/root/usr/local/etc/php/conf.d/* /usr/local/etc/php/conf.d/'
passthru docker-compose exec php-fpm bash -c 'cp -r /.my127ws/docker/image/php-fpm/root/usr/local/etc/php/conf.d/* /usr/local/etc/php/conf.d/'
passthru docker-compose exec php-fpm supervisorctl restart php-fpm

command('set <attribute> <value>'):
Expand Down Expand Up @@ -217,9 +217,10 @@ command('feature xdebug cli (on|off)'):
command('db import <database_file>'):
env:
COMPOSE_PROJECT_NAME: = @('namespace')
DATABASE_FILE: = input.argument('database_file')
exec: |
#!bash(workspace:/)|=
passthru "docker-compose exec -u build console bash -i -c 'app database:import ={input.argument('database_file')}'"
passthru docker-compose exec -u build console app database:import "$DATABASE_FILE"

command('harness update existing'):
env:
Expand Down
17 changes: 10 additions & 7 deletions src/_base/harness/config/events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ after('harness.install'): |
#!bash
ws enable

after('harness.refresh'): |
#!bash(workspace:/)|@
run docker-compose -p @('namespace') stop
passthru "docker-compose config --services | grep -v php-fpm | xargs docker-compose pull"
passthru "docker-compose config --services | grep -v cron | grep -v job-queue-consumer | grep -v jenkins-runner | xargs docker-compose build --pull"
passthru "docker-compose config --services | grep -v cron | grep -v job-queue-consumer | grep -v jenkins-runner | xargs docker-compose up -d"
run docker-compose -p @('namespace') up -d --build
after('harness.refresh'):
env:
andytson-inviqa marked this conversation as resolved.
Show resolved Hide resolved
COMPOSE_PROJECT: = @('namespace')
exec: |
#!bash(workspace:/)|@
run docker-compose stop
passthru "docker-compose config --services | grep -v php-fpm | xargs docker-compose pull"
passthru "docker-compose config --services | grep -v cron | grep -v job-queue-consumer | grep -v jenkins-runner | xargs docker-compose build --pull"
passthru "docker-compose config --services | grep -v cron | grep -v job-queue-consumer | grep -v jenkins-runner | xargs docker-compose up -d"
run docker-compose up -d --build
Loading