
    biK                     D    d dl mZmZmZ d dlmZ d Zd Z G d d      Zy)   )autherrorsutils)ServiceModec                 r   d }|t        j                  | d      rd|v r	 |dd       d|v r	 |dd       t        j                  | d      r|j                  d      d	k(  r	 |d
d       t        j                  | d      rd|v r	 |dd       |Bt        j                  | d      r	 |dd       t        j                  | d      rd|v r	 |dd       |8t        j                  | d      r"d|v rt        d |d   D              r	 |dd       |d|v rt        j                  | d      r	 |dd       |j                  d      rft        j                  | d      r|d   j                  d      r	 |dd       t        j                  | d      r|d   j                  d      r	 |dd       |j                  d      r|j                  d      }t        j                  | d      r|j                  d      r	 |dd       |j                  d       	 |d!d       |j                  d"      	 |d#d       |j                  d$      	 |d%d       |j                  d&      	 |d'd       |j                  d(      	 |d)d       t        j                  | d      r4|j                  d*      	 |d'd       |j                  d+      	 |d,d       t        j                  | d      r4|j                  d-      	 |d.d       |j                  d/      	 |d0d       t        j                  | d1      r|j                  d2      	 |d3d1       t        j                  | d4      r|j                  d5      	 |d6d4       |j                  d7      r6t        j                  | d      r|d7   j                  d8      r
 |d9d       y y y y y ):Nc                 6    t        j                  |  d|       )Nz# is not supported in API version < )r   InvalidVersion)parammin_versions     M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/docker/api/service.pyraise_version_errorz0_check_api_features.<locals>.raise_version_error   s$    ##g8F
 	
    1.25MaxFailureRatiozUpdateConfig.max_failure_ratioMonitorzUpdateConfig.monitorz1.28FailureActionrollbackz$UpdateConfig.failure_action rollback1.29OrderzUpdateConfig.orderrollback_configzRollbackConfig.orderz1.32Portsc              3   >   K   | ]  }|j                  d         yw)PublishModeN)get).0ps     r   	<genexpr>z&_check_api_features.<locals>.<genexpr>(   s     HA155'Hs   zEndpointSpec.Ports[].modeForceUpdateforce_update	Placementz1.30	PlatformszPlacement.platformsz1.27PreferenceszPlacement.preferencesContainerSpecTTYzContainerSpec.ttyHostnamezContainerSpec.hostnameHostszContainerSpec.hostsGroupszContainerSpec.groups	DNSConfigzContainerSpec.dns_configHealthcheckzContainerSpec.healthcheckReadOnly
StopSignalzContainerSpec.stop_signalConfigszContainerSpec.configs
PrivilegeszContainerSpec.privilegesz1.35	IsolationzContainerSpec.isolationz1.38InitzContainerSpec.init	ResourcesGenericResourceszResources.generic_resources)r   
version_ltr   any)versiontask_templateupdate_configendpoint_specr   r   container_specs          r   _check_api_featuresr9      s   

  GV, M1#$DfMM)#$:FCGV,  1Z?#:F GV,-'#$8&A"GV, 16:GV,-'#$:FC GV,M1IHw1GHH#$?H M)e.>.>/!7[)0 -11+>'(=vF0 -11-@'(?H_-*..?N0!%%e,'(;VD!%%j1='(@&I!%%g.:'(=vF!%%h/;'(>G!%%k2>'(BFK!%%m4@'(CVL0!%%j1='(BFK!%%l3?'(CVL0!%%i0<'(?H!%%l3?'(BFK0!%%k2>'(A6J0!%%f-9'(<fE[)0 -112DE'(EvN F 1 *e !r   c                     | j                         }|U|j                         D ]B  \  }}|dk(  r0d|vri |d<   |d   j                         D ]  \  }}|	||d   |<    ;|>|||<   D |S )Nr#   )copyitems)currentoverridemergedts_keyts_valuecs_keycs_values          r   _merge_task_templaterD   c   s    \\^F ( 0 	*FH("&0.0F?+(0(A(G(G(I C$FH+:B/7C %!)v	* Mr   c                      e Zd Z ej                  d      	 	 	 dd       Z ej                  d       ej                  d      dd              Z ej                  d       ej                  d      d               Z ej                  d       ej                  d      d               Z	 ej                  d      dd	       Z
 ej                  d
       ej                  d      	 	 	 dd              Z ej                  d      dd       Z ej                  d       ej                  d      	 	 	 	 	 dd              Zy)ServiceApiMixinz1.24Nc
                 x   t        | j                  ||||	       | j                  d      }
