Skip to main content

Automation - SDK

The CGC SDK allows users to construct pipelines and incorporate logic into resource management.

Information

A valid context is required for connection to the platform and telemetry permissions must be set to avoid process interruption.

Basic Usage​

Creating and deleting an application can be done as follows:

Compute create​

import cgc.sdk as cgc
# Create a custom app
response_created = cgc.resource.compute_create_custom("name", "image-name:tag", cpu = 12, memory = 128,
gpu = 1, gpu_type="A100")

Function arguments​

Arg nameValueDefaultDescriptionRequired
namestringNoneName of the compute resource*
image_namestringNonePublic or private repo with image name and tag*
repository_secretstringNoneSecret name provided by CGC Team for private repo
cpuint1vCPU count
memoryint [GB]2RAM in GBs
shm_sizeint [GB]0Shared memory cut off from resource RAM
gpuint0GPU count
gpu_typestring [A100 | A5000]A5000Type of GPU
volumeslist[]List of volumes to attach
volume_full_pathstringNoneWay to specify where volume will be mounted
startup_commandstringNoneIf not specified in Docker Image
  • node_port_enabled - A flag indicating whether the node port is enabled, defaults to False. Available only on CGC OnPremise.

Compute delete​

import cgc.sdk as cgc

# Delete a custom app
response_deleted = cgc.resource.resource_delete("name")

Function arguments​

Arg nameValueDefaultDescriptionRequired
namestringNoneName of the compute resource*

Listing running and pending applications​

import cgc.sdk as cgc
# Get the status of apps
response_list = cgc.resource.resource_list()

Using Loops​

Create and delete application​

The start_function_loop function has been introduced to repeatedly execute a function until it returns a valid response with code 200. If the function is successful, it returns a server response, otherwise it throws an SDKException with code=-1.

Code​

import cgc.sdk as cgc
# Start a loop that creates a custom app, continue until the app is created
# start_function_loop supports both args and kwargs
try:
cgc.resource.start_function_loop(
    cgc.resource.compute_create_custom, # function to loop
    False, # infinite loop
    "my-app",
    "repository.domain.com/user/image:tag",
    repository_secret="my-secret",
)

# Your code to verify if the app is running and in healthy state
# ...
except cgc.exceptions.SDKException as e:
print(e)
print(e.code)

Responses​

FunctionStatusResponse codeResponse msgException
start_function_loopOK409NoneNone
stop_function_loopOK404NoneNone
start_function_loopError*Response code not 200 after 5 attempts.SDKException
stop_function_loopError*Response code not 200 after 5 attempts.SDKException

Check if application ready​

If you only need to check if the app is running, you can use the resource_ready function from the sdk.

Code​

import cgc.sdk as cgc
try:
# Create compute resource
response = cgc.resource.compute_create_custom("my-app", "image-name:tag", cpu = 12, memory = 128,
gpu = 1, gpu_type="A100")
# Start a loop that checks if the app is running
cgc.resource.start_function_loop(cgc.resource.resource_ready, True, "my-app")
# By now the resource is ready to use - which is not indication that your service in a container is ready
# Your code to verify if YOUR app is running and in healthy state
# ...
except cgc.exceptions.SDKException as e:
print(e)
print(e.code)

Custom Application ports​

The resource can be still in state Pending or Running when the function returns a response.

caution

Adding, editing and deleting will restart the running app.

If you'd like to know more please visit Custom Ports doc page.

Add custom port​

Code​

cgc.resource.resource_add_port(name="my_app", port_name="api", new_port=3000)

Function arguments​

Arg nameValueDefaultDescriptionRequired
namestringNoneName of the compute resource to edit*
port_namestringNoneName of port to add*
new_portintNoneNumber of port to add*
ingressbooleanTrueCheck if port should be visible from public Internet

Update custom port​

Code​

cgc.resource.resource_update_port(name="my_app", port_name="api", new_port=2345,ingress=False)

Function arguments​

Arg nameValueDefaultDescriptionRequired
namestringNoneName of the compute resource to edit*
port_namestringNoneName of port to edit*
new_portintNoneNumber of edited port*
ingressBooleanTrueCheck if port should be visible from public Internet

Delete custom port​

Code​

cgc.resource.resource_delete_port(name="my_app", port_name="api")

Function arguments​

Arg nameValueDefaultDescriptionRequired
namestringNoneName of the compute resource to edit*
port_namestringNoneName of port to delete*