qvm_run.py 4.7 KB

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