qvm_run.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 sys
  23. import subprocess
  24. import multiprocessing
  25. import qubesadmin.tools
  26. import qubesadmin.exc
  27. parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs='+')
  28. parser.add_argument('--user', '-u', metavar='USER',
  29. help='run command in a qube as USER (available only from dom0)')
  30. parser.add_argument('--autostart', '--auto', '-a',
  31. action='store_true', default=True,
  32. help='option ignored, this is default')
  33. parser.add_argument('--no-autostart', '--no-auto', '-n',
  34. action='store_false',
  35. help='do not autostart qube')
  36. parser.add_argument('--pass-io', '-p',
  37. action='store_true', dest='passio', default=False,
  38. help='pass stdio from remote program')
  39. parser.add_argument('--localcmd', metavar='COMMAND',
  40. help='with --pass-io, pass stdio to the given program')
  41. parser.add_argument('--gui',
  42. action='store_true', default=True,
  43. help='run the command with GUI (default on)')
  44. parser.add_argument('--no-gui', '--nogui',
  45. action='store_false', dest='gui',
  46. help='run the command without GUI')
  47. parser.add_argument('--colour-output', '--color-output', metavar='COLOUR',
  48. action='store', dest='color_output', default=None,
  49. help='mark the qube output with given ANSI colour (ie. "31" for red)')
  50. parser.add_argument('--colour-stderr', '--color-stderr', metavar='COLOUR',
  51. action='store', dest='color_stderr', default=None,
  52. help='mark the qube stderr with given ANSI colour (ie. "31" for red)')
  53. parser.add_argument('--no-colour-output', '--no-color-output',
  54. action='store_false', dest='color_output',
  55. help='disable colouring the stdio')
  56. parser.add_argument('--no-colour-stderr', '--no-color-stderr',
  57. action='store_false', dest='color_stderr',
  58. help='disable colouring the stderr')
  59. parser.add_argument('--filter-escape-chars',
  60. action='store_true', dest='filter_esc',
  61. default=os.isatty(sys.stdout.fileno()),
  62. help='filter terminal escape sequences (default if output is terminal)')
  63. parser.add_argument('--no-filter-escape-chars',
  64. action='store_false', dest='filter_esc',
  65. help='do not filter terminal escape sequences; DANGEROUS when output is a'
  66. ' terminal emulator')
  67. parser.add_argument('--service',
  68. action='store_true', dest='service',
  69. help='run a qrexec service (named by COMMAND) instead of shell command')
  70. parser.add_argument('cmd', metavar='COMMAND',
  71. help='command to run')
  72. def copy_stdin(stream):
  73. '''Copy stdin to *stream*'''
  74. # multiprocessing.Process have sys.stdin connected to /dev/null
  75. stdin = open(0)
  76. for data in iter(lambda: stdin.buffer.read(4096), b''):
  77. if data is None:
  78. break
  79. stream.write(data)
  80. stream.close()
  81. def main(args=None, app=None):
  82. '''Main function of qvm-run tool'''
  83. args = parser.parse_args(args, app=app)
  84. if args.color_output is None and args.filter_esc:
  85. args.color_output = '31'
  86. if args.color_output is None and os.isatty(sys.stderr.fileno()):
  87. args.color_stderr = 31
  88. if len(args.domains) > 1 and args.passio and not args.localcmd:
  89. parser.error('--passio cannot be used when more than 1 qube is chosen '
  90. 'and no --localcmd is used')
  91. if args.localcmd and not args.passio:
  92. parser.error('--localcmd have no effect without --pass-io')
  93. if args.color_output and not args.filter_esc:
  94. parser.error('--color-output must be used with --filter-escape-chars')
  95. retcode = 0
  96. run_kwargs = {}
  97. if not args.passio:
  98. run_kwargs['stdout'] = subprocess.DEVNULL
  99. run_kwargs['stderr'] = subprocess.DEVNULL
  100. else:
  101. # connect process output to stdout/err directly if --pass-io is given
  102. run_kwargs['stdout'] = None
  103. run_kwargs['stderr'] = None
  104. if isinstance(args.app, qubesadmin.app.QubesLocal) and \
  105. not args.passio and not args.localcmd and args.service:
  106. # wait=False works only in dom0; but it's still useful, to save on
  107. # simultaneous vchan connections
  108. run_kwargs['wait'] = False
  109. verbose = args.verbose - args.quiet
  110. if args.passio:
  111. verbose -= 1
  112. if args.color_output:
  113. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  114. sys.stdout.flush()
  115. if args.color_stderr:
  116. sys.stderr.write('\033[0;{}m'.format(args.color_stderr))
  117. sys.stderr.flush()
  118. copy_proc = None
  119. try:
  120. procs = []
  121. for vm in args.domains:
  122. if not args.autostart and not vm.is_running():
  123. continue
  124. try:
  125. if verbose > 0:
  126. if args.color_output:
  127. print('\033[0mRunning \'{}\' on {}\033[0;{}m'.format(
  128. args.cmd, vm.name, args.color_output),
  129. file=sys.stderr)
  130. else:
  131. print('Running \'{}\' on {}'.format(args.cmd, vm.name),
  132. file=sys.stderr)
  133. if args.gui:
  134. wait_session = vm.run_service('qubes.WaitForSession',
  135. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  136. wait_session.communicate(vm.default_user.encode())
  137. if args.service:
  138. proc = vm.run_service(args.cmd,
  139. user=args.user,
  140. localcmd=args.localcmd,
  141. filter_esc=args.filter_esc,
  142. **run_kwargs)
  143. else:
  144. proc = vm.run_service('qubes.VMShell',
  145. user=args.user,
  146. localcmd=args.localcmd,
  147. filter_esc=args.filter_esc,
  148. **run_kwargs)
  149. proc.stdin.write(vm.prepare_input_for_vmshell(args.cmd))
  150. proc.stdin.flush()
  151. if args.passio and not args.localcmd:
  152. copy_proc = multiprocessing.Process(target=copy_stdin,
  153. args=(proc.stdin,))
  154. copy_proc.start()
  155. # keep the copying process running
  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. if copy_proc is not None:
  174. copy_proc.terminate()
  175. return retcode
  176. if __name__ == '__main__':
  177. sys.exit(main())