Skip to main content

Package that provides utilities for handling images in Aliyun Cloud.

Project description

Overview

aliyun-img-utils provides a command line utility and API for publishing images in the Aliyun Cloud. This includes helper functions for uploading image blobs, creating compute images and replicating/publishing/deprecating/ images across all available regions.

See the Alibaba docs to get more info on the Aliyun cloud.

Requirements

  • oss2
  • Click
  • PyYAML
  • aliyun-python-sdk-core
  • aliyun-python-sdk-ecs

Installation

To install the package on openSUSE and SLES use the following commands as root:

$ zypper ar http://download.opensuse.org/repositories/Cloud:/Tools/<distribution>
$ zypper refresh
$ zypper in python3-aliyun-img-utils

To install from PyPI:

$ pip install aliyun-img-utils

Configuration

aliyun-img-utils can be configured with yaml based profiles. The configuration directory is ~/.config/aliyun_img_utils and the default profile is default.yaml (~/.config/aliyun_img_utils/default.yaml).

The following configration options are available in a configuration profile:

  • no_color
  • log_level
  • region
  • access_key
  • access_secret
  • bucket_name

An example configuration profile may look like:

region: cn-beijing
access_key: FakeKEY
access_secret: FAKESecret
bucket_name: smarlow-testing

When running any command the profile can be chosen via the --profile option. For example, aliyun-img-utils image upload --profile production would pull configuration from ~/.config/aliyun_img_utils/production.yaml.

CLI

The CLI is broken into multiple distinct subcommands that handle different steps of creating and publishing images in the Aliyun cloud framework.

Image blob upload

The first step is to upload a qcow2 image to a storage bucket. For this aliyun-img-utils image upload is available.

Example:

$ aliyun-img-utils image upload --image-file ~/Documents/test.qcow2

In this example the qcow2 file will be uploaded to the storage bucket configured for the given profile and the blob will be named test.qcow2. If you want to override the name of the blob there is a --blob-name option.

For more information about the image upload function see the help message:

$ aliyun-img-utils image upload --help

Compute image create

The next step is to create a compute image from the qcow2 blob. For this aliyun-img-utils image create is available.

Example:

$ aliyun-img-utils image create --image-name SLES15-SP2-BYOS --image-description "Test image" --platform SUSE --blob-name SLES15-SP2-BYOS.qcow2

In this example the qcow2 blob will be used to create the compute image. for the given profile and the blob will be named test.qcow2. If you want to override the default (20GB) root disk size there is a --disk-size option.

For more information about the image create function see the help message:

$ aliyun-img-utils image create --help

Replicate (copy) image

Once an image is created in a single region it can be replicated or copied to any other region: aliyun-img-utils image replicate.

Example:

$ aliyun-img-utils image replicate --image-name test-image-v20210303 --regions cn-shanghai

In this example the image will be replicated to the cn-shanghai region. If no regions are provided the image will be replicated to all available regions.

For more information about the image replicate function see the help message:

$ aliyun-img-utils image replicate --help

Publish image

The image can then be published or shared to other accounts with aliyun-img-utils image publish.

Example:

$ aliyun-img-utils image publish --image-name test-image-v20210303 --launch-permission EXAMPLE

In this example the launch permission for the image will be set to EXAMPLE. If no regions are provided the image will be published in all available regions.

For more information about the image publish function see the help message:

$ aliyun-img-utils image publish --help

Deprecate image

An image can be set to the deprecated state with aliyun-img-utils image deprecate.

Example:

$ aliyun-img-utils image deprecate --image-name test-image-v20210303

As with the other commands, if no regions are provided the image will be deprecated in all available regions.

For more information about the image deprecate function see the help message:

$ aliyun-img-utils image deprecate --help

Activate image

An image can be set back to the active state with aliyun-img-utils image activate.

Example:

$ aliyun-img-utils image activate --image-name test-image-v20210303

As with the other commands, if no regions are provided the image will be activated in all available regions.

For more information about the image activate function see the help message:

$ aliyun-img-utils image activate --help

Get image info

Info about a specific compute image can be retrieved with aliyun-img-utils image info.

Example:

$ aliyun-img-utils image info --image-name test-image-v20210303

The image can be searched by the --image-name or --image-id. By default only active images will be searched. To filter deprecated images there is a --deprecated option.

For more information about the image info function see the help message:

$ aliyun-img-utils image info --help

Delete image

A compute image can be deleted with aliyun-img-utils image delete.

Example:

$ aliyun-img-utils image delete --image-name test-image-v20210303

As with the other commands, if no regions are provided the image will be deleted in all available regions.

For more information about the image delete function see the help message:

$ aliyun-img-utils image delete --help

API

The AliyunImage class can be instantiated and used as an API from code. This provides all the same functions as the CLI with a few additional helpers. For example there are waiter functions which will wait for a compute image to be created and/or deleted.

To create an instance of AliyunImage you need an access_key, access_secret, region and bucket_name. optionally you can pass in a Python log object and/or a log_level.

