2016-06-02 17:34:03 +12:00
angular . module ( 'container' , [ ] )
2017-08-13 12:17:41 +02:00
. controller ( 'ContainerController' , [ '$scope' , '$state' , '$stateParams' , '$filter' , 'Container' , 'ContainerCommit' , 'ContainerHelper' , 'ContainerService' , 'ImageHelper' , 'Network' , 'NetworkService' , 'Notifications' , 'Pagination' , 'ModalService' , 'ResourceControlService' , 'RegistryService' , 'ImageService' ,
function ( $scope , $state , $stateParams , $filter , Container , ContainerCommit , ContainerHelper , ContainerService , ImageHelper , Network , NetworkService , Notifications , Pagination , ModalService , ResourceControlService , RegistryService , ImageService ) {
2016-08-19 18:41:45 +12:00
$scope . activityTime = 0 ;
$scope . portBindings = [ ] ;
$scope . config = {
Image : '' ,
Registry : ''
2016-06-02 17:34:03 +12:00
} ;
2017-01-24 14:28:40 +13:00
$scope . state = { } ;
$scope . state . pagination _count = Pagination . getPaginationCount ( 'container_networks' ) ;
$scope . changePaginationCount = function ( ) {
Pagination . setPaginationCount ( 'container_networks' , $scope . state . pagination _count ) ;
} ;
2016-06-02 17:34:03 +12:00
var update = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-06-02 17:34:03 +12:00
Container . get ( { id : $stateParams . id } , function ( d ) {
2017-05-23 20:56:10 +02:00
var container = new ContainerDetailsViewModel ( d ) ;
$scope . container = container ;
2016-06-02 17:34:03 +12:00
$scope . container . edit = false ;
2017-05-23 20:56:10 +02:00
$scope . container . newContainerName = $filter ( 'trimcontainername' ) ( container . Name ) ;
2016-06-02 17:34:03 +12:00
2017-05-23 20:56:10 +02: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 18:41:45 +12:00
} else {
2017-05-23 20:56:10 +02:00
$scope . activityTime = moment . duration ( moment ( ) . utc ( ) . diff ( moment ( container . State . FinishedAt ) . utc ( ) ) ) . humanize ( ) ;
2016-06-02 17:34:03 +12:00
}
2016-08-19 18:41:45 +12:00
$scope . portBindings = [ ] ;
2017-05-23 20:56:10 +02:00
if ( container . NetworkSettings . Ports ) {
angular . forEach ( Object . keys ( container . NetworkSettings . Ports ) , function ( portMapping ) {
if ( container . NetworkSettings . Ports [ portMapping ] ) {
2016-08-19 18:41:45 +12:00
var mapping = { } ;
mapping . container = portMapping ;
2017-05-23 20:56:10 +02:00
mapping . host = container . NetworkSettings . Ports [ portMapping ] [ 0 ] . HostIp + ':' + container . NetworkSettings . Ports [ portMapping ] [ 0 ] . HostPort ;
2016-08-19 18:41:45 +12:00
$scope . portBindings . push ( mapping ) ;
}
} ) ;
}
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . hide ( ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . hide ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to retrieve container info' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . start = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-09-02 13:51:49 +12:00
Container . start ( { id : $scope . container . Id } , { } , function ( d ) {
2016-06-02 17:34:03 +12:00
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container started' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to start container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . stop = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-06-02 17:34:03 +12:00
Container . stop ( { id : $stateParams . id } , function ( d ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container stopped' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to stop container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . kill = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-06-02 17:34:03 +12:00
Container . kill ( { id : $stateParams . id } , function ( d ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container killed' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to kill container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . commit = function ( ) {
2016-08-19 18:41:45 +12:00
$ ( '#createImageSpinner' ) . show ( ) ;
2016-12-31 13:25:42 +13:00
var image = $scope . config . Image ;
var registry = $scope . config . Registry ;
2017-07-05 19:06:28 +02:00
var imageConfig = ImageHelper . createImageConfigForCommit ( image , registry . URL ) ;
2016-08-19 18:41:45 +12:00
ContainerCommit . commit ( { id : $stateParams . id , tag : imageConfig . tag , repo : imageConfig . repo } , function ( d ) {
$ ( '#createImageSpinner' ) . hide ( ) ;
2016-09-02 17:40:03 +12:00
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container commited' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
2016-08-19 18:41:45 +12:00
$ ( '#createImageSpinner' ) . hide ( ) ;
2016-09-02 17:40:03 +12:00
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to commit container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
2016-08-19 18:41:45 +12:00
2016-06-02 17:34:03 +12:00
$scope . pause = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-06-02 17:34:03 +12:00
Container . pause ( { id : $stateParams . id } , function ( d ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container paused' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to pause container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . unpause = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-06-02 17:34:03 +12:00
Container . unpause ( { id : $stateParams . id } , function ( d ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container unpaused' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to unpause container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
2017-04-25 10:20:57 +02:00
$scope . confirmRemove = function ( ) {
2017-05-01 12:18:06 +02:00
var title = 'You are about to remove a container.' ;
2017-04-25 10:20:57 +02:00
if ( $scope . container . State . Running ) {
2017-05-01 12:18:06 +02:00
title = 'You are about to remove a running container.' ;
2017-04-25 10:20:57 +02:00
}
2017-05-01 12:18:06 +02:00
ModalService . confirmContainerDeletion (
title ,
function ( result ) {
if ( ! result ) { return ; }
var cleanAssociatedVolumes = false ;
if ( result [ 0 ] ) {
cleanAssociatedVolumes = true ;
}
$scope . remove ( cleanAssociatedVolumes ) ;
}
) ;
2017-04-25 10:20:57 +02:00
} ;
2017-05-01 12:18:06 +02:00
$scope . remove = function ( cleanAssociatedVolumes ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2017-05-23 20:56:10 +02:00
ContainerService . remove ( $scope . container , cleanAssociatedVolumes )
. then ( function success ( ) {
Notifications . success ( 'Container successfully removed' ) ;
$state . go ( 'containers' , { } , { reload : true } ) ;
} )
. catch ( function error ( err ) {
Notifications . error ( 'Failure' , err , 'Unable to remove container' ) ;
} )
. finally ( function final ( ) {
$ ( '#loadingViewSpinner' ) . hide ( ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . restart = function ( ) {
2016-07-07 12:44:58 +12:00
$ ( '#loadingViewSpinner' ) . show ( ) ;
2016-06-02 17:34:03 +12:00
Container . restart ( { id : $stateParams . id } , function ( d ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container restarted' , $stateParams . id ) ;
2016-06-02 17:34:03 +12:00
} , function ( e ) {
update ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to restart container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
} ;
$scope . renameContainer = function ( ) {
2017-06-01 10:23:59 +02:00
var container = $scope . container ;
Container . rename ( { id : $stateParams . id , 'name' : container . newContainerName } , function ( d ) {
if ( d . message ) {
container . newContainerName = container . Name ;
Notifications . error ( 'Unable to rename container' , { } , d . message ) ;
2016-09-02 15:25:20 +12:00
} else {
2017-06-01 10:23:59 +02:00
container . Name = container . newContainerName ;
Notifications . success ( 'Container successfully renamed' , container . Name ) ;
2016-09-02 15:25:20 +12:00
}
} , function ( e ) {
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to rename container' ) ;
2016-06-02 17:34:03 +12:00
} ) ;
$scope . container . edit = false ;
} ;
2016-11-07 05:36:00 +01:00
$scope . containerLeaveNetwork = function containerLeaveNetwork ( container , networkId ) {
$ ( '#loadingViewSpinner' ) . show ( ) ;
Network . disconnect ( { id : networkId } , { Container : $stateParams . id , Force : false } , function ( d ) {
2017-05-23 20:56:10 +02:00
if ( container . message ) {
2016-11-07 05:36:00 +01:00
$ ( '#loadingViewSpinner' ) . hide ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Error' , d , 'Unable to disconnect container from network' ) ;
2016-11-07 05:36:00 +01:00
} else {
$ ( '#loadingViewSpinner' ) . hide ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . success ( 'Container left network' , $stateParams . id ) ;
2016-11-10 03:25:31 +01:00
$state . go ( 'container' , { id : $stateParams . id } , { reload : true } ) ;
2016-11-07 05:36:00 +01:00
}
} , function ( e ) {
$ ( '#loadingViewSpinner' ) . hide ( ) ;
2017-05-23 20:56:10 +02:00
Notifications . error ( 'Failure' , e , 'Unable to disconnect container from network' ) ;
2016-11-07 05:36:00 +01:00
} ) ;
} ;
2017-08-13 12:17:41 +02:00
$scope . duplicate = function ( ) {
ModalService . confirmExperimentalFeature ( function ( experimental ) {
if ( ! experimental ) { return ; }
$state . go ( 'actions.create.container' , { from : $stateParams . id } , { reload : true } ) ;
} ) ;
} ;
$scope . recreate = function ( ) {
ModalService . confirmExperimentalFeature ( function ( experimental ) {
if ( ! experimental ) { return ; }
ModalService . confirm ( {
title : 'Are you sure ?' ,
message : 'You\'re about to re-create this container, any non-persisted data will be lost. This container will be removed and another one will be created using the same configuration.' ,
buttons : {
confirm : {
label : 'Recreate' ,
className : 'btn-danger'
}
} ,
callback : function onConfirm ( confirmed ) {
if ( ! confirmed ) { return ; }
else {
$ ( '#loadingViewSpinner' ) . show ( ) ;
var container = $scope . container ;
var config = ContainerHelper . configFromContainer ( container . Model ) ;
ContainerService . remove ( container , true )
. then ( function success ( ) {
return RegistryService . retrieveRegistryFromRepository ( container . Config . Image ) ;
} )
. then ( function success ( data ) {
return 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' ) ;
$state . go ( 'containers' , { } , { reload : true } ) ;
} )
. catch ( function error ( err ) {
Notifications . error ( 'Failure' , err , 'Unable to re-create container' ) ;
} )
. finally ( function final ( ) {
$ ( '#loadingViewSpinner' ) . hide ( ) ;
} ) ;
}
}
} ) ;
} ) ;
} ;
2017-06-29 08:49:35 -05:00
$scope . containerJoinNetwork = function containerJoinNetwork ( container , networkId ) {
$ ( '#joinNetworkSpinner' ) . show ( ) ;
Network . connect ( { id : networkId } , { Container : $stateParams . id } , function ( d ) {
if ( container . message ) {
$ ( '#joinNetworkSpinner' ) . hide ( ) ;
Notifications . error ( 'Error' , d , 'Unable to connect container to network' ) ;
} else {
$ ( '#joinNetworkSpinner' ) . hide ( ) ;
Notifications . success ( 'Container joined network' , $stateParams . id ) ;
$state . go ( 'container' , { id : $stateParams . id } , { reload : true } ) ;
}
} , function ( e ) {
$ ( '#joinNetworkSpinner' ) . hide ( ) ;
Notifications . error ( 'Failure' , e , 'Unable to connect container to network' ) ;
} ) ;
} ;
2017-08-11 09:46:55 +02:00
var provider = $scope . applicationState . endpoint . mode . provider ;
var apiVersion = $scope . applicationState . endpoint . apiVersion ;
NetworkService . networks (
provider === 'DOCKER_STANDALONE' || provider === 'DOCKER_SWARM_MODE' ,
false ,
provider === 'DOCKER_SWARM_MODE' && apiVersion >= 1.25 ,
provider === 'DOCKER_SWARM'
)
. 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 08:49:35 -05:00
2016-06-02 17:34:03 +12:00
update ( ) ;
} ] ) ;