i }|j                  di       j                  dd      }|t	        j
                  d      |rt        |t              st        |      }t        j                  |      \  }}t        j                  | |      }|r||d<   t        j                  | j                  d      r|xs |j                  dd      }||||t        j                  |      |d	}|||d
<   |	|	|d<   | j!                  | j#                  |
||      d      S )a  
        Create a service.

        Args:
            task_template (TaskTemplate): Specification of the task to start as
                part of the new service.
            name (string): User-defined name for the service. Optional.
            labels (dict): A map of labels to associate with the service.
                Optional.
            mode (ServiceMode): Scheduling mode for the service (replicated
                or global). Defaults to replicated.
            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``
            networks (:py:class:`list`): List of network names or IDs or
                :py:class:`~docker.types.NetworkAttachmentConfig` to attach the
                service to. Default: ``None``.
            endpoint_spec (EndpointSpec): Properties that can be configured to
                access and load balance a service. Default: ``None``.

        Returns:
            A dictionary containing an ``ID`` key for the newly created
            service.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        z/services/creater#   ImageNz,Missing mandatory Image key in ContainerSpecX-Registry-Authr   Networks)NameLabelsTaskTemplateModerJ   EndpointSpecUpdateConfigRollbackConfig)dataheadersT)r9   _version_urlr   r   DockerException
isinstancedictr   r   resolve_repository_nameget_config_headerr   r2   popconvert_service_networks_result
_post_json)selfr5   namelabelsmoder6   networksendpoint_configr7   r   urlrS   imageregistry	repo_nameauth_headerrR   s                    r   create_servicezServiceApiMixin.create_services   sQ   H 	MM=-	

 ii*+!!/26::7DI=((>  
4.t$D"::5A),,T8<)4G%&DMM62F=#4#4Z#FH)66x@)
 $#0D &%4D!"||OOCdGO<d
 	
r   servicec                     | j                  d|      }i }|:t        j                  | j                  d      rt	        j
                  d      ||d<   | j                  | j                  ||      d      S )a  
        Return information about a service.

        Args:
            service (str): Service name or ID.
            insert_defaults (boolean): If true, default values will be merged
                into the service inspect output.

        Returns:
            (dict): A dictionary of the server-side representation of the
                service, including all relevant properties.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        /services/{0}r   z6insert_defaults is not supported in API version < 1.29insertDefaultsparamsT)rU   r   r2   rT   r   r	   r]   _get)r_   rk   insert_defaultsre   rp   s        r   inspect_servicezServiceApiMixin.inspect_service   st    & ii1&v6++L  (7F#$||DIIc&I94@@r   taskc                 h    | j                  d|      }| j                  | j                  |      d      S )a  
        Retrieve information about a task.

        Args:
            task (str): Task ID

        Returns:
            (dict): Information about the task.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        z
/tasks/{0}T)rU   r]   rq   )r_   rt   re   s      r   inspect_taskzServiceApiMixin.inspect_task   s-      iid+||DIIcND11r   c                 l    | j                  d|      }| j                  |      }| j                  |       y)a  
        Stop and remove a service.

        Args:
            service (str): Service name or ID

        Returns:
            ``True`` if successful.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        rm   T)rU   _delete_raise_for_status)r_   rk   re   resps       r   remove_servicezServiceApiMixin.remove_service   s3    " ii1||C t$r   c                    d|rt        j                  |      ndi}|:t        j                  | j                  d      rt	        j
                  d      ||d<   | j                  d      }| j                  | j                  ||      d      S )	a"  
        List services.

        Args:
            filters (dict): Filters to process on the nodes list. Valid
                filters: ``id``, ``name`` , ``label`` and ``mode``.
                Default: ``None``.
            status (bool): Include the service task count of running and
                desired tasks. Default: ``None``.

        Returns:
            A list of dictionaries containing data about each service.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        filtersNz1.41z-status is not supported in API version < 1.41statusz	/servicesro   T)	r   convert_filtersr2   rT   r   r	   rU   r]   rq   )r_   r}   r~   rp   re   s        r   serviceszServiceApiMixin.services  s    ( u,,W5d
 v6++C   &F8ii$||DIIc&I94@@r   r   c
                     |||||||d}
| j                  d|      }| j                  ||
d      }|	*| j                  |      d   d   d   j                  dd	      }	| j	                  d||	      S )
ar  
            Get log stream for a service.
            Note: This endpoint works only for services with the ``json-file``
            or ``journald`` logging drivers.

            Args:
                service (str): ID or name of the service
                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``
                is_tty (bool): Whether the service's :py:class:`ContainerSpec`
                    enables the TTY option. If omitted, the method will query
                    the Engine for the information, causing an additional
                    roundtrip.

            Returns (generator): Logs for the service.
        )detailsfollowstdoutstderrsince
