Skip to content

Issue 3373 #268

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/build-util-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
run: echo "tag-suffix=-edge" >> $GITHUB_OUTPUT
- name: Login to DockerHub
uses: docker/login-action@v2
if: ${{ github.event_name != 'pull_request' }}
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand All @@ -47,7 +48,7 @@ jobs:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
platforms: linux/amd64,linux/arm64
push: true
push: ${{ github.event_name != 'pull_request' }}
tags: devwithlando/${{ matrix.image }}:${{ matrix.tag }}${{ steps.pr.outputs.tag-suffix }}
cache-from: type=gha
cache-to: type=gha,mode=max
4 changes: 4 additions & 0 deletions bin/lando
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ const cores = [
path.resolve(__dirname, '..'),
];

if (typeof _.get(config, 'plugins.@lando/core') === 'string') {
cores.unshift(path.resolve(appConfig.root, config.plugins['@lando/core']));
}

// if appConfig points to a different core lets set that here
if (typeof _.get(appConfig, 'plugins.@lando/core') === 'string') {
cores.unshift(path.resolve(appConfig.root, appConfig.plugins['@lando/core']));
Expand Down
11 changes: 11 additions & 0 deletions builders/_lando-compose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
name: '_lando-compose',
parent: '_lando',
builder: (parent, config) => class LandoComposeServiceV3 extends parent {
constructor(id, options = {}) {
super(id, options);
};
},
};
20 changes: 16 additions & 4 deletions builders/_lando.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
supportedIgnore = false,
root = '',
webroot = '/app',
_app = null,
appMount = '/app',
} = {},
...sources
) {
Expand Down Expand Up @@ -81,6 +83,9 @@ module.exports = {
const environment = {
LANDO_SERVICE_NAME: name,
LANDO_SERVICE_TYPE: type,
LANDO_WEBROOT_USER: meUser,
LANDO_WEBROOT_GROUP: meUser,
LANDO_MOUNT: appMount,
};

// Handle labels
Expand All @@ -96,7 +101,6 @@ module.exports = {
`${userConfRoot}:/lando:cached`,
`${scriptsDir}:/helpers`,
`${entrypointScript}:/lando-entrypoint.sh`,
`${dataHome}:/var/www`,
];

// Handle ssl
Expand Down Expand Up @@ -139,9 +143,16 @@ module.exports = {

// Add named volumes and other thingz into our primary service
const namedVols = {};
_.set(namedVols, data, {});
_.set(namedVols, dataHome, {});

if (null !== data) {
_.set(namedVols, data, {});
}
if (null !== dataHome) {
_.set(namedVols, dataHome, {});
volumes.push(`${dataHome}:/var/www`);
}
if (null === entrypoint) {
entrypoint = undefined;
}
sources.push({
services: _.set({}, name, {
entrypoint,
Expand Down Expand Up @@ -171,6 +182,7 @@ module.exports = {
info.meUser = meUser;
info.hasCerts = ssl;
info.api = 3;
info.appMount = appMount;

// Add the healthcheck if it exists
if (healthcheck) info.healthcheck = healthcheck;
Expand Down
7 changes: 7 additions & 0 deletions hooks/app-add-v3-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ module.exports = async (app, lando) => {
// add parsed services to app object so we can use them downstream
app.cachedInfo = _.get(lando.cache.get(app.composeCache), 'info', []);
app.parsedServices = require('../utils/parse-v3-services')(_.get(app, 'config.services', {}), app);
app.parsedServices = app.parsedServices.concat(
require('../utils/parse-compose-services')(
_.get(app, 'config.services', {}),
_.keys(_.get(app, 'composeData[0].data[0].services', {})),
app,
),
);
app.parsedV3Services = _(app.parsedServices).filter(service => service.api === 3).value();
app.servicesList = app.parsedV3Services.map(service => service.name);

Expand Down
28 changes: 19 additions & 9 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,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(composeFileData => {
if (undefined !== composeFileData) {
this.composeData = [new this.ComposeService('compose', {}, composeFileData)];
}
// 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 @@ -293,8 +302,9 @@ 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
Expand Down
7 changes: 7 additions & 0 deletions lib/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const composeFlags = {
rm: '--rm',
timestamps: '--timestamps',
volumes: '-v',
outputFilePath: '-o',
};

// Default options nad things
Expand All @@ -33,6 +34,7 @@ const defaultOptions = {
pull: {},
rm: {force: true, volumes: true},
up: {background: true, noRecreate: true, recreate: false, removeOrphans: true},
config: {},
};

/*
Expand Down Expand Up @@ -155,3 +157,8 @@ exports.start = (compose, project, opts = {}) => buildShell('up', project, compo
* Run docker compose stop
*/
exports.stop = (compose, project, opts = {}) => buildShell('stop', project, compose, opts);

/*
* Run docker compose config
*/
exports.config = (compose, project, opts = {}) => buildShell('config', project, compose, opts);
20 changes: 20 additions & 0 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,25 @@ module.exports = class Engine {
// stop
return this.engineCmd('stop', data);
};

/**
* Get dumped docker compose config for compose files from project
* using a `compose` object with `{compose: compose, project: project, opts: opts}`
*
* @since 3.0.0
* @param {Object} data Config needs a service within a compose context
* @param {Array} data.compose An Array of paths to Docker compose files
* @param {String} data.project A String of the project name (Usually this is the same as the app name)
* @param {String} [data.outputFilePath='/path/to/file.yml'] String to output path
* @param {Object} [data.opts] Options
* @return {Promise} A Promise.
* @example
* return lando.engine.stop(app);
*/
getComposeConfig(data) {
data.opts = {cmd: ['-o', data.outputFilePath]};
delete data.outputFilePath;
return this.engineCmd('config', data);
};
};

2 changes: 2 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ exports.start = (data, compose) => retryEach(data, datum => compose('start', dat
exports.stop = (data, compose, docker) => retryEach(data, datum => {
return (datum.compose) ? compose(data.kill ? 'kill' : 'stop', datum) : docker.stop(getContainerId(datum));
});

exports.config = (data, compose) => retryEach(data, datum => compose('config', datum));
32 changes: 12 additions & 20 deletions scripts/user-perm-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,25 @@ LANDO_MODULE="userperms"
add_user() {
local USER=$1
local GROUP=$2
local UID=$3
local GID=$4
local DISTRO=$5
local EXTRAS="$6"
if [ "$DISTRO" = "alpine" ]; then
if ! groups | grep "$GROUP" > /dev/null 2>&1; then addgroup -g "$GID" "$GROUP" 2>/dev/null; fi
if ! id -u "$GROUP" > /dev/null 2>&1; then adduser -H -D -G "$GROUP" -u "$UID" "$USER" "$GROUP" 2>/dev/null; fi
else
if ! groups | grep "$GROUP" > /dev/null 2>&1; then groupadd --force --gid "$GID" "$GROUP" 2>/dev/null; fi
if ! id -u "$GROUP" > /dev/null 2>&1; then useradd --gid "$GID" --uid "$UID" $EXTRAS "$USER" 2>/dev/null; fi
fi;
local WEBROOT_UID=$3
local WEBROOT_GID=$4
if ! getent group | cut -d: -f1 | grep "$GROUP" > /dev/null 2>&1; then addgroup -g "$WEBROOT_GID" "$GROUP" 2>/dev/null; fi
if ! id -u "$USER" > /dev/null 2>&1; then adduser -H -D -G "$GROUP" -u "$WEBROOT_UID" "$USER" "$GROUP" 2>/dev/null; fi
}

# Verify user
verify_user() {
local USER=$1
local GROUP=$2
local DISTRO=$3
id -u "$USER" > /dev/null 2>&1
groups | grep "$GROUP" > /dev/null 2>&1
if [ "$DISTRO" = "alpine" ]; then
groups "$USER" | grep "$GROUP" > /dev/null 2>&1
if command -v chsh > /dev/null 2>&1 ; then
if command -v /bin/bash > /dev/null 2>&1 ; then
chsh -s /bin/bash $USER || true
fi;
else
true
# is there a chsh we can use? do we need to?
else
chsh -s /bin/bash $USER || true
fi;
}

Expand All @@ -59,11 +53,10 @@ reset_user() {
if [ "$(id -u $USER)" != "$HOST_UID" ]; then
usermod -o -u "$HOST_UID" "$USER" 2>/dev/null
fi
groupmod -g "$HOST_GID" "$GROUP" 2>/dev/null || true
if [ "$(id -u $USER)" != "$HOST_UID" ]; then
groupmod -o -g "$HOST_GID" "$GROUP" 2>/dev/null || true
if [ "$(id -g $USER)" != "$HOST_GID" ]; then
usermod -g "$HOST_GID" "$USER" 2>/dev/null || true
fi
usermod -a -G "$GROUP" "$USER" 2>/dev/null || true
fi;
# If this mapping is incorrect lets abort here
if [ "$(id -u $USER)" != "$HOST_UID" ]; then
Expand Down Expand Up @@ -97,7 +90,6 @@ perm_sweep() {
nohup find /user/.ssh -not -user $USER -execdir chown $USER:$GROUP {} \+ > /tmp/perms.out 2> /tmp/perms.err &
nohup find /var/www -not -user $USER -execdir chown $USER:$GROUP {} \+ > /tmp/perms.out 2> /tmp/perms.err &
nohup find /usr/local/bin -not -user $USER -execdir chown $USER:$GROUP {} \+ > /tmp/perms.out 2> /tmp/perms.err &
nohup chmod -R 755 /var/www >/dev/null 2>&1 &

# Lets also make some /usr/locals chowned
nohup find /usr/local/lib -not -user $USER -execdir chown $USER:$GROUP {} \+ > /tmp/perms.out 2> /tmp/perms.err &
Expand Down
3 changes: 2 additions & 1 deletion tasks/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ module.exports = lando => ({
const getData = async () => {
// go deep
if (options.deep) {
const separator = _.get(app, '_config.orchestratorSeparator', '_');
return await lando.engine.list({project: app.project})
.map(async container => await lando.engine.scan(container))
.filter(container => {
if (!options.service) return true;
return options.service.map(service => `/${app.project}_${service}_1`).includes(container.Name);
return options.service.map(service => `/${app.project}${separator}${service}${separator}1`).includes(container.Name);
});

// normal info
Expand Down
12 changes: 12 additions & 0 deletions test/compose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,16 @@ describe('compose', () => {
expect(stopResult).to.be.an('object');
});
});

describe('#config', () => {
it('should return the correct default options when not specified');
it('#config should return an object.', () => {
const configResult = compose.config(
['string1', 'string2'],
'my_project',
myOpts,
);
expect(configResult).to.be.an('object');
});
});
});
5 changes: 5 additions & 0 deletions test/get-user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe('get-user', function() {
expect(getUser('test-service', info)).to.equal('www-data');
});

it('should return specified user if service is a "no-api" docker-compose service and user is specified', function() {
const info = [{service: 'test-service', type: 'docker-compose', meUser: 'custom-user'}];
expect(getUser('test-service', info)).to.equal('custom-user');
});

it('should return "www-data" if service.api is 4 but no user is specified', function() {
const info = [{service: 'test-service', api: 4}];
expect(getUser('test-service', info)).to.equal('www-data');
Expand Down
3 changes: 2 additions & 1 deletion utils/build-tooling-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const _ = require('lodash');
const path = require('path');

const getContainer = (app, service) => {
return app?.containers?.[service] ?? `${app.project}_${service}_1`;
const separator = _.get(app, '_config.orchestratorSeparator', '_');
return app?.containers?.[service] ?? `${app.project}${separator}${service}${separator}1`;
};

const getContainerPath = (appRoot, appMount = undefined) => {
Expand Down
2 changes: 1 addition & 1 deletion utils/build-tooling-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = (config, injected) => {
// Kick off the pre event wrappers
.then(() => app.events.emit(`pre-${eventName}`, config, answers))
// Get an interable of our commandz
.then(() => _.map(require('./parse-tooling-config')(cmd, service, options, answers, canExec)))
.then(() => _.map(require('./parse-tooling-config')(cmd, service, name, options, answers, canExec)))
// Build run objects
.map(({command, service}) => require('./build-tooling-runner')(app, command, service, user, env, dir, appMount))
// Try to run the task quickly first and then fallback to compose launch
Expand Down
2 changes: 1 addition & 1 deletion utils/get-app-mounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = app => _(app.services)
// Objectify
.map(service => _.merge({name: service}, _.get(app, `config.services.${service}`, {})))
// Set the default
.map(config => _.merge({}, config, {app_mount: _.get(config, 'app_mount', 'cached')}))
.map(config => _.merge({}, config, {app_mount: _.get(config, 'app_mount', app.config.app_mount || 'cached')}))
// Filter out disabled mountes
.filter(config => config.app_mount !== false && config.app_mount !== 'disabled')
// Combine together
Expand Down
4 changes: 2 additions & 2 deletions utils/get-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (name, info = []) => {
if (!_.find(info, {service: name})) return 'www-data';
// otherwise get the service
const service = _.find(info, {service: name});
// if this is a "no-api" service eg type "docker-compose" also return www-data
if (!service.api && service.type === 'docker-compose') return 'www-data';
// if this is a "no-api" service eg type "docker-compose" return meUser or www-data as default
if (!service.api && service.type === 'docker-compose') return service.meUser || 'www-data';
// otherwise return different things based on the api
return service.api === 4 ? service.user || 'www-data' : service.meUser || 'www-data';
};
30 changes: 27 additions & 3 deletions utils/load-compose-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,32 @@

const _ = require('lodash');
const Yaml = require('./../lib/yaml');
const path = require('path');
const yaml = new Yaml();
const fs = require('fs');
const remove = require('./remove');

module.exports = (files, dir) => _(require('./normalize-files')(files, dir))
.map(file => yaml.load(file))
.value();
// This just runs `docker compose --project-directory ${dir} config -f ${files} --output ${outputPaths}` to
// make all paths relative to the lando config root
module.exports = async (files, dir, landoComposeConfigDir = undefined, outputConfigFunction = undefined) => {
const composeFilePaths = _(require('./normalize-files')(files, dir)).value();
if (_.isEmpty(composeFilePaths)) {
return {};
}

if (undefined === outputConfigFunction) {
return _(composeFilePaths)
.map(file => yaml.load(file))
.value();
}

const outputFile = path.join(landoComposeConfigDir, 'resolved-compose-config.yml');

fs.mkdirSync(path.dirname(outputFile), {recursive: true});
await outputConfigFunction(composeFilePaths, outputFile);
const result = yaml.load(outputFile);
fs.unlinkSync(outputFile);
remove(path.dirname(outputFile));

return result;
};
Loading
Loading