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

Port various docker-related changes from php harness #73

Merged
merged 1 commit into from
Jun 8, 2023
Merged
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
4 changes: 2 additions & 2 deletions .ci/test
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ function check_environment_started()
project="$(git log -n 1 --pretty=format:'%H')"
fi

if [ "$(docker-compose -p "$project" ps | grep -v "lighthouse" | grep -c Exit)" -gt 0 ]; then
if [ "$(docker compose -p "$project" ps --all | grep -v "lighthouse" | grep -c Exit)" -gt 0 ]; then
echo 'Some containers failed to start'
docker-compose -p "$project" ps
docker compose -p "$project" ps --all
return 1
fi
)
Expand Down
9 changes: 4 additions & 5 deletions docker/image/console/root/lib/task/assets/apply.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/bin/bash

function task_assets_apply()
{
(
local ASSETS_DIR="${ASSETS_DIR:-tools/assets/development}"
local IMPORT_COMMAND=()
local PRE_COMMAND=()

if [ "${DB_PLATFORM}" == "mysql" ]; then
IMPORT_COMMAND=(mysql -h "$DB_HOST" -u "${DB_ADMIN_USER:-$DB_USER}" "-p${DB_ROOT_PASS:-${DB_ADMIN_PASS:-$DB_PASS}}" "$DB_NAME")
elif [ "${DB_PLATFORM}" == "postgres" ]; then
PRE_COMMAND=("PGPASSWORD=$DB_PASS")
export PGPASSWORD="$DB_PASS"
IMPORT_COMMAND=(psql -h "$DB_HOST" -U "$DB_USER" "$DB_NAME")
elif [ -n "${DB_PLATFORM}" ]; then
(>&2 echo "invalid database type")
Expand All @@ -25,7 +24,7 @@ function task_assets_apply()
fi

if [ -f "$DATABASE_FILE" ]; then
"${PRE_COMMAND[@]}" passthru "pv --force '$DATABASE_FILE' | zcat - | $(printf ' %q' "${IMPORT_COMMAND[@]}")"
passthru "pv --force '$DATABASE_FILE' | zcat - | $(printf ' %q' "${IMPORT_COMMAND[@]}")"
else
task install
fi
Expand All @@ -35,4 +34,4 @@ function task_assets_apply()
[ -f "$file" ] || continue
run tar -xvf "${file}" -C /app
done
}
)
6 changes: 5 additions & 1 deletion harness/attributes/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ attributes.default:
buildkit:
enabled: true
compose:
bin: 'docker-compose'
host_volume_options: "= ':' ~ (@('delegated-volumes') ? 'delegated' : 'cached')"
# If using gitops helm chart repositories, it's recommended to put this configuration
# in the values.yml in there now instead of project application repositories
Expand Down Expand Up @@ -159,7 +160,10 @@ attributes.default:
database:
# possible platforms are mysql, postgres or ~ for none
platform: mysql
platform_version: 8.0
platform_version: >
= @('database.platform') == 'mysql' ? '8.0'
: @('database.platform') == 'postgres' ? '9.6'
: null
host: mysql
port: 3306
user: app
Expand Down
9 changes: 6 additions & 3 deletions harness/config/build.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
command('app build'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
exec: |
#!bash(workspace:/)|@
ws external-images pull

passthru docker-compose build
passthru $COMPOSE_BIN build

command('app build <service>'):
env:
SERVICE: = input.argument('service')
COMPOSE_BIN: = @('docker.compose.bin')
SERVICE: = input.argument('service')
exec: |
#!bash(workspace:/)|@
passthru docker-compose build "${SERVICE}"
passthru $COMPOSE_BIN build "${SERVICE}"
13 changes: 9 additions & 4 deletions harness/config/cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ function('built_images', [services]): |
$allImages = explode("\n", shell_exec('docker image ls -a --format \'{{ print .Repository ":" .Tag }}\''));

# workspace commands don't allow non-string types
= join(' ', array_intersect($builtImages, $allImages));
= join("\n", array_intersect($builtImages, $allImages));

command('built-images ls'):
env:
IMAGES: = built_images(docker_service_images(@('docker.compose.bin')))
exec: |
#!bash
echo "$IMAGES"

command('cleanup built-images'):
env:
BUILD_LABEL: = @('namespace') ~ ':' ~ @('app.version')
IMAGES: = built_images(docker_service_images())
exec: |
#!bash
IMAGES=($IMAGES)
IMAGES=($(ws built-images ls))
if [ "${#IMAGES[@]}" -gt 0 ]; then
run docker image rm --force -- "${IMAGES[@]}"
fi
run docker image prune --force --filter=label=build="$BUILD_LABEL"
[ -z "$(docker builder 2>&1)" ] || run docker builder prune --force --filter=label=build="$BUILD_LABEL"

33 changes: 23 additions & 10 deletions harness/config/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ command('enable'):
APP_MODE: = @('app.mode')
NAMESPACE: = @('namespace')
HAS_ASSETS: = boolToString(@('aws.bucket') !== null and @('aws.bucket') !== '')
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
COMPOSE_DOCKER_CLI_BUILD: = @('docker.buildkit.enabled') ? '1':'0'
DOCKER_BUILDKIT: = @('docker.buildkit.enabled') ? '1':'0'
Expand All @@ -21,6 +22,7 @@ command('enable console'):
APP_MODE: = @('app.mode')
NAMESPACE: = @('namespace')
HAS_ASSETS: = boolToString(@('aws.bucket') !== null and @('aws.bucket'))
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
COMPOSE_DOCKER_CLI_BUILD: = @('docker.buildkit.enabled') ? '1':'0'
DOCKER_BUILDKIT: = @('docker.buildkit.enabled') ? '1':'0'
Expand All @@ -33,6 +35,7 @@ command('disable'):
env:
USE_MUTAGEN: = boolToString(@('host.os') == 'darwin' and @('mutagen'))
NAMESPACE: = @('namespace')
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)
Expand All @@ -42,6 +45,7 @@ command('destroy [--all]'):
env:
USE_MUTAGEN: = boolToString(@('host.os') == 'darwin' and @('mutagen'))
NAMESPACE: = @('namespace')
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
DESTROY_ALL: = boolToString(input.option('all'))
exec: |
Expand All @@ -52,14 +56,15 @@ command('rebuild'):
env:
USE_MUTAGEN: = boolToString(@('host.os') == 'darwin' and @('mutagen'))
NAMESPACE: = @('namespace')
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
source .my127ws/harness/scripts/rebuild.sh

