Skip to content

Commit

Permalink
backend: self-identify the resalloc resource in logs
Browse files Browse the repository at this point in the history
If the `cmd_new` (pools.yaml config) commands returns a YAML-formatted
output, try to parse 'host' and 'name' fields.  The 'name' in particular
should match the RESALLOC_NAME.
  • Loading branch information
praiskup committed Nov 9, 2023
1 parent 7f6ed7c commit f5e6e17
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 32 additions & 1 deletion backend/copr_backend/vm_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import time
import yaml
from resalloc.client import Connection as ResallocConnection


Expand Down Expand Up @@ -91,14 +92,42 @@ class ResallocHost(RemoteHost):
"""
ticket = None

""" RESALLOC_NAME """
name = None

def parse_ticket_data(self):
"""
Historically we expected a single-line input containing hostname/IP. We
continue to support this format. But if the output looks like YAML
document, we parse the output and detect additional metadata.
"""
output = str(self.ticket.output)
lines = output.split("\n")
if lines[0] == "---":
try:
data = yaml.safe_load(output)
# We expect IP or hostname here
self.hostname = data["host"]
# RESALLOC_NAME
self.name = data["name"]
except yaml.YAMLError as exc:
raise RemoteHostAllocationTerminated(
f"Can't parse YAML data from the resalloc ticket:\n{output}") from exc
except KeyError as exc:
raise RemoteHostAllocationTerminated(
f"Missing YAML fields in the resalloc ticket output\n{output}") from exc
else:
# The old format
self.hostname = lines[0]

def check_ready(self):
self.ticket.collect()
if self.ticket.closed:
# canceled, or someone else closed the ticket
raise RemoteHostAllocationTerminated
if not self.ticket.ready:
return False
self.hostname = str(self.ticket.output).strip()
self.parse_ticket_data()
return True

def release(self):
Expand All @@ -111,6 +140,8 @@ def info(self):
message += ", ticket_id={}".format(self.ticket.id)
if self.hostname:
message += ", hostname={}".format(self.hostname)
if self.name:
message += ", name={}".format(self.name)
return message


Expand Down
9 changes: 9 additions & 0 deletions backend/tests/test_vm_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def test_ticket(_rcon):
assert host.check_ready()
assert host.hostname == "1.1.1.1"

host.ticket.output = (
"---\n"
"host: 1.2.3.4\n"
"name: copr_pool_123456\n"
)
assert host.check_ready()
assert host.hostname == "1.2.3.4"
assert host.name == "copr_pool_123456"

host.ticket.closed = True
with pytest.raises(RemoteHostAllocationTerminated):
assert host.check_ready()
Expand Down

0 comments on commit f5e6e17

Please sign in to comment.