Skip to content

Commit

Permalink
update event tests for new v4 stuff and general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Sep 5, 2023
1 parent c8abf4c commit 24d8059
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
46 changes: 34 additions & 12 deletions examples/events/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
18 changes: 17 additions & 1 deletion examples/events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -48,4 +63,5 @@ lando poweroff

# Should trigger pre-destroy event
stat test/destroy.txt
stat test/destroy-l337.txt
```
2 changes: 2 additions & 0 deletions examples/events/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: '3.6'
services:
appserver:
image: php:7.1-fpm-alpine
environment:
SERVICE: 'appserver'
web:
image: nginx
environment:
Expand Down
3 changes: 3 additions & 0 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion plugins/lando-events/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 23 additions & 10 deletions plugins/lando-events/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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',
Expand Down

0 comments on commit 24d8059

Please sign in to comment.