From 24d80597b5d91f8e8eba81c2839303ae4fbf8586 Mon Sep 17 00:00:00 2001 From: Mike Pirog Date: Tue, 5 Sep 2023 13:51:00 -0400 Subject: [PATCH] update event tests for new v4 stuff and general improvements --- examples/events/.lando.yml | 46 +++++++++++++++++++++++-------- examples/events/README.md | 18 +++++++++++- examples/events/compose.yml | 2 ++ lib/bootstrap.js | 3 ++ plugins/lando-events/app.js | 1 - plugins/lando-events/lib/utils.js | 33 +++++++++++++++------- 6 files changed, 79 insertions(+), 24 deletions(-) diff --git a/examples/events/.lando.yml b/examples/events/.lando.yml index a714a505b..54a36dfc7 100644 --- a/examples/events/.lando.yml +++ b/examples/events/.lando.yml @@ -2,46 +2,59 @@ name: lando-events compose: - compose.yml services: - web4: + l337: api: 4 image: | FROM nginx:1.22.1 - ENV SERVICE web4 + ENV SERVICE l337 volumes: - ./:/app events: pre-start: - mkdir -p /app/test && echo "$(hostname -s)" > /app/test/appserver-pre-start.txt - web: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/web-pre-start.txt - - web4: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/web4-pre-start.txt + - l337: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/l337-pre-start.txt post-start: - web: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/web-post-start.txt - - web4: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/web4-post-start.txt + - l337: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/l337-post-start.txt post-thing: - web: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/web-post-thing.txt - env | grep "SERVICE=web" post-stuff: - - web4: chown www-data:www-data /app && mkdir -p /app/test && echo "$(hostname -s)" > /app/test/web4-post-stuff.txt - - env | grep "SERVICE=web4" + - l337: mkdir -p /app/test && echo "$(hostname -s)" > /app/test/l337-post-stuff.txt + - env | grep "SERVICE=l337" pre-rebuild: - web: mkdir -p /app/test && echo "rebuilding" > /app/test/web-pre-rebuild.txt - - web4: mkdir -p /app/test && echo "rebuilding" > /app/test/web4-pre-rebuild.txt + - l337: mkdir -p /app/test && echo "rebuilding" > /app/test/l337-pre-rebuild.txt post-rebuild: - web: echo "ET TU, BRUT?" - - web4: echo "ET TU, BRUT?" + - l337: echo "ET TU, BRUT?" post-dynamic: - - env | grep "SERVICE=web" - - echo "thing" + - web: env | grep "SERVICE=web" + - l337: env | grep "SERVICE=l337" + - web: echo "thing" + - echo "$SERVICE" + - echo "stuff" + post-multi-pass: + - env | grep "SERVICE=appserver" + - web: env | grep "SERVICE=web" + - l337: env | grep "SERVICE=l337" + post-what-service: + - echo "$SERVICE" pre-destroy: - web: mkdir -p /app/test && touch /app/test/destroy.txt - - web4: mkdir -p /app/test && touch /app/test/destroy-web.txt + - l337: mkdir -p /app/test && touch /app/test/destroy-l337.txt tooling: thing: service: web cmd: echo "THINGS" stuff: - service: web4 + service: l337 cmd: echo "STUFF" + multi-pass: + cmd: + - web: echo "KORBEN" + - l337: echo "DALLAS" dynamic: cmd: env service: :host @@ -51,3 +64,12 @@ tooling: alias: - h describe: Run a different service + what-service: + cmd: echo "$SERVICE" + service: :service + options: + service: + default: l337 + alias: + - s + describe: Run a different service diff --git a/examples/events/README.md b/examples/events/README.md index 9e0d46d1b..467260b90 100644 --- a/examples/events/README.md +++ b/examples/events/README.md @@ -4,6 +4,7 @@ Events Example This example exists primarily to test the following documentation: * [Events](http://docs.devwithlando.io/config/events.html) +* [V4 Services](https://docs.lando.dev/core/v4/landofile/services.html) See the [Landofiles](http://docs.devwithlando.io/config/lando.html) in this directory for the exact magicks. @@ -28,14 +29,28 @@ lando ssh -s appserver -c "cat /app/test/appserver-pre-start.txt | grep \$(hostn # Should run events on the specified service lando ssh -s web -c "cat /app/test/web-pre-start.txt | grep \$(hostname -s)" lando ssh -s web -c "cat /app/test/web-post-start.txt | grep \$(hostname -s)" +lando ssh -s l337 -c "cat /app/test/l337-pre-start.txt | grep \$(hostname -s)" +lando ssh -s l337 -c "cat /app/test/l337-post-start.txt | grep \$(hostname -s)" -# Should run on tooling commands as well +# Should run tooling command events using the tooling command service as the default lando thing lando ssh -s web -c "cat /app/test/web-post-thing.txt | grep \$(hostname -s)" +lando stuff +lando ssh -s l337 -c "cat /app/test/l337-post-stuff.txt | grep \$(hostname -s)" + +# Should run dynamic tooling command events using argv if set or option default otherwise +lando dynamic +lando dynamic --host l337 +lando what-service | grep l337 | wc -l | grep 2 +lando what-service --service web | grep web | wc -l | grep 2 + +# Should use the app default service as the default in multi-service tooling +lando multi-pass # Should run on rebuild without failin and trigger pre-rebuild event lando rebuild -y | grep "ET TU, BRUT" lando ssh -s web -c "cat /app/test/web-pre-rebuild.txt | grep rebuilding" +lando ssh -s l337 -c "cat /app/test/l337-pre-rebuild.txt | grep rebuilding" ``` Destroy tests @@ -48,4 +63,5 @@ lando poweroff # Should trigger pre-destroy event stat test/destroy.txt +stat test/destroy-l337.txt ``` diff --git a/examples/events/compose.yml b/examples/events/compose.yml index 5d7dd3ddf..6d330823a 100644 --- a/examples/events/compose.yml +++ b/examples/events/compose.yml @@ -2,6 +2,8 @@ version: '3.6' services: appserver: image: php:7.1-fpm-alpine + environment: + SERVICE: 'appserver' web: image: nginx environment: diff --git a/lib/bootstrap.js b/lib/bootstrap.js index e0bff41d7..22955d554 100644 --- a/lib/bootstrap.js +++ b/lib/bootstrap.js @@ -209,6 +209,9 @@ exports.getLandoFiles = (files = [], startFrom = process.cwd()) => _(files) * @TODO: do we need some validation of the dumped tasks here? */ exports.getTasks = (config = {}, argv = {}, tasks = []) => { + // add the argv to the config + config.argv = argv; + // If we have a tooling router lets rebase on that if (fs.existsSync(config.toolingRouter)) { // Get the closest route diff --git a/plugins/lando-events/app.js b/plugins/lando-events/app.js index f98edfea8..996c6a9ea 100644 --- a/plugins/lando-events/app.js +++ b/plugins/lando-events/app.js @@ -21,7 +21,6 @@ module.exports = (app, lando) => { .value(); lando.log.debug('added preemptive perm sweeping to evented v3 services %j', v3EventCommands); _.forEach(v3EventCommands, container => { - console.log(container); eventCommands.unshift({ id: container, cmd: '/helpers/user-perms.sh --silent', diff --git a/plugins/lando-events/lib/utils.js b/plugins/lando-events/lib/utils.js index 95e6ba8d9..46827f6c4 100644 --- a/plugins/lando-events/lib/utils.js +++ b/plugins/lando-events/lib/utils.js @@ -6,16 +6,21 @@ const getUser = require('./../../../lib/utils').getUser; // Helper to find the default service const getDefaultService = (data = {}, defaultService = 'appserver') => { - if (_.has(data, 'service')) { - if (_.startsWith(data.service, ':')) { - const option = _.trimStart(data.service, ':'); - return _.get(data, `options.${option}.default`, defaultService); - } else { - return _.get(data, 'service'); - } - } else { - return defaultService; + // if this is an event built on a service-dynamic command + if (_.has(data, 'service') && _.startsWith(data.service, ':')) { + // get the option name + const option = _.trimStart(data.service, ':'); + // get the argv + const argv = _.get(data, 'app.config.argv', {}); + // return the argv of the option or its default value + return _.has(argv, option) ? argv[option] : _.get(data, `options.${option}.default`, defaultService); } + + // or if the service is explicitly set by a tooling command + if (_.has(data, 'service')) return _.get(data, 'service'); + + // otherwise just return the default + return defaultService; }; // Helper to find a command @@ -36,6 +41,14 @@ exports.events2Runz = (cmds, app, data = {}) => _.map(cmds, cmd => { // Discover the service const command = getCommand(cmd); const service = getService(cmd, data, app._defaultService); + + // try to get a list of v4 services a few ways, we have to look at different places because the event could + // run at various points in the bootstrap + const v4s = _([ + _(app.info).filter(service => service.api === 4).map('service').value(), + _.get(app, 'v4.servicesList', []), + ]).flatten().compact().uniq().value(); + // Validate the service if we can // @NOTE fast engine runs might not have this data yet if (app.services && !_.includes(app.services, service)) { @@ -48,7 +61,7 @@ exports.events2Runz = (cmds, app, data = {}) => _.map(cmds, cmd => { cmd: ['/bin/sh', '-c', _.isArray(command) ? command.join(' ') : command], compose: app.compose, project: app.project, - api: _.includes(_.get(app, 'v4.servicesList', []), service) ? 4 : 3, + api: _.includes(v4s, service) ? 4 : 3, opts: { cstdio: ['inherit', 'pipe', 'pipe'], mode: 'attach',