lancium.api.Image

Image

This is an object that represents a computational environment. Used to create a Singularity container.

class Image:
Image(path=None, name=None, **kwargs)

Initialize an Image object.

ARGS:

  • path (string): Image path
  • name (string): Image name
  • description (string): description of job
  • source_type (string): Image type
  • source_url (string): url to source of Image
  • source (string): path to source file
  • build_script (string): build script for data
  • environment (list of dict): environment variables to be added to image
  • status (string): status of Image
def error_handle( res=None, name=None, data=None, error_handling=True, code=None, key=None):

Helper Method -- Handles status checking for server response objects

ARGS:

  • res (response object): response object from the server
  • name (string): Image name
  • data (json): json of payload for the api call
  • code (int): status code to check for
  • error_handling (boolean): flag for whether to do internal handling
  • key ('str'): key for response object

RETURNS: Nothing unless the request is a status code 422 in which case it returns list with the server response and a boolean stating that it is a directory.

def input_validation(source_type=None, build_script=None, source=None, source_url=None):

Helper Method -- Handles argument checking

ARGS:

  • source_type (string): Image type (singularity_image, singularity_file, docker_image, docker_file)
  • source_url (string): url to source file
  • source (string): path to source file
  • build_script (string): path to the build script for a singularity_file or docker_file

RETURNS: Nothing.

@staticmethod
def all(**kwargs):

Retrieve all Images.

GET /images

ARGS:

  • **kwargs (dictionary): can contain auth key to perform this method using a different account. {'auth': ANOTHER_API_KEY}

Returns: list: list of Images

from lancium.api.Image import Image

imgs = Image().all()
print(imgs)


### OUTPUT BELOW
'[<lancium.api.Image.Image object at 0x7f737491e8c0>,..., <lancium.api.Image.Image object at 0x7f737491f430>]'
@staticmethod
def create(path, name, source_type, **kwargs):

Create a new image.

POST /images

Args:

  • path (string): Image path
  • name (string): Image name
  • description (string): description of image
  • Source_type (string): Image type [docker_file, docker_image, singularity_image, singularity_file)
  • source_url (string): url ot source file
  • source (string): path to source image file
  • build_script (string) : path to build script for docker_file and singualrity_file
  • environment (list of dict): environment variables to be added to image
  • kwargs(dictionary): can contain auth key if you would like to perform this method using a different account. {'auth': ANOTHER_API_KEY}

Returns: Image: Image object

import os
from lancium.api.Image import Image

with open(os.path.abspath('pythonDocker.txt')) as build_script:
    contents = build_script.read()
params = {'build_script': contents}
imgs = Image().create(path='testing123', name='testing123', source_type='docker_file',**params)

### IMAGE OUTPUT BELOW

[
    {
        "path": "testing123",
        "name": "testing123",
        "source_type": "docker_file",
        "build_script": "contents of build-script",
        "status": "building",
        "created_at": "2022-07-07T16:53:37.237Z",
        "updated_at": "2022-07-07T16:53:39.000Z"
    }
]
@staticmethod
def get(path, **kwargs):

Retrieve specific image(s) by path.

GET /images/

Args:

  • path (string): path to image(s)
  • kwargs (dictionary): can contain auth to perform this method using a different account. ('auth': ANOTHER_API_KEY)

Return: list: list of Images

from lancium.api.Image import Image

imgs = Image().get('testing123')
print(imgs[0].path)
print(imgs[0].name)
print(imgs[0].source_type)

### OUTPUT BELOW
'testing123'
'testing123'
'docker_file'
def update(self, **kwargs):

Updates an existing image.

PUT /images/

ARGS:

  • path (string): Image path
  • name (string): Image name
  • description (string): description of image
  • source_type (string): Image type [docker_file, docker_image, singularity_image, singularity_file)
  • source_url (string): url ot source file
  • source (string): path to source image file
  • buidl_script (string) : path to build script for docker_file and singualrity_file
  • environment (list of dict): environment variables to be added to image

RETURNS: None: None

from lancium.api.Image import Image
 
 imgs = Image().get('testing123')
 testing123 = imgs[0]
 update = {'name': 'testing321'}
 testing123.update(**update)
 imgs2 = Image().get('testing123')
 print(imgs2[0].name)
 
 ### OUTPUT BELOW
 'testing321'
 

def refresh(self):

Refresh a specific image to reflect database.

GET /images/

Args: None (None)

RETURNS: None: None

from lancium.api.Image import Image
from lancium.errors.common import *


img = Image.get('ubuntu')[0]

img.name = 'not_ubuntu'

print(img.name)

img.refresh()

print(img.name)

###OUTPUT BELOW
not ubuntu
ubuntu
def upload(self, file_path, callback=None):

Upload image.

PATCH /images/

Args:

  • file_path (string): file to upload
  • callback (func, optional): called after each chunk is successfully uploaded, accepts arguments in the format of (file_size, file_start, total chunks, current chunk)

Returns: Response object: response object in json format

def rebuild(self):

Rebuild existing image.

POST /images//rebuild

ARGS: None (None)

RETURNS: None: None

from lancium.api.Image import Image

imgs = Image().get('testing123')
testing321 = imgs[0]
testing321.rebuild()

### IMAGE BELOW
[
    {
        "path": "testing123",
        "name": "testing123",
        "source_type": "docker_file",
        "build_script": "build script",
        "status": "building",
        "created_at": "2022-07-07T16:53:37.237Z",
        "updated_at": "2022-07-07T18:26:37.541Z",
        "built_at": "2022-07-07T17:08:51.243Z"
    }
]
@staticmethod
def delete(path, **kwargs):

Delete image.

DELETE /images/

ARGS:

  • path (string): path to image object within Lancium
  • kwargs (dictionary): can contain auth key if you would like to perform this method using a different account. {'auth': ANOTHER_API_KEY}

RETURNS: None: None

from lancium.api.Image import Image
from lancium.errors.common import *


Image.delete('test/mlperf2')

try:
    Image.get('test/mlperf2')
except:
    print('not found')

### OUTPUT BELOW
not found
def destroy(self):

Delete current image

DELETE /images/

Args: None (None)

Returns: None: None

from lancium.api.Image import Image
from lancium.errors.common import *


img = Image.get('zzzz')[0]

img.destroy()

try:
    Image.get('zzzz')
except:
    print('not found')

###OUTPUT BELOW
not found