Guide: OpenStack Python SDK – Basic Usage: Difference between revisions

From bwCloud-OS
Jump to navigation Jump to search
No edit summary
No edit summary
Line 34: Line 34:
conn = openstack.connect(cloud="openstack")
conn = openstack.connect(cloud="openstack")
</pre>
</pre>


== Compute ==
== Compute ==
Line 40: Line 39:


<pre>
<pre>
>>> for image in conn.image.images():
for image in conn.image.images():
...     print(image.name, image.id)
     print(image.name, image.id)
</pre>
</pre>


Line 47: Line 46:


<pre>
<pre>
>>> with open("/path/to/images/ubuntu-24.04-server-cloudimg-amd64.img", "rb") as img:
with open("/path/to/images/ubuntu-24.04-server-cloudimg-amd64.img", "rb") as img:
...     image = conn.image.upload_image(
     image = conn.image.upload_image(
...         name="ubuntu-24.04",
         name="ubuntu-24.04",
...         data=img,
         data=img,
...         disk_format="qcow2",
         disk_format="qcow2",
...         container_format="bare"
         container_format="bare"
...     )
     )
...     print(image.id)
     print(image.id)
</pre>
</pre>


Line 60: Line 59:


<pre>
<pre>
>>> image = conn.image.get_image("<IMAGE_ID>")
image = conn.image.get_image("<IMAGE_ID>")
>>> print(image)
print(image)
</pre>
</pre>


Line 67: Line 66:


<pre>
<pre>
>>> conn.image.delete_image("<IMAGE_ID_OR_NAME>")
conn.image.delete_image("<IMAGE_ID_OR_NAME>")
</pre>
</pre>


Line 74: Line 73:


<pre>
<pre>
>>> keypair = conn.compute.create_keypair(name="myKey")
keypair = conn.compute.create_keypair(name="myKey")
>>> with open("myKey.pem", "w") as f:
with open("myKey.pem", "w") as f:
...     f.write(keypair.private_key)
     f.write(keypair.private_key)
</pre>
</pre>


Line 88: Line 87:


<pre>
<pre>
>>> for kp in conn.compute.keypairs():
for kp in conn.compute.keypairs():
...     print(kp.name)
     print(kp.name)
</pre>
</pre>


Line 95: Line 94:


<pre>
<pre>
>>> conn.compute.delete_keypair("myKey")
conn.compute.delete_keypair("myKey")
</pre>
</pre>



Revision as of 22:18, 25 April 2026

This guide provides a quick introduction to managing your bwCloud-OS resources using the OpenStack Python SDK. It covers basic authentication and common operations for compute, storage, and networking.

In the following, we demonstrate the commands using an interactive Python shell (python3). In practice, these routines are typically used within Python scripts or programs.


Prerequisites

Install the OpenStack SDK (Ubuntu example):

sudo apt install python3-openstacksdk 

Alternatively, you can install it via pip (preferably inside a virtual environment):

python3 -mvenv openstack-sdk_venv
. openstack-sdk_venv
pip install openstacksdk

Authentication

The SDK uses the same configuration as the OpenStack CLI.

You can either:

  • source an OpenRC file, or
  • use a clouds.yaml file

Basic Connection

Create a connection in Python:

import openstack
conn = openstack.connect(cloud="openstack")

Compute

List available images:

for image in conn.image.images():
    print(image.name, image.id)

Upload a new image:

with open("/path/to/images/ubuntu-24.04-server-cloudimg-amd64.img", "rb") as img:
    image = conn.image.upload_image(
        name="ubuntu-24.04",
        data=img,
        disk_format="qcow2",
        container_format="bare"
    )
    print(image.id)

Show image details:

image = conn.image.get_image("<IMAGE_ID>")
print(image)

Delete an image:

conn.image.delete_image("<IMAGE_ID_OR_NAME>")

Key Pairs (SSH Access)

Create a key pair:

keypair = conn.compute.create_keypair(name="myKey")
with open("myKey.pem", "w") as f:
    f.write(keypair.private_key)

Set correct permissions (outside Python shell):

chmod 600 myKey.pem

List key pairs:

for kp in conn.compute.keypairs():
    print(kp.name)

Delete a key pair:

conn.compute.delete_keypair("myKey")

Instances

List instances:

for server in conn.compute.servers():
    print(server.name, server.status)

Create an instance:

image = conn.compute.find_image("ubuntu-24.04")
flavor = conn.compute.find_flavor("p1.tiny")
network = conn.network.find_network("myNet")
server = conn.compute.create_server(
    name="myVM",
    image_id=image.id,
    flavor_id=flavor.id,
    networks=[{"uuid": network.id}],
    key_name="myKey"
)
server = conn.compute.wait_for_server(server)
print("Created:", server.name)

Delete an instance:

server = conn.compute.find_server("myVM")
conn.compute.delete_server(server.id)


Storage

Volumes

List volumes:

for vol in conn.block_storage.volumes():
    print(vol.name, vol.size)

Create a volume:

volume = conn.block_storage.create_volume(
    name="myVolume",
    size=10
)

Delete a volume:

volume = conn.block_storage.find_volume("myVolume")
conn.block_storage.delete_volume(volume.id)

Volume Management for Instances

Attach a volume to an instance:

server = conn.compute.find_server("myVM")
volume = conn.block_storage.find_volume("myVolume")
conn.compute.create_volume_attachment(
    server,
    volumeId=volume.id
)

Detach a volume:

attachments = conn.compute.volume_attachments(server)
for att in attachments:
    if att.volume_id == volume.id:
        conn.compute.delete_volume_attachment(att.id, server)

Networking

Networks

List networks:

for net in conn.network.networks():
    print(net.name)

Create a network:

network = conn.network.create_network(name="myNet")

Delete a network:

network = conn.network.find_network("myNet")
conn.network.delete_network(network.id)

Floating IPs

Create a floating IP:

fip = conn.network.create_ip(floating_network_id="provider_default_net")
print(fip.floating_ip_address)

Associate a floating IP with an instance:

server = conn.compute.find_server("myVM")
conn.compute.add_floating_ip_to_server(server, fip.floating_ip_address)

Disassociate a floating IP:

conn.compute.remove_floating_ip_from_server(server, fip.floating_ip_address)

Delete a floating IP:

conn.network.delete_ip(fip.id)


Security Groups

List security groups:

for sg in conn.network.security_groups():
    print(sg.name)

Create a security group:

sg = conn.network.create_security_group(name="mySecGroup")

Add a rule (HTTP):

conn.network.create_security_group_rule(
    security_group_id=sg.id,
    protocol="tcp",
    port_range_min=80,
    port_range_max=80,
    direction="ingress"
)

Assign security group to an instance:

server = conn.compute.find_server("myVM")
conn.compute.add_security_group_to_server(server, sg.name)

Remove a security group from an instance:

conn.compute.remove_security_group_from_server(server, sg.name)

Delete a security group:

conn.network.delete_security_group(sg.id)