main.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import datetime
  2. import logging
  3. import sys
  4. import os
  5. import re
  6. from docopt import docopt
  7. from inspect import getdoc
  8. from .. import __version__
  9. from ..service_collection import ServiceCollection
  10. from .command import Command
  11. from .errors import UserError
  12. from .docopt_command import NoSuchCommand
  13. log = logging.getLogger(__name__)
  14. def main():
  15. try:
  16. command = TopLevelCommand()
  17. command.sys_dispatch()
  18. except KeyboardInterrupt:
  19. log.error("\nAborting.")
  20. exit(1)
  21. except UserError, e:
  22. log.error(e.msg)
  23. exit(1)
  24. except NoSuchCommand, e:
  25. log.error("No such command: %s", e.command)
  26. log.error("")
  27. log.error("\n".join(parse_doc_section("commands:", getdoc(e.supercommand))))
  28. exit(1)
  29. # stolen from docopt master
  30. def parse_doc_section(name, source):
  31. pattern = re.compile('^([^\n]*' + name + '[^\n]*\n?(?:[ \t].*?(?:\n|$))*)',
  32. re.IGNORECASE | re.MULTILINE)
  33. return [s.strip() for s in pattern.findall(source)]
  34. class TopLevelCommand(Command):
  35. """.
  36. Usage:
  37. plum [options] [COMMAND] [ARGS...]
  38. plum -h|--help
  39. Options:
  40. --verbose Show more output
  41. --version Print version and exit
  42. Commands:
  43. ps List services and containers
  44. """
  45. def ps(self, options):
  46. """
  47. List services and containers.
  48. Usage: ps
  49. """
  50. for service in self.service_collection:
  51. for container in service.containers:
  52. print container['Names'][0]
  53. def start(self, options):
  54. """
  55. Start all services
  56. Usage: start
  57. """
  58. self.service_collection.start()
  59. def stop(self, options):
  60. """
  61. Stop all services
  62. Usage: stop
  63. """
  64. self.service_collection.stop()