2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-02-28 06:19:06 +00:00
|
|
|
.controller('ContainerController', ['$q', '$scope', '$state','$transition$', '$filter', 'Container', 'Commit', 'ContainerHelper', 'ContainerService', 'ImageHelper', 'Network', 'NetworkService', 'Notifications', 'ModalService', 'ResourceControlService', 'RegistryService', 'ImageService',
|
|
|
|
function ($q, $scope, $state, $transition$, $filter, Container, Commit, ContainerHelper, ContainerService, ImageHelper, Network, NetworkService, Notifications, ModalService, ResourceControlService, RegistryService, ImageService) {
|
2016-08-19 06:41:45 +00:00
|
|
|
$scope.activityTime = 0;
|
|
|
|
$scope.portBindings = [];
|
2017-12-06 11:04:02 +00:00
|
|
|
|
2016-08-19 06:41:45 +00:00
|
|
|
$scope.config = {
|
|
|
|
Image: '',
|
|
|
|
Registry: ''
|
2016-06-02 05:34:03 +00:00
|
|
|
};
|
2017-12-06 11:04:02 +00:00
|
|
|
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state = {
|
2017-12-05 16:46:11 +00:00
|
|
|
recreateContainerInProgress: false,
|
2017-11-12 19:27:28 +00:00
|
|
|
joinNetworkInProgress: false,
|
2017-12-06 11:04:02 +00:00
|
|
|
leaveNetworkInProgress: false
|
2017-01-24 01:28:40 +00:00
|
|
|
};
|
2016-06-02 05:34:03 +00:00
|
|
|
|
|
|
|
var update = function () {
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.get({id: $transition$.params().id}, function (d) {
|
2017-05-23 18:56:10 +00:00
|
|
|
var container = new ContainerDetailsViewModel(d);
|
|
|
|
$scope.container = container;
|
2016-06-02 05:34:03 +00:00
|
|
|
$scope.container.edit = false;
|
2017-05-23 18:56:10 +00:00
|
|
|
$scope.container.newContainerName = $filter('trimcontainername')(container.Name);
|
2016-06-02 05:34:03 +00:00
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
if (container.State.Running) {
|
|
|
|
$scope.activityTime = moment.duration(moment(container.State.StartedAt).utc().diff(moment().utc())).humanize();
|
|
|
|
} else if (container.State.Status === 'created') {
|
|
|
|
$scope.activityTime = moment.duration(moment(container.Created).utc().diff(moment().utc())).humanize();
|
2016-08-19 06:41:45 +00:00
|
|
|
} else {
|
2017-05-23 18:56:10 +00:00
|
|
|
$scope.activityTime = moment.duration(moment().utc().diff(moment(container.State.FinishedAt).utc())).humanize();
|
2016-06-02 05:34:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 06:41:45 +00:00
|
|
|
$scope.portBindings = [];
|
2017-05-23 18:56:10 +00:00
|
|
|
if (container.NetworkSettings.Ports) {
|
|
|
|
angular.forEach(Object.keys(container.NetworkSettings.Ports), function(portMapping) {
|
|
|
|
if (container.NetworkSettings.Ports[portMapping]) {
|
2016-08-19 06:41:45 +00:00
|
|
|
var mapping = {};
|
|
|
|
mapping.container = portMapping;
|
2017-05-23 18:56:10 +00:00
|
|
|
mapping.host = container.NetworkSettings.Ports[portMapping][0].HostIp + ':' + container.NetworkSettings.Ports[portMapping][0].HostPort;
|
2016-08-19 06:41:45 +00:00
|
|
|
$scope.portBindings.push(mapping);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to retrieve container info');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.start = function () {
|
2016-09-02 01:51:49 +00:00
|
|
|
Container.start({id: $scope.container.Id}, {}, function (d) {
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container started', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to start container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.stop = function () {
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.stop({id: $transition$.params().id}, function (d) {
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container stopped', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to stop container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.kill = function () {
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.kill({id: $transition$.params().id}, function (d) {
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container killed', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to kill container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.commit = function () {
|
2016-12-31 00:25:42 +00:00
|
|
|
var image = $scope.config.Image;
|
|
|
|
var registry = $scope.config.Registry;
|
2017-07-05 17:06:28 +00:00
|
|
|
var imageConfig = ImageHelper.createImageConfigForCommit(image, registry.URL);
|
2018-02-28 06:19:06 +00:00
|
|
|
Commit.commitContainer({id: $transition$.params().id, tag: imageConfig.tag, repo: imageConfig.repo}, function (d) {
|
2016-09-02 05:40:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container commited', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
2016-09-02 05:40:03 +00:00
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to commit container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
2016-08-19 06:41:45 +00:00
|
|
|
|
2016-06-02 05:34:03 +00:00
|
|
|
$scope.pause = function () {
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.pause({id: $transition$.params().id}, function (d) {
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container paused', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to pause container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.unpause = function () {
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.unpause({id: $transition$.params().id}, function (d) {
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container unpaused', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to unpause container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-25 08:20:57 +00:00
|
|
|
$scope.confirmRemove = function () {
|
2017-05-01 10:18:06 +00:00
|
|
|
var title = 'You are about to remove a container.';
|
2017-04-25 08:20:57 +00:00
|
|
|
if ($scope.container.State.Running) {
|
2017-05-01 10:18:06 +00:00
|
|
|
title = 'You are about to remove a running container.';
|
2017-04-25 08:20:57 +00:00
|
|
|
}
|
2017-05-01 10:18:06 +00:00
|
|
|
ModalService.confirmContainerDeletion(
|
|
|
|
title,
|
|
|
|
function (result) {
|
|
|
|
if(!result) { return; }
|
|
|
|
var cleanAssociatedVolumes = false;
|
|
|
|
if (result[0]) {
|
|
|
|
cleanAssociatedVolumes = true;
|
|
|
|
}
|
|
|
|
$scope.remove(cleanAssociatedVolumes);
|
|
|
|
}
|
|
|
|
);
|
2017-04-25 08:20:57 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 10:18:06 +00:00
|
|
|
$scope.remove = function(cleanAssociatedVolumes) {
|
2017-05-23 18:56:10 +00:00
|
|
|
ContainerService.remove($scope.container, cleanAssociatedVolumes)
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Container successfully removed');
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.containers', {}, {reload: true});
|
2017-05-23 18:56:10 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to remove container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.restart = function () {
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.restart({id: $transition$.params().id}, function (d) {
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container restarted', $transition$.params().id);
|
2016-06-02 05:34:03 +00:00
|
|
|
}, function (e) {
|
|
|
|
update();
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to restart container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.renameContainer = function () {
|
2017-06-01 08:23:59 +00:00
|
|
|
var container = $scope.container;
|
2017-09-21 14:00:53 +00:00
|
|
|
Container.rename({id: $transition$.params().id, 'name': container.newContainerName}, function (d) {
|
2017-06-01 08:23:59 +00:00
|
|
|
if (d.message) {
|
|
|
|
container.newContainerName = container.Name;
|
|
|
|
Notifications.error('Unable to rename container', {}, d.message);
|
2016-09-02 03:25:20 +00:00
|
|
|
} else {
|
2017-06-01 08:23:59 +00:00
|
|
|
container.Name = container.newContainerName;
|
|
|
|
Notifications.success('Container successfully renamed', container.Name);
|
2016-09-02 03:25:20 +00:00
|
|
|
}
|
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to rename container');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
$scope.container.edit = false;
|
|
|
|
};
|
|
|
|
|
2016-11-07 04:36:00 +00:00
|
|
|
$scope.containerLeaveNetwork = function containerLeaveNetwork(container, networkId) {
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state.leaveNetworkInProgress = true;
|
2017-09-21 14:00:53 +00:00
|
|
|
Network.disconnect({id: networkId}, { Container: $transition$.params().id, Force: false }, function (d) {
|
2017-05-23 18:56:10 +00:00
|
|
|
if (container.message) {
|
|
|
|
Notifications.error('Error', d, 'Unable to disconnect container from network');
|
2016-11-07 04:36:00 +00:00
|
|
|
} else {
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container left network', $transition$.params().id);
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.containers.container', {id: $transition$.params().id}, {reload: true});
|
2016-11-07 04:36:00 +00:00
|
|
|
}
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state.leaveNetworkInProgress = false;
|
2016-11-07 04:36:00 +00:00
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to disconnect container from network');
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state.leaveNetworkInProgress = false;
|
2016-11-07 04:36:00 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-13 10:17:41 +00:00
|
|
|
$scope.duplicate = function() {
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.containers.new', {from: $transition$.params().id}, {reload: true});
|
2017-08-13 10:17:41 +00:00
|
|
|
};
|
|
|
|
|
2017-08-13 10:55:52 +00:00
|
|
|
$scope.confirmRemove = function () {
|
|
|
|
var title = 'You are about to remove a container.';
|
|
|
|
if ($scope.container.State.Running) {
|
|
|
|
title = 'You are about to remove a running container.';
|
|
|
|
}
|
|
|
|
ModalService.confirmContainerDeletion(
|
|
|
|
title,
|
|
|
|
function (result) {
|
|
|
|
if(!result) { return; }
|
|
|
|
var cleanAssociatedVolumes = false;
|
|
|
|
if (result[0]) {
|
|
|
|
cleanAssociatedVolumes = true;
|
|
|
|
}
|
|
|
|
$scope.remove(cleanAssociatedVolumes);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function recreateContainer(pullImage) {
|
|
|
|
var container = $scope.container;
|
|
|
|
var config = ContainerHelper.configFromContainer(container.Model);
|
2017-12-05 16:46:11 +00:00
|
|
|
$scope.state.recreateContainerInProgress = true;
|
2017-08-13 10:55:52 +00:00
|
|
|
ContainerService.remove(container, true)
|
|
|
|
.then(function success() {
|
|
|
|
return RegistryService.retrieveRegistryFromRepository(container.Config.Image);
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
return $q.when(!pullImage || ImageService.pullImage(container.Config.Image, data, true));
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
return ContainerService.createAndStartContainer(config);
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
if (!container.ResourceControl) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
var containerIdentifier = data.Id;
|
|
|
|
var resourceControl = container.ResourceControl;
|
|
|
|
var users = resourceControl.UserAccesses.map(function(u) {
|
|
|
|
return u.UserId;
|
|
|
|
});
|
|
|
|
var teams = resourceControl.TeamAccesses.map(function(t) {
|
|
|
|
return t.TeamId;
|
|
|
|
});
|
|
|
|
return ResourceControlService.createResourceControl(resourceControl.AdministratorsOnly,
|
|
|
|
users, teams, containerIdentifier, 'container', []);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
Notifications.success('Container successfully re-created');
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.containers', {}, {reload: true});
|
2017-08-13 10:55:52 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to re-create container');
|
2017-12-05 16:46:11 +00:00
|
|
|
$scope.state.recreateContainerInProgress = false;
|
2017-08-13 10:55:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-13 10:17:41 +00:00
|
|
|
$scope.recreate = function() {
|
2017-11-07 08:20:59 +00:00
|
|
|
ModalService.confirmContainerRecreation(function (result) {
|
|
|
|
if(!result) { return; }
|
|
|
|
var pullImage = false;
|
|
|
|
if (result[0]) {
|
|
|
|
pullImage = true;
|
|
|
|
}
|
|
|
|
recreateContainer(pullImage);
|
2017-08-13 10:17:41 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-29 13:49:35 +00:00
|
|
|
$scope.containerJoinNetwork = function containerJoinNetwork(container, networkId) {
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state.joinNetworkInProgress = true;
|
2017-09-21 14:00:53 +00:00
|
|
|
Network.connect({id: networkId}, { Container: $transition$.params().id }, function (d) {
|
2017-06-29 13:49:35 +00:00
|
|
|
if (container.message) {
|
|
|
|
Notifications.error('Error', d, 'Unable to connect container to network');
|
|
|
|
} else {
|
2017-09-21 14:00:53 +00:00
|
|
|
Notifications.success('Container joined network', $transition$.params().id);
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.containers.container', {id: $transition$.params().id}, {reload: true});
|
2017-06-29 13:49:35 +00:00
|
|
|
}
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state.joinNetworkInProgress = false;
|
2017-06-29 13:49:35 +00:00
|
|
|
}, function (e) {
|
|
|
|
Notifications.error('Failure', e, 'Unable to connect container to network');
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state.joinNetworkInProgress = false;
|
2017-06-29 13:49:35 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-11 07:46:55 +00:00
|
|
|
var provider = $scope.applicationState.endpoint.mode.provider;
|
|
|
|
var apiVersion = $scope.applicationState.endpoint.apiVersion;
|
|
|
|
NetworkService.networks(
|
|
|
|
provider === 'DOCKER_STANDALONE' || provider === 'DOCKER_SWARM_MODE',
|
|
|
|
false,
|
2018-04-04 00:31:04 +00:00
|
|
|
provider === 'DOCKER_SWARM_MODE' && apiVersion >= 1.25
|
2017-08-11 07:46:55 +00:00
|
|
|
)
|
|
|
|
.then(function success(data) {
|
|
|
|
var networks = data;
|
|
|
|
$scope.availableNetworks = networks;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve networks');
|
|
|
|
});
|
2017-06-29 13:49:35 +00:00
|
|
|
|
2016-06-02 05:34:03 +00:00
|
|
|
update();
|
|
|
|
}]);
|