
    q_aY5                         d dl Z d dlmZmZ d dlmZmZmZmZ ddl	m
Z
mZ  G d de
      Z G d d	e      Zg d
Zg dZg dZg dZd Zy)    N)create_unexpected_kwargs_errorInvalidArgument)TaskTemplateContainerSpec	PlacementServiceMode   )Model
Collectionc                   Z    e Zd ZdZdZed        Zed        Zd ZddZ	d Z
d	 Zd
 Zd Zy)Servicez
A service.IDc                 &    | j                   d   d   S )zThe service's name.SpecName)attrsselfs    8/usr/lib/python3/dist-packages/docker/models/services.pynamezService.name   s     zz&!&))    c                 V    | j                   j                  d      j                  d      S )z
        The version number of the service. If this is not the same as the
        server, the :py:meth:`update` function will not work and you will
        need to call :py:meth:`reload` before calling it again.
        VersionIndex)r   getr   s    r   versionzService.version   s"     zz~~i(,,W55r   c                 `    | j                   j                  j                  | j                        S )z
        Stop and remove the service.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        )clientapiremove_serviceidr   s    r   removezService.remove   s!     {{--dgg66r   Nc                 t    |i }| j                   |d<   | j                  j                  j                  |      S )a  
        List the tasks in this service.

        Args:
            filters (dict): A map of filters to process on the tasks list.
                Valid filters: ``id``, ``name``, ``node``,
                ``label``, and ``desired-state``.

        Returns:
            :py:class:`list`: List of task dictionaries.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        service)filters)r!   r   r   tasks)r   r%   s     r   r&   zService.tasks#   s8      ?G!WW	{{$$W$55r   c                 d   d|vr| j                   d   d   d   }|d   |d<   |j                  d      du r5| j                   d   d   }t        |j                  dd	            }|d
z   |d<   t        d|      } | j                  j
                  j                  | j                  | j                  fi |S )a)  
        Update a service's configuration. Similar to the ``docker service
        update`` command.

        Takes the same parameters as :py:meth:`~ServiceCollection.create`.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        imager   r   r   Imageforce_updateTForceUpdater   r	   update)	r   r   int_get_create_service_kwargsr   r   update_servicer!   r   )r   kwargsspectask_templatecurrent_valuecreate_kwargss         r   r,   zService.update8   s     & ::f%n5oFD"7mF7O::n%- JJv.~>M 1 1- CDM%2Q%6F>"28VD-t{{--GGLL
 
 	
r   c                     | j                   d   d   d   j                  dd      } | j                  j                  j                  | j
                  fd|i|S )a  
        Get log stream for the service.
        Note: This method works only for services with the ``json-file``
        or ``journald`` logging drivers.

        Args:
            details (bool): Show extra details provided to logs.
                Default: ``False``
            follow (bool): Keep connection open to read logs as they are
                sent by the Engine. Default: ``False``
            stdout (bool): Return logs from ``stdout``. Default: ``False``
            stderr (bool): Return logs from ``stderr``. Default: ``False``
            since (int): UNIX timestamp for the logs staring point.
                Default: 0
            timestamps (bool): Add timestamps to every log line.
            tail (string or int): Number of log lines to be returned,
                counting from the current end of the logs. Specify an
                integer or ``'all'`` to output all log lines.
                Default: ``all``

        Returns:
            generator: Logs for the service.
        r   r   r   TTYFis_tty)r   r   r   r   service_logsr!   )r   r0   r7   s      r   logszService.logsU   sW    0 F#N3ODHH5
 ,t{{++DGGMFMfMMr   c                     d| j                   d   d   j                         v rt        d      t        d|      }| j                  j
                  j                  | j                  | j                  |d      S )z
        Scale service container.

        Args:
            replicas (int): The number of containers that should be running.

        Returns:
            bool: ``True`` if successful.
        Globalr   ModezCannot scale a global container
