index.tex 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. \section{MAD: Erlang Build and Deploy Tool}
  2. \subsection{History}
  3. We realized no matter how perfect your libraries are,
  4. the comfort and easy come mostly from developing tools.
  5. Everything got started when \footahref{https://github.com/proger}{Vladimir Kirillov} decided to
  6. replace Rusty's sync beam reloader. As you know sync uses
  7. filesystem polling which is neither energy-efficient nor elegant. Also
  8. sync is only able to recompile separate modules while
  9. common use-case in N2O is to recompile DTL templates
  10. and LESS/SCSS stylesheets. That is why we need to recompile
  11. the whole project. That's the story how active emerged.
  12. Under the hood active is a client subscriber
  13. of fs library, native filesystem listener for Linux, Windows and Mac.
  14. De-facto standard in Erlang world is rebar.
  15. We love rebar interface despite its implementation.
  16. First we plugged rebar into active and then decided to drop its support.
  17. Later switched to Makefile-based build tool otp.mk.
  18. And of course it is slow, especially in cold recompilation.
  19. It was designed to be a stand-alone tool, so it has some
  20. glitches while using as embedded library.
  21. The idea to build rebar replacement was shared in space for a long time.
  22. The best minimal approach was picked up by \footahref{https://github.com/s1n4}{Sina Samavati},
  23. he implemented the first prototype called mad. Initially mad
  24. was able to compile DTL templated, YECC files, escript (like
  25. bundled in gproc), also it was support caching with side-effects.
  26. After a month I forked mad and took over the development under the same name.
  27. \vspace{1\baselineskip}
  28. \begin{lstlisting}[caption=Example of building N2O sample]
  29. Cold Hot
  30. rebar get-deps compile 53.156s 4.714s
  31. mad deps compile 54.097s 0.899s
  32. \end{lstlisting}
  33. \vspace{1\baselineskip}
  34. \vspace{1\baselineskip}
  35. \begin{lstlisting}[caption=Example of building Cowboy]
  36. Hot
  37. make (erlang.mk) 2.588s
  38. mad compile 2.521s
  39. \end{lstlisting}
  40. \vspace{1\baselineskip}
  41. \subsection{Introduction}
  42. We were trying to make something minimalistic that fits out Web Stack.
  43. Besides we wanted to use our knowledge of other build tools like lein, sbt etc.
  44. Also for sure we tried sinan, ebt, Makefile-based scripts.
  45. Synrc mad has a simple interface as follows:
  46. \vspace{1\baselineskip}
  47. \begin{lstlisting}
  48. BNF:
  49. invoke := mad params
  50. params := [] | run params
  51. run := command [ options ]
  52. command := app | lib | deps | compile | bundle
  53. run | stop | attach | repl
  54. \end{lstlisting}
  55. \vspace{1\baselineskip}
  56. It seems to us more natural, you can specify random
  57. commands set with different specifiers (options).
  58. \subsection{Single-File Bundling}
  59. The key feature of mad is ability to create single-file bundled web sites.
  60. Thus making Joe's dream to boot like node.js come true.
  61. This target escript is ready to run on Windows, Linux and Mac.
  62. To make this possible we implemented a zip filesytem inside escript.
  63. mad packages priv directories along with ebin and configs.
  64. You can redefine each file in zip fs inside target
  65. escript by creation the copy with same path locally near escript.
  66. After launch all files are copied to ETS.
  67. N2O also comes with custom cowboy static handler that is able to
  68. read static files from this cached ETS filesystem.
  69. Also bundle are compatible with active online realoading and recompilation.
  70. \subsection{Templates}
  71. mad also comes with N2O templates. So you can bootstrap a N2O-based site
  72. just having a single copy of mad.
  73. \vspace{1\baselineskip}
  74. \begin{lstlisting}
  75. # mad app sample
  76. # cd sample
  77. # mad deps compile plan bundle web_app
  78. \end{lstlisting}
  79. \vspace{1\baselineskip}
  80. After that you can just run ./web_app under Windows, Linux and
  81. Mac and open \footahref{http://localhost:8000}{http://localhost:8000}.
  82. \vspace{1\baselineskip}
  83. \begin{lstlisting}
  84. C:\> escript web_app
  85. Applications: [kernel,stdlib,crypto,cowlib,ranch,cowboy,compiler,syntax_tools,
  86. erlydtl,gproc,xmerl,n2o,n2o_sample,fs,active,mad,sh]
  87. Configuration: [{n2o,[{port,8000},{route,routes}]},
  88. {kvs,[{dba,store_mnesia},
  89. {schema,[kvs_user,kvs_acl,kvs_feed,kvs_subscription]}]}]
  90. Erlang/OTP 17 [erts-6.0] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
  91. Eshell V6.0 (abort with ^G)
  92. 1>
  93. \end{lstlisting}
  94. \vspace{1\baselineskip}
  95. \subsection{Deploy}
  96. mad is also supposed to be also a deploy tool with ability to
  97. deploy not only to our resources like Erlang on Xen, Voxoz (LXC/Xen) but
  98. also to Heroku and others.
  99. \subsection{OTP Compliant}
  100. mad supports rebar umbrella project structure.
  101. Specifically two kinds of directory layouts:
  102. \vspace{1\baselineskip}
  103. \begin{lstlisting}[caption=Solution]
  104. ├── apps
  105. ├── deps
  106. ├── rebar.config
  107. └── sys.config
  108. \end{lstlisting}
  109. \vspace{1\baselineskip}
  110. \vspace{1\baselineskip}
  111. \begin{lstlisting}[caption=OTP Application]
  112. ├── deps
  113. ├── ebin
  114. ├── include
  115. ├── priv
  116. ├── src
  117. └── rebar.config
  118. \end{lstlisting}
  119. \vspace{1\baselineskip}
  120. Also you can create OTP releases with reltool, rebar configure or relx.
  121. Internally mad uses reltool to order your dependencies and generation of reltool.config.
  122. \vspace{1\baselineskip}
  123. \begin{lstlisting}
  124. # mad plan
  125. Ordered: [kernel,stdlib,mnesia,kvs,crypto,cowlib,ranch,
  126. cowboy,compiler,syntax_tools,erlydtl,gproc,
  127. xmerl,n2o,n2o_sample,fs,active,mad,rest,sh]
  128. \end{lstlisting}
  129. \vspace{1\baselineskip}