feat(container-details): add the ability to update restart policy

pull/2193/head
Chaim Lev-Ari 2018-08-16 10:31:00 +01:00 committed by Anthony Lapenna
parent de9f99d030
commit 8769fadd5c
7 changed files with 130 additions and 13 deletions

View File

@ -0,0 +1,36 @@
angular
.module('portainer.docker')
.controller('ContainerRestartPolicyController', [
function ContainerRestartPolicyController() {
var ctrl = this;
this.state = {
editMode :false,
editModel :{}
};
ctrl.toggleEdit = toggleEdit;
ctrl.save = save;
function toggleEdit() {
ctrl.state.editMode = true;
ctrl.state.editModel = {
name: ctrl.name,
maximumRetryCount: ctrl.maximumRetryCount
};
}
function save() {
if (ctrl.state.editModel.name === ctrl.name &&
ctrl.state.editModel.maximumRetryCount === ctrl.maximumRetryCount) {
ctrl.state.editMode = false;
return;
}
ctrl.updateRestartPolicy(ctrl.state.editModel)
.then(function onUpdateSucceed() {
ctrl.state.editMode = false;
});
}
}
]);

View File

@ -0,0 +1,45 @@
<div>
<table class="table table-bordered table-condensed" ng-if="!$ctrl.state.editMode">
<tr>
<td class="col-md-3">
<a href="" data-toggle="tooltip" title="Edit restart policy" ng-click="$ctrl.toggleEdit()">
<i class="fa fa-edit"></i>
</a>
<span>Name</span>
</td>
<td>{{$ctrl.name }}</td>
</tr>
<tr ng-if="$ctrl.name === 'on-failure'">
<td class="col-md-3">Maximum Retry Count</td>
<td>
{{ $ctrl.maximumRetryCount }}
</td>
</tr>
</table>
<table class="table table-bordered table-condensed" ng-if="$ctrl.state.editMode">
<tr>
<td class="col-md-3">
<span>Name</span>
</td>
<td>
<select class="form-control" ng-model="$ctrl.state.editModel.name">
<option value="no">None</option>
<option value="on-failure">On Failure</option>
<option value="always">Always</option>
<option value="unless-stopped">Unless Stopped</option>
</select>
</td>
<td class="col-md-2">
<button class="btn btn-success" ng-click="$ctrl.save()">Save</button>
</td>
</tr>
<tr ng-if="$ctrl.state.editModel.name === 'on-failure'">
<td class="col-md-3">Maximum Retry Count</td>
<td>
<input type="number" class="form-control" ng-model="$ctrl.state.editModel.maximumRetryCount" />
</td>
<td class="col-md-2"></td>
</tr>
</table>
</div>

View File

@ -0,0 +1,10 @@
angular.module('portainer.docker')
.component('containerRestartPolicy', {
templateUrl: 'app/docker/components/container-restart-policy/container-restart-policy.html',
controller: 'ContainerRestartPolicyController',
bindings: {
'name': '<',
'maximumRetryCount': '<',
'updateRestartPolicy': '&'
}
});

View File

@ -65,6 +65,9 @@ function ContainerFactory($resource, API_ENDPOINT_ENDPOINTS, EndpointProvider) {
},
inspect: {
method: 'GET', params: { id: '@id', action: 'json' }
},
update: {
method: 'POST', params: { id: '@id', action: 'update'}
}
});
}]);

View File

@ -63,6 +63,14 @@ function ContainerServiceFactory($q, Container, ResourceControlService, LogHelpe
return Container.rename({id: id, name: newContainerName }, {}).$promise;
};
service.updateRestartPolicy = updateRestartPolicy;
function updateRestartPolicy(id, restartPolicy, maximumRetryCounts) {
return Container.update({ id: id },
{ RestartPolicy: { Name: restartPolicy, MaximumRetryCount: maximumRetryCounts } }
).$promise;
}
service.createContainer = function(configuration) {
var deferred = $q.defer();
Container.create(configuration).$promise

View File

@ -223,21 +223,13 @@
</table>
</td>
</tr>
<tr ng-if="container.HostConfig.RestartPolicy.Name !== 'no'">
<tr>
<td>Restart policies</td>
<td>
<table class="table table-bordered table-condensed">
<tr>
<td class="col-md-3">Name</td>
<td>{{ container.HostConfig.RestartPolicy.Name }}</td>
</tr>
<tr>
<td class="col-md-3">MaximumRetryCount</td>
<td>
{{ container.HostConfig.RestartPolicy.MaximumRetryCount }}
</td>
</tr>
</table>
<container-restart-policy
name="container.HostConfig.RestartPolicy.Name"
maximum-retry-count="container.HostConfig.RestartPolicy.MaximumRetryCount"
update-restart-policy="updateRestartPolicy(name, maximumRetryCount)">
</td>
</tr>
</tbody>

View File

@ -15,6 +15,8 @@ function ($q, $scope, $state, $transition$, $filter, Commit, ContainerHelper, Co
leaveNetworkInProgress: false
};
$scope.updateRestartPolicy = updateRestartPolicy;
var update = function () {
var nodeName = $transition$.params().nodeName;
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
@ -308,6 +310,27 @@ function ($q, $scope, $state, $transition$, $filter, Commit, ContainerHelper, Co
});
};
function updateRestartPolicy(restartPolicy, maximumRetryCount) {
maximumRetryCount = restartPolicy === 'on-failure' ? maximumRetryCount : undefined;
return ContainerService
.updateRestartPolicy($scope.container.Id, restartPolicy, maximumRetryCount)
.then(onUpdateSuccess)
.catch(notifyOnError);
function onUpdateSuccess() {
$scope.container.HostConfig.RestartPolicy = {
Name: restartPolicy,
MaximumRetryCount: maximumRetryCount
};
}
function notifyOnError(err) {
Notifications.error('Failure', err, 'Unable to update restart policy');
return $q.reject(err);
}
}
var provider = $scope.applicationState.endpoint.mode.provider;
var apiVersion = $scope.applicationState.endpoint.apiVersion;
NetworkService.networks(