qvm-service 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection
  24. from optparse import OptionParser;
  25. import subprocess
  26. import sys
  27. import re
  28. def do_list(vm):
  29. max_len = 0
  30. for s in vm.services.keys():
  31. max_len = max(max_len, len(s))
  32. fmt="{{0:<{0}}}: {{1}}".format(max_len)
  33. for s in vm.services.keys():
  34. print fmt.format (s, "Enabled" if vm.services[s] else "Disabled")
  35. def main():
  36. usage = "usage: %prog <vm-name> [action] [service]\n"
  37. parser = OptionParser (usage)
  38. parser.add_option ("-l", "--list", dest="do_list", action="store_true", default=True,
  39. help="List services (default action)")
  40. parser.add_option ("-e", "--enable", dest="set_enable", action="store_true", default=False,
  41. help="Enable service")
  42. parser.add_option ("-d", "--disable", dest="set_disable", action="store_true", default=False,
  43. help="Disable service")
  44. parser.add_option ("-D", "--default", dest="set_default", action="store_true", default=False,
  45. help="Reset service to its default state (remove from the list)")
  46. (options, args) = parser.parse_args ()
  47. if (len (args) < 1):
  48. parser.error ("You must specify VM name!")
  49. vmname = args[0]
  50. args = args[1:]
  51. if options.set_enable or options.set_disable or options.set_default:
  52. if (len(args) < 1):
  53. parser.error("You must specify service name!")
  54. options.do_list = False
  55. qvm_collection = QubesVmCollection()
  56. if options.do_list:
  57. qvm_collection.lock_db_for_reading()
  58. qvm_collection.load()
  59. qvm_collection.unlock_db()
  60. else:
  61. qvm_collection.lock_db_for_writing()
  62. qvm_collection.load()
  63. vm = qvm_collection.get_vm_by_name(vmname)
  64. if vm is None:
  65. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  66. exit(1)
  67. changed = False
  68. if options.do_list:
  69. do_list(vm)
  70. elif options.set_enable:
  71. vm.services[args[0]] = True
  72. changed = True
  73. elif options.set_disable:
  74. vm.services[args[0]] = False
  75. changed = True
  76. elif options.set_default:
  77. if vm.services.has_key(args[0]):
  78. vm.services.pop(args[0])
  79. changed = True
  80. if changed:
  81. qvm_collection.save()
  82. if not options.do_list:
  83. qvm_collection.unlock_db()
  84. main()