Skip to content

Commit

Permalink
fix(compose): Fix compose file path resolution as it is copied into the
Browse files Browse the repository at this point in the history
lando directory and be able to set app mount user and use docker compose v2 seperator

Refs: lando/lando#3373
  • Loading branch information
florianPat committed May 14, 2024
1 parent 811250f commit 6503ba7
Show file tree
Hide file tree
Showing 30 changed files with 220 additions and 81 deletions.
62 changes: 31 additions & 31 deletions examples/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ Run the following commands to verify things work as expected

```bash
# Should merge in all Landofiles correctly
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_log_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web3_1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-log-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web3-1

# Should merge in all Landofiles correctly even if we are down a directory
cd docker-compose
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_log_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web3_1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-log-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web3-1
cd ..

# Should load environment files from all Landofiles
Expand Down Expand Up @@ -120,16 +120,16 @@ lando info --format json
lando info --path "[0]" | grep service | wc -l | grep 1

# Should list this apps containers
lando list | grep landobase_log_1
lando list | grep landobase_web_1
lando list | grep landobase_web2_1
lando list | grep landobase_web3_1
lando list | grep lando-base-log-1
lando list | grep lando-base-web-1
lando list | grep lando-base-web2-1
lando list | grep lando-base-web3-1

# Should output JSON in lando list without error
lando list --format json

# Should return a specified path when given with lando list
lando list --path "landobase" | grep landobase
lando list --path "lando-base" | grep lando-base

# Should return logs without error
lando logs
Expand Down Expand Up @@ -166,33 +166,33 @@ docker ps --filter label=io.lando.container=TRUE -q | wc -l | grep 0

# Should rebuild the services without errors
lando rebuild -y
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_log_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web3_1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-log-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web3-1

# Should only rebuild the specified services
lando rebuild -y --service web2
lando rebuild -y -s web2
docker ps --latest | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_log_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web3_1
docker ps --latest | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-log-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web3-1
lando rebuild -y --service web3
lando rebuild -y -s web3
docker ps --latest | grep landobase_web3_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_log_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web3_1
docker ps --latest | grep lando-base-web3-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-log-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web3-1

# Should restart the services without errors
lando restart
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_log_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web2_1
docker ps --filter label=com.docker.compose.project=landobase | grep landobase_web3_1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-log-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web2-1
docker ps --filter label=com.docker.compose.project=lando-base | grep lando-base-web3-1

# Should have non-numeric keys in LANDO_INFO
lando php info.php
Expand Down
2 changes: 1 addition & 1 deletion examples/l337/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ lando env | grep SERVICE | grep web
lando whoami | grep nginx

# should allow legacy moreHttpPorts to work like it does for v3
docker inspect l337_web_1 | grep io.lando.http-ports | grep "80,443,8888"
docker inspect l337-web-1 | grep io.lando.http-ports | grep "80,443,8888"

# should automatically set appMount if appRoot is volume mounted
lando pwd | grep /site
Expand Down
3 changes: 3 additions & 0 deletions examples/lando-101/.lando.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ name: lando-101
recipe: lamp
config:
php: 7.4

plugins:
"@lando/core": "../../.."
3 changes: 3 additions & 0 deletions examples/lando-101/.lando.proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ services:
proxy:
mailhog:
- mail.lando-101.lndo.site

plugins:
"@lando/core": "../../.."
3 changes: 3 additions & 0 deletions examples/lando-101/.lando.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ services:
portforward: true
hogfrom:
- appserver

plugins:
"@lando/core": "../../.."
3 changes: 3 additions & 0 deletions examples/lando-101/.lando.tooling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ tooling:
phpcs:
service: appserver
cmd: /app/vendor/bin/phpcs

plugins:
"@lando/core": "../../.."
3 changes: 3 additions & 0 deletions examples/lando-101/.lando.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
name: lando-101
recipe: lamp

plugins:
"@lando/core": "../../.."
22 changes: 11 additions & 11 deletions examples/networking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ Run the following commands to verify things work as expected
```bash
# Should have the correct entries in /certs/cert.ext
cd lamp
lando ssh -s appserver -c "cat /certs/cert.ext" | grep DNS.1 | grep -w appserver.landolamp.internal
lando ssh -s appserver -c "cat /certs/cert.ext" | grep DNS.1 | grep -w appserver.lando-lamp.internal
lando ssh -s appserver -c "cat /certs/cert.ext" | grep DNS.2 | grep -w appserver
lando ssh -s appserver -c "cat /certs/cert.ext" | grep DNS.3 | grep -w localhost
lando ssh -s appserver -c "cat /certs/cert.ext" | grep lando-lamp.lndo.site
cd .. && cd lemp
lando ssh -s placeholder -c "cat /certs/cert.ext" | grep DNS.1 | grep -w placeholder.landolemp.internal
lando ssh -s placeholder -c "cat /certs/cert.ext" | grep DNS.1 | grep -w placeholder.lando-lemp.internal
lando ssh -s placeholder -c "cat /certs/cert.ext" | grep DNS.2 | grep -w placeholder
lando ssh -s placeholder -c "cat /certs/cert.ext" | grep DNS.3 | grep -w localhost
lando ssh -s placeholder -c "cat /certs/cert.ext" | grep placeholder.lando-lemp.lndo.site

