The Server class provides an interface for working with OpenStack virtual machine instances. You can query server details, access their configurations, and traverse relationships to users, hypervisors, and projects.
from myos.server import Serverserver = Server(server_id="79b3b46d-c7d8-47d2-a59d-ce5ded79b63b")# Get server detailsprint(server.name) # Server nameprint(server.id) # Server IDprint(server.status) # Current status (e.g., ACTIVE, SHUTOFF)
server = Server(server_id="79b3b46d-c7d8-47d2-a59d-ce5ded79b63b")# Get the user who created this serveruser = server.userprint(f"Created by: {user.name}")print(f"User ID: {user.id}")print(f"User Email: {user.email}")print(f"User Domain: {user.domain.name}")
The user property returns a User object, enabling full access to the creator’s information and related resources.
Determine which physical host is running the server:
server = Server(server_id="79b3b46d-c7d8-47d2-a59d-ce5ded79b63b")# Get the hypervisor running this serverhypervisor = server.hypervisorprint(f"Hypervisor: {hypervisor.hostname}")print(f"Status: {hypervisor.status}")print(f"State: {hypervisor.state}")
Example Output
Hypervisor: hv-a100x8-8.nubes.rl.ac.ukStatus: enabledState: up
Knowing the hypervisor is useful for troubleshooting, capacity planning, and understanding the physical infrastructure.
The myos SDK supports deep relationship traversal across multiple resource types:
from myos.server import Serverserver = Server(server_id="79b3b46d-c7d8-47d2-a59d-ce5ded79b63b")print(f"Server: {server.name}\n")# Traverse to user and their other resourcesuser = server.userprint(f"Created by: {user.name}")print(f"User's domain: {user.domain.name}\n")# Get all projects the creator has access touser_projects = user.projectsprint(f"Creator has access to {len(user_projects)} projects:")for project in user_projects: print(f" - {project.name}")print()# Traverse to hypervisor and other servers on ithypervisor = server.hypervisorprint(f"Running on hypervisor: {hypervisor.hostname}")other_servers = hypervisor.serversprint(f"This hypervisor hosts {len(other_servers)} total servers:")for srv in other_servers[:5]: # Show first 5 print(f" - {srv.name} ({srv.status})")
Deep traversal operations can be slow as they make multiple API calls. Consider caching results when possible.
from myos.user import Useruser = User(name="wup22514", domain_name="stfc")# Get all servers created by this userservers = user.serversprint(f"User has created {len(servers)} servers:\n")for server in servers: print(f"Server: {server.name}") print(f" ID: {server.id}") print(f" Status: {server.status}") print(f" Flavor: {server.flavor.name}") print(f" Image: {server.image.name}") print(f" Hypervisor: {server.hypervisor.hostname}") print()
from myos.project import Projectproject = Project(name="Condor")# Get all servers in this projectservers = project.serversprint(f"Project has {len(servers)} servers:\n")for server in servers: print(f"{server.name}:") print(f" Status: {server.status}") print(f" Creator: {server.user.name}") print()
Find all servers running on a specific hypervisor:
from myos.hypervisor import Hypervisorhypervisor = Hypervisor(name="hv-a100x8-8.nubes.rl.ac.uk")# Get all servers on this hypervisorservers = hypervisor.serversprint(f"Hypervisor hosts {len(servers)} servers:\n")for server in servers: print(f"Server: {server.name}") print(f" Status: {server.status}") print(f" Flavor: {server.flavor.name}") print()
This is useful for capacity planning and understanding hypervisor workload distribution.