1
0

main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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, project, 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 [options] [SERVICE...]
  90. Options:
  91. --no-cache Do not use cache when building the image.
  92. """
  93. no_cache = bool(options.get('--no-cache', False))
  94. project.build(service_names=options['SERVICE'], no_cache=no_cache)
  95. def help(self, project, options):
  96. """
  97. Get help on a command.
  98. Usage: help COMMAND
  99. """
  100. command = options['COMMAND']
  101. if not hasattr(self, command):
  102. raise NoSuchCommand(command, self)
  103. raise SystemExit(getdoc(getattr(self, command)))
  104. def kill(self, project, options):
  105. """
  106. Force stop service containers.
  107. Usage: kill [SERVICE...]
  108. """
  109. project.kill(service_names=options['SERVICE'])
  110. def logs(self, project, options):
  111. """
  112. View output from containers.
  113. Usage: logs [options] [SERVICE...]
  114. Options:
  115. --no-color Produce monochrome output.
  116. """
  117. containers = project.containers(service_names=options['SERVICE'], stopped=True)
  118. monochrome = options['--no-color']
  119. print("Attaching to", list_containers(containers))
  120. LogPrinter(containers, attach_params={'logs': True}, monochrome=monochrome).run()
  121. def ps(self, project, options):
  122. """
  123. List containers.
  124. Usage: ps [options] [SERVICE...]
  125. Options:
  126. -q Only display IDs
  127. """
  128. containers = project.containers(service_names=options['SERVICE'], stopped=True) + project.containers(service_names=options['SERVICE'], one_off=True)
  129. if options['-q']:
  130. for container in containers:
  131. print(container.id)
  132. else:
  133. headers = [
  134. 'Name',
  135. 'Command',
  136. 'State',
  137. 'Ports',
  138. ]
  139. rows = []
  140. for container in containers:
  141. command = container.human_readable_command
  142. if len(command) > 30:
  143. command = '%s ...' % command[:26]
  144. rows.append([
  145. container.name,
  146. command,
  147. container.human_readable_state,
  148. container.human_readable_ports,
  149. ])
  150. print(Formatter().table(headers, rows))
  151. def rm(self, project, options):
  152. """
  153. Remove stopped service containers.
  154. Usage: rm [options] [SERVICE...]
  155. Options:
  156. --force Don't ask to confirm removal
  157. -v Remove volumes associated with containers
  158. """
  159. all_containers = project.containers(service_names=options['SERVICE'], stopped=True)
  160. stopped_containers = [c for c in all_containers if not c.is_running]
  161. if len(stopped_containers) > 0:
  162. print("Going to remove", list_containers(stopped_containers))
  163. if options.get('--force') \
  164. or yesno("Are you sure? [yN] ", default=False):
  165. project.remove_stopped(
  166. service_names=options['SERVICE'],
  167. v=options.get('-v', False)
  168. )
  169. else:
  170. print("No stopped containers")
  171. def run(self, project, options):
  172. """
  173. Run a one-off command on a service.
  174. For example:
  175. $ fig run web python manage.py shell
  176. By default, linked services will be started, unless they are already
  177. running. If you do not want to start linked services, use
  178. `fig run --no-deps SERVICE COMMAND [ARGS...]`.
  179. Usage: run [options] SERVICE [COMMAND] [ARGS...]
  180. Options:
  181. -d Detached mode: Run container in the background, print
  182. new container name.
  183. -T Disable pseudo-tty allocation. By default `fig run`
  184. allocates a TTY.
  185. --rm Remove container after run. Ignored in detached mode.
  186. --no-deps Don't start linked services.
  187. """
  188. service = project.get_service(options['SERVICE'])
  189. if not options['--no-deps']:
  190. deps = service.get_linked_names()
  191. if len(deps) > 0:
  192. project.up(
  193. service_names=deps,
  194. start_links=True,
  195. recreate=False,
  196. )
  197. tty = True
  198. if options['-d'] or options['-T'] or not sys.stdin.isatty():
  199. tty = False
  200. if options['COMMAND']:
  201. command = [options['COMMAND']] + options['ARGS']
  202. else:
  203. command = service.options.get('command')
  204. container_options = {
  205. 'command': command,
  206. 'tty': tty,
  207. 'stdin_open': not options['-d'],
  208. }
  209. container = service.create_container(one_off=True, **container_options)
  210. if options['-d']:
  211. service.start_container(container, ports=None, one_off=True)
  212. print(container.name)
  213. else:
  214. service.start_container(container, ports=None, one_off=True)
  215. dockerpty.start(project.client, container.id)
  216. exit_code = container.wait()
  217. if options['--rm']:
  218. log.info("Removing %s..." % container.name)
  219. project.client.remove_container(container.id)
  220. sys.exit(exit_code)
  221. def scale(self, project, options):
  222. """
  223. Set number of containers to run for a service.
  224. Numbers are specified in the form `service=num` as arguments.
  225. For example:
  226. $ fig scale web=2 worker=3
  227. Usage: scale [SERVICE=NUM...]
  228. """
  229. for s in options['SERVICE=NUM']:
  230. if '=' not in s:
  231. raise UserError('Arguments to scale should be in the form service=num')
  232. service_name, num = s.split('=', 1)
  233. try:
  234. num = int(num)
  235. except ValueError:
  236. raise UserError('Number of containers for service "%s" is not a '
  237. 'number' % service_name)
  238. try:
  239. project.get_service(service_name).scale(num)
  240. except CannotBeScaledError:
  241. raise UserError(
  242. 'Service "%s" cannot be scaled because it specifies a port '
  243. 'on the host. If multiple containers for this service were '
  244. 'created, the port would clash.\n\nRemove the ":" from the '
  245. 'port definition in fig.yml so Docker can choose a random '
  246. 'port for each container.' % service_name)
  247. def start(self, project, options):
  248. """
  249. Start existing containers.
  250. Usage: start [SERVICE...]
  251. """
  252. project.start(service_names=options['SERVICE'])
  253. def stop(self, project, options):
  254. """
  255. Stop running containers without removing them.
  256. They can be started again with `fig start`.
  257. Usage: stop [SERVICE...]
  258. """
  259. project.stop(service_names=options['SERVICE'])
  260. def up(self, project, options):
  261. """
  262. Build, (re)create, start and attach to containers for a service.
  263. By default, `fig up` will aggregate the output of each container, and
  264. when it exits, all containers will be stopped. If you run `fig up -d`,
  265. it'll start the containers in the background and leave them running.
  266. If there are existing containers for a service, `fig up` will stop
  267. and recreate them (preserving mounted volumes with volumes-from),
  268. so that changes in `fig.yml` are picked up. If you do not want existing
  269. containers to be recreated, `fig up --no-recreate` will re-use existing
  270. containers.
  271. Usage: up [options] [SERVICE...]
  272. Options:
  273. -d Detached mode: Run containers in the background,
  274. print new container names.
  275. --no-color Produce monochrome output.
  276. --no-deps Don't start linked services.
  277. --no-recreate If containers already exist, don't recreate them.
  278. """
  279. detached = options['-d']
  280. monochrome = options['--no-color']
  281. start_links = not options['--no-deps']
  282. recreate = not options['--no-recreate']
  283. service_names = options['SERVICE']
  284. project.up(
  285. service_names=service_names,
  286. start_links=start_links,
  287. recreate=recreate
  288. )
  289. to_attach = [c for s in project.get_services(service_names) for c in s.containers()]
  290. if not detached:
  291. print("Attaching to", list_containers(to_attach))
  292. log_printer = LogPrinter(to_attach, attach_params={"logs": True}, monochrome=monochrome)
  293. try:
  294. log_printer.run()
  295. finally:
  296. def handler(signal, frame):
  297. project.kill(service_names=service_names)
  298. sys.exit(0)
  299. signal.signal(signal.SIGINT, handler)
  300. print("Gracefully stopping... (press Ctrl+C again to force)")
  301. project.stop(service_names=service_names)
  302. def list_containers(containers):
  303. return ", ".join(c.name for c in containers)