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 <chiptus@users.noreply.github.com>
fix2626-endpoint-management-cmd-line
Chaim Lev-Ari 2019-01-17 21:59:43 +02:00 committed by Anthony Lapenna
parent 808eb7d341
commit 62eb47b3cb
1 changed files with 37 additions and 4 deletions

View File

@ -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) {