-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenstack_utils.py
More file actions
64 lines (56 loc) · 2.43 KB
/
openstack_utils.py
File metadata and controls
64 lines (56 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Utility functions for managing OpenStack VMs.
"""
import base64
def create_vm(conn, name, settings):
"""Create a new VM in OpenStack."""
print(f"Creating VM: {name}")
network = conn.network.find_network(settings["vm_network"])
if not network:
raise ValueError(f"Network '{settings['vm_network']}' not found in OpenStack.")
flavor = conn.compute.find_flavor(settings["vm_flavor"])
if not flavor:
raise ValueError(f"Flavor '{settings['vm_flavor']}' not found in OpenStack.")
image = conn.compute.find_image(settings["vm_image"])
if not image:
raise ValueError(f"Image '{settings['vm_image']}' not found in OpenStack.")
if settings["key_name"]:
keypair = conn.compute.find_keypair(settings["key_name"])
if not keypair or keypair.id != settings["key_name"]:
raise ValueError(f"Key pair '{settings['key_name']}' not found or does not match the expected ID.")
try:
server = conn.compute.create_server(
name=name,
flavor_id=flavor.id,
image_id=image.id,
networks=[{"uuid": network.id}],
key_name=settings["key_name"], # Ensure the SSH key is used
)
conn.compute.wait_for_server(server)
server = conn.compute.get_server(server.id)
if server.status != "ACTIVE":
raise RuntimeError(f"VM {name} failed to reach 'ACTIVE' state. Current state: {server.status}")
print(f"VM {name} is in 'ACTIVE' state.")
return server
except RuntimeError as e:
print(f"Error creating VM: {e}")
raise
def get_running_vms(conn, vm_name_prefix=None):
"""Get the names of currently running VMs with an optional prefix filter."""
try:
servers = conn.compute.servers()
running_vms = [server.name for server in servers if server.status == "ACTIVE"]
if vm_name_prefix:
running_vms = [vm for vm in running_vms if vm.startswith(vm_name_prefix)]
return running_vms
except Exception as e:
print(f"Error fetching running VMs: {e}")
return []
def get_next_vm_name(conn, vm_name_prefix, max_vms):
"""Generate the next available VM name."""
existing_vms = [server.name for server in conn.compute.servers()]
for i in range(1, max_vms + 1):
vm_name = f"{vm_name_prefix}{i}" # Remove the hyphen from the VM name
if vm_name not in existing_vms:
return vm_name
return None