Skip to content

Commit 8fbf31a

Browse files
Jayant-kernelclaude
andcommitted
vmupdate: move shutdown_domains to shutdown_utils to avoid circular import
Extract shutdown_domains() into a new vmupdate/shutdown_utils.py module so that both vmupdate.py and qube_connection.py can import it cleanly, without any circular dependency or late imports. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4670c56 commit 8fbf31a

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

vmupdate/qube_connection.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from vmupdate.agent.source.log_config import LOGPATH, LOG_FILE
3434
from vmupdate.agent.source.status import StatusInfo, FinalStatus
3535
from vmupdate.agent.source.common.process_result import ProcessResult
36+
from vmupdate.shutdown_utils import shutdown_domains
3637

3738

3839
class QubeConnection:
@@ -94,9 +95,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
9495

9596
if self.qube.is_running() and not self._initially_running:
9697
if self._has_assigned_pci_devices(self.qube):
97-
# Late import to avoid circular dependency:
98-
# qube_connection <- vmupdate <- update_manager <- qube_connection
99-
from vmupdate.vmupdate import shutdown_domains # pylint: disable=import-outside-toplevel
10098
self.logger.info(
10199
'Waiting for full shutdown %s (PCI devices assigned)',
102100
self.qube.name)

vmupdate/shutdown_utils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# coding=utf-8
2+
#
3+
# The Qubes OS Project, http://www.qubes-os.org
4+
#
5+
# Copyright (C) 2022 Piotr Bartman <prbartman@invisiblethingslab.com>
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License
9+
# as published by the Free Software Foundation; either version 2
10+
# of the License, or (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20+
# USA.
21+
import asyncio
22+
23+
import qubesadmin.exc
24+
from qubesadmin.events.utils import wait_for_domain_shutdown
25+
from vmupdate.agent.source.common.exit_codes import EXIT
26+
27+
28+
def shutdown_domains(to_shutdown, log):
29+
"""
30+
Try to shut down vms and wait to finish.
31+
"""
32+
ret_code = EXIT.OK
33+
wait_for = []
34+
for vm in to_shutdown:
35+
try:
36+
vm.shutdown(force=True)
37+
wait_for.append(vm)
38+
except qubesadmin.exc.QubesVMError as exc:
39+
log.error(str(exc))
40+
ret_code = EXIT.ERR_SHUTDOWN_APP
41+
42+
asyncio.run(wait_for_domain_shutdown(wait_for))
43+
44+
return ret_code, wait_for

vmupdate/vmupdate.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Update qubes.
44
"""
55
import argparse
6-
import asyncio
76
import logging
87
import sys
98
import os
@@ -13,9 +12,9 @@
1312

1413
import qubesadmin
1514
import qubesadmin.exc
16-
from qubesadmin.events.utils import wait_for_domain_shutdown
1715
from vmupdate.agent.source.status import FinalStatus
1816
from vmupdate.agent.source.common.exit_codes import EXIT
17+
from vmupdate.shutdown_utils import shutdown_domains
1918
from . import update_manager
2019
from .agent.source.args import AgentArgs
2120

@@ -394,24 +393,6 @@ def get_derived_vm_to_apply(templates, derived_statuses):
394393
return to_restart, to_shutdown
395394

396395

397-
def shutdown_domains(to_shutdown, log):
398-
"""
399-
Try to shut down vms and wait to finish.
400-
"""
401-
ret_code = EXIT.OK
402-
wait_for = []
403-
for vm in to_shutdown:
404-
try:
405-
vm.shutdown(force=True)
406-
wait_for.append(vm)
407-
except qubesadmin.exc.QubesVMError as exc:
408-
log.error(str(exc))
409-
ret_code = EXIT.ERR_SHUTDOWN_APP
410-
411-
asyncio.run(wait_for_domain_shutdown(wait_for))
412-
413-
return ret_code, wait_for
414-
415396

416397
def restart_vms(to_restart, log):
417398
"""

0 commit comments

Comments
 (0)