diff --git a/app/portainer/helpers/stackHelper.js b/app/portainer/helpers/stackHelper.js index bfaf24b9f..63e959e84 100644 --- a/app/portainer/helpers/stackHelper.js +++ b/app/portainer/helpers/stackHelper.js @@ -23,28 +23,45 @@ angular.module('portainer.app').factory('StackHelper', [ ); } - helper.validateYAML = function (yaml, containerNames) { - let yamlObject; - - try { - yamlObject = YAML.parse(yaml); - } catch (err) { - return 'There is an error in the yaml syntax: ' + err; - } - - const names = _.uniq(GenericHelper.findDeepAll(yamlObject, 'container_name')); - const duplicateContainers = _.intersection(containerNames, names); - - if (duplicateContainers.length === 0) return; - - return ( - (duplicateContainers.length === 1 ? 'This container name is' : 'These container names are') + - ' already used by another container running in this environment: ' + - _.join(duplicateContainers, ', ') + - '.' - ); - }; + helper.validateYAML = validateYAML; return helper; }, ]); + +function validateYAML(yaml, containerNames, originalContainersNames = []) { + let yamlObject; + + try { + yamlObject = YAML.parse(yaml); + } catch (err) { + return 'There is an error in the yaml syntax: ' + err; + } + + const names = _.uniq(GenericHelper.findDeepAll(yamlObject, 'container_name')); + + const duplicateContainers = _.intersection(_.difference(containerNames, originalContainersNames), names); + + if (duplicateContainers.length === 0) { + return ''; + } + + return ( + (duplicateContainers.length === 1 ? 'This container name is' : 'These container names are') + + ' already used by another container running in this environment: ' + + _.join(duplicateContainers, ', ') + + '.' + ); +} + +export function extractContainerNames(yaml = '') { + let yamlObject; + + try { + yamlObject = YAML.parse(yaml); + } catch (err) { + return []; + } + + return _.uniq(GenericHelper.findDeepAll(yamlObject, 'container_name')); +} diff --git a/app/portainer/views/stacks/edit/stack.html b/app/portainer/views/stacks/edit/stack.html index 5af957241..adfeb9d02 100644 --- a/app/portainer/views/stacks/edit/stack.html +++ b/app/portainer/views/stacks/edit/stack.html @@ -141,6 +141,9 @@ You can get more information about Compose file format in the official documentation. +