1
0

main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. from __future__ import print_function
  2. from __future__ import unicode_literals
  3. import logging
  4. import sys
  5. import re
  6. import signal
  7. from inspect import getdoc
  8. import dockerpty
  9. from .. import __version__
  10. from ..project import NoSuchService, ConfigurationError
  11. from ..service import BuildError, CannotBeScaledError
  12. from .command import Command
  13. from .formatter import Formatter
  14. from .log_printer import LogPrinter
  15. from .utils import yesno
  16. from ..packages.docker.errors import APIError
  17. from .errors import UserError
  18. from .docopt_command import NoSuchCommand
  19. log = logging.getLogger(__name__)
  20. def main():
  21. setup_logging()
  22. try:
  23. command = TopLevelCommand()
  24. command.sys_dispatch()
  25. except KeyboardInterrupt:
  26. log.error("\nAborting.")
  27. sys.exit(1)
  28. except (UserError, NoSuchService, ConfigurationError) as e:
  29. log.error(e.msg)
  30. sys.exit(1)
  31. except NoSuchCommand as e:
  32. log.error("No such command: %s", e.command)
  33. log.error("")
  34. log.error("\n".join(parse_doc_section("commands:", getdoc(e.supercommand))))
  35. sys.exit(1)
  36. except APIError as e:
  37. log.error(e.explanation)
  38. sys.exit(1)
  39. except BuildError as e:
  40. log.error("Service '%s' failed to build: %s" % (e.service.name, e.reason))
  41. sys.exit(1)
  42. def setup_logging():
  43. console_handler = logging.StreamHandler(sys.stderr)
  44. console_handler.setFormatter(logging.Formatter())
  45. console_handler.setLevel(logging.INFO)
  46. root_logger = logging.getLogger()
  47. root_logger.addHandler(console_handler)
  48. root_logger.setLevel(logging.DEBUG)
  49. # Disable requests logging
  50. logging.getLogger("requests").propagate = False
  51. # stolen from docopt master
  52. def parse_doc_section(name, source):
  53. pattern = re.compile('^([^\n]*' + name + '[^\n]*\n?(?:[ \t].*?(?:\n|$))*)',
  54. re.IGNORECASE | re.MULTILINE)
  55. return [s.strip() for s in pattern.findall(source)]
  56. class TopLevelCommand(Command):
  57. """Punctual, lightweight development environments using Docker.
  58. Usage:
  59. fig [options] [COMMAND] [ARGS...]
  60. fig -h|--help
  61. Options:
  62. --verbose Show more output
  63. --version Print version and exit
  64. -f, --file FILE Specify an alternate fig file (default: fig.yml)
  65. -p, --project-name NAME Specify an alternate project name (default: directory name)
  66. Commands:
  67. build Build or rebuild services
  68. help Get help on a command
  69. kill Kill containers
  70. logs View output from containers
  71. ps List containers
  72. rm Remove stopped containers
  73. run Run a one-off command
  74. scale Set number of containers for a service
  75. start Start services
  76. stop Stop services
  77. up Create and start containers
  78. """
  79. def docopt_options(self):
  80. options = super(TopLevelCommand, self).docopt_options()
  81. options['version'] = "fig %s" % __version__
  82. return options
  83. def build(self, options):
  84. """
  85. Build or rebuild services.
  86. Services are built once and then tagged as `project_service`,
  87. e.g. `figtest_db`. If you change a service's `Dockerfile` or the
  88. contents of its build directory, you can run `fig build` to rebuild it.
  89. Usage: build [SERVICE...]
  90. """
  91. self.project.build(service_names=options['SERVICE'])
  92. def help(self, options):
  93. """
  94. Get help on a command.
  95. Usage: help COMMAND
  96. """
  97. command = options['COMMAND']
  98. if not hasattr(self, command):
  99. raise NoSuchCommand(command, self)
  100. raise SystemExit(getdoc(getattr(self, command)))
  101. def kill(self, options):
  102. """
  103. Force stop service containers.
  104. Usage: kill [SERVICE...]
  105. """
  106. self.project.kill(service_names=options['SERVICE'])
  107. def logs(self, options):
  108. """
  109. View output from containers.
  110. Usage: logs [SERVICE...]
  111. """
  112. containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
  113. print("Attaching to", list_containers(containers))
  114. LogPrinter(containers, attach_params={'logs': True}).run()
  115. def ps(self, options):
  116. """
  117. List containers.
  118. Usage: ps [options] [SERVICE...]
  119. Options:
  120. -q Only display IDs
  121. """
  122. containers = self.project.containers(service_names=options['SERVICE'], stopped=True) + self.project.containers(service_names=options['SERVICE'], one_off=True)
  123. if options['-q']:
  124. for container in containers:
  125. print(container.id)
  126. else:
  127. headers = [
  128. 'Name',
  129. 'Command',
  130. 'State',
  131. 'Ports',
  132. ]
  133. rows = []
  134. for container in containers:
  135. command = container.human_readable_command
  136. if len(command) > 30:
  137. command = '%s ...' % command[:26]
  138. rows.append([
  139. container.name,
  140. command,
  141. container.human_readable_state,
  142. container.human_readable_ports,
  143. ])
  144. print(Formatter().table(headers, rows))
  145. def rm(self, options):
  146. """
  147. Remove stopped service containers.
  148. Usage: rm [options] [SERVICE...]
  149. Options:
  150. --force Don't ask to confirm removal
  151. -v Remove volumes associated with containers
  152. """
  153. all_containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
  154. stopped_containers = [c for c in all_containers if not c.is_running]
  155. if len(stopped_containers) > 0:
  156. print("Going to remove", list_containers(stopped_containers))
  157. if options.get('--force') \
  158. or yesno("Are you sure? [yN] ", default=False):
  159. self.project.remove_stopped(
  160. service_names=options['SERVICE'],
  161. v=options.get('-v', False)
  162. )
  163. else:
  164. print("No stopped containers")
  165. def run(self, options):
  166. """
  167. Run a one-off command on a service.
  168. For example:
  169. $ fig run web python manage.py shell
  170. By default, linked services will be started, unless they are already
  171. running. If you do not want to start linked services, use
  172. `fig run --no-deps SERVICE COMMAND [ARGS...]`.
  173. Usage: run [options] SERVICE [COMMAND] [ARGS...]
  174. Options:
  175. -d Detached mode: Run container in the background, print
  176. new container name.
  177. -T Disable pseudo-tty allocation. By default `fig run`
  178. allocates a TTY.
  179. --rm Remove container after run. Ignored in detached mode.
  180. --no-deps Don't start linked services.
  181. """
  182. service = self.project.get_service(options['SERVICE'])
  183. if not options['--no-deps']:
  184. deps = service.get_linked_names()
  185. if len(deps) > 0:
  186. self.project.up(
  187. service_names=deps,
  188. start_links=True,
  189. recreate=False,
  190. )
  191. tty = True
  192. if options['-d'] or options['-T'] or not sys.stdin.isatty():
  193. tty = False
  194. if options['COMMAND']:
  195. command = [options['COMMAND']] + options['ARGS']
  196. else:
  197. command = service.options.get('command')
  198. container_options = {
  199. 'command': command,
  200. 'tty': tty,
  201. 'stdin_open': not options['-d'],
  202. }
  203. container = service.create_container(one_off=True, **container_options)
  204. if options['-d']:
  205. service.start_container(container, ports=None, one_off=True)
  206. print(container.name)
  207. else:
  208. service.start_container(container, ports=None, one_off=True)
  209. dockerpty.start(self.client, container.id)
  210. exit_code = container.wait()
  211. if options['--rm']:
  212. log.info("Removing %s..." % container.name)
  213. self.client.remove_container(container.id)
  214. sys.exit(exit_code)
  215. def scale(self, options):
  216. """
  217. Set number of containers to run for a service.
  218. Numbers are specified in the form `service=num` as arguments.
  219. For example:
  220. $ fig scale web=2 worker=3
  221. Usage: scale [SERVICE=NUM...]
  222. """
  223. for s in options['SERVICE=NUM']:
  224. if '=' not in s:
  225. raise UserError('Arguments to scale should be in the form service=num')
  226. service_name, num = s.split('=', 1)
  227. try:
  228. num = int(num)
  229. except ValueError:
  230. raise UserError('Number of containers for service "%s" is not a number' % service)
  231. try:
  232. self.project.get_service(service_name).scale(num)
  233. except CannotBeScaledError:
  234. raise UserError('Service "%s" cannot be scaled because it specifies a port on the host. If multiple containers for this service were created, the port would clash.\n\nRemove the ":" from the port definition in fig.yml so Docker can choose a random port for each container.' % service_name)
  235. def start(self, options):
  236. """
  237. Start existing containers.
  238. Usage: start [SERVICE...]
  239. """
  240. self.project.start(service_names=options['SERVICE'])
  241. def stop(self, options):
  242. """
  243. Stop running containers without removing them.
  244. They can be started again with `fig start`.
  245. Usage: stop [SERVICE...]
  246. """
  247. self.project.stop(service_names=options['SERVICE'])
  248. def up(self, options):
  249. """
  250. Build, (re)create, start and attach to containers for a service.
  251. By default, `fig up` will aggregate the output of each container, and
  252. when it exits, all containers will be stopped. If you run `fig up -d`,
  253. it'll start the containers in the background and leave them running.
  254. If there are existing containers for a service, `fig up` will stop
  255. and recreate them (preserving mounted volumes with volumes-from),
  256. so that changes in `fig.yml` are picked up. If you do not want existing
  257. containers to be recreated, `fig up --no-recreate` will re-use existing
  258. containers.
  259. Usage: up [options] [SERVICE...]
  260. Options:
  261. -d Detached mode: Run containers in the background,
  262. print new container names.
  263. --no-deps Don't start linked services.
  264. --no-recreate If containers already exist, don't recreate them.
  265. """
  266. detached = options['-d']
  267. start_links = not options['--no-deps']
  268. recreate = not options['--no-recreate']
  269. service_names = options['SERVICE']
  270. self.project.up(
  271. service_names=service_names,
  272. start_links=start_links,
  273. recreate=recreate
  274. )
  275. to_attach = [c for s in self.project.get_services(service_names) for c in s.containers()]
  276. if not detached:
  277. print("Attaching to", list_containers(to_attach))
  278. log_printer = LogPrinter(to_attach, attach_params={"logs": True})
  279. try:
  280. log_printer.run()
  281. finally:
  282. def handler(signal, frame):
  283. self.project.kill(service_names=service_names)
  284. sys.exit(0)
  285. signal.signal(signal.SIGINT, handler)
  286. print("Gracefully stopping... (press Ctrl+C again to force)")
  287. self.project.stop(service_names=service_names)
  288. def list_containers(containers):
  289. return ", ".join(c.name for c in containers)