From 62eb47b3cb0b8e95ab687fe7eb5109bcf7cf28e5 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Thu, 17 Jan 2019 21:59:43 +0200 Subject: [PATCH] fix(container-creation): revert container state if creation failed (#2565) * fix(container): rename old container only if exist * fix(container): remove new container only if created * style(container): fix typo Co-Authored-By: chiptus --- .../create/createContainerController.js | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/app/docker/views/containers/create/createContainerController.js b/app/docker/views/containers/create/createContainerController.js index 552d9e65c..cf96c9b3c 100644 --- a/app/docker/views/containers/create/createContainerController.js +++ b/app/docker/views/containers/create/createContainerController.js @@ -633,9 +633,9 @@ function ($q, $scope, $state, $timeout, $transition$, $filter, Container, Contai function create() { var oldContainer = null; - HttpRequestHelper.setPortainerAgentTargetHeader($scope.formValues.NodeName); return findCurrentContainer() + .then(setOldContainer) .then(confirmCreateContainer) .then(startCreationProcess) .catch(notifyOnError) @@ -645,6 +645,11 @@ function ($q, $scope, $state, $timeout, $transition$, $filter, Container, Contai $scope.state.actionInProgress = false; } + function setOldContainer(container) { + oldContainer = container; + return container; + } + function findCurrentContainer() { return Container.query({ all: 1, filters: { name: ['^/' + $scope.config.name + '$'] } }) .$promise @@ -652,8 +657,7 @@ function ($q, $scope, $state, $timeout, $transition$, $filter, Container, Contai if (!containers.length) { return; } - oldContainer = containers[0]; - return oldContainer; + return containers[0]; }) .catch(notifyOnError); @@ -676,7 +680,36 @@ function ($q, $scope, $state, $timeout, $transition$, $filter, Container, Contai .then(applyResourceControl) .then(connectToExtraNetworks) .then(removeOldContainer) - .then(onSuccess); + .then(onSuccess) + .catch(onCreationProcessFail); + } + + function onCreationProcessFail(error) { + var deferred = $q.defer(); + removeNewContainer() + .then(restoreOldContainerName) + .then(function() { + deferred.reject(error); + }) + .catch(function(restoreError) { + deferred.reject(restoreError); + }); + return deferred.promise; + } + + function removeNewContainer() { + return findCurrentContainer().then(function onContainerLoaded(container) { + if (container && (!oldContainer || container.Id !== oldContainer.Id)) { + return ContainerService.remove(container, true); + } + }); + } + + function restoreOldContainerName() { + if (!oldContainer) { + return; + } + return ContainerService.renameContainer(oldContainer.Id, oldContainer.Names[0].substring(1)); } function confirmCreateContainer(container) {