qvm_run.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 logging
  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'] = open(os.devnull, 'wb')
  102. run_kwargs['stderr'] = run_kwargs['stdout']
  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. log = logging.getLogger('qvm_run')
  108. if args.color_output:
  109. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  110. sys.stdout.flush()
  111. if args.color_stderr:
  112. sys.stderr.write('\033[0;{}m'.format(args.color_stderr))
  113. sys.stderr.flush()
  114. try:
  115. procs = []
  116. for vm in args.domains:
  117. if not args.autostart and not vm.is_running():
  118. continue
  119. try:
  120. log.info('Running \'%s\' on %s', args.cmd, vm.name)
  121. if args.passio and not args.localcmd:
  122. loop = asyncio.new_event_loop()
  123. loop.add_signal_handler(signal.SIGCHLD, loop.stop)
  124. proc = vm.run_service('qubes.VMShell',
  125. user=args.user,
  126. localcmd=args.localcmd,
  127. filter_esc=args.filter_esc,
  128. **run_kwargs)
  129. proc.stdin.write(vm.prepare_input_for_vmshell(args.cmd))
  130. proc.stdin.flush()
  131. if args.passio and not args.localcmd:
  132. asyncio.ensure_future(loop.connect_read_pipe(
  133. functools.partial(DataCopyProtocol, proc.stdin,
  134. loop.stop),
  135. sys.stdin), loop=loop)
  136. loop.run_forever()
  137. loop.close()
  138. proc.stdin.close()
  139. procs.append(proc)
  140. except qubesadmin.exc.QubesException as e:
  141. if args.color_output:
  142. sys.stdout.write('\033[0m')
  143. sys.stdout.flush()
  144. vm.log.error(str(e))
  145. return -1
  146. for proc in procs:
  147. retcode = max(retcode, proc.wait())
  148. finally:
  149. if args.color_output:
  150. sys.stdout.write('\033[0m')
  151. sys.stdout.flush()
  152. if args.color_stderr:
  153. sys.stderr.write('\033[0m')
  154. sys.stderr.flush()
  155. if run_kwargs['stdout'] is not None:
  156. run_kwargs['stdout'].close()
  157. return retcode
  158. if __name__ == '__main__':
  159. sys.exit(main())