qvm_run.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. ''' qvm-run tool'''
  21. import os
  22. import signal
  23. import sys
  24. import asyncio
  25. import functools
  26. import subprocess
  27. import qubesadmin.tools
  28. import qubesadmin.exc
  29. parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs='+')
  30. parser.add_argument('--user', '-u', metavar='USER',
  31. help='run command in a qube as USER (available only from dom0)')
  32. parser.add_argument('--autostart', '--auto', '-a',
  33. action='store_true', default=True,
  34. help='option ignored, this is default')
  35. parser.add_argument('--no-autostart', '--no-auto', '-n',
  36. action='store_false',
  37. help='do not autostart qube')
  38. parser.add_argument('--pass-io', '-p',
  39. action='store_true', dest='passio', default=False,
  40. help='pass stdio from remote program')
  41. parser.add_argument('--localcmd', metavar='COMMAND',
  42. help='with --pass-io, pass stdio to the given program')
  43. parser.add_argument('--gui',
  44. action='store_true', default=True,
  45. help='run the command with GUI (default on)')
  46. parser.add_argument('--no-gui', '--nogui',
  47. action='store_false', dest='gui',
  48. help='run the command without GUI')
  49. parser.add_argument('--colour-output', '--color-output', metavar='COLOUR',
  50. action='store', dest='color_output', default=None,
  51. help='mark the qube output with given ANSI colour (ie. "31" for red)')
  52. parser.add_argument('--colour-stderr', '--color-stderr', metavar='COLOUR',
  53. action='store', dest='color_stderr', default=None,
  54. help='mark the qube stderr with given ANSI colour (ie. "31" for red)')
  55. parser.add_argument('--no-colour-output', '--no-color-output',
  56. action='store_false', dest='color_output',
  57. help='disable colouring the stdio')
  58. parser.add_argument('--no-colour-stderr', '--no-color-stderr',
  59. action='store_false', dest='color_stderr',
  60. help='disable colouring the stderr')
  61. parser.add_argument('--filter-escape-chars',
  62. action='store_true', dest='filter_esc',
  63. default=os.isatty(sys.stdout.fileno()),
  64. help='filter terminal escape sequences (default if output is terminal)')
  65. parser.add_argument('--no-filter-escape-chars',
  66. action='store_false', dest='filter_esc',
  67. help='do not filter terminal escape sequences; DANGEROUS when output is a'
  68. ' terminal emulator')
  69. parser.add_argument('cmd', metavar='COMMAND',
  70. help='command to run')
  71. class DataCopyProtocol(asyncio.Protocol):
  72. '''Simple protocol to copy received data into another stream'''
  73. def __init__(self, target_stream, eof_callback=None):
  74. self.target_stream = target_stream
  75. self.eof_callback = eof_callback
  76. def data_received(self, data):
  77. '''Handle received data'''
  78. self.target_stream.write(data)
  79. self.target_stream.flush()
  80. def eof_received(self):
  81. '''Handle received EOF'''
  82. if self.eof_callback:
  83. self.eof_callback()
  84. def main(args=None, app=None):
  85. '''Main function of qvm-run tool'''
  86. args = parser.parse_args(args, app=app)
  87. if args.color_output is None and args.filter_esc:
  88. args.color_output = '31'
  89. if args.color_output is None and os.isatty(sys.stderr.fileno()):
  90. args.color_stderr = 31
  91. if len(args.domains) > 1 and args.passio and not args.localcmd:
  92. parser.error('--passio cannot be used when more than 1 qube is chosen '
  93. 'and no --localcmd is used')
  94. if args.localcmd and not args.passio:
  95. parser.error('--localcmd have no effect without --pass-io')
  96. if args.color_output and not args.filter_esc:
  97. parser.error('--color-output must be used with --filter-escape-chars')
  98. retcode = 0
  99. run_kwargs = {}
  100. if not args.passio:
  101. run_kwargs['stdout'] = subprocess.DEVNULL
  102. run_kwargs['stderr'] = subprocess.DEVNULL
  103. else:
  104. # connect process output to stdout/err directly if --pass-io is given
  105. run_kwargs['stdout'] = None
  106. run_kwargs['stderr'] = None
  107. if isinstance(args.app, qubesadmin.app.QubesLocal) and \
  108. not args.passio and not args.localcmd:
  109. # wait=False works only in dom0; but it's still useful, to save on
  110. # simultaneous vchan connections
  111. run_kwargs['wait'] = False
  112. verbose = args.verbose - args.quiet
  113. if args.passio:
  114. verbose -= 1
  115. if args.color_output:
  116. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  117. sys.stdout.flush()
  118. if args.color_stderr:
  119. sys.stderr.write('\033[0;{}m'.format(args.color_stderr))
  120. sys.stderr.flush()
  121. try:
  122. procs = []
  123. for vm in args.domains:
  124. if not args.autostart and not vm.is_running():
  125. continue
  126. try:
  127. if verbose > 0:
  128. if args.color_output:
  129. print('\033[0mRunning \'{}\' on {}\033[0;{}m'.format(
  130. args.cmd, vm.name, args.color_output),
  131. file=sys.stderr)
  132. else:
  133. print('Running \'{}\' on {}'.format(args.cmd, vm.name),
  134. file=sys.stderr)
  135. if args.gui:
  136. wait_session = vm.run_service('qubes.WaitForSession',
  137. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  138. wait_session.communicate(vm.default_user.encode())
  139. if args.passio and not args.localcmd:
  140. loop = asyncio.new_event_loop()
  141. loop.add_signal_handler(signal.SIGCHLD, loop.stop)
  142. proc = vm.run_service('qubes.VMShell',
  143. user=args.user,
  144. localcmd=args.localcmd,
  145. filter_esc=args.filter_esc,
  146. **run_kwargs)
  147. proc.stdin.write(vm.prepare_input_for_vmshell(args.cmd))
  148. proc.stdin.flush()
  149. if args.passio and not args.localcmd:
  150. asyncio.ensure_future(loop.connect_read_pipe(
  151. functools.partial(DataCopyProtocol, proc.stdin,
  152. loop.stop),
  153. sys.stdin), loop=loop)
  154. loop.run_forever()
  155. loop.close()
  156. proc.stdin.close()
  157. procs.append(proc)
  158. except qubesadmin.exc.QubesException as e:
  159. if args.color_output:
  160. sys.stdout.write('\033[0m')
  161. sys.stdout.flush()
  162. vm.log.error(str(e))
  163. return -1
  164. for proc in procs:
  165. retcode = max(retcode, proc.wait())
  166. finally:
  167. if args.color_output:
  168. sys.stdout.write('\033[0m')
  169. sys.stdout.flush()
  170. if args.color_stderr:
  171. sys.stderr.write('\033[0m')
  172. sys.stderr.flush()
  173. return retcode
  174. if __name__ == '__main__':
  175. sys.exit(main())