Python SDK
The PiVirt Python SDK provides a native, modern Python client for interacting with PiVirt ConnectRPC and gRPC services, supporting synchronous and asynchronous workflows.
Installation
Section titled “Installation”pip install pivirt# orpip install pivirt[grpc] # with gRPC supportQuick Start
Section titled “Quick Start”from pivirt.vm.v1 import VMServiceClient
# Create clientclient = VMServiceClient( 'agent.pilab.hu:8443', tls_ca='ca.crt', tls_cert='client.crt', tls_key='client.key',)
# List VMsvms = client.list_vms(namespace='production')for vm in vms: print(f"VM: {vm.name} (Status: {vm.status})")Authentication
Section titled “Authentication”from pivirt.vm.v1 import VMServiceClient
client = VMServiceClient( 'agent.pilab.hu:8443', tls_ca='ca.crt', tls_cert='client.crt', tls_key='client.key',)Bearer Token
Section titled “Bearer Token”client = VMServiceClient( 'agent.pilab.hu:8443', token='your-jwt-token',)VM Operations
Section titled “VM Operations”Create VM
Section titled “Create VM”from pivirt.vm.v1 import VMServiceClient, VMSpec, Disk, NetworkInterface
client = VMServiceClient('agent.pilab.hu:8443')
spec = VMSpec( name='web-01', vcpus=2, memory_bytes=4294967296, disks=[ Disk( name='root', pool='default', size_bytes=21474836480, format='QCOW2', bus='virtio', ), ], nics=[ NetworkInterface( name='eth0', bridge='virbr0', model='virtio', ), ],)
vm = client.create_vm(spec=spec, namespace='production')print(f"Created VM: {vm.id}")Get VM
Section titled “Get VM”vm = client.get_vm(id='vm-abc123', namespace='production')print(f"VM: {vm.name}, Status: {vm.status}")Start/Stop VM
Section titled “Start/Stop VM”# Start VMclient.start_vm(id='vm-abc123', namespace='production')
# Stop VMclient.stop_vm(id='vm-abc123', namespace='production')Migrate VM
Section titled “Migrate VM”operation = client.migrate_vm( id='vm-abc123', namespace='production', target_node='node-02', options={ 'live': True, 'bandwidth_limit_mbps': 1000, 'downtime_limit_ms': 500, 'compress': True, },)
print(f"Migration started: {operation.id}")Storage Operations
Section titled “Storage Operations”Create Volume
Section titled “Create Volume”from pivirt.storage.v1 import StorageServiceClient
client = StorageServiceClient('agent.pilab.hu:8443')
volume = client.create_volume( name='data-01', pool='default', size_bytes=107374182400, format='QCOW2',)
print(f"Created volume: {volume.id}")Create Snapshot
Section titled “Create Snapshot”snapshot = client.create_snapshot( volume_id='vol-abc123', name='backup-01',)
print(f"Created snapshot: {snapshot.id}")Async Support
Section titled “Async Support”import asynciofrom pivirt.vm.v1 import AsyncVMServiceClient
async def main(): client = AsyncVMServiceClient('agent.pilab.hu:8443')
vms = await client.list_vms(namespace='production') for vm in vms: print(f"VM: {vm.name} (Status: {vm.status})")
await client.close()
asyncio.run(main())Error Handling
Section titled “Error Handling”from pivirt.exceptions import PivirtError, NotFoundError, PermissionDeniedError
try: vm = client.get_vm(id='vm-abc123')except NotFoundError: print("VM not found")except PermissionDeniedError: print("Permission denied")except PivirtError as e: print(f"Error: {e.message}")Type Hints
Section titled “Type Hints”from pivirt.vm.v1.types import VM, VMSpec, VMStatus
def process_vm(vm: VM) -> None: print(f"VM: {vm.name}, Status: {vm.status}")
# Type checking with mypymypy your_script.pyExamples
Section titled “Examples”See the examples directory for complete examples.