qvm_run.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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(
  30. want_vm=True,
  31. want_vm_all=True)
  32. parser.add_argument('--user', '-u', metavar='USER',
  33. help='run command in a qube as USER')
  34. parser.add_argument('--autostart', '--auto', '-a',
  35. action='store_true', default=False,
  36. help='start the qube if it is not running')
  37. parser.add_argument('--pass-io', '-p',
  38. action='store_true', dest='passio', default=False,
  39. help='pass stdio from remote program')
  40. parser.add_argument('--localcmd', metavar='COMMAND',
  41. help='with --pass-io, pass stdio to the given program')
  42. parser.add_argument('--gui',
  43. action='store_true', default=True,
  44. help='run the command with GUI (default on)')
  45. parser.add_argument('--no-gui', '--nogui',
  46. action='store_false', dest='gui',
  47. help='run the command without GUI')
  48. parser.add_argument('--colour-output', '--color-output', metavar='COLOUR',
  49. action='store', dest='color_output', default=None,
  50. help='mark the qube output with given ANSI colour (ie. "31" for red)')
  51. parser.add_argument('--no-colour-output', '--no-color-output',
  52. action='store_false', dest='color_output',
  53. help='disable colouring the stdio')
  54. parser.add_argument('--filter-escape-chars',
  55. action='store_true', dest='filter_esc',
  56. default=os.isatty(sys.stdout.fileno()),
  57. help='filter terminal escape sequences (default if output is terminal)')
  58. parser.add_argument('--no-filter-escape-chars',
  59. action='store_false', dest='filter_esc',
  60. help='do not filter terminal escape sequences; DANGEROUS when output is a'
  61. ' terminal emulator')
  62. parser.add_argument('cmd', metavar='COMMAND',
  63. help='command to run')
  64. def main(args=None):
  65. args = parser.parse_args(args)
  66. if args.color_output is None and args.filter_esc:
  67. args.color_output = '31'
  68. if len(args.vm) > 1 and args.passio:
  69. parser.error('--passio cannot be used when more than 1 qube is chosen')
  70. if args.localcmd and not passio.passio:
  71. parser.error('--localcmd have no effect without --pass-io')
  72. if args.color_output and not args.filter_esc:
  73. parser.error('--color-output must be used with --filter-escape-chars')
  74. retcode = 0
  75. for vm in args.vm:
  76. if args.autostart and not vm.is_running():
  77. vm.start()
  78. if args.color_output:
  79. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  80. sys.stdout.flush()
  81. try:
  82. retcode = max(retcode, vm.run(args.cmd,
  83. user=args.user,
  84. passio=args.passio,
  85. localcmd=args.localcmd,
  86. gui=args.gui,
  87. filter_esc=args.filter_esc))
  88. except qubes.exc.QubesException as e:
  89. if args.color_output:
  90. sys.stdout.write('\033[0m')
  91. sys.stdout.flush()
  92. vm.log.error(str(e))
  93. return -1
  94. finally:
  95. if args.color_output:
  96. sys.stdout.write('\033[0m')
  97. sys.stdout.flush()
  98. return retcode
  99. if __name__ == '__main__':
  100. sys.exit(main())