Skip to content
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

Add local files volume mount support for both relative path and absolute path #19

Merged
merged 5 commits into from
Aug 18, 2022
Merged
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: 8 additions & 2 deletions lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function down(docker, projectName, recipe, output, options) {
async function up(docker, projectName, recipe, output, options) {
var services = [];
var serviceNames = tools.sortServices(recipe);
const cwdPath = path.dirname(output.file);
for (var serviceName of serviceNames) {
var pathScope = {};
pathScope.file = output.file;
Expand Down Expand Up @@ -153,7 +154,12 @@ async function up(docker, projectName, recipe, output, options) {
var opts = {
name: projectName + '_' + serviceName + '_1',
Image: service.image,
HostConfig: servicesTools.buildHostConfig(projectName, service, recipe),
HostConfig: servicesTools.buildHostConfig(
projectName,
service,
recipe,
cwdPath
),
Env: servicesTools.buildEnvVars(service),
NetworkingConfig: {
EndpointsConfig: {},
Expand All @@ -167,7 +173,7 @@ async function up(docker, projectName, recipe, output, options) {
if (service.networks !== undefined) {
servicesTools.buildNetworks(service.networks, networksToAttach);
} else {
opts.HostConfig.NetworkMode = projectName + '_default'
opts.HostConfig.NetworkMode = projectName + '_default';
opts.NetworkingConfig.EndpointsConfig[projectName + '_default'] = {
IPAMConfig: null,
Links: null,
Expand Down
100 changes: 88 additions & 12 deletions lib/servicesTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ const tar = require('tar-fs');
const path = require('path');
const stream = require('stream');

/**
* if host path in the volume string (e.g. `./mylocal/file:/container/file`) is not absolute path,
* this function will convert it to an absolute path using `cwd` (current working directory) parameter.
* Otherwise, it will return volume string as it is.
*
* @param {*} volumeStr
* @param {*} cwd
*/
function standardizeVolumeStr(volumeStr, cwd) {
if (typeof volumeStr !== 'string' || volumeStr.length < 1) {
return volumeStr;
}
volumeStr = volumeStr.trim();
if (
volumeStr.substring(0, 2) !== './' &&
volumeStr.substring(0, 3) !== '../'
) {
return volumeStr;
}
const parts = volumeStr.split(':');
if (parts.length !== 2) {
return volumeStr;
}
if (!cwd) {
throw new Error(
'Current working dir path not available when local path is a relative path: ' +
parts[0]
);
}
const localPath = parts[0];
return path.resolve(cwd, localPath) + ':' + parts[1];
}

module.exports = {
buildPorts: function (servicePorts, output) {
var ports = {};
Expand Down Expand Up @@ -41,15 +74,19 @@ module.exports = {

for (let index in split_port_split0_array) {
ports[split_port_split1_array[index] + '/tcp'] = [
{ HostPort: split_port_split0_array[index].toString() },
{
HostPort: split_port_split0_array[index].toString(),
},
];
}
} else if (port_split[0].includes('-')) {
// "3000-3005"
let split_port_split = port_split[0].split('-');
ports[port_split[1] + '/tcp'] = [];
for (let i = split_port_split[0]; i <= split_port_split[1]; i++) {
ports[port_split[1] + '/tcp'].push({ HostPort: i.toString() });
ports[port_split[1] + '/tcp'].push({
HostPort: i.toString(),
});
}
} else if (port_split[1].includes('/')) {
// "6060:6060/udp"
Expand Down Expand Up @@ -114,7 +151,7 @@ module.exports = {
},

//ToDo: complete the compose specification
buildHostConfig: function (projectName, service, recipe) {
buildHostConfig: function (projectName, service, recipe, cwd) {
var output = {
RestartPolicy: { Name: service.restart },
};
Expand All @@ -123,12 +160,24 @@ module.exports = {
for (var volume_from of service.volumes_from) {
var vf = volume_from.split(':');
var svf = recipe.services[vf[0]];
this.buildVolumesHostconfig(projectName, svf.volumes, output, vf[1]);
this.buildVolumesHostconfig(
projectName,
svf.volumes,
output,
vf[1],
cwd
);
}
}

if (service.volumes !== undefined) {
this.buildVolumesHostconfig(projectName, service.volumes, output);
this.buildVolumesHostconfig(
projectName,
service.volumes,
output,
undefined,
cwd
);
}

if (service.ports !== undefined) {
Expand Down Expand Up @@ -334,22 +383,46 @@ module.exports = {
return output;
},

buildVolumesHostconfig: function (projectName, volumes, output, type) {
buildVolumesHostconfig: function (projectName, volumes, output, type, cwd) {
if (output['Binds'] === undefined) {
output['Binds'] = [];
}
for (var volume of volumes) {
if (typeof volume === 'string' || volume instanceof String) {
var aux = projectName + '_' + volume;
if (type == 'ro') {
aux += ':ro';
if (
volume.substring(0, 2) === './' ||
volume.substring(0, 3) === '../' ||
volume[0] === '/'
) {
const stdVolume = standardizeVolumeStr(volume, cwd);
const aux = stdVolume;
if (type == 'ro') {
aux += ':ro';
}
output['Binds'].push(aux);
} else {
var aux = projectName + '_' + volume;
if (type == 'ro') {
aux += ':ro';
}
output['Binds'].push(aux);
}
output['Binds'].push(aux);
} else {
var volumestr = '';
if (volume.source && volume.target) {
volumestr +=
projectName + '_' + volume.source + ':' + volume.target + ':';
if (
volume.source.substring(0, 2) === './' ||
volume.source.substring(0, 3) === '../' ||
volume.source[0] === '/'
) {
volumestr += standardizeVolumeStr(
volume.source + ':' + volume.target,
cwd
);
} else {
volumestr +=
projectName + '_' + volume.source + ':' + volume.target + ':';
}
}
if (volume.read_only || type == 'ro') {
volumestr += 'ro,';
Expand All @@ -371,6 +444,9 @@ module.exports = {
opts['Volumes'] = {};
}
for (var volume of volumes) {
if (volume.substring(0, 2) === './' || volume[0] === '/') {
continue;
}
if (typeof volume === 'string' || volume instanceof String) {
var v = volume.split(':');
opts['Volumes'][v[1]] = {};
Expand Down