Skip to content

Add support .env #29

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 1 commit 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
10 changes: 5 additions & 5 deletions compose.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const yaml = require('js-yaml');
const fs = require('fs');
const { readYamlEnvSync } = require('yaml-env-defaults');
const stream = require('stream');

const secrets = require('./lib/secrets');
Expand All @@ -10,7 +9,7 @@ const services = require('./lib/services');
const tools = require('./lib/tools');

class Compose {
constructor(dockerode, file, projectName) {
constructor(dockerode, file, projectName, customEnv = undefined) {
this.docker = dockerode;

if (file === undefined || projectName === undefined) {
Expand All @@ -19,9 +18,10 @@ class Compose {

this.file = file;
this.projectName = projectName;
this.loadYaml = (path) => readYamlEnvSync(path, customEnv);

try {
this.recipe = yaml.load(fs.readFileSync(file, 'utf8'));
this.recipe = this.loadYaml(file);
} catch (e) {
throw e;
}
Expand Down Expand Up @@ -52,7 +52,7 @@ class Compose {
output.volumes = await volumes.up(this.docker, this.projectName, this.recipe, output);
output.configs = await configs(this.docker, this.projectName, this.recipe, output);
output.networks = await networks.up(this.docker, this.projectName, this.recipe, output);
output.services = await services.up(this.docker, this.projectName, this.recipe, output, options);
output.services = await services.up(this.docker, this.projectName, this.recipe, output, options, this.loadYaml);
return output;
} catch (e) {
throw e;
Expand Down
25 changes: 11 additions & 14 deletions lib/services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const tools = require('./tools');
const servicesTools = require('./servicesTools');
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');

async function down(docker, projectName, recipe, output, options) {
Expand All @@ -21,7 +20,7 @@ async function down(docker, projectName, recipe, output, options) {
return services;
}

async function up(docker, projectName, recipe, output, options) {
async function up(docker, projectName, recipe, output, options, loadYaml) {
var services = [];
var serviceNames = tools.sortServices(recipe);
const cwdPath = path.dirname(output.file);
Expand All @@ -32,7 +31,7 @@ async function up(docker, projectName, recipe, output, options) {
var service = recipe.services[serviceName];
if (service.extends !== undefined) {
if (service.extends.service !== undefined) {
service = extendsServices(service, recipe, pathScope);
service = extendsServices(service, recipe, pathScope, loadYaml);
} else {
throw new Error('Service key in extends is required!');
}
Expand Down Expand Up @@ -341,30 +340,27 @@ var convertFancyDurationToMs = function (value) {
};

// https://github.com/compose-spec/compose-spec/blob/master/spec.md#extends
var extendsServices = function (service, recipe, pathScope) {
var extendsServices = function (service, recipe, pathScope, loadYaml) {
// https://github.com/compose-spec/compose-spec/blob/master/spec.md#finding-referenced-service
if (service.extends.file === undefined) {
// EXTENDS OF THE SAME RECIPE
return buildExtendsService(
service,
service.extends.service,
recipe,
pathScope
pathScope,
loadYaml
);
} else {
// EXTENDS OF ANOTHER RECIPE
var absolutePath = path.dirname(pathScope.file);
var extendsRecipe = yaml.load(
fs.readFileSync(
path.resolve(path.join(absolutePath, service.extends.file)),
'utf8'
)
);
var extendsRecipe = loadYaml(path.resolve(path.join(absolutePath, service.extends.file)));
return buildExtendsService(
service,
service.extends.service,
extendsRecipe,
pathScope
pathScope,
loadYaml
);
}
};
Expand All @@ -373,7 +369,8 @@ var buildExtendsService = function (
service,
extendsServiceName,
recipe,
pathScope
pathScope,
loadYaml
) {
var extend = false;
var extendsRecipeServiceNames = Object.keys(recipe.services);
Expand Down Expand Up @@ -403,7 +400,7 @@ var buildExtendsService = function (
path.join(absolutePath, oldService.extends.file)
);
}
if (extend) service = extendsServices(service, recipe, pathScope);
if (extend) service = extendsServices(service, recipe, pathScope, loadYaml);

return service;
}
Expand Down
28 changes: 22 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"homepage": "https://github.com/apocas/dockerode-compose#readme",
"dependencies": {
"dockerode": "^3.3.0",
"js-yaml": "^4.0.0",
"tar-fs": "^2.1.1"
"tar-fs": "^2.1.1",
"yaml-env-defaults": "^2.0.2"
},
"devDependencies": {
"chai": "~4.2.0",
Expand Down
30 changes: 30 additions & 0 deletions test/assets/wordpress_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: "3.9"

services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
volumes_from:
- db:ro
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
30 changes: 30 additions & 0 deletions test/compose.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const expect = require('chai').expect,
assert = require('assert');
const DockerodeCompose = require('../compose');

var compose = require('./spec_helper').compose;
var compose_complex = require('./spec_helper').compose_complex;
Expand Down Expand Up @@ -53,6 +54,35 @@ describe('compose', function () {
});
});

describe('#up with env', function () {

const envMap = {
WORDPRESS_DB_USER: 'wordpress',
WORDPRESS_DB_PASSWORD: 'password'
};
const getProperty = (key) => {
// without default value will be throwed error in case of missing
return envMap[key] || 'Default value';
}

var compose = new DockerodeCompose(docker, './test/assets/wordpress_env.yml', 'wordpress_env', getProperty);
it("should do compose up", function (done) {
this.timeout(60000);
(async () => {
var report = await compose.up();
expect(report.services).to.be.ok;
done();
})();
});
afterEach('clean up', function (done) {
this.timeout(60000);
(async () => {
await compose.down({ volumes: true });
done();
})();
});
});

describe('#down', function () {
beforeEach('bring up', function (done) {
this.timeout(20000);
Expand Down