or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/python-libmaas@0.6.x

docs

index.md
tile.json

tessl/pypi-python-libmaas

tessl install tessl/pypi-python-libmaas@0.6.0

Python client library for MAAS 2.0+ with sync/async support, providing machine provisioning, network management, and storage configuration.

quick-start.mddocs/guides/

Quick Start Guide

Get started with python-libmaas in minutes.

Installation

pip install python-libmaas

Basic Setup

1. Connect to MAAS

from maas.client import connect

# Connect with API key
client = connect(
    'http://maas.example.com:5240/MAAS/',
    apikey='consumer_key:token_key:token_secret'
)

2. Get Your API Key

From MAAS web UI:

  1. Navigate to your user profile
  2. Click "API keys"
  3. Generate or copy existing key in format: consumer:token:secret

Or via CLI:

maas login myprofile http://maas.example.com:5240/MAAS/

Common Workflows

Allocate and Deploy a Machine

from maas.client import connect
from maas.client.enum import NodeStatus

# Connect
client = connect('http://maas.example.com:5240/MAAS/', apikey='key')

# Allocate a machine with requirements
machine = client.machines.allocate(
    cpu_count=4,        # Minimum 4 CPUs
    mem=8192,           # Minimum 8GB RAM
    tags=['ssd']        # Must have 'ssd' tag
)

print(f"Allocated: {machine.hostname} ({machine.system_id})")

# Deploy Ubuntu 22.04
machine.deploy(distro_series='jammy')
print(f"Deploying {machine.hostname}...")

# Optional: Wait for deployment to complete
# machine.deploy(distro_series='jammy', wait=True, wait_interval=10)

List and Filter Machines

# List all machines
machines = client.machines.list()
for machine in machines:
    print(f"{machine.hostname}: {machine.status} ({machine.power_state})")

# Get specific machine
machine = client.machines.get('system_id')

# Check machine status
from maas.client.enum import NodeStatus

if machine.status == NodeStatus.READY:
    print("Machine is ready for allocation")
elif machine.status == NodeStatus.DEPLOYED:
    print(f"Machine deployed at: {machine.ip_addresses}")

Power Management

from maas.client.enum import PowerStopMode

# Power on
machine.power_on()

# Power off (graceful)
machine.power_off(stop_mode=PowerStopMode.SOFT)

# Power off (immediate)
machine.power_off(stop_mode=PowerStopMode.HARD)

# Check power state
from maas.client.enum import PowerState
state = machine.query_power_state()
if state == PowerState.ON:
    print("Machine is powered on")

Configure Network Interface

from maas.client.enum import LinkMode

# Get machine and interface
machine = client.machines.get('system_id')
interface = machine.interfaces.get_by_name('eth0')

# Get subnet
subnet = client.subnets.get(id=1)

# Configure static IP
link = interface.links.create(
    interface,
    LinkMode.STATIC,
    subnet=subnet,
    ip_address='10.0.0.100',
    default_gateway=True
)

print(f"Configured {interface.name} with {link.ip_address}")

Configure Storage

# Get machine and block device
machine = client.machines.get('system_id')
sdb = machine.block_devices.get_by_name('sdb')

# Create partition (100GB)
partition = sdb.partitions.create(sdb, size=107374182400)

# Format and mount
partition.format('ext4')
partition.mount('/data')

print(f"Mounted {partition.path} at /data")

Async Usage

For async applications:

import asyncio
from maas.client import connect

async def deploy_machine():
    # Connect (async in async context)
    client = await connect('http://maas.example.com:5240/MAAS/', apikey='key')
    
    # Allocate
    machine = await client.machines.allocate(cpu_count=2, mem=4096)
    
    # Deploy and wait
    await machine.deploy(distro_series='jammy', wait=True)
    
    print(f"Deployed: {machine.hostname}")
    return machine

# Run
machine = asyncio.run(deploy_machine())

Error Handling

Always handle potential errors:

from maas.client import connect
from maas.client.errors import PowerError, OperationNotAllowed
from maas.client.viscera.machines import MachineNotFound, FailedDeployment

client = connect('http://maas.example.com:5240/MAAS/', apikey='key')

try:
    # Allocate machine
    machine = client.machines.allocate(cpu_count=8, mem=16384)
    
    # Power on
    machine.power_on(wait=True)
    
    # Deploy
    machine.deploy(distro_series='jammy', wait=True)
    
    print(f"Success: {machine.hostname} at {machine.ip_addresses}")
    
except MachineNotFound:
    print("No machine matching requirements available")
    
except PowerError as e:
    print(f"Power management failed: {e}")
    # Check BMC configuration
    
except FailedDeployment as e:
    print(f"Deployment failed: {e}")
    print(f"Machine status: {e.obj.status}")
    # Release machine for retry
    e.obj.release()
    
except OperationNotAllowed as e:
    print(f"Operation not allowed in current state: {e}")

Using the CLI

The library includes a CLI tool:

# Login
maas login myprofile http://maas.example.com:5240/MAAS/

# List machines
maas myprofile machines list

# Allocate machine
maas myprofile machine allocate --cpu-count 4 --mem 8192

# Deploy machine
maas myprofile machine deploy <system-id> --distro-series jammy

# Interactive Python shell
maas myprofile shell

Next Steps

  • Real-World Scenarios - Complete usage examples
  • API Reference - Detailed API documentation
  • Edge Cases - Advanced scenarios

Common Issues

Connection Failed

  • Verify URL ends with /MAAS/
  • Check network connectivity
  • Ensure MAAS server is running

Authentication Failed

  • Verify API key format: consumer:token:secret
  • Check key is valid and not expired
  • Ensure user has appropriate permissions

No Machines Available

  • Check machine status: client.machines.list()
  • Verify machines are in READY state
  • Check allocation constraints aren't too restrictive

Async/Sync Confusion

  • In async functions, use await for all API calls
  • In sync functions, don't use await
  • Use asyncio.run() to call async from sync code