fix: volume names for undefined volume names in compose

This commit is contained in:
Andras Bacsai 2022-11-14 11:26:12 +01:00
parent 47c0d522db
commit 66276be1d2
2 changed files with 26 additions and 13 deletions

View File

@ -38,9 +38,10 @@ export default async function (data) {
if (!dockerComposeYaml.services) {
throw 'No Services found in docker-compose file.'
}
const envs = [
`PORT=${port}`
];
const envs = [];
if (Object.entries(dockerComposeYaml.services).length === 1) {
envs.push(`PORT=${port}`)
}
if (secrets.length > 0) {
secrets.forEach((secret) => {
if (pullmergeRequestId) {
@ -65,30 +66,36 @@ export default async function (data) {
//
}
const composeVolumes = [];
for (const volume of volumes) {
let [v, _] = volume.split(':');
composeVolumes[v] = {
name: v,
if (volumes.length > 0) {
for (const volume of volumes) {
let [v, path] = volume.split(':');
composeVolumes[v] = {
name: v,
}
}
}
let networks = {}
for (let [key, value] of Object.entries(dockerComposeYaml.services)) {
value['container_name'] = `${applicationId}-${key}`
value['env_file'] = envFound ? [`${workdir}/.env`] : []
value['labels'] = labels
// TODO: If we support separated volume for each service, we need to add it here
if (value['volumes'].length > 0) {
if (value['volumes']?.length > 0) {
value['volumes'] = value['volumes'].map((volume) => {
let [v, path, permission] = volume.split(':');
v = `${applicationId}-${v}`
if (!path) {
path = v;
v = `${applicationId}${v.replace(/\//gi, '-')}`
} else {
v = `${applicationId}-${v}`
}
composeVolumes[v] = {
name: v
}
return `${v}:${path}${permission ? ':' + permission : ''}`
})
}
if (volumes.length > 0) {
for (const volume of volumes) {
value['volumes'].push(volume)
@ -106,6 +113,7 @@ export default async function (data) {
}
value['networks'] = [...value['networks'] || '', network]
dockerComposeYaml.services[key] = { ...dockerComposeYaml.services[key], restart: defaultComposeConfiguration(network).restart, deploy: defaultComposeConfiguration(network).deploy }
}
if (Object.keys(composeVolumes).length > 0) {
dockerComposeYaml['volumes'] = { ...composeVolumes }

View File

@ -27,7 +27,7 @@
import { get } from '$lib/api';
import { t } from '$lib/translations';
import Explainer from '$lib/components/Explainer.svelte';
let composeJson = JSON.parse(application?.dockerComposeFile || '{}');
let predefinedVolumes: any[] = [];
if (composeJson?.services) {
@ -35,7 +35,12 @@
if (service?.volumes) {
for (const [_, volumeName] of Object.entries(service.volumes)) {
let [volume, target] = volumeName.split(':');
volume = `${application.id}-${volume}`;
if (!target) {
target = volume;
volume = `${application.id}${volume.replace(/\//gi, '-')}`;
} else {
volume = `${application.id}-${volume}`;
}
predefinedVolumes.push({ id: volume, path: target, predefined: true });
}
}