Team Ai
Apppublic

openenv/coding_env

sourceHugging Faceupdated 3mo agoView on Hugging Face
21likes
providers.cpython-313.pyc158 linesDownload Raw Back to __pycache__
1�

2���h!��z�SrSSKJr SSKJrJr SSKJrJrJ	r	 "SS\5r3"SS\45r"S	S5\65rg)z�7Container provider abstractions for running environment servers.8 9This module provides a pluggable architecture for different container providers10(local Docker, Kubernetes, cloud providers, etc.) to be used with HTTPEnvClient.11�)�annotations)�ABC�abstractmethod)�Any�Dict�Optionalc�r�\rSrSrSr\SS	Sjj5r\S12Sj5r\SSSjj5rSr	g)
�ContainerProvider�a�13Abstract base class for container providers.14 15Providers implement this interface to support different container platforms:16- LocalDockerProvider: Runs containers on local Docker daemon17- KubernetesProvider: Runs containers in Kubernetes cluster18- FargateProvider: Runs containers on AWS Fargate19- CloudRunProvider: Runs containers on Google Cloud Run20 21The provider manages a single container lifecycle and provides the base URL22for connecting to it.23 24Example:25    >>> provider = LocalDockerProvider()26    >>> base_url = provider.start_container("echo-env:latest")27    >>> print(base_url)  # http://localhost:800028    >>> # Use the environment via base_url29    >>> provider.stop_container()30Nc��g)a�31Start a container from the specified image.32 33Args:34    image: Container image name (e.g., "echo-env:latest")35    port: Port to expose (if None, provider chooses)36    env_vars: Environment variables to pass to container37    **kwargs: Provider-specific options38 39Returns:40    Base URL to connect to the container (e.g., "http://localhost:8000")41 42Raises:43    RuntimeError: If container fails to start44N�)�self�image�port�env_vars�kwargss     �J/Users/zwentz/Development/OpenEnv/src/core/containers/runtime/providers.py�start_container�!ContainerProvider.start_container)s��.	
�c��g)zm45Stop and remove the running container.46 47This cleans up the container that was started by start_container().48Nr
)rs r�stop_container� ContainerProvider.stop_containerBs��	
rc��g)a49Wait for the container to be ready to accept requests.50 51This typically polls the /health endpoint until it returns 200.52 53Args:54    base_url: Base URL of the container55    timeout_s: Maximum time to wait56 57Raises:58    TimeoutError: If container doesn't become ready in time59Nr
)r�base_url�	timeout_ss   r�wait_for_ready� ContainerProvider.wait_for_readyKs��	
rr
�NN�60r�strrz
Optional[int]rzOptional[Dict[str, str]]rr�returnr!�r"�None�g>@�rr!r�floatr"r$)61�__name__�62__module__�__qualname__�__firstlineno__�__doc__rrrr�__static_attributes__r
rrr63r64s{���(�#�-1�	
��
��
�+�	
�65�
�66
�

��
�0�
��
��

��

rr67c�n�\rSrSrSrSrSSSjjrS
SjrSSSjjrSSjr	SS	jr68S69rg)�LocalDockerProvider�\ae70Container provider for local Docker daemon.71 72This provider runs containers on the local machine using Docker.73Useful for development and testing.74 75Example:76    >>> provider = LocalDockerProvider()77    >>> base_url = provider.start_container("echo-env:latest")78    >>> # Container running on http://localhost:<random-port>79    >>> provider.stop_container()80c��SUlSUlSSKnURSS/SSSS9 g!UR[81UR4a [S5ef=f)	z%Initialize the local Docker provider.Nr�docker�versionT�)�check�capture_output�timeoutzHDocker is not available. Please install Docker Desktop or Docker Engine.)�
_container_id�_container_name�82subprocess�run�CalledProcessError�FileNotFoundError�TimeoutExpired�RuntimeError�rr:s  r�__init__�LocalDockerProvider.__init__jss��,0���.2���	�83	��N�N��9�%��#��	
�
���-�-�/@�*�B[�B[�\�	��Z��
�	�s	�)�-ANc��SSKnSSKnUcUR5nURU5UlSSSSURSUS3/nU(a1UR5Hup�UR
S	US84U	3/5 M URU5 URUSSSS9n85U86RR5UlURS
5 SU3nU$)a87Start a Docker container locally.88 89Args:90    image: Docker image name91    port: Port to expose (if None, finds available port)92    env_vars: Environment variables for the container93    **kwargs: Additional Docker run options94 95Returns:96    Base URL to connect to the container97rNr2r;z-dz--namez-pz:8000z-e�=T)r6�textr5�zhttp://localhost:)
r:�time�_find_available_port�_generate_container_namer9�items�extend�appendr;�stdout�stripr8�sleep)rrrrrr:rG�cmd�key�value�resultrs            rr�#LocalDockerProvider.start_container~s���&	���<��,�,�.�D� $�<�<�U�C���
�e���d�*�*��T�F�%�.�	98���&�n�n�.�99���100�101�D�S�E��5�'�"2�3�4�/�	�102�103�5������D�t�4��P��#�]�]�0�0�2���	
�104�105�1�
�&�t�f�-���rc��URcgSSKnURSSUR/SSSS9 URSSUR/SSSS9 SUlSUlg!URa N!f=f!SUlSUlf=f)	z'106Stop and remove the Docker container.107Nrr2�stopT�108)r6r5r7�rm)r8r:r;r<r9r@s  rr�"LocalDockerProvider.stop_container�s������%���	(��N�N��6�4�#5�#5�6�#���	
�
�
�N�N��4��!3�!3�4�#���	
�
�"&�D��#'�D� ���,�,�	��	��"&�D��#'�D� �s#�>A!�!A4�1A7�3A4�4A7�7Bc�T�SSKnSSKnUR5nUS3nUR5U-109U:aMURUSS9nURS:XagURS5 UR5U-110U:aMM[
SUS	US11135e!URa NNf=f)z�112Wait for container to be ready by polling /health endpoint.113 114Args:115    base_url: Base URL of the container116    timeout_s: Maximum time to wait117 118Raises:119    TimeoutError: If container doesn't become ready120rNz/healthg@)r7��g�?z
Container at z did not become ready within �s)rG�requests�get�status_code�RequestExceptionrO�TimeoutError)rrrrGr]�121start_time�122health_url�responses        rr�"LocalDockerProvider.wait_for_ready�s���	���Y�Y�[�123� �z��)�124��i�i�k�J�&��2�
�#�<�<�125�C�<�@���'�'�3�.��/�126
�J�J�s�O��i�i�k�J�&��2���H�:�%B�9�+�Q�O�127�	128���,�,�
��
�s� B�B'�&B'c���SSKnURURUR5nURS5 UR	S5 UR5SnSSS5 U$!,(df   W$=f)zM129Find an available port on localhost.130 131Returns:132    An available port number133rN)�rrF)�socket�AF_INET�SOCK_STREAM�bind�listen�getsockname)rrhr\rs    rrH�(LocalDockerProvider._find_available_port�sc��	�
�]�]�6�>�>�6�+=�+=�
>�!�
�F�F�7�O�
�H�H�Q�K��=�=�?�1�%�D�?���	?�
>���s�6A+�+134A:c��SSKnURS5SRS5Sn[UR5S-5nUSU3$)z�135Generate a unique container name based on image name and timestamp.136 137Args:138    image: Docker image name139 140Returns:141    A unique container name142rN�/������:i��-)rG�split�int)rrrG�clean_image�	timestamps     rrI�,LocalDockerProvider._generate_container_namesP��	��k�k�#�&�r�*�0�0��5�a�8����	�	��d�*�+�	���a�	�{�+�+r)r8r9rr r#r%r&)r"ru)rr!r"r!)r(r)r*r+r,rArrrrHrIr-r
rrr/r/\s_����.#�-1�	5��5��5�+�	5�143�5�144
�
5�n(�@145�>
�,rr/c��\rSrSrSrSrg)�KubernetesProvideria�146Container provider for Kubernetes clusters.147 148This provider creates pods in a Kubernetes cluster and exposes them149via services or port-forwarding.150 151Example:152    >>> provider = KubernetesProvider(namespace="envtorch-dev")153    >>> base_url = provider.start_container("echo-env:latest")154    >>> # Pod running in k8s, accessible via service or port-forward155    >>> provider.stop_container()156r
N)r(r)r*r+r,r-r
rrrzrzs���	rrzN)
r,�157__future__r�abcrr�typingrrrr158r/rzr
rr�<module>r~sD���#�#�&�&�E
��E
�Pu,�+�u,�p
	�*�
	r