Automation - SDK
The CGC SDK allows users to construct pipelines and incorporate logic into resource management.
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 name | Value | Default | Description | Required |
---|---|---|---|---|
name | string | None | Name of the compute resource | * |
image_name | string | None | Public or private repo with image name and tag | * |
repository_secret | string | None | Secret name provided by CGC Team for private repo | |
cpu | int | 1 | vCPU count | |
memory | int [GB] | 2 | RAM in GBs | |
shm_size | int [GB] | 0 | Shared memory cut off from resource RAM | |
gpu | int | 0 | GPU count | |
gpu_type | string [A100 | A5000] | A5000 | Type of GPU | |
volumes | list | [] | List of volumes to attach | |
volume_full_path | string | None | Way to specify where volume will be mounted | |
startup_command | string | None | If 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 name | Value | Default | Description | Required |
---|---|---|---|---|
name | string | None | Name 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​
Function | Status | Response code | Response msg | Exception |
---|---|---|---|---|
start_function_loop | OK | 409 | None | None |
stop_function_loop | OK | 404 | None | None |
start_function_loop | Error | * | Response code not 200 after 5 attempts. | SDKException |
stop_function_loop | Error | * | 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.
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 name | Value | Default | Description | Required |
---|---|---|---|---|
name | string | None | Name of the compute resource to edit | * |
port_name | string | None | Name of port to add | * |
new_port | int | None | Number of port to add | * |
ingress | boolean | True | Check 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 name | Value | Default | Description | Required |
---|---|---|---|---|
name | string | None | Name of the compute resource to edit | * |
port_name | string | None | Name of port to edit | * |
new_port | int | None | Number of edited port | * |
ingress | Boolean | True | Check 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 name | Value | Default | Description | Required |
---|---|---|---|---|
name | string | None | Name of the compute resource to edit | * |
port_name | string | None | Name of port to delete | * |