aliyun_image = AliyunImage(
    access_key,
    access_secret,
    region,
    bucket_name,
    log_level=log_level,
    log_callback=logger
)

Code examples

With an instance of AliyunImage you can perform any of the image functions which are available through the CLI.

aliyun_image = AliyunImage(
    'accessKEY',
    'superSECRET',
    'cn-beijing',
    'images
)

# Upload image blob
blob_name = aliyun_image.upload_image_tarball('/path/to/image.qcow2')

# Create compute image
image_id = aliyun_image.create_compute_image(
    'test-image-v20220202',
    'A great image to use.',
    'test_image.qcow2',
    'SUSE'
)

# Delete compute image
# Deletes the image from the current region
deleted = aliyun_image.delete_compute_image('test-image-v20220202')

# Delete compute image in all available regions
aliyun_image.delete_compute_image_in_regions('test-image-v20220202')

# Delete storage blob from current bucket
deleted = aliyun_image.delete_storage_blob('test_image.qcow2')

# Copy image to a single region
image_id = aliyun_image.copy_compute_image(
    'test-image-v20220202',
    'cn-shanghai'
)

# Replicate (copy) image to all available regions
# A dictionary mapping region names to image ids is returned.
images = aliyun_image.replicate_image('test-image-v20220202')

# Publish image in current region
aliyun_image.publish_image('test-image-v20220202', 'EXAMPLE_PERMISSION')

# Publish image in all available regions
aliyun_image.publish_image_to_regions(
    'test-image-v20220202',
    'EXAMPLE_PERMISSION'
)

# Deprecate image in current region
aliyun_image.deprecate_image('test-image-v20220202')

# Deprecate image in all available regions
aliyun_image.deprecate_image_in_regions('test-image-v20220202')

# Activate image in current region
aliyun_image.activate_image('test-image-v20220202')

# Activate image in all available regions
aliyun_image.activate_image_in_regions('test-image-v20220202')

# Wait for image to become available based on image id
aliyun_image.wait_on_compute_image('i-123456789')

# Wait for image to be deleted based on image id
aliyun_image.wait_on_compute_image_delete('i-123456789')

# Return True if the image exists based on image name
exists = aliyun_image.image_exists('test-image-v20220202')

# 
exists = aliyun_image.image_tarball_exists('test_image.qcow2')

# Get image info as a dictionary
image_info = aliyun_image.get_compute_image(image_name='test_image.qcow2')

# Get a list of available regions
regions = aliyun_image.get_regions()

The current region or bucket_name can be changed at any time.

When the bucket_name is changed the current bucket_client session is closed. The session will reconnect in a lazy fashion on the next storage operation.

aliyun_image = AliyunImage(
    'accessKEY',
    'superSECRET',
    'cn-beijing',
    'images
)
exists = aliyun_image.image_tarball_exists('test_image.qcow2')

# Resets the storage bucket client
aliyun_image.bucket_name = 'old-images'

# Storage bucket client connects to the new bucket lazily
exists = aliyun_image.image_tarball_exists('test_image.qcow2')

Similarly when the region is changed both the bucket_client and the compute_client sessions are closed.

aliyun_image = AliyunImage(
    'accessKEY',
    'superSECRET',
    'cn-beijing',
    'images
)
image_info = aliyun_image.get_compute_image(image_name='test_image.qcow2')

# Resets the compute client
aliyun_image.region = 'cn-shanghai'

# Compute client connects to the new region lazily
image_info = aliyun_image.get_compute_image(image_name='test_image.qcow2')

Issues/Enhancements

Please submit issues and requests to Github.

Contributing

Contributions to aliyun-img-utils are welcome and encouraged. See CONTRIBUTING for info on getting started.

License

Copyright (c) 2021 SUSE LLC.

Distributed under the terms of GPL-3.0+ license, see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aliyun-img-utils-1.7.0.tar.gz (33.9 kB view details)

Uploaded Source

Built Distribution

aliyun_img_utils-1.7.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file aliyun-img-utils-1.7.0.tar.gz.

File metadata

  • Download URL: aliyun-img-utils-1.7.0.tar.gz
  • Upload date:
  • Size: 33.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for aliyun-img-utils-1.7.0.tar.gz
Algorithm Hash digest
SHA256 0775da4955240944fb70f9737b9a2c337af3ead0ecd63950b55e680617a30146
MD5 a7a0816ceeafb036a791de710241ed28
BLAKE2b-256 d5b980920cd9cc7cc151c31a163dc221bcc580682e2b4a405a7436025fe88e0a

See more details on using hashes here.

File details

Details for the file aliyun_img_utils-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: aliyun_img_utils-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for aliyun_img_utils-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0ddf3387fa566e4d72c1b63dfd75df9d18089e9102597ccd352fa96ebd5b3bb
MD5 b1de31f667c2fbd19e9ab63491bcf270
BLAKE2b-256 cc0be68f5b6b9f6288d78b1d6d80a9662887fcc04661620653de63792eca4f08

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page