2023-02-14 08:19:41 +00:00
import { confirmDelete } from '@@/modals/confirm' ;
import { confirmServiceForceUpdate } from '@/react/docker/services/common/update-service-modal' ;
2020-04-10 21:54:53 +00:00
angular . module ( 'portainer.docker' ) . controller ( 'ServicesDatatableActionsController' , [
'$q' ,
'$state' ,
'ServiceService' ,
'ServiceHelper' ,
'Notifications' ,
'ImageHelper' ,
'WebhookService' ,
2023-02-14 08:19:41 +00:00
function ( $q , $state , ServiceService , ServiceHelper , Notifications , ImageHelper , WebhookService ) {
2021-12-14 07:34:54 +00:00
const ctrl = this ;
2020-04-10 21:54:53 +00:00
this . scaleAction = function scaleService ( service ) {
2018-06-11 13:13:19 +00:00
var config = ServiceHelper . serviceToConfig ( service . Model ) ;
2020-04-10 21:54:53 +00:00
config . Mode . Replicated . Replicas = service . Replicas ;
2018-06-11 13:13:19 +00:00
ServiceService . update ( service , config )
2020-04-10 21:54:53 +00:00
. then ( function success ( ) {
Notifications . success ( 'Service successfully scaled' , 'New replica count: ' + service . Replicas ) ;
2018-06-11 13:13:19 +00:00
$state . reload ( ) ;
2020-04-10 21:54:53 +00:00
} )
. catch ( function error ( err ) {
Notifications . error ( 'Failure' , err , 'Unable to scale service' ) ;
service . Scale = false ;
service . Replicas = service . ReplicaCount ;
} ) ;
} ;
this . removeAction = function ( selectedItems ) {
2023-02-14 08:19:41 +00:00
confirmDelete ( 'Do you want to remove the selected service(s)? All the containers associated to the selected service(s) will be removed too.' ) . then ( ( confirmed ) => {
if ( ! confirmed ) {
return ;
2018-06-11 13:13:19 +00:00
}
2023-02-14 08:19:41 +00:00
removeServices ( selectedItems ) ;
} ) ;
2020-04-10 21:54:53 +00:00
} ;
2018-06-11 13:13:19 +00:00
2020-04-10 21:54:53 +00:00
this . updateAction = function ( selectedItems ) {
2023-02-14 08:19:41 +00:00
confirmServiceForceUpdate ( 'Do you want to force an update of the selected service(s)? All the tasks associated to the selected service(s) will be recreated.' ) . then (
( result ) => {
2020-04-10 21:54:53 +00:00
if ( ! result ) {
return ;
}
2023-02-14 08:19:41 +00:00
forceUpdateServices ( selectedItems , result . pullLatest ) ;
2020-04-10 21:54:53 +00:00
}
) ;
} ;
2018-09-03 10:08:03 +00:00
2020-04-10 21:54:53 +00:00
function forceUpdateServices ( services , pullImage ) {
var actionCount = services . length ;
angular . forEach ( services , function ( service ) {
var config = ServiceHelper . serviceToConfig ( service . Model ) ;
if ( pullImage ) {
config . TaskTemplate . ContainerSpec . Image = ImageHelper . removeDigestFromRepository ( config . TaskTemplate . ContainerSpec . Image ) ;
2018-06-11 13:13:19 +00:00
}
2020-04-10 21:54:53 +00:00
// As explained in https://github.com/docker/swarmkit/issues/2364 ForceUpdate can accept a random
// value or an increment of the counter value to force an update.
config . TaskTemplate . ForceUpdate ++ ;
ServiceService . update ( service , config )
. then ( function success ( ) {
Notifications . success ( 'Service successfully updated' , service . Name ) ;
} )
. catch ( function error ( err ) {
2021-10-11 21:37:07 +00:00
Notifications . error ( 'Failure' , err , 'Unable to force update service' + service . Name ) ;
2020-04-10 21:54:53 +00:00
} )
. finally ( function final ( ) {
-- actionCount ;
if ( actionCount === 0 ) {
$state . reload ( ) ;
}
} ) ;
} ) ;
}
function removeServices ( services ) {
var actionCount = services . length ;
angular . forEach ( services , function ( service ) {
ServiceService . remove ( service )
. then ( function success ( ) {
2021-12-14 07:34:54 +00:00
return WebhookService . webhooks ( service . Id , ctrl . endpointId ) ;
2020-04-10 21:54:53 +00:00
} )
. then ( function success ( data ) {
return $q . when ( data . length !== 0 && WebhookService . deleteWebhook ( data [ 0 ] . Id ) ) ;
} )
. then ( function success ( ) {
Notifications . success ( 'Service successfully removed' , service . Name ) ;
} )
. catch ( function error ( err ) {
Notifications . error ( 'Failure' , err , 'Unable to remove service' ) ;
} )
. finally ( function final ( ) {
-- actionCount ;
if ( actionCount === 0 ) {
$state . reload ( ) ;
}
} ) ;
2018-06-11 13:13:19 +00:00
} ) ;
2020-04-10 21:54:53 +00:00
}
} ,
] ) ;