# Should have the correct internal hostname info
cd lamp
lando info -s appserver | grep hostnames: | grep appserver.landolamp.internal
lando info -s appserver | grep hostnames: | grep appserver.lando-lamp.internal
cd .. && cd lemp
lando info -s placeholder | grep hostnames: | grep placeholder.landolemp.internal
lando info -s placeholder | grep hostnames: | grep placeholder.lando-lemp.internal

# Should be able to self connect from lamp
cd lamp
Expand All @@ -64,24 +64,24 @@ lando ssh -s placeholder -c "curl https://localhost"
# Should be able to curl lemp from lamp at proxy addresses and internal hostnames
cd lamp
lando ssh -s appserver -c "curl http://lando-lemp.lndo.site"
lando ssh -s appserver -c "curl http://appserver_nginx.landolemp.internal"
lando ssh -s appserver -c "curl http://appserver_nginx.lando-lemp.internal"
# lando ssh -s appserver -c "curl https://lando-lemp.lndo.site"
# lando ssh -s appserver -c "curl https://appserver_nginx.landolemp.internal"
# lando ssh -s appserver -c "curl https://appserver_nginx.lando-lemp.internal"
lando ssh -s appserver -c "curl https://placeholder.lando-lemp.lndo.site"
lando ssh -s appserver -c "curl https://placeholder.landolemp.internal"
lando ssh -s appserver -c "curl https://placeholder.lando-lemp.internal"

# Should be able to curl lamp from lemp at proxy addresses and internal hostname
cd lemp
lando ssh -s appserver -c "curl http://lando-lamp.lndo.site"
lando ssh -s appserver -c "curl http://appserver.landolamp.internal"
lando ssh -s appserver -c "curl http://appserver.lando-lamp.internal"
# lando ssh -s appserver -c "curl https://lando-lamp.lndo.site"
# lando ssh -s appserver -c "curl https://appserver.landolamp.internal"
# lando ssh -s appserver -c "curl https://appserver.lando-lamp.internal"
lando ssh -s placeholder -c "curl https://lando-lamp.lndo.site"
lando ssh -s placeholder -c "curl https://appserver.landolamp.internal"
lando ssh -s placeholder -c "curl https://appserver.lando-lamp.internal"

