qvm_remove.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2017 Marek Marczykowski-Górecki
  5. # <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser 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. ''' Remove domains from the system '''
  22. import sys
  23. import qubesadmin.exc
  24. from qubesadmin.tools import QubesArgumentParser
  25. import qubesadmin.utils
  26. parser = QubesArgumentParser(description=__doc__,
  27. vmname_nargs='+')
  28. parser.add_argument("--force", "-f", action="store_true", dest="no_confirm",
  29. default=False, help="Do not prompt for confirmation")
  30. def main(args=None, app=None): # pylint: disable=missing-docstring
  31. args = parser.parse_args(args, app=app)
  32. go_ahead = ""
  33. if not args.no_confirm:
  34. print("This will completely remove the selected VM(s)...")
  35. for vm in args.domains:
  36. print(" ", vm.name)
  37. go_ahead = input("Are you sure? [y/N] ").upper()
  38. if args.no_confirm or go_ahead == "Y":
  39. for vm in args.domains:
  40. try:
  41. del args.app.domains[vm.name]
  42. except qubesadmin.exc.QubesVMInUseError as e:
  43. dependencies = qubesadmin.utils.vm_dependencies(vm.app, vm)
  44. if dependencies:
  45. print("VM {} cannot be removed. It is in use as:".format(
  46. vm.name))
  47. for (holder, prop) in dependencies:
  48. if holder:
  49. print(" - {} for {}".format(prop, holder.name))
  50. else:
  51. print(" - global property {}".format(prop))
  52. # Display the original message as well
  53. parser.error_runtime(e)
  54. except qubesadmin.exc.QubesException as e:
  55. parser.error_runtime(e)
  56. retcode = 0
  57. else:
  58. print("Remove cancelled.")
  59. retcode = 1
  60. return retcode
  61. if __name__ == '__main__':
  62. sys.exit(main())