command('networks external'):
env:
NETWORKS: = get_docker_external_networks()
NETWORKS: = get_docker_external_networks(@('docker.compose.bin'))
exec: |
#!bash(workspace:/)
for NETWORK in ${NETWORKS}; do
Expand All @@ -70,35 +75,39 @@ command('networks external'):

command('exec %'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|=
if [ -t 0 ] && [ -t 1 ] ; then
docker-compose exec -u build console ={ input.argument('%') }
$COMPOSE_BIN exec -u build console ={ input.argument('%') }
else
docker-compose exec -T -u build console ={ input.argument('%') }
$COMPOSE_BIN exec -T -u build console ={ input.argument('%') }
fi

command('logs %'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(harness:/)|=
docker-compose logs ={input.argument('%')}
$COMPOSE_BIN logs ={input.argument('%')}

command('ps'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
docker-compose ps
$COMPOSE_BIN ps

command('console'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru docker-compose exec -u build console bash
passthru $COMPOSE_BIN exec -u build console bash

command('composer %'):
exec: |
Expand All @@ -111,10 +120,11 @@ command('db-console'): |

command('db console'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
exec: |
#!bash(workspace:/)|@
passthru "docker-compose exec console bash -c 'mysql --host \"\$DB_HOST\" --user \"\$DB_USER\" -p\"\$DB_PASS\" \"\$DB_NAME\"'"
passthru "$COMPOSE_BIN exec console bash -c 'mysql --host \"\$DB_HOST\" --user \"\$DB_USER\" -p\"\$DB_PASS\" \"\$DB_NAME\"'"

command('assets download'):
env:
Expand All @@ -138,10 +148,11 @@ command('assets upload'):

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

command('set <attribute> <value>'):
env:
Expand All @@ -165,11 +176,12 @@ command('set <attribute> <value>'):

command('db import <database_file>'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
DATABASE_FILE: = input.argument('database_file')
exec: |
#!bash(workspace:/)|=
passthru docker-compose exec -u build console app database:import "$DATABASE_FILE"
passthru $COMPOSE_BIN exec -u build console app database:import "$DATABASE_FILE"

command('harness update existing'):
env:
Expand Down Expand Up @@ -217,8 +229,9 @@ command('generate token <length>'):

command('lighthouse [--with-results]'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT_NAME: = @('namespace')
OUTPUT_RESULTS: = boolToString(input.option('with-results'))
exec: |
#!bash(workspace:/)|@
passthru docker-compose run --rm lighthouse bash -i /app/run.sh
passthru $COMPOSE_BIN run -T --rm lighthouse bash -i /app/run.sh
7 changes: 3 additions & 4 deletions harness/config/events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ after('harness.install'): |

after('harness.refresh'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
COMPOSE_PROJECT: = @('namespace')
exec: |
#!bash(workspace:/)|@
run docker-compose stop
run $COMPOSE_BIN stop
ws external-images pull
passthru "docker-compose config --services | grep -v cron | grep -v jenkins-runner | grep -v job-queue-consumer | xargs docker-compose build"
passthru "docker-compose config --services | grep -v cron | grep -v jenkins-runner | grep -v job-queue-consumer | xargs docker-compose up -d"
run docker-compose up -d --build
run $COMPOSE_BIN up -d --build
7 changes: 4 additions & 3 deletions harness/config/external-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function('external_images', [services]): |

command('external-images config [--skip-exists] [<service>]'):
env:
IMAGES: = external_images(docker_service_images(input.argument('service')))
IMAGES: = external_images(docker_service_images(@('docker.compose.bin'), input.argument('service')))
SKIP_EXISTS: "= input.option('skip-exists') ? 1 : 0"
exec: |
#!php
Expand All @@ -47,6 +47,7 @@ command('external-images config [--skip-exists] [<service>]'):

command('external-images pull [<service>]'):
env:
COMPOSE_BIN: = @('docker.compose.bin')
SERVICE: = input.argument('service')
exec: |
#!bash(workspace:/)|@
Expand All @@ -59,11 +60,11 @@ command('external-images pull [<service>]'):
CONF_ARGS+=("$SERVICE")
fi

passthru "ws external-images config $(printf '%q ' "${CONF_ARGS[@]}") | docker-compose -f - pull"
passthru "ws external-images config $(printf '%q ' "${CONF_ARGS[@]}") | $COMPOSE_BIN -f - pull"

command('external-images ls [--all]'):
env:
IMAGES: = external_images(docker_service_images())
IMAGES: = external_images(docker_service_images(@('docker.compose.bin')))
SHOW_REMOTE: "= input.option('all') ? 1 : 0"
exec: |
#!php
Expand Down
21 changes: 16 additions & 5 deletions harness/config/functions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function('filter_local_services', [services]): |
case 'enabled':
case 'environment':
case 'environment_dynamic':
case 'extends':
case 'image':
case 'resources':
$filteredService[$key] = $value;
Expand All @@ -96,14 +97,18 @@ function('filter_local_services', [services]): |
}
= $filteredServices;

function('filter_empty', [array_input]): |
#!php
= array_filter($array_input);

function('flatten', [array_input]): |
#!php
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array_input));
= iterator_to_array($iterator, false);

function('get_docker_external_networks'): |
function('get_docker_external_networks', [compose_bin]): |
#!php
$configRaw = shell_exec('docker-compose config');
$configRaw = shell_exec($compose_bin . ' config');
if ($configRaw === null) {
exit(1);
}
Expand All @@ -122,9 +127,9 @@ function('get_docker_external_networks'): |
}
= join(" ", $externalNetworks);

function('docker_service_images', [filterService]): |
function('docker_service_images', [compose_bin, filterService]): |
#!php
$configRaw = shell_exec('docker-compose config');
$configRaw = shell_exec($compose_bin . ' config');
if ($configRaw === null) {
exit(1);
}
Expand Down Expand Up @@ -243,8 +248,14 @@ function('replace', [haystack, needle, replacement]): |

function('template_key_value', [template, key_value]): |
#!php
if (empty($key_value)) {
return [];
}
$output = [];
foreach ($key_value as $key => $value) {
if ($value === null) {
continue;
}
$output[str_replace('{{key}}', $key, $template)] = $value;
}
= $output;
Expand All @@ -264,4 +275,4 @@ function('boolToString', [value]): |
case false: return 'no';
}
echo 'error: unknown boolean "'.$value.'"'."\n";
exit(1);
exit(1);
Loading