2014-11-12 17:54:49 +00:00
|
|
|
angular.module('containers', [])
|
2017-06-01 08:14:55 +00:00
|
|
|
.controller('ContainersController', ['$q', '$scope', '$filter', 'Container', 'ContainerService', 'ContainerHelper', 'Info', 'Notifications', 'Pagination', 'EntityListService', 'ModalService', 'ResourceControlService', 'EndpointProvider',
|
|
|
|
function ($q, $scope, $filter, Container, ContainerService, ContainerHelper, Info, Notifications, Pagination, EntityListService, ModalService, ResourceControlService, EndpointProvider) {
|
2016-06-02 05:34:03 +00:00
|
|
|
$scope.state = {};
|
2017-01-24 01:28:40 +00:00
|
|
|
$scope.state.pagination_count = Pagination.getPaginationCount('containers');
|
2017-06-01 08:14:55 +00:00
|
|
|
$scope.state.displayAll = true;
|
2016-07-12 22:53:03 +00:00
|
|
|
$scope.state.displayIP = false;
|
2016-06-29 09:04:29 +00:00
|
|
|
$scope.sortType = 'State';
|
2016-08-23 22:58:55 +00:00
|
|
|
$scope.sortReverse = false;
|
2016-06-02 05:34:03 +00:00
|
|
|
$scope.state.selectedItemCount = 0;
|
|
|
|
$scope.order = function (sortType) {
|
|
|
|
$scope.sortReverse = ($scope.sortType === sortType) ? !$scope.sortReverse : false;
|
|
|
|
$scope.sortType = sortType;
|
|
|
|
};
|
2017-05-01 10:19:43 +00:00
|
|
|
$scope.PublicURL = EndpointProvider.endpointPublicURL();
|
2016-06-02 05:34:03 +00:00
|
|
|
|
2017-01-24 01:28:40 +00:00
|
|
|
$scope.changePaginationCount = function() {
|
|
|
|
Pagination.setPaginationCount('containers', $scope.state.pagination_count);
|
|
|
|
};
|
|
|
|
|
2017-05-01 10:18:06 +00:00
|
|
|
$scope.cleanAssociatedVolumes = false;
|
|
|
|
|
2016-09-07 04:38:54 +00:00
|
|
|
var update = function (data) {
|
2016-07-07 01:17:44 +00:00
|
|
|
$('#loadContainersSpinner').show();
|
2016-06-02 05:34:03 +00:00
|
|
|
$scope.state.selectedItemCount = 0;
|
|
|
|
Container.query(data, function (d) {
|
2016-06-23 22:11:49 +00:00
|
|
|
var containers = d;
|
|
|
|
$scope.containers = containers.map(function (container) {
|
2016-07-12 22:53:03 +00:00
|
|
|
var model = new ContainerViewModel(container);
|
2016-12-25 09:43:53 +00:00
|
|
|
model.Status = $filter('containerstatus')(model.Status);
|
|
|
|
|
2017-02-15 22:08:18 +00:00
|
|
|
EntityListService.rememberPreviousSelection($scope.containers, model, function onSelect(model){
|
|
|
|
$scope.selectItem(model);
|
|
|
|
});
|
|
|
|
|
2016-07-12 22:53:03 +00:00
|
|
|
if (model.IP) {
|
|
|
|
$scope.state.displayIP = true;
|
|
|
|
}
|
2017-01-22 23:14:34 +00:00
|
|
|
if ($scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM') {
|
2016-08-10 06:11:36 +00:00
|
|
|
model.hostIP = $scope.swarm_hosts[_.split(container.Names[0], '/')[1]];
|
|
|
|
}
|
2016-07-12 22:53:03 +00:00
|
|
|
return model;
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
$('#loadContainersSpinner').hide();
|
2016-10-08 01:59:58 +00:00
|
|
|
}, function (e) {
|
|
|
|
$('#loadContainersSpinner').hide();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to retrieve containers');
|
2016-10-08 01:59:58 +00:00
|
|
|
$scope.containers = [];
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var batch = function (items, action, msg) {
|
2016-07-07 01:17:44 +00:00
|
|
|
$('#loadContainersSpinner').show();
|
2016-06-02 05:34:03 +00:00
|
|
|
var counter = 0;
|
|
|
|
var complete = function () {
|
|
|
|
counter = counter - 1;
|
|
|
|
if (counter === 0) {
|
2016-07-07 01:17:44 +00:00
|
|
|
$('#loadContainersSpinner').hide();
|
2017-06-01 08:14:55 +00:00
|
|
|
update({all: $scope.state.displayAll ? 1 : 0});
|
2016-06-02 05:34:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
angular.forEach(items, function (c) {
|
|
|
|
if (c.Checked) {
|
2016-06-29 10:11:22 +00:00
|
|
|
counter = counter + 1;
|
2016-06-02 05:34:03 +00:00
|
|
|
if (action === Container.start) {
|
2016-09-02 01:51:49 +00:00
|
|
|
action({id: c.Id}, {}, function (d) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.success('Container ' + msg, c.Id);
|
2016-09-02 01:51:49 +00:00
|
|
|
complete();
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to start container');
|
2016-06-02 05:34:03 +00:00
|
|
|
complete();
|
|
|
|
});
|
|
|
|
}
|
2016-06-29 10:11:22 +00:00
|
|
|
else if (action === Container.remove) {
|
2017-05-23 18:56:10 +00:00
|
|
|
ContainerService.remove(c, $scope.cleanAssociatedVolumes)
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Container successfully removed');
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to remove container');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
2016-06-29 10:11:22 +00:00
|
|
|
complete();
|
|
|
|
});
|
|
|
|
}
|
2017-01-25 23:11:38 +00:00
|
|
|
else if (action === Container.pause) {
|
|
|
|
action({id: c.Id}, function (d) {
|
|
|
|
if (d.message) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.success('Container is already paused', c.Id);
|
2017-01-25 23:11:38 +00:00
|
|
|
} else {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.success('Container ' + msg, c.Id);
|
2017-01-25 23:11:38 +00:00
|
|
|
}
|
|
|
|
complete();
|
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to pause container');
|
2017-01-25 23:11:38 +00:00
|
|
|
complete();
|
|
|
|
});
|
|
|
|
}
|
2016-06-02 05:34:03 +00:00
|
|
|
else {
|
|
|
|
action({id: c.Id}, function (d) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.success('Container ' + msg, c.Id);
|
2016-06-02 05:34:03 +00:00
|
|
|
complete();
|
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'An error occured');
|
2016-06-02 05:34:03 +00:00
|
|
|
complete();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (counter === 0) {
|
2016-07-07 01:17:44 +00:00
|
|
|
$('#loadContainersSpinner').hide();
|
2016-06-02 05:34:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-04 20:15:41 +00:00
|
|
|
$scope.selectItems = function (allSelected) {
|
|
|
|
angular.forEach($scope.state.filteredContainers, function (container) {
|
|
|
|
if (container.Checked !== allSelected) {
|
|
|
|
container.Checked = allSelected;
|
|
|
|
$scope.selectItem(container);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-06-02 05:34:03 +00:00
|
|
|
$scope.selectItem = function (item) {
|
|
|
|
if (item.Checked) {
|
|
|
|
$scope.state.selectedItemCount++;
|
|
|
|
} else {
|
|
|
|
$scope.state.selectedItemCount--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.toggleGetAll = function () {
|
2017-06-01 08:14:55 +00:00
|
|
|
update({all: $scope.state.displayAll ? 1 : 0});
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.startAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.start, 'Started');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.stopAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.stop, 'Stopped');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.restartAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.restart, 'Restarted');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.killAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.kill, 'Killed');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.pauseAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.pause, 'Paused');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.unpauseAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.unpause, 'Unpaused');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeAction = function () {
|
2017-05-23 18:56:10 +00:00
|
|
|
batch($scope.containers, Container.remove, 'Removed');
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
|
|
|
|
2017-04-25 08:20:57 +00:00
|
|
|
$scope.confirmRemoveAction = function () {
|
|
|
|
var isOneContainerRunning = false;
|
|
|
|
angular.forEach($scope.containers, function (c) {
|
|
|
|
if (c.Checked && c.State === 'running') {
|
|
|
|
isOneContainerRunning = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2017-05-01 10:18:06 +00:00
|
|
|
var title = 'You are about to remove one or more container.';
|
2017-04-25 08:20:57 +00:00
|
|
|
if (isOneContainerRunning) {
|
2017-05-01 10:18:06 +00:00
|
|
|
title = 'You are about to remove one or more running containers.';
|
2017-04-25 08:20:57 +00:00
|
|
|
}
|
2017-05-01 10:18:06 +00:00
|
|
|
ModalService.confirmContainerDeletion(
|
|
|
|
title,
|
|
|
|
function (result) {
|
|
|
|
if(!result) { return; }
|
|
|
|
$scope.cleanAssociatedVolumes = false;
|
|
|
|
if (result[0]) {
|
|
|
|
$scope.cleanAssociatedVolumes = true;
|
|
|
|
}
|
|
|
|
$scope.removeAction();
|
|
|
|
}
|
|
|
|
);
|
2017-04-25 08:20:57 +00:00
|
|
|
};
|
|
|
|
|
2016-08-10 06:11:36 +00:00
|
|
|
function retrieveSwarmHostsInfo(data) {
|
|
|
|
var swarm_hosts = {};
|
|
|
|
var systemStatus = data.SystemStatus;
|
|
|
|
var node_count = parseInt(systemStatus[3][1], 10);
|
|
|
|
var node_offset = 4;
|
|
|
|
for (i = 0; i < node_count; i++) {
|
|
|
|
var host = {};
|
|
|
|
host.name = _.trim(systemStatus[node_offset][0]);
|
|
|
|
host.ip = _.split(systemStatus[node_offset][1], ':')[0];
|
|
|
|
swarm_hosts[host.name] = host.ip;
|
|
|
|
node_offset += 9;
|
|
|
|
}
|
|
|
|
return swarm_hosts;
|
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
function initView(){
|
2017-01-22 23:14:34 +00:00
|
|
|
if ($scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM') {
|
2016-08-10 06:11:36 +00:00
|
|
|
Info.get({}, function (d) {
|
2016-12-15 03:33:47 +00:00
|
|
|
$scope.swarm_hosts = retrieveSwarmHostsInfo(d);
|
2017-06-01 08:14:55 +00:00
|
|
|
update({all: $scope.state.displayAll ? 1 : 0});
|
2016-08-10 06:11:36 +00:00
|
|
|
});
|
|
|
|
} else {
|
2017-06-01 08:14:55 +00:00
|
|
|
update({all: $scope.state.displayAll ? 1 : 0});
|
2016-08-10 06:11:36 +00:00
|
|
|
}
|
2017-06-01 08:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initView();
|
2016-06-02 05:34:03 +00:00
|
|
|
}]);
|