Skip to content
Snippets Groups Projects
examples.rst 12.33 KiB

Examples

This is a collection of python scripts that demonstrate the usage of the kamaki clients API library

Initial steps

Initialize clients

Initialize the identity (auth), storage (store), compute, network, block storage (volume) and image clients, given an authentication URL and TOKEN.

Warning

Line 4 sets the CA certificates bundle to support secure connections. Secure connections are enabled by default and must be managed before setting any clients. See :ref:`clients-ssl` for more details.

Authentication URL and TOKEN from config file

Drag the URL and TOKEN information from the kamaki configuration file, using the "Config" class from kamaki CLI.

Note

The cloud URL and TOKEN are stored under a cloud name. Kamaki can be configured to use multiple clouds. It is common practice to set the value of the default cloud using the global.default_cloud configuration option. Here it was assumed that the value global.default_cloud is the name of the preferred cloud

Log HTTP

Instruct kamaki to output the HTTP logs on the console.

Containers and files

Information on object

List all objects in the storage_client's default container and ask user to pick one of them for more information.

Backup container

Back up the contents of the default container to a new container.

Empty and delete containers

Delete all containers if their names start with "backup".

Note

The "del_container" method will empty the container. The "purge_container" method will destroy an empty container. If the container is not empty, it cannot be destroyed.

Note

The "try-finally" clause is used to preserve the original container settings of the client (usually "pithos")

Upload and Download

Upload a local file

Download a remote object as local file

Note

The _gen callback function is used to show upload/download progress. It is optional. It must be a python generator, for example:

Asynchronous batch upload

Upload all files in a directory asynchronously

Reassign container

Each resource is assigned to a project, where the resource quotas are defined. With this script, users are prompted to choose a project to assign the default container.

Download and stream in parallel

Download an object in chunks. Stream them as they are being downloaded.

Note

The kamaki.clients.SilentEvent class extends threading.Thread in order to simplify thread handling.

Images

Register image

Upload an image to container "images" and register it to Plankton.

Note

Properties are mandatory in order to create a working image. In this example it is assumed a Debian Linux image. The suggested method for creating, uploading and registering custom images is by using the snf-image-creator tool.

Find image

Find images belonging to current user, by its name.

Modify image

Change the name and add a properties to an image. Use the image created above. One of the properties (description) is new, the other (users) exists and will be updated with a new value.

Unregister image

Unregister the image created above.

Virtual Machines (Servers)

Find flavors

Find all flavors with 2048 MB of RAM, 2 CPU cores and disk space between 20 GB and 40 GB.

Create server

To create a server, pick a name, a flavor id and an image id. In this example, assume the image from a previous step.

Note

To access the virtual server, a password is returned by the creation method. This password is revealed only once, when the server is created and it's not stored anywhere on the service side.

A popular access method is to inject the user ssh keys, as shown bellow.

Connection information

There are many ways to connect to a server: using a password or ssh keys, through a VNC console, the IP address or the qualified domain name.

Credentials for connection through a VNC console:

The following script collects all network information available: the F.Q.D.N. (fully qualified domain name) and the IP addresses (v4 as well as v6).

Update server

Rename the server and then add/change some metadata.

Start, Shutdown, Reboot or Delete server

First, get the current status of the server, and write a method for handling the wait results.

Shutdown a server, assuming it is currently active.

Start the stopped server.

Reboot the active server.

Destroy the server.

Server snapshots

Lookup volume

Each virtual server has at least one volume. This information is already stored in the "srv" object retrieved in a previous step.

Retrieve this information using the Block Storage client and check if it matches.

Create and delete volume

Create an extra volume. A server can have multiple volumes attached.

Note

In this example, the size of the volume is retrieved from the size of the server flavor. This is the safest method to set a fitting size.

Destroy the volume.

Warning

volume creation and deletion may take some time to complete.

Lookup snapshot

Find the snapshots of the server's first volume.

Create and delete snapshot

Create a snapshot of the first server volume.

Delete the snapshot.

Backup and restore snapshot

A snapshot can be thought as a backup, stored at users "snapshots" container.

Restore server from snapshot (assume the one created in the previous step).

To be safer, download the snapshot to local disk.

If the snapshot has been removed from the system, the server can be restored by uploading it from the local copy.

Note

By uploading from a local copy, we must register the snapshot as an image. Use the "exclude_all_taks" to register such images.

Networks

Term Description
network A public or private network
sunet A subnet of a network
port A connection between a device (e.g., vm) and a network
floating_ip An external IP v4, reserved by the current user for a network

Public and private networks

Public networks are created by the system. Private networks are created and managed by users.

Separate public from private networks.

Create and destroy virtual private network

Create a VPN.

Note

The "type" of the network is a Cyclades-specific parameter. To see all network types:

Delete the VPN.

Lookup IP

Find the ID of an IP.

Lookup server from IP

Find the server ID from an IP ID.

Reserve and release IP

Reserve an IP.

Note

Reserving an IP means "make it available for use". A freshly reserved IP is not used by any servers.

Release an IP.

Attach and dettach IP

Attach IP to server, by creating a connection (port) between the server and the network related to the IP.

Note

The "srv" object and the "assert_status" method from an earlier script are used here too.

Detach IP from server.