# Should even be able to connect to a database in a different app
cd lamp
lando ssh -s database -c "mysql -uroot -h database.landolemp.internal -e 'quit'"
lando ssh -s database -c "mysql -uroot -h database.lando-lemp.internal -e 'quit'"
```

Destroy tests
Expand Down
8 changes: 4 additions & 4 deletions examples/proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ curl http://object-format.lndo.site | grep X-Lando-Test-Ssl || echo $? | grep 1
curl -k https://object-format.lndo.site | grep X-Lando-Test-Ssl | grep on

# Should generate a default certs config file and put it in the right place
docker exec landoproxyhyperion5000gandalfedition_proxy_1 cat /proxy_config/default-certs.yaml | grep certFile | grep /certs/cert.crt
docker exec landoproxyhyperion5000gandalfedition_proxy_1 cat /proxy_config/default-certs.yaml | grep keyFile | grep /certs/cert.key
docker exec landoproxyhyperion5000gandalfedition-proxy-1 cat /proxy_config/default-certs.yaml | grep certFile | grep /certs/cert.crt
docker exec landoproxyhyperion5000gandalfedition-proxy-1 cat /proxy_config/default-certs.yaml | grep keyFile | grep /certs/cert.key

# Should generate proxy cert files and move them into the right location as needed
docker exec landoproxy_web3_1 cat /proxy_config/web3.landoproxy.yaml| grep certFile | grep "/lando/certs/web3.landoproxy.crt"
docker exec landoproxy_web3_1 cat /proxy_config/web3.landoproxy.yaml| grep keyFile | grep "/lando/certs/web3.landoproxy.key"
docker exec lando-proxy-web3-1 cat /proxy_config/web3.lando-proxy.yaml| grep certFile | grep "/lando/certs/web3.lando-proxy.crt"
docker exec lando-proxy-web3-1 cat /proxy_config/web3.lando-proxy.yaml| grep keyFile | grep "/lando/certs/web3.lando-proxy.key"
```

Destroy tests
Expand Down
6 changes: 3 additions & 3 deletions examples/scanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Run the following commands to validate things are rolling as they should.

```bash
# Should set 80,443 in io.lando.http-ports label by default
docker inspect landoscanner_scanme_1 | grep io.lando.http-ports | grep "80,443"
docker inspect lando-scanner-scanme-1 | grep io.lando.http-ports | grep "80,443"

# Should add an extra port to io.lando.http-ports if specified
docker inspect landoscanner_moreports_1 | grep io.lando.http-ports | grep "80,443,8888"
docker inspect landoscanner_l337_1 | grep io.lando.http-ports | grep "80,443,8888"
docker inspect lando-scanner-moreports-1 | grep io.lando.http-ports | grep "80,443,8888"
docker inspect lando-scanner-l337-1 | grep io.lando.http-ports | grep "80,443,8888"
```

