Image resource
Import the image
resource module and instantiate the client
[ ]:
from collections.abc import Iterator
from typing import Any
from peak.resources import images
image_client: images.Image = images.get_client()
List all existing image
(s) and iterate over them
[ ]:
images_list_iterator: Iterator[dict[str, Any]] = image_client.list_images(
name="test",
status=["in-use"],
)
images_list_iterated: dict[str, Any] = next(images_list_iterator)
List all existing image version
(s) and iterate over them
[ ]:
image_versions_list_iterator: Iterator[dict[str, Any]] = image_client.list_image_versions(
image_id=9999,
version="test",
status=["in-use"],
tags=["dnd"],
)
image_versions_iterated: dict[str, Any] = next(image_versions_list_iterator)
Create a new image
resource
[ ]:
# Build details is optional and would be set to following if not provided.
default_build_details = {
"source": "upload",
}
# Within build details, source is optional and would be set to 'upload' if not provided.
# Creating an image with upload source.
upload_body = {
"version": "0.0.1",
"name": "image-sdk-101",
"description": "Hello from SDK",
"type": "workflow",
}
artifact = {
"path": "./image",
}
upload_image = image_client.create_image(
body=upload_body,
artifact=artifact,
)
# Creating an image with dockerfile source
dockerfile_body = {
"version": "0.0.1",
"name": "image-sdk-101",
"description": "Hello from SDK",
"type": "workflow",
"buildDetails": {
"source": "dockerfile",
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"dockerfile": "FROM nginx",
},
}
dockerfile_image = image_client.create_image(
body=dockerfile_body,
)
# Creating image with github source
github_body = {
"version": "0.0.1",
"name": "image-sdk-101",
"description": "Hello from SDK",
"type": "workflow",
"buildDetails": {
"source": "github",
"useCache": True,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"context": ".",
"branch": "main",
"repository": "https://github.com/username/repository",
"token": "token-name",
},
}
github_image = image_client.create_image(
body=github_body,
)
Create a new image version
resource
[ ]:
# Creating a version with upload source. Build details and source are optional. If not provided, source would be set to upload.
upload_body = {
"version": "0.0.1",
"description": "Hello from SDK",
"buildDetails": {
"source": "upload",
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"context": ".",
},
}
artifact = {
"path": "./image",
}
upload_version = image_client.create_version(
image_id=upload_image["imageId"],
body=upload_body,
artifact=artifact,
)
# Creating a version with dockerfile source
dockerfile_body = {
"version": "0.0.1",
"description": "Hello from SDK",
"buildDetails": {
"source": "dockerfile",
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"dockerfile": "FROM nginx",
},
}
dockerfile_version = image_client.create_version(
image_id=dockerfile_image["imageId"],
body=dockerfile_body,
)
# Creating version with github source
github_body = {
"version": "0.0.1",
"description": "Hello from SDK",
"buildDetails": {
"source": "github",
"useCache": True,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"context": ".",
"branch": "main",
"repository": "https://github.com/username/repository",
"token": "token-name",
},
}
github_version = image_client.create_version(
image_id=github_image["imageId"],
body=github_body,
)
Create or update a image version
[ ]:
# Create a image or version. Build details and source are optional. If not provided, source would be set to upload.
upload_body = {
"version": "0.0.1",
"name": "image-sdk-101",
"description": "Hello from SDK",
"type": "workflow",
"buildDetails": {
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"context": ".",
},
}
artifact = {
"path": "./image",
}
upload_image = image_client.create_or_update_image_version(
body=upload_body,
artifact=artifact,
)
# Update an existing image version. Build details and source are optional. If not provided, the build details and source of existing version would be used.
dockerfile_body = {
"version": "0.0.1",
"name": "image-sdk-101",
"description": "Hello from SDK",
"type": "workflow",
"buildDetails": {
"source": "dockerfile",
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"dockerfile": "FROM nginx",
},
}
dockerfile_image = image_client.create_or_update_image_version(
body=dockerfile_body,
)
Describe an existing image
resource
[ ]:
image_client.describe_image(image_id=int(upload_image["imageId"]))
Describe an existing version
resource
[ ]:
image_client.describe_version(image_id=int(upload_image["imageId"]), version_id=int(upload_image["versionId"]))
Update an existing image version
resource
[ ]:
# Update a version with upload source. Build details and source are optional. If not provided, the build details and source of existing version would be used.
updated_upload_body = {
"description": "Hello from SDK",
"buildDetails": {
"source": "upload",
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"context": ".",
},
}
artifact = {
"path": "./image",
}
updated_upload_version = image_client.update_version(
image_id=upload_image["imageId"],
version_id=upload_version["versionId"],
body=updated_upload_body,
artifact=artifact,
)
# Update a version with dockerfile source
updated_dockerfile_body = {
"description": "Hello from SDK",
"buildDetails": {
"source": "dockerfile",
"useCache": False,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"dockerfile": "FROM nginx",
},
}
updated_dockerfile_version = image_client.update_version(
image_id=dockerfile_image["imageId"],
version_id=dockerfile_version["versionId"],
body=updated_dockerfile_body,
)
# Update a version with github source
updated_github_body = {
"description": "Hello from SDK",
"buildDetails": {
"source": "github",
"useCache": True,
"buildArguments": [
{
"name": "HELLO",
"value": "world",
},
],
"context": ".",
"branch": "main",
"repository": "https://github.com/username/repository",
"token": "token-name",
},
}
updated_github_version = image_client.update_version(
image_id=github_image["imageId"],
version_id=github_version["versionId"],
body=updated_github_body,
)
List all builds in an existing image
resource and iterate over them
[ ]:
image_builds_iterator: Iterator[dict[str, Any]] = image_client.list_image_builds(image_id=int(upload_image["imageId"]))
image_builds_iterated: dict[str, Any] = next(image_builds_iterator)
Get logs for a specific build in an existing image
resource
[ ]:
image_build_logs = image_client.get_build_logs(image_id=1, build_id=1)
# To get next set of logs, pass the nextToken returned in the response of previous function call
next_set_of_image_build_logs = image_client.get_build_logs(
image_id=1,
build_id=1,
next_token=image_build_logs["nextToken"],
)
# To download the logs locally the `save` parameter can be used along with `file_name`
next_set_of_image_build_logs = image_client.get_build_logs(
image_id=1,
build_id=1,
save=True,
file_name="image_build_logs.log",
)
Delete an existing image version
resource
[ ]:
image_client.delete_version(image_id=int(upload_image["imageId"]), version_id=int(upload_image["versionId"]))
Delete multiple existing image version
resources
[ ]:
image_client.delete_versions(
image_id=int(upload_image["imageId"]),
version_ids=[
int(upload_image["versionId"]),
int(upload_version["versionId"]),
],
)
Note: All the versions will first go into deleting
state and will then be asynchronously deleted. If the deletion of any version fails because of some issue, it will go into delete_failed
state.
Delete an existing image
resource
[ ]:
image_client.delete_image(image_id=int(upload_image["imageId"]))
Note: All the versions associated with the given image will first go into deleting
state and will then be asynchronously deleted. If the deletion of any version fails because of some issue, it will go into delete_failed
state.