
    tni[9              -       f   d Z ddlmZ ddlmZ ddlmZ ddlmZ	 ddlm
Z ddlmZ dd	lmZ ddlmZ dd
lmZ ddlm
Z dd	lmZ dPdee         defdZdPdedee         defdZdPdee         defdZdQdededee         fdZdPdee         defdZdPdedee         fdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRdedee         dee         ded ed!ed"ee         d#ee         d$ed%ed&ee         d'ed(ed)ed*ee         d+ed,ee         d-ee         d.ee         d/ee         d0ee         def,d1Z defd2Z!ded$efd3Z"defd4Z#	 	 	 	 	 	 	 	 dSdeded7ed&ed%ed+ed*ed,ed8ed9efd:Z$defd;Z%	 	 	 	 	 	 	 	 	 	 	 dTded-edAed.edBedCedDedEedFedGed/ed$efdHZ&dIed-efdJZ'dedKedLefdMZ(d9edKedLefdNZ)d9efdOZ*dS )Uz%
Runpod | API Wrapper | CTL Commands
    )Optional   )run_graphql_query)container_register_auth)	endpoints)pods)	templates)user)gpusNapi_keyreturnc                 X    t          t          j        |           }|d         d         }|S )z
    Get the current user with optional API key override.
    
    Args:
        api_key: Optional API key to use for this query.
    r   datamyself)r   user_queries
QUERY_USERr   raw_responsecleaned_returns      T/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/runpod/api/ctl_commands.pyget_userr      s.     %\%<gNNNL!&)(3N    pubkeyc                 h    t          t          j        |           |          }|d         d         }|S )z
    Update the current user

    Args:
        pubkey: the public key of the user
        api_key: Optional API key to use for this query.
    r   r   updateUserSettings)r   user_mutationsgenerate_user_mutation)r   r   r   r   s       r   update_user_settingsr   !   sA     %-f55  L "&)*>?Nr   c                 X    t          t          j        |           }|d         d         }|S )zc
    Get all GPU types
    
    Args:
        api_key: Optional API key to use for this query.
    r   r   gpuTypes)r   r   QUERY_GPU_TYPESr   s      r   get_gpusr#   1   s.     %T%97KKKL!&)*5Nr   gpu_idgpu_quantityc                     t          t          j        | |          |          }|d         d         }t          |          dk     rt	          d          |d         S )z
    Get a specific GPU type

    Args:
        gpu_id: the id of the gpu
        gpu_quantity: how many of the gpu should be returned
        api_key: Optional API key to use for this query.
    r   r   r!   r   zSNo GPU found with the specified ID, run runpod.get_gpus() to get a list of all GPUsr   )r   r   generate_gpu_querylen
ValueError)r$   r%   r   r   r   s        r   get_gpur*   =   ss     %55  L
 "&)*5N
>Q>
 
 	

 !r   c                 d    t          t          j        |           }|d         d         d         }|S )z^
    Get all pods
    
    Args:
        api_key: Optional API key to use for this query.
    r   r   r   r   )r   pod_queries	QUERY_POD)r   
raw_returnr   s      r   get_podsr/   V   s3     #;#8'JJJJ'1&9Nr   pod_idc                 d    t          t          j        |           |          }|d         d         S )z
    Get a specific pod

    Args:
        pod_id: the id of the pod
        api_key: Optional API key to use for this query.
    r   r   pod)r   r,   generate_pod_query)r0   r   r   s      r   get_podr4   b   s;     %&v..  L &&r    ALLT/runpod-volumename
