main.py 14 KB

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