qvm_run.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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('--no-colour-output', '--no-color-output',
  50. action='store_false', dest='color_output',
  51. help='disable colouring the stdio')
  52. parser.add_argument('--filter-escape-chars',
  53. action='store_true', dest='filter_esc',
  54. default=os.isatty(sys.stdout.fileno()),
  55. help='filter terminal escape sequences (default if output is terminal)')
  56. parser.add_argument('--no-filter-escape-chars',
  57. action='store_false', dest='filter_esc',
  58. help='do not filter terminal escape sequences; DANGEROUS when output is a'
  59. ' terminal emulator')
  60. parser.add_argument('cmd', metavar='COMMAND',
  61. help='command to run')
  62. def main(args=None):
  63. args = parser.parse_args(args)
  64. if args.color_output is None and args.filter_esc:
  65. args.color_output = '31'
  66. if len(args.domains) > 1 and args.passio:
  67. parser.error('--passio cannot be used when more than 1 qube is chosen')
  68. if args.localcmd and not args.passio:
  69. parser.error('--localcmd have no effect without --pass-io')
  70. if args.color_output and not args.filter_esc:
  71. parser.error('--color-output must be used with --filter-escape-chars')
  72. retcode = 0
  73. for vm in args.domains:
  74. if args.autostart and not vm.is_running():
  75. vm.start()
  76. if args.color_output:
  77. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  78. sys.stdout.flush()
  79. try:
  80. retcode = max(retcode, vm.run(args.cmd,
  81. user=args.user,
  82. passio=args.passio,
  83. localcmd=args.localcmd,
  84. gui=args.gui,
  85. filter_esc=args.filter_esc))
  86. except qubes.exc.QubesException as e:
  87. if args.color_output:
  88. sys.stdout.write('\033[0m')
  89. sys.stdout.flush()
  90. vm.log.error(str(e))
  91. return -1
  92. finally:
  93. if args.color_output:
  94. sys.stdout.write('\033[0m')
  95. sys.stdout.flush()
  96. return retcode
  97. if __name__ == '__main__':
  98. sys.exit(main())