index.tex 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. \section{MAD: Erlang Build and Deploy Tool}
  2. \subsection{History}
  3. We came to conclusion that no matter how perfect your libraries are,
  4. the comfort and ease 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 \footahref{https://github.com/synrc/active}{active} emerged.
  12. Under the hood active is a client subscriber
  13. of \footahref{https://github.com/synrc/fs}{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. it was slow, especially in cold recompilation.
  18. It was designed to be a stand-alone tool, so it has some
  19. glitches while using as embedded library.
  20. Later we switched to Makefile-based build tool \footahref{https://github.com/synrc/otp.mk}{otp.mk}.
  21. The idea to build rebar replacement was up in the air for a long time.
  22. The best minimal approach was picked up by \footahref{https://github.com/s1n4}{Sina~Samavati},
  23. who 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 had support for caching with side-effects.
  26. In 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 \footahref{https://github.com/synrc}{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. start | stop | 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 dream to boot simpler than 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 binary.
  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 escript 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 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. \subsection{Apps Ordering}
  121. As you may know you can create OTP releases with
  122. reltool (rebar generate) or systools (relx). mad currently
  123. creates releases with relx but is going to do it independently soon.
  124. Now it can only order applications.
  125. \vspace{1\baselineskip}
  126. \begin{lstlisting}
  127. # mad plan
  128. Ordered: [kernel,stdlib,mnesia,kvs,crypto,cowlib,ranch,
  129. cowboy,compiler,syntax_tools,erlydtl,gproc,
  130. xmerl,n2o,n2o_sample,fs,active,mad,rest,sh]
  131. \end{lstlisting}
  132. \vspace{1\baselineskip}
  133. And the good part:
  134. \vspace{1\baselineskip}
  135. \begin{lstlisting}
  136. Sources Binary
  137. mad 567 LOC 39 KB
  138. rebar 7717 LOC 181 KB
  139. \end{lstlisting}
  140. \vspace{1\baselineskip}