image_namegpu_type_id
cloud_typesupport_public_ip	start_sshdata_center_idcountry_code	gpu_countvolume_in_gbcontainer_disk_in_gbmin_vcpu_countmin_memory_in_gbdocker_argsportsvolume_mount_pathenvtemplate_idnetwork_volume_idallowed_cuda_versionsinstance_idc                    |s|st          d          |t          |           |dvrt          d          |r1|/t                      }|d         D ]}|d         |k    r
|d         } n|
|d}
t          t	          j        | |||||||||nd|	|
||||||||||||                    }t          d	|            ||d
         d         }n|d
         d         }|S )a/  
    Create a pod

    :param name: the name of the pod
    :param image_name: the name of the docker image to be used by the pod
    :param gpu_type_id: the gpu type wanted by the pod (retrievable by get_gpus). If None, creates a CPU-only pod
    :param cloud_type: if secure cloud, community cloud or all is wanted
    :param data_center_id: the id of the data center
    :param country_code: the code for country to start the pod in
    :param gpu_count: how many gpus should be attached to the pod (ignored for CPU-only pods)
    :param volume_in_gb: how big should the pod volume be
    :param ports: the ports to open in the pod, example format - "8888/http,666/tcp"
    :param volume_mount_path: where to mount the volume?
    :param env: the environment variables to inject into the pod,
                for example {EXAMPLE_VAR:"example_value", EXAMPLE_VAR2:"example_value 2"}, will
                inject EXAMPLE_VAR and EXAMPLE_VAR2 into the pod with the mentioned values
    :param template_id: the id of the template to use for the pod
    :param min_download: minimum download speed in Mbps
    :param min_upload: minimum upload speed in Mbps
    :param instance_id: the id of a specific instance to deploy to (for CPU pods)
    :example:

    >>> # Create GPU pod
    >>> pod_id = runpod.create_pod("test", "runpod/stack", "NVIDIA GeForce RTX 3070")
    >>> # Create CPU pod
    >>> pod_id = runpod.create_pod("test", "runpod/stack")
    >>> # Create CPU pod on specific instance
    >>> pod_id = runpod.create_pod("test", "runpod/stack", instance_id="cpu3c-2-4")
    z1Either image_name or template_id must be providedN)r6   	COMMUNITYSECUREz2cloud_type must be one of ALL, COMMUNITY or SECUREnetworkVolumesiddataCenterId
   zraw_response: r   podFindAndDeployOnDemanddeployCpuPod)r)   r*   r   r   pod_mutations generate_pod_deployment_mutationprint)r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   min_download
min_uploadrL   	user_infonetwork_volumer   cleaned_responses                              r   
create_podr^   q   sm   p  Nk NLMMM777MNNN ^3JJ	'(89 	 	Nd#'888!/!? 9 #(;!$6$0IId !/	
 	
 L8 

)<
)
)***'/0JK'/?r   c                 d    t          t          j        |                     }|d         d         }|S )z
    Stop a pod

    :param pod_id: the id of the pod

    :example:

    >>> pod_id = runpod.create_pod("test", "runpod/stack", "NVIDIA GeForce RTX 3070")
    >>> runpod.stop_pod(pod_id)
    r   podStop)r   rV   generate_pod_stop_mutation)r0   r   r]   s      r   stop_podrb      s2     %]%Mf%U%UVVL#F+I6r   c                 f    t          t          j        | |                    }|d         d         }|S )a"  
    Resume a pod

    :param pod_id: the id of the pod
    :param gpu_count: the number of GPUs to attach to the pod

    :example:

    >>> pod_id = runpod.create_pod("test", "runpod/stack", "NVIDIA GeForce RTX 3070")
    >>> runpod.stop_pod(pod_id)
    >>> runpod.resume_pod(pod_id)
    r   	podResume)r   rV   generate_pod_resume_mutation)r0   r@   r   r]   s       r   
resume_podrf      s;     %269EE L $F+K8r   c                 H    t          t          j        |                      dS )z
    Terminate a pod

    :param pod_id: the id of the pod

    :example:

    >>> pod_id = runpod.create_pod("test", "runpod/stack", "NVIDIA GeForce RTX 3070")
    >>> runpod.terminate_pod(pod_id)
    N)r   rV   generate_pod_terminate_mutation)r0   s    r   terminate_podri     s#     mCFKKLLLLLr   rS   Fdocker_start_cmdis_serverlessregistry_auth_idc
                 t    t          t          j        | |||||||||	
  
                  }
|
d         d         S )a  
    Create a template

    :param name: the name of the template
    :param image_name: the name of the docker image to be used by the template
    :param docker_start_cmd: the command to start the docker container with
    :param container_disk_in_gb: how big should the container disk be
    :param volume_in_gb: how big should the volume be
    :param ports: the ports to open in the pod, example format - "8888/http,666/tcp"
    :param volume_mount_path: where to mount the volume?
    :param env: the environment variables to inject into the pod,
                for example {EXAMPLE_VAR:"example_value", EXAMPLE_VAR2:"example_value 2"}, will
                inject EXAMPLE_VAR and EXAMPLE_VAR2 into the pod with the mentioned values
    :param is_serverless: is the template serverless?
    :param registry_auth_id: the id of the registry auth

    :example:

    >>> template_id = runpod.create_template("test", "runpod/stack", "python3 main.py")
    )
r8   r9   rj   rB   rA   rG   rF   rH   rk   rl   r   saveTemplate)r   template_mutationsgenerate_pod_template)r8   r9   rj   rB   rA   rG   rF   rH   rk   rl   r   s              r   create_templaterq     sY    @ %0!-!5%/'-	
 	
 	
 L //r   c                  `    t          t          j                  } | d         d         d         }|S )z
    Get all endpoints
    r   r   r   )r   endpoint_queriesQUERY_ENDPOINT)r.   r   s     r   get_endpointsru   F  s/     ##3#BCCJ'1+>Nr   	AMPERE_16   QUEUE_DELAY      gpu_ids	locationsidle_timeoutscaler_typescaler_valueworkers_minworkers_maxc                 x    t          t          j        | |||||||||	|