Destroy tests
Expand Down
2 changes: 1 addition & 1 deletion examples/services/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ events:
- cat /var/www/run_internal.txt | grep www-data
- cat /run_as_root.txt | grep root
- cat /var/www/run.txt | grep www-data
- /bin/sh -c 'echo "$LANDO_APP_PROJECT" | grep landoservices'
- /bin/sh -c 'echo "$LANDO_APP_PROJECT" | grep lando-services'
# uncomment below to test out https://github.com/lando/core/issues/70
# this is commented out by default because of https://github.com/actions/runner/issues/241
# - bash /app/post-start.bash
Expand Down
4 changes: 2 additions & 2 deletions examples/services/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ lando rebuild -y
# Should rerun build steps even if containers are manually removed and stuff
lando destroy -y
lando start -y
docker rm -f landoservices_nginx_1
docker rm -f landoservices_appserver_1
docker rm -f lando-services-nginx-1
docker rm -f lando-services-appserver-1
lando start -y
lando ssh -s appserver -c "vim --version"
lando ssh -s appserver -c "cat /var/www/build.txt"
Expand Down
7 changes: 6 additions & 1 deletion hooks/app-run-v3-build-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ module.exports = async (app, lando) => {

// get v3 buildable services
const buildServices = _.get(app, 'opts.services', app.services);
const dockerComposeServices = _(app.info)
.filter(info => info.type === 'docker-compose')
.map(info => info.service)
.value();
const buildV3Services = _(app.parsedV3Services)
.filter(service => _.includes(buildServices, service.name))
.map(service => service.name)
.value();
.value()
.concat(dockerComposeServices);
app.log.debug('going to build v3 services if applicable', buildV3Services);

// Make sure containers for this app exist; if they don't and we have build locks, we need to kill them
Expand Down
70 changes: 58 additions & 12 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = class App {
* @alias app.name
*/
this.name = require('../utils/slugify')(name);
this.project = require('../utils/docker-composify')(this.name);
this.project = this.name;
this._serviceApi = 3;
this._config = lando.config;
this._defaultService = 'appserver';
Expand Down Expand Up @@ -272,14 +272,23 @@ module.exports = class App {
// We should only need to initialize once, if we have just go right to app ready
if (this.initialized) return this.events.emit('ready', this);
// Get compose data if we have any, otherwise set to []
const composeFiles = require('../utils/load-compose-files')(_.get(this, 'config.compose', []), this.root);
this.composeData = [new this.ComposeService('compose', {}, ...composeFiles)];
// Validate and set env files
this.envFiles = require('../utils/normalize-files')(_.get(this, 'config.env_file', []), this.root);
// Log some things
this.log.verbose('initiatilizing app at %s...', this.root);
this.log.silly('app has config', this.config);

return require('../utils/load-compose-files')(
_.get(this, 'config.compose', []),
this.root,
this._dir,
(composeFiles, outputFilePath) =>
this.engine.getComposeConfig({compose: composeFiles, project: this.project, outputFilePath}),
)
.then(composeFile => {
if (undefined !== composeFile) {
this.composeData = [new this.ComposeService('compose', {}, composeFile)];
}
// Validate and set env files
this.envFiles = require('../utils/normalize-files')(_.get(this, 'config.env_file', []), this.root);
// Log some things
this.log.verbose('initiatilizing app at %s...', this.root);
this.log.silly('app has config', this.config);
})
/**
* Event that allows altering of the app object right before it is
* initialized.
Expand All @@ -292,16 +301,17 @@ module.exports = class App {
* @event pre_init
* @property {App} app The app instance.
*/
return loadPlugins(this, this._lando).then(() => this.events.emit('pre-init', this))
.then(() => loadPlugins(this, this._lando))

.then(() => this.events.emit('pre-init', this))
// Actually assemble this thing so its ready for that engine
.then(() => {
// Get all the services
this.services = require('../utils/get-app-services')(this.composeData);
// Merge whatever we have thus far together
this.info = require('../utils/get-app-info-defaults')(this);
// finally just make a list of containers
const separator = _.get(this, '_config.orchestratorSeparator', '_');
const separator = _.get(this, '_config.orchestratorSeparator', '-');
this.containers = _(this.info)
.map(service => ([service.service, `${this.project}${separator}${service.service}${separator}1`]))
.fromPairs()
Expand All @@ -322,7 +332,43 @@ module.exports = class App {
// Front load our app mounts
this.add(new this.ComposeService('mounts', {}, {services: require('../utils/get-app-mounts')(this)}), true);
// Then front load our globals
this.add(new this.ComposeService('globals', {}, {services: require('../utils/get-app-globals')(this)}), true);
const globalsData = _.merge(
{services: require('../utils/get-app-globals')(this)},
{
services:
_.omitBy(
_.zipObject(_.keys(this.config.services), _.map(
this.config.services,
service => _.omitBy(
{
environment: _.omitBy({
LANDO_WEBROOT_USER: service.api === 4 ? service.user : service.meUser,
LANDO_WEBROOT_GROUP: service.api === 4 ? service.user : service.meUser,
LANDO_MOUNT: service.api === 4 ? service.working_dir ?? service.appMount : service.appMount,
}, _.isNil),
},
_.isEmpty,
),
)),
_.isEmpty,
),
},
);
_.forEach(globalsData.services, (service, name) => {
if ('docker-compose' !== _.find(this.info, {service: name})?.type) {
return;
}
service.volumes.push(`${this._config.userConfRoot}:/lando:cached`);

if (!this.config.services[name]?.ssl) {
return;
}

service.volumes.push(
`${path.join(path.join(this._config.userConfRoot, 'scripts'), 'add-cert.sh')}:/scripts/000-add-cert.sh`,
);
});
this.add(new this.ComposeService('globals', {}, globalsData), true);
// Take the big dump of all our compose stuff
this.compose = require('../utils/dump-compose-data')(this.composeData, this._dir);

Expand Down
Loading

0 comments on commit 6503ba7

Please sign in to comment.