tessl install tessl/pypi-python-libmaas@0.6.0Python client library for MAAS 2.0+ with sync/async support, providing machine provisioning, network management, and storage configuration.
Get started with python-libmaas in minutes.
pip install python-libmaasfrom maas.client import connect
# Connect with API key
client = connect(
'http://maas.example.com:5240/MAAS/',
apikey='consumer_key:token_key:token_secret'
)From MAAS web UI:
consumer:token:secretOr via CLI:
maas login myprofile http://maas.example.com:5240/MAAS/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 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}")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")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}")# 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")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())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}")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/MAAS/consumer:token:secretclient.machines.list()await for all API callsawaitasyncio.run() to call async from sync code