||                    }|d         d         S )a  
    Create an endpoint

    :param name: the name of the endpoint
    :param template_id: the id of the template to use for the endpoint
    :param gpu_ids: the ids of the GPUs to use for the endpoint
    :param network_volume_id: the id of the network volume to use for the endpoint
    :param locations: the locations to use for the endpoint
    :param idle_timeout: the idle timeout for the endpoint
    :param scaler_type: the scaler type for the endpoint
    :param scaler_value: the scaler value for the endpoint
    :param workers_min: the minimum number of workers for the endpoint
    :param workers_max: the maximum number of workers for the endpoint
    :param allowed_cuda_versions: Comma-separated list of allowed CUDA versions (e.g., ["12.4", "12.5"]).
    :param gpu_count: the number of GPUs to use for the endpoint

    :example:

    >>> endpoint_id = runpod.create_endpoint("test", "template_id")
    r   saveEndpoint)r   endpoint_mutationsgenerate_endpoint_mutation)r8   rI   r{   rJ   r|   r}   r~   r   r   r   	flashbootrK   r@   r   s                 r   create_endpointr   O  s]    F %5!	
 	
 L$ //r   endpoint_idc                 b    t          t          j        | |                    }|d         d         S )z
    Update an endpoint template

    :param endpoint_id: the id of the endpoint
    :param template_id: the id of the template to use for the endpoint

    :example:

    >>> endpoint_id = runpod.update_endpoint_template("test", "template_id")
    r   updateEndpointTemplate)r   r   !update_endpoint_template_mutation)r   rI   r   s      r   update_endpoint_templater     s6     %<[+VV L  899r   usernamepasswordc                 d    t          t          j        | ||                    }|d         d         S )aP  
    Create a container registry authentication.

    Args:
        name (str): The name of the container registry.
        username (str): The username for authentication.
        password (str): The password for authentication.

    Returns:
        dict: The response data containing the saved container registry authentication.
    r   saveRegistryAuth)r   !container_register_auth_mutations generate_container_registry_auth)r8   r   r   r   s       r   create_container_registry_authr     s=     %)J(H	
 	
 L
  233r   c                 d    t          t          j        | ||                    }|d         d         S )aj  
    Update a container registry authentication.

    Args:
        registry_auth_id (str): The id of the container registry authentication
        username (str): The username for authentication.
        password (str): The password for authentication.

    Returns:
        dict: The response data containing the updated container registry authentication.
    r   updateRegistryAuth)r   r   update_container_registry_auth)rl   r   r   r   s       r   r   r     s=     %)Hh	
 	
 L
  455r   c                 `    t          t          j        |                     }|d         d         S )z
    Delete a container registry authentication.

    Args:
        registry_auth_id (str): The id of the container registry authentication
    r   deleteRegistryAuth)r   r   delete_container_registry_auth)rl   r   s     r   r   r     s9     %)H	
 	
 L
  455r   )N)r   N)r5   Nr6   TTNNr   r   Nr   r   r5   Nr7   NNNNNNN)NrS   NNNNFN)rv   NNrw   rx   ry   r   rz   FNr   )+__doc__typingr   graphqlr   	mutationsr   r   r   r   r   rV   r	   ro   r
   r   queriesrs   r   r,   r   strdictr   r   r#   intr*   r/   r4   boollistr^   rb   rf   ri   rq   ru   r   r   r   r   r    r   r   <module>r      s          & & & & & & S S S S S S 6 6 6 6 6 6 , , , , , , 6 6 6 6 6 6 - - - - - - 2 2 2 2 2 2       ( ( ( ( ( ( ) ) ) ) ) )	 	hsm 	t 	 	 	 	  x}      	 	hsm 	t 	 	 	 	 C s #    2	 	hsm 	t 	 	 	 	' 'C '(3- ' ' ' '" !#!%"$("&*.-!%'+,0!%/l l
ll #l 	l
 l l SMl 3-l l l #3-l l l l C=l  !l" 
$#l$ #%l&  }'l( $D>)l. #/l0 
1l l l l^S    "s s    *M# M M M M" ! "! /0 /0
/0/0 /0 	/0
 /0 /0 /0 
/0 /0 /0 /0 /0 /0dt     !$!%50 50
5050 50 	50
 50 50 50 50 50 50 50 50 50 50 50p:# :C : : : :$4 4 4s 4 4 4 4(6S 6C 6SV 6 6 6 6(6S 6 6 6 6 6 6r   