main.py 13 KB

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