lxcdev-202006.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. .. _blog-lxcdev-202006:
  2. =======================================
  3. Developing in Linux containers [202006]
  4. =======================================
  5. .. _LXC: https://linuxcontainers.org/lxc/introduction/
  6. .. sidebar:: Audience
  7. This blog post is written for experienced admins and developers / readers
  8. should have a serious meaning about: *distributed*, *merge* and *linux
  9. container*.
  10. .. contents:: Contents
  11. :depth: 2
  12. :local:
  13. :backlinks: entry
  14. In PR :PR:`1803` we added a lot of scripts to Searx's boilerplate. In this blog
  15. post I will show you, how you can make use of them in *distributed and
  16. heterogeneous development cycles* (TL;DR; jump to the :ref:`blog-lxcdev-202006
  17. abstract`).
  18. Motivation
  19. ==========
  20. Normally in our development cycle, we edit the sources and run some test and/or
  21. builds by using ``make`` before we commit. This cycle is simple and perfect but
  22. might fail in some aspects we should not overlook.
  23. The environment in which we run all our development processes matters!
  24. The :ref:`makefile` and the :ref:`make install` encapsulate a lot for us, but they
  25. do not have access to all prerequisites. For example, there may have
  26. dependencies on packages that are installed on the developer's desktop, but
  27. usually are not preinstalled on a server or client system. Another examples
  28. are; settings have been made to the software on the developer's host that would
  29. never be set on a *production* system.
  30. *Linux Containers* (LXC_) are isolate environments and not to mix up on
  31. developer's all the prerequisites of all the projects he contribute to, is
  32. always a good choice.
  33. The scripts from PR :PR:`1803` can divide in those to install and maintain
  34. software:
  35. - :ref:`searx.sh`
  36. - :ref:`filtron.sh`
  37. - :ref:`morty.sh`
  38. and the script :ref:`lxc.sh`, with we can scale our installation, maintenance or
  39. even development tasks over a stack of containers, what we call: *Searx's lxc
  40. suite*.
  41. Gentlemen, start your engines!
  42. ==============================
  43. .. _LXD: https://linuxcontainers.org/lxd/introduction/
  44. .. _archlinux: https://www.archlinux.org/
  45. Before you can start with containers, you need to install and initiate LXD_
  46. once:
  47. .. tabs::
  48. .. group-tab:: desktop
  49. .. code:: sh
  50. $ snap install lxd
  51. $ lxd init --auto
  52. And you need to clone from origin or if you have your own fork, clone from your
  53. fork:
  54. .. tabs::
  55. .. group-tab:: desktop
  56. .. code:: sh
  57. $ cd ~/Downloads
  58. $ git clone https://github.com/searx/searx.git
  59. $ cd searx
  60. The :ref:`lxc-searx.env` consists of several images, see ``export
  61. LXC_SUITE=(...`` near by :origin:`utils/lxc-searx.env#L19`. For this blog post
  62. we exercise on a archlinux_ image. The container of this image is named
  63. ``searx-archlinux``. Lets build the container, but be sure that this container
  64. does not already exists, so first lets remove possible old one:
  65. .. tabs::
  66. .. group-tab:: desktop
  67. .. code:: sh
  68. $ sudo -H ./utils/lxc.sh remove searx-archlinux
  69. $ sudo -H ./utils/lxc.sh build searx-archlinux
  70. .. sidebar:: The ``searx-archlinux`` container
  71. is the base of all our exercises here.
  72. In this container we install all services :ref:`including searx, morty & filtron
  73. <lxc.sh install suite>` in once:
  74. .. tabs::
  75. .. group-tab:: desktop
  76. .. code:: sh
  77. $ sudo -H ./utils/lxc.sh install suite searx-archlinux
  78. To proxy HTTP from filtron and morty in the container to the outside of the
  79. container, install nginx into the container. Once for the bot blocker filtron:
  80. .. tabs::
  81. .. group-tab:: desktop
  82. .. code:: sh
  83. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  84. ./utils/filtron.sh nginx install
  85. ...
  86. INFO: got 429 from http://10.174.184.156/searx
  87. and once for the content sanitizer (content proxy morty):
  88. .. tabs::
  89. .. group-tab:: desktop
  90. .. code:: sh
  91. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  92. ./utils/morty.sh nginx install
  93. ...
  94. INFO: got 200 from http://10.174.184.156/morty/
  95. .. sidebar:: Fully functional searx suite
  96. From here on you have a fully functional searx suite running with bot blocker
  97. (filtron) and Web content sanitizer (content proxy morty) needed for a
  98. *privacy protecting* search engine.
  99. On your system, the IP of your ``searx-archlinux`` container differs from
  100. http://10.174.184.156/searx, just open the URL reported in your installation
  101. protocol in your WEB browser from the desktop to test the instance from outside
  102. of the container.
  103. In such a searx suite admins can maintain and access the debug log of the
  104. different services quite easy.
  105. .. _working in containers:
  106. In containers, work as usual
  107. ============================
  108. Usually you open a root-bash using ``sudo -H bash``. In case of LXC containers
  109. open the root-bash in the container using ``./utils/lxc.sh cmd
  110. searx-archlinux``:
  111. .. tabs::
  112. .. group-tab:: desktop
  113. .. code:: sh
  114. $ sudo -H ./utils/lxc.sh cmd searx-archlinux bash
  115. INFO: [searx-archlinux] bash
  116. [root@searx-archlinux searx]# pwd
  117. /share/searx
  118. The prompt ``[root@searx-archlinux ...]`` signals, that you are the root user in
  119. the searx-container. To debug the running searx instance use:
  120. .. tabs::
  121. .. group-tab:: root@searx-archlinux
  122. .. code:: sh
  123. $ ./utils/searx.sh inspect service
  124. ...
  125. use [CTRL-C] to stop monitoring the log
  126. ...
  127. Back in the browser on your desktop open the service http://10.174.184.156/searx
  128. and run your application tests while the debug log is shown in the terminal from
  129. above. You can stop monitoring using ``CTRL-C``, this also disables the *"debug
  130. option"* in searx's settings file and restarts the searx uwsgi application. To
  131. debug services from filtron and morty analogous use:
  132. .. tabs::
  133. .. group-tab:: root@searx-archlinux
  134. .. code:: sh
  135. $ ./utils/filtron.sh inspect service
  136. $ ./utils/morty.sh inspect service
  137. Another point we have to notice is that each service (:ref:`searx <searx.sh>`,
  138. :ref:`filtron <filtron.sh>` and :ref:`morty <morty.sh>`) runs under dedicated
  139. system user account with the same name (compare :ref:`create searx user`). To
  140. get a shell from these accounts, simply call one of the scripts:
  141. .. tabs::
  142. .. group-tab:: root@searx-archlinux
  143. .. code:: sh
  144. $ ./utils/searx.sh shell
  145. $ ./utils/filtron.sh shell
  146. $ ./utils/morty.sh shell
  147. To get in touch, open a shell from the service user (searx@searx-archlinux):
  148. .. tabs::
  149. .. group-tab:: desktop
  150. .. code:: sh
  151. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  152. ./utils/searx.sh shell
  153. // exit with [CTRL-D]
  154. (searx-pyenv) [searx@searx-archlinux ~]$ ...
  155. The prompt ``[searx@searx-archlinux]`` signals that you are logged in as system
  156. user ``searx`` in the ``searx-archlinux`` container and the python *virtualenv*
  157. ``(searx-pyenv)`` environment is activated.
  158. .. tabs::
  159. .. group-tab:: searx@searx-archlinux
  160. .. code:: sh
  161. (searx-pyenv) [searx@searx-archlinux ~]$ pwd
  162. /usr/local/searx
  163. Wrap production into developer suite
  164. ====================================
  165. In this section we will see how to change the *"Fully functional searx suite"*
  166. from a LXC container (which is quite ready for production) into a developer
  167. suite. For this, we have to keep an eye on the :ref:`installation basic`:
  168. - searx setup in: ``/etc/searx/settings.yml``
  169. - searx user's home: ``/usr/local/searx``
  170. - virtualenv in: ``/usr/local/searx/searx-pyenv``
  171. - searx software in: ``/usr/local/searx/searx-src``
  172. The searx software is a clone of the ``git_url`` (see :ref:`settings global`) and
  173. the working tree is checked out from the ``git_branch``. With the use of the
  174. :ref:`searx.sh` the searx service was installed as :ref:`uWSGI application
  175. <searx uwsgi>`. To maintain this service, we can use ``systemctl`` (compare
  176. :ref:`service architectures on distributions <uwsgi configuration>`).
  177. .. tabs::
  178. .. group-tab:: desktop
  179. .. code:: sh
  180. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  181. systemctl stop uwsgi@searx
  182. With the command above, we stopped the searx uWSGI-App in the archlinux
  183. container.
  184. The uWSGI-App for the archlinux dsitros is configured in
  185. :origin:`utils/templates/etc/uwsgi/apps-archlinux/searx.ini`, from where at
  186. least you should attend the settings of ``uid``, ``chdir``, ``env`` and
  187. ``http``::
  188. env = SEARX_SETTINGS_PATH=/etc/searx/settings.yml
  189. http = 127.0.0.1:8888
  190. chdir = /usr/local/searx/searx-src/searx
  191. virtualenv = /usr/local/searx/searx-pyenv
  192. pythonpath = /usr/local/searx/searx-src
  193. If you have read the :ref:`"Good to know section" <lxc.sh>` you remember, that
  194. each container shares the root folder of the repository and the command
  195. ``utils/lxc.sh cmd`` handles relative path names **transparent**. To wrap the
  196. searx installation into a developer one, we simple have to create a smylink to
  197. the **transparent** reposetory from the desktop. Now lets replace the
  198. repository at ``searx-src`` in the container with the working tree from outside
  199. of the container:
  200. .. tabs::
  201. .. group-tab:: container becomes a developer suite
  202. .. code:: sh
  203. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  204. mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old
  205. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  206. ln -s /share/searx/ /usr/local/searx/searx-src
  207. Now we can develop as usual in the working tree of our desktop system. Every
  208. time the software was changed, you have to restart the searx service (in the
  209. container):
  210. .. tabs::
  211. .. group-tab:: desktop
  212. .. code:: sh
  213. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  214. systemctl restart uwsgi@searx
  215. Remember: :ref:`working in containers` .. here are just some examples from my
  216. daily usage:
  217. .. tabs::
  218. .. group-tab:: desktop
  219. To *inspect* the searx instance (already described above):
  220. .. code:: sh
  221. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  222. ./utils/searx.sh inspect service
  223. Run :ref:`makefile`, e.g. to test inside the container:
  224. .. code:: sh
  225. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  226. make test
  227. To install all prerequisites needed for a :ref:`buildhosts`:
  228. .. code:: sh
  229. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  230. ./utils/searx.sh install buildhost
  231. To build the docs on a buildhost :ref:`buildhosts`:
  232. .. code:: sh
  233. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  234. make docs.html
  235. .. _blog-lxcdev-202006 abstract:
  236. Abstract
  237. ========
  238. We build up a fully functional searx suite in a archlinux container:
  239. .. code:: sh
  240. $ sudo -H ./utils/lxc.sh install suite searx-archlinux
  241. To access HTTP from the desktop we installed nginx for the services inside the
  242. container:
  243. .. tabs::
  244. .. group-tab:: [root@searx-archlinux]
  245. .. code:: sh
  246. $ ./utils/filtron.sh nginx install
  247. $ ./utils/morty.sh nginx install
  248. To wrap the suite into a developer one, we created a symbolic link to the
  249. repository which is shared **transparent** from the desktop's file system into
  250. the container :
  251. .. tabs::
  252. .. group-tab:: [root@searx-archlinux]
  253. .. code:: sh
  254. $ mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old
  255. $ ln -s /share/searx/ /usr/local/searx/searx-src
  256. $ systemctl restart uwsgi@searx
  257. To get remarks from the suite of the archlinux container we can use:
  258. .. tabs::
  259. .. group-tab:: desktop
  260. .. code:: sh
  261. $ sudo -H ./utils/lxc.sh show suite searx-archlinux
  262. ...
  263. [searx-archlinux] INFO: (eth0) filtron: http://10.174.184.156:4004/ http://10.174.184.156/searx
  264. [searx-archlinux] INFO: (eth0) morty: http://10.174.184.156:3000/
  265. [searx-archlinux] INFO: (eth0) docs.live: http://10.174.184.156:8080/
  266. [searx-archlinux] INFO: (eth0) IPv6: http://[fd42:573b:e0b3:e97e:216:3eff:fea5:9b65]
  267. ...