lxcdev.rst 12 KB

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