replicatedT)modefetch_current_spec)	r   keysr   r   r   r   r/   r!   r   )r   replicasservice_modes      r   scalezService.scaler   so     tzz&)&16688!"CDD"<:{{--dggt||3?AE . G 	Gr   c                 (    | j                  dd      S )z
        Force update the service even if no changes require it.

        Returns:
            bool: ``True`` if successful.
        T)r*   r?   )r,   r   s    r   r*   zService.force_update   s     {{{FFr   N)__name__
__module____qualname____doc__id_attributepropertyr   r   r"   r&   r,   r9   rC   r*    r   r   r   r      sQ    L* * 6 676*
:N:G&Gr   r   c                   *    e Zd ZdZeZddZddZd Zy)ServiceCollectionzServices on the Docker server.Nc                     ||d<   ||d<   t        d|      } | j                  j                  j                  di |}| j	                  |      S )a1  
        Create a service. Similar to the ``docker service create`` command.

        Args:
            image (str): The image name to use for the containers.
            command (list of str or str): Command to run.
            args (list of str): Arguments to the command.
            constraints (list of str): :py:class:`~docker.types.Placement`
                constraints.
            preferences (list of tuple): :py:class:`~docker.types.Placement`
                preferences.
            maxreplicas (int): :py:class:`~docker.types.Placement` maxreplicas
                or (int) representing maximum number of replicas per node.
            platforms (list of tuple): A list of platform constraints
                expressed as ``(arch, os)`` tuples.
            container_labels (dict): Labels to apply to the container.
            endpoint_spec (EndpointSpec): Properties that can be configured to
                access and load balance a service. Default: ``None``.
            env (list of str): Environment variables, in the form
                ``KEY=val``.
            hostname (string): Hostname to set on the container.
            init (boolean): Run an init inside the container that forwards
                signals and reaps processes
            isolation (string): Isolation technology used by the service's
                containers. Only used for Windows containers.
            labels (dict): Labels to apply to the service.
            log_driver (str): Log driver to use for containers.
            log_driver_options (dict): Log driver options.
            mode (ServiceMode): Scheduling mode for the service.
                Default:``None``
            mounts (list of str): Mounts for the containers, in the form
                ``source:target:options``, where options is either
                ``ro`` or ``rw``.
            name (str): Name to give to the service.
            networks (:py:class:`list`): List of network names or IDs or
                :py:class:`~docker.types.NetworkAttachmentConfig` to attach the
                service to. Default: ``None``.
            resources (Resources): Resource limits and reservations.
            restart_policy (RestartPolicy): Restart policy for containers.
            secrets (list of :py:class:`~docker.types.SecretReference`): List
                of secrets accessible to containers for this service.
            stop_grace_period (int): Amount of time to wait for
                containers to terminate before forcefully killing them.
            update_config (UpdateConfig): Specification for the update strategy
                of the service. Default: ``None``
            rollback_config (RollbackConfig): Specification for the rollback
                strategy of the service. Default: ``None``
            user (str): User to run commands as.
            workdir (str): Working directory for commands to run.
            tty (boolean): Whether a pseudo-TTY should be allocated.
            groups (:py:class:`list`): A list of additional groups that the
                container process will run as.
            open_stdin (boolean): Open ``stdin``
            read_only (boolean): Mount the container's root filesystem as read
                only.
            stop_signal (string): Set signal to stop the service's containers
            healthcheck (Healthcheck): Healthcheck
                configuration for this service.
            hosts (:py:class:`dict`): A set of host to IP mappings to add to
                the container's `hosts` file.
            dns_config (DNSConfig): Specification for DNS
                related configurations in resolver configuration file.
            configs (:py:class:`list`): List of
                :py:class:`~docker.types.ConfigReference` that will be exposed
                to the service.
            privileges (Privileges): Security options for the service's
                containers.
            cap_add (:py:class:`list`): A list of kernel capabilities to add to
                the default set for the container.
            cap_drop (:py:class:`list`): A list of kernel capabilities to drop
                from the default set for the container.

        Returns:
            :py:class:`Service`: The created service.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        r(   commandcreaterL   )r.   r   r   create_servicer   )r   r(   rP   r0   r4   
service_ids         r   rQ   zServiceCollection.create   sP    `  w#y28VD3T[[__33DmD
xx
##r   c                 l    | j                  | j                  j                  j                  ||            S )a}  
        Get a service.

        Args:
            service_id (str): The ID of the service.
            insert_defaults (boolean): If true, default values will be merged
                into the output.

        Returns:
            :py:class:`Service`: The service.

        Raises:
            :py:class:`docker.errors.NotFound`
                If the service does not exist.
            :py:class:`docker.errors.APIError`
                If the server returns an error.
            :py:class:`docker.errors.InvalidVersion`
                If one of the arguments is not supported with the current
                API version.
        )prepare_modelr   r   inspect_service)r   rS   insert_defaultss      r   r   zServiceCollection.get   s/    * !!KKOO++JH
 	
r   c                      | j                   j                  j                  di |D cg c]  }| j                  |       c}S c c}w )a  
        List services.

        Args:
            filters (dict): Filters to process on the nodes list. Valid
                filters: ``id``, ``name`` , ``label`` and ``mode``.
                Default: ``None``.

        Returns:
            list of :py:class:`Service`: The services.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        rL   )r   r   servicesrU   )r   r0   ss      r   listzServiceCollection.list  sG    $ .T[[__--77
 q!
 	
 
s   ArE   )	rF   rG   rH   rI   r   modelrQ   r   r[   rL   r   r   rN   rN      s    (ET$l
2
r   rN   )argscap_addcap_droprP   configs
dns_configenvgroupshealthcheckhostnamehostsr(   init	isolationlabelsmounts
open_stdin
privileges	read_onlysecretsstop_grace_periodstop_signalttyuserworkdir)networks	resourcesrestart_policy)r   ri   r>   update_configendpoint_spec)constraintspreferences	platformsmaxreplicasc                 4   i }t        j                   |      D ]  }|t        v s|j                  |      ||<   ! i }t        j                   |      D ]  }|t        v s|j                  |      ||<   ! i }t        j                   |      D ]  }|t        v s|j                  |      ||<   ! d|v r|j                  d      |d<   i }t        j                   |      D ]  }|t
        v s|j                  |      ||<   ! t        di |}||d<   d|v r'|j                  d      |j                  di       d|d<   | dk(  r/d|v r|j                  d      |d<   |j                  d	d
      }||d	<   |rt        | |      t        di |}||d<   t        di ||d<   |S )Ncontainer_labelsri   	placement
log_driverlog_driver_options)r   Optionsr,   r*   r?   Tcontainer_specr2   rL   )
copyCREATE_SERVICE_KWARGSpopCONTAINER_SPEC_KWARGSTASK_TEMPLATE_KWARGSPLACEMENT_KWARGSr   r   r   r   )		func_namer0   r4   keycontainer_spec_kwargstask_template_kwargsr   r?   r   s	            r   r.   r.   N  s   Myy  1''!'CM#1 yy  9'')/C!#&9 yy  8&&(.

3 %8 V#*0**5G*Hh'Iyy  -""#ZZ_IcN- &I&I(1%vJJ|,zz"6;.
\*
 HV#39::n3M 0 $ZZ(<dC.@*+ ,Y??";%:;N-;)*%1%I4H%IM/"r   )r   docker.errorsr   r   docker.typesr   r   r   r   resourcer
   r   r   rN   r   r   r   r   r.   rL   r   r   <module>r      sY     I L L 'FGe FGRF

 F
T :   0r   