Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: self-identify the resalloc resource in logs #2991

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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