27
27
import argparse
28
28
from base64 import b64decode , b64encode
29
29
from os .path import basename
30
+ import subprocess
30
31
from time import sleep
32
+ from typing import Iterator , Tuple
31
33
32
34
from robot .libraries .BuiltIn import BuiltIn , DotDict
33
- from robot .libraries .Process import Process , ExecutionResult
34
35
from robot .utils .robottime import timestr_to_secs
35
36
36
37
37
- def _execute (vm_name : str , agent_message : dict ) -> dict | int :
38
+ def _execute (vm_name : str , agent_message : dict ) -> dict :
38
39
virsh_args = f'virsh --connect=qemu:///system qemu-agent-command --domain={ vm_name } --cmd='
39
40
msg = json .dumps (agent_message )
40
41
virsh_args += f'\' { msg } \' '
41
42
42
- result : ExecutionResult = Process (). run_process (virsh_args , shell = True )
43
+ result = subprocess . run (virsh_args , shell = True , capture_output = True , text = True )
43
44
# Only raise an error if the virsh command itself fails. The guest-agent may return a non-zero exit code which
44
45
# should be handled by the keyword caller.
45
- if result .rc != 0 :
46
+ if result .returncode != 0 :
46
47
raise RuntimeError (f'virsh command failed:\n stdout={ result .stdout } '
47
48
f'\n stderr={ result .stderr } '
48
- f'\n rc={ result .rc } ' )
49
+ f'\n rc={ result .returncode } ' )
49
50
# qemu-agent-command returns data to stdout as:
50
51
# {
51
52
# return: {
@@ -77,7 +78,7 @@ def _do_guest_exec(vm_name: str, cmd: str, *args, env: dict, stdin: str) -> int:
77
78
return content ['pid' ]
78
79
79
80
80
- def _do_guest_exec_status (vm_name : str , pid : int ) -> ( dict , bool ) :
81
+ def _do_guest_exec_status (vm_name : str , pid : int ) -> Tuple [ dict , bool ] :
81
82
# _do_guest_exec_status wraps a given Process ID (pid) in a qemu-guest-agent guest-exec-status API call. For more
82
83
# info this API, see https://qemu-project.gitlab.io/qemu/interop/qemu-ga-ref.html#qapidoc-198. For information on
83
84
# the guest-exec-status return message, see https://qemu-project.gitlab.io/qemu/interop/qemu-ga-ref.html#qapidoc-194
@@ -117,7 +118,7 @@ def _do_kill(vm_name: str, pid: str, signal: str) -> int:
117
118
return _do_guest_exec (vm_name , '/usr/bin/kill' , signal , pid )
118
119
119
120
120
- def terminate_guest_process (vm_name : str , pid : str | int , kill : bool = False ) -> ( int , bool ) :
121
+ def terminate_guest_process (vm_name : str , pid : str | int , kill : bool = False ) -> Tuple [ int , bool ] :
121
122
"""
122
123
:param vm_name: The name of the VM to execute the command on.
123
124
:type vm_name: str
@@ -152,7 +153,7 @@ def terminate_guest_process(vm_name: str, pid: str | int, kill: bool = False) ->
152
153
return _do_kill (vm_name , pid , "-15" if not kill else "-9" )
153
154
154
155
155
- def get_guest_process_result (vm_name : str , pid : int ) -> ( DotDict , bool ) :
156
+ def get_guest_process_result (vm_name : str , pid : int ) -> Tuple [ DotDict , bool ] :
156
157
"""
157
158
:param vm_name: The name of the VM to execute the command on
158
159
:type vm_name: str
@@ -174,8 +175,7 @@ def get_guest_process_result(vm_name: str, pid: int) -> (DotDict, bool):
174
175
return DotDict (_do_guest_exec_status (vm_name , pid ))
175
176
176
177
177
- def wait_for_guest_process (vm_name : str , pid : int , timeout : int = None , on_timeout : str = "continue" ) -> (
178
- DotDict , bool ):
178
+ def wait_for_guest_process (vm_name : str , pid : int , timeout : int = None , on_timeout : str = "continue" ) -> Tuple [DotDict , bool ]:
179
179
"""
180
180
:param vm_name: The name of the VM to execute the command on
181
181
:type vm_name: str
@@ -247,7 +247,7 @@ def wait_for_guest_process(vm_name: str, pid: int, timeout: int = None, on_timeo
247
247
248
248
249
249
def run_guest_process (vm_name : str , cmd : str , * args , env : dict = None , stdin : str = None , timeout : int = None ,
250
- on_timeout : str = "continue" ) -> ( DotDict , bool ) :
250
+ on_timeout : str = "continue" ) -> Tuple [ DotDict , bool ] :
251
251
"""
252
252
:param vm_name: The name of the VM to execute the command on
253
253
:type vm_name: str
@@ -351,15 +351,15 @@ def _close_file(vm_name, handle):
351
351
_execute (vm_name , agent_cmd_wrapper )
352
352
353
353
354
- def _stream_file (vm_name : str , path : str ) -> str :
354
+ def _stream_file (vm_name : str , path : str ) -> Iterator [ str ] :
355
355
handle = _open_file (vm_name , path , 'r' )
356
356
try :
357
357
while True :
358
358
content = _execute (vm_name , {
359
359
'execute' : 'guest-file-read' ,
360
360
'arguments' : {
361
361
'handle' : handle ,
362
- 'count' : 10240
362
+ 'count' : 100 * 1024
363
363
}
364
364
})
365
365
0 commit comments