main.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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, DependencyError
  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. exit(1)
  35. except (UserError, NoSuchService, DependencyError) as e:
  36. log.error(e.msg)
  37. 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. exit(1)
  43. except APIError as e:
  44. log.error(e.explanation)
  45. 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. Commands:
  60. build Build or rebuild services
  61. help Get help on a command
  62. kill Kill containers
  63. logs View output from containers
  64. ps List containers
  65. rm Remove stopped containers
  66. run Run a one-off command
  67. scale Set number of containers for a service
  68. start Start services
  69. stop Stop services
  70. up Create and start containers
  71. """
  72. def docopt_options(self):
  73. options = super(TopLevelCommand, self).docopt_options()
  74. options['version'] = "fig %s" % __version__
  75. return options
  76. def build(self, options):
  77. """
  78. Build or rebuild services.
  79. Services are built once and then tagged as `project_service`,
  80. e.g. `figtest_db`. If you change a service's `Dockerfile` or the
  81. contents of its build directory, you can run `fig build` to rebuild it.
  82. Usage: build [SERVICE...]
  83. """
  84. self.project.build(service_names=options['SERVICE'])
  85. def help(self, options):
  86. """
  87. Get help on a command.
  88. Usage: help COMMAND
  89. """
  90. command = options['COMMAND']
  91. if not hasattr(self, command):
  92. raise NoSuchCommand(command, self)
  93. raise SystemExit(getdoc(getattr(self, command)))
  94. def kill(self, options):
  95. """
  96. Force stop service containers.
  97. Usage: kill [SERVICE...]
  98. """
  99. self.project.kill(service_names=options['SERVICE'])
  100. def logs(self, options):
  101. """
  102. View output from containers.
  103. Usage: logs [SERVICE...]
  104. """
  105. containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
  106. print("Attaching to", list_containers(containers))
  107. LogPrinter(containers, attach_params={'logs': True}).run()
  108. def ps(self, options):
  109. """
  110. List containers.
  111. Usage: ps [options] [SERVICE...]
  112. Options:
  113. -q Only display IDs
  114. """
  115. containers = self.project.containers(service_names=options['SERVICE'], stopped=True) + self.project.containers(service_names=options['SERVICE'], one_off=True)
  116. if options['-q']:
  117. for container in containers:
  118. print(container.id)
  119. else:
  120. headers = [
  121. 'Name',
  122. 'Command',
  123. 'State',
  124. 'Ports',
  125. ]
  126. rows = []
  127. for container in containers:
  128. command = container.human_readable_command
  129. if len(command) > 30:
  130. command = '%s ...' % command[:26]
  131. rows.append([
  132. container.name,
  133. command,
  134. container.human_readable_state,
  135. container.human_readable_ports,
  136. ])
  137. print(Formatter().table(headers, rows))
  138. def rm(self, options):
  139. """
  140. Remove stopped service containers.
  141. Usage: rm [SERVICE...]
  142. """
  143. all_containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
  144. stopped_containers = [c for c in all_containers if not c.is_running]
  145. if len(stopped_containers) > 0:
  146. print("Going to remove", list_containers(stopped_containers))
  147. if yesno("Are you sure? [yN] ", default=False):
  148. self.project.remove_stopped(service_names=options['SERVICE'])
  149. else:
  150. print("No stopped containers")
  151. def run(self, options):
  152. """
  153. Run a one-off command on a service.
  154. For example:
  155. $ fig run web python manage.py shell
  156. Note that this will not start any services that the command's service
  157. links to. So if, for example, your one-off command talks to your
  158. database, you will need to run `fig up -d db` first.
  159. Usage: run [options] SERVICE COMMAND [ARGS...]
  160. Options:
  161. -d Detached mode: Run container in the background, print new
  162. container name
  163. -T Disable pseudo-tty allocation. By default `fig run`
  164. allocates a TTY.
  165. """
  166. service = self.project.get_service(options['SERVICE'])
  167. tty = True
  168. if options['-d'] or options['-T'] or not sys.stdin.isatty():
  169. tty = False
  170. container_options = {
  171. 'command': [options['COMMAND']] + options['ARGS'],
  172. 'tty': tty,
  173. 'stdin_open': not options['-d'],
  174. }
  175. container = service.create_container(one_off=True, **container_options)
  176. if options['-d']:
  177. service.start_container(container, ports=None)
  178. print(container.name)
  179. else:
  180. with self._attach_to_container(container.id, raw=tty) as c:
  181. service.start_container(container, ports=None)
  182. c.run()
  183. def scale(self, options):
  184. """
  185. Set number of containers to run for a service.
  186. Numbers are specified in the form `service=num` as arguments.
  187. For example:
  188. $ fig scale web=2 worker=3
  189. Usage: scale [SERVICE=NUM...]
  190. """
  191. for s in options['SERVICE=NUM']:
  192. if '=' not in s:
  193. raise UserError('Arguments to scale should be in the form service=num')
  194. service_name, num = s.split('=', 1)
  195. try:
  196. num = int(num)
  197. except ValueError:
  198. raise UserError('Number of containers for service "%s" is not a number' % service)
  199. try:
  200. self.project.get_service(service_name).scale(num)
  201. except CannotBeScaledError:
  202. 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)
  203. def start(self, options):
  204. """
  205. Start existing containers.
  206. Usage: start [SERVICE...]
  207. """
  208. self.project.start(service_names=options['SERVICE'])
  209. def stop(self, options):
  210. """
  211. Stop running containers without removing them.
  212. They can be started again with `fig start`.
  213. Usage: stop [SERVICE...]
  214. """
  215. self.project.stop(service_names=options['SERVICE'])
  216. def up(self, options):
  217. """
  218. Build, (re)create, start and attach to containers for a service.
  219. By default, `fig up` will aggregate the output of each container, and
  220. when it exits, all containers will be stopped. If you run `fig up -d`,
  221. it'll start the containers in the background and leave them running.
  222. If there are existing containers for a service, `fig up` will stop
  223. and recreate them (preserving mounted volumes with volumes-from),
  224. so that changes in `fig.yml` are picked up.
  225. Usage: up [options] [SERVICE...]
  226. Options:
  227. -d Detached mode: Run containers in the background, print new
  228. container names
  229. """
  230. detached = options['-d']
  231. (old, new) = self.project.recreate_containers(service_names=options['SERVICE'])
  232. if not detached:
  233. to_attach = [c for (s, c) in new]
  234. print("Attaching to", list_containers(to_attach))
  235. log_printer = LogPrinter(to_attach)
  236. for (service, container) in new:
  237. service.start_container(container)
  238. for (service, container) in old:
  239. container.remove()
  240. if not detached:
  241. try:
  242. log_printer.run()
  243. finally:
  244. def handler(signal, frame):
  245. self.project.kill(service_names=options['SERVICE'])
  246. sys.exit(0)
  247. signal.signal(signal.SIGINT, handler)
  248. print("Gracefully stopping... (press Ctrl+C again to force)")
  249. self.project.stop(service_names=options['SERVICE'])
  250. def _attach_to_container(self, container_id, raw=False):
  251. socket_in = self.client.attach_socket(container_id, params={'stdin': 1, 'stream': 1})
  252. socket_out = self.client.attach_socket(container_id, params={'stdout': 1, 'logs': 1, 'stream': 1})
  253. socket_err = self.client.attach_socket(container_id, params={'stderr': 1, 'logs': 1, 'stream': 1})
  254. return SocketClient(
  255. socket_in=socket_in,
  256. socket_out=socket_out,
  257. socket_err=socket_err,
  258. raw=raw,
  259. )
  260. def list_containers(containers):
  261. return ", ".join(c.name for c in containers)