main.py 11 KB

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