main.py 11 KB

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