timestampstailz/services/{0}/logsT)rp   streamSpecrM   r#   r$   F)rU   rq   rs   r   _get_result_tty)r_   rk   r   r   r   r   r   r   r   is_ttyrp   re   ress                r   service_logszServiceApiMixin.service_logs%  s    B $
 ii,g6iiF4i8>))$&&577:s5%7H  ##D#v66r   c                     d|rt        j                  |      ndi}| j                  d      }| j                  | j	                  ||      d      S )a  
        Retrieve a list of tasks.

        Args:
            filters (dict): A map of filters to process on the tasks list.
                Valid filters: ``id``, ``name``, ``service``, ``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.
        r}   Nz/tasksro   T)r   r   rU   r]   rq   )r_   r}   rp   re   s       r   taskszServiceApiMixin.tasksW  sM    & u,,W5d
 ii!||DIIc&I94@@r   c                 @   t        | j                  |||
|       |r;d}t        j                  | j                  d      rd}| j	                  ||      d   }ni }| j                  d|      }i }i }||j                  d      n||d<   ||j                  d      n||d<   |!t        |t              st        |      }||d	<   n|j                  d	      |d	<   t        |j                  d
i       |      |d
<   |d
   j                  di       }|j                  dd      }|5t        j                  |      \  }}t        j                  | |      }|r||d<   |||d<   n|j                  d      |d<   |||d<   n|j                  d      |d<   |Dt        j                  |      }t        j                  | j                  d      r||d<   n||d
   d<   nt        j                  | j                  d      r|j                  d      |d<   nT|d
   j                  d      @|j                  d
i       }|j                  d      }||j                  d      }|||d
   d<   |
|
|d<   n|j                  d      |d<   | j                  ||d|i|      }| j!                  |d      S )a  
        Update a service.

        Args:
            service (string): A service identifier (either its name or service
                ID).
            version (int): The version number of the service object being
                updated. This is required to avoid conflicting writes.
            task_template (TaskTemplate): Specification of the updated task to
                start as part of the service.
            name (string): New name for the service. Optional.
            labels (dict): A map of labels to associate with the service.
                Optional.
            mode (ServiceMode): Scheduling mode for the service (replicated
                or global). Defaults to replicated.
            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``
            networks (:py:class:`list`): List of network names or IDs or
                :py:class:`~docker.types.NetworkAttachmentConfig` to attach the
                service to. Default: ``None``.
            endpoint_spec (EndpointSpec): Properties that can be configured to
                access and load balance a service. Default: ``None``.
            fetch_current_spec (boolean): Use the undefined settings from the
                current specification of the service. Default: ``False``

        Returns:
            A dictionary containing a ``Warnings`` key.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        Tr   N)rr   r   z/services/{0}/updaterK   rL   rN   rM   r#   rH   rI   rP   rQ   r   rJ   rO   r4   )rR   rp   rS   )json)r9   rT   r   r2   rs   rU   r   rW   rX   r   rD   r   rY   rZ   r\   r^   r]   )r_   rk   r4   r5   r`   ra   rb   r6   rc   rd   r7   fetch_current_specr   inspect_defaultsr=   re   rR   rS   r8   rf   rg   rh   ri   converted_networkscurrent_task_templatecurrent_networksrz   s                              r   update_servicezServiceApiMixin.update_serviceo  s   T 	MM=-	

 #v6#' **)9 + G
 Gii.8.2lw{{6*V28.X.fXdD)"4(DL";;v.DL3KK+] 
^ n-11/2F""7D1"&">">u"EHi00x@K-8)*$#0D #*;;~#>D &%4D!"%,[[1A%BD!"!&!?!?!Iv6#5Z 3E^$Z0dmmV4&{{:6D.!%%j19$+KK$C!488D'#*;;z#: +3C^$Z0$#0D #*;;~#>D dIw#7  
 ||Dt|,,r   )NNNNNNNN)N)NN)FFFF    FallN)
NNNNNNNNFN)__name__
__module____qualname__r   minimum_versionrj   check_resourcers   rv   r{   r   r   r   r    r   r   rF   rF   r   s   U6">B?C04I
 #I
V U6"U)$A % #A8 U6"U&!2 " #2" U6"U)$ % #( U6"A #A< U6"U)$HMCH .7 % #.7` U6"A #A. U6"U)$HL=A6:>C'+	u- % #u-r   rF   N)	 r   r   r   typesr   r9   rD   rF   r   r   r   <module>r      s'    " " [O|t- t-r   