qvm_run.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (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 along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. from __future__ import print_function
  22. import os
  23. import sys
  24. import qubes
  25. import qubes.exc
  26. import qubes.tools
  27. parser = qubes.tools.QubesArgumentParser(vmname_nargs='+')
  28. parser.add_argument('--user', '-u', metavar='USER',
  29. help='run command in a qube as USER')
  30. parser.add_argument('--autostart', '--auto', '-a',
  31. action='store_true', default=False,
  32. help='start the qube if it is not running')
  33. parser.add_argument('--pass-io', '-p',
  34. action='store_true', dest='passio', default=False,
  35. help='pass stdio from remote program')
  36. parser.add_argument('--localcmd', metavar='COMMAND',
  37. help='with --pass-io, pass stdio to the given program')
  38. parser.add_argument('--gui',
  39. action='store_true', default=True,
  40. help='run the command with GUI (default on)')
  41. parser.add_argument('--no-gui', '--nogui',
  42. action='store_false', dest='gui',
  43. help='run the command without GUI')
  44. parser.add_argument('--colour-output', '--color-output', metavar='COLOUR',
  45. action='store', dest='color_output', default=None,
  46. help='mark the qube output with given ANSI colour (ie. "31" for red)')
  47. parser.add_argument('--colour-stderr', '--color-stderr', metavar='COLOUR',
  48. action='store', dest='color_stderr', default=None,
  49. help='mark the qube stderr with given ANSI colour (ie. "31" for red)')
  50. parser.add_argument('--no-colour-output', '--no-color-output',
  51. action='store_false', dest='color_output',
  52. help='disable colouring the stdio')
  53. parser.add_argument('--no-colour-stderr', '--no-color-stderr',
  54. action='store_false', dest='color_stderr',
  55. help='disable colouring the stderr')
  56. parser.add_argument('--filter-escape-chars',
  57. action='store_true', dest='filter_esc',
  58. default=os.isatty(sys.stdout.fileno()),
  59. help='filter terminal escape sequences (default if output is terminal)')
  60. parser.add_argument('--no-filter-escape-chars',
  61. action='store_false', dest='filter_esc',
  62. help='do not filter terminal escape sequences; DANGEROUS when output is a'
  63. ' terminal emulator')
  64. parser.add_argument('cmd', metavar='COMMAND',
  65. help='command to run')
  66. def main(args=None):
  67. args = parser.parse_args(args)
  68. if args.color_output is None and args.filter_esc:
  69. args.color_output = '31'
  70. if args.color_output is None and os.isatty(sys.stderr.fileno()):
  71. args.color_stderr = 31
  72. if len(args.domains) > 1 and args.passio:
  73. parser.error('--passio cannot be used when more than 1 qube is chosen')
  74. if args.localcmd and not args.passio:
  75. parser.error('--localcmd have no effect without --pass-io')
  76. if args.color_output and not args.filter_esc:
  77. parser.error('--color-output must be used with --filter-escape-chars')
  78. retcode = 0
  79. for vm in args.domains:
  80. if args.autostart and not vm.is_running():
  81. vm.start()
  82. if args.color_output:
  83. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  84. sys.stdout.flush()
  85. if args.color_stderr:
  86. sys.stderr.write('\033[0;{}m'.format(args.color_stderr))
  87. sys.stderr.flush()
  88. try:
  89. retcode = max(retcode, vm.run(args.cmd,
  90. user=args.user,
  91. passio=args.passio,
  92. localcmd=args.localcmd,
  93. gui=args.gui,
  94. filter_esc=args.filter_esc))
  95. except qubes.exc.QubesException as e:
  96. if args.color_output:
  97. sys.stdout.write('\033[0m')
  98. sys.stdout.flush()
  99. vm.log.error(str(e))
  100. return -1
  101. finally:
  102. if args.color_output:
  103. sys.stdout.write('\033[0m')
  104. sys.stdout.flush()
  105. if args.color_stderr:
  106. sys.stderr.write('\033[0m')
  107. sys.stderr.flush()
  108. return retcode
  109. if __name__ == '__main__':
  110. sys.exit(main())