main.py 13 KB

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