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

Added bootstrap command fixes in case of recreation #16

Merged
merged 1 commit into from
Feb 28, 2025
Merged
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
42 changes: 36 additions & 6 deletions genesis_devtools/libvirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ def get_domain_ip(name: str) -> tp.Optional[str]:
return re.findall(r"\d+\.\d+\.\d+\.\d+", line)[0]


def get_domain_disk(name: str) -> tp.Optional[str]:
out = subprocess.check_output(f"sudo virsh dumpxml {name}", shell=True)
out = out.decode().strip()

# The simplest implementation, take first disk
if disks := re.findall(r"<source file='(.*?)'", out):
return disks[0]


def has_domain(name: str) -> bool:
return name in list_domains()

Expand All @@ -227,12 +236,33 @@ def has_net(name: str) -> bool:

def destroy_domain(name: str) -> None:
"""Delete domain."""
subprocess.run(
f"sudo virsh destroy {name} 1>/dev/null && "
f"sudo virsh undefine {name} 1>/dev/null",
shell=True,
check=True,
)
disk_path = get_domain_disk(name)

try:
subprocess.run(
f"sudo virsh destroy {name} 1>/dev/null",
shell=True,
check=True,
)
except subprocess.CalledProcessError:
# Nothing to do, the domain is already destroyed
pass

try:
subprocess.run(
f"sudo virsh undefine {name} 1>/dev/null",
shell=True,
check=True,
)
except subprocess.CalledProcessError:
# Nothing to do, the domain is already undefined
pass

# Remove the disk
if disk_path:
subprocess.run(
f"sudo rm -f {disk_path} 1>/dev/null", shell=True, check=True
)


def destroy_net(name: str) -> None:
Expand Down