lxcdev.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. Gentlemen, start your engines!
  41. ==============================
  42. .. _LXD: https://linuxcontainers.org/lxd/introduction/
  43. .. _archlinux: https://www.archlinux.org/
  44. Before you can start with containers, you need to install and initiate LXD_
  45. once:
  46. .. tabs::
  47. .. group-tab:: desktop
  48. .. code:: sh
  49. $ snap install lxd
  50. $ lxd init --auto
  51. And you need to clone from origin or if you have your own fork, clone from your
  52. fork:
  53. .. tabs::
  54. .. group-tab:: desktop
  55. .. code:: sh
  56. $ cd ~/Downloads
  57. $ git clone https://github.com/searxng/searxng.git searx
  58. $ cd searx
  59. The :ref:`lxc-searx.env` consists of several images, see ``export
  60. LXC_SUITE=(...`` near by :origin:`utils/lxc-searx.env#L19`. For this blog post
  61. we exercise on a archlinux_ image. The container of this image is named
  62. ``searx-archlinux``. Lets build the container, but be sure that this container
  63. does not already exists, so first lets remove possible old one:
  64. .. tabs::
  65. .. group-tab:: desktop
  66. .. code:: sh
  67. $ sudo -H ./utils/lxc.sh remove searx-archlinux
  68. $ sudo -H ./utils/lxc.sh build searx-archlinux
  69. .. sidebar:: The ``searx-archlinux`` container
  70. is the base of all our exercises here.
  71. In this container we install all services :ref:`including searx, morty & filtron
  72. <lxc.sh install suite>` in once:
  73. .. tabs::
  74. .. group-tab:: desktop
  75. .. code:: sh
  76. $ sudo -H ./utils/lxc.sh install suite searx-archlinux
  77. To proxy HTTP from filtron and morty in the container to the outside of the
  78. container, install nginx into the container. Once for the bot blocker filtron:
  79. .. tabs::
  80. .. group-tab:: desktop
  81. .. code:: sh
  82. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  83. ./utils/filtron.sh nginx install
  84. ...
  85. INFO: got 429 from http://10.174.184.156/searx
  86. and once for the content sanitizer (content proxy morty):
  87. .. tabs::
  88. .. group-tab:: desktop
  89. .. code:: sh
  90. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  91. ./utils/morty.sh nginx install
  92. ...
  93. INFO: got 200 from http://10.174.184.156/morty/
  94. .. sidebar:: Fully functional searXNG suite
  95. From here on you have a fully functional searXNG suite running with bot
  96. blocker (filtron) and WEB content sanitizer (content proxy morty), both are
  97. needed for a *privacy protecting* search engine.
  98. On your system, the IP of your ``searx-archlinux`` container differs from
  99. http://10.174.184.156/searx, just open the URL reported in your installation
  100. protocol in your WEB browser from the desktop to test the instance from outside
  101. of the container.
  102. In such a searXNG suite admins can maintain and access the debug log of the
  103. different services quite easy.
  104. .. _working in containers:
  105. In containers, work as usual
  106. ============================
  107. Usually you open a root-bash using ``sudo -H bash``. In case of LXC containers
  108. open the root-bash in the container using ``./utils/lxc.sh cmd
  109. searx-archlinux``:
  110. .. tabs::
  111. .. group-tab:: desktop
  112. .. code:: sh
  113. $ sudo -H ./utils/lxc.sh cmd searx-archlinux bash
  114. INFO: [searx-archlinux] bash
  115. [root@searx-archlinux searx]# pwd
  116. /share/searx
  117. The prompt ``[root@searx-archlinux ...]`` signals, that you are the root user in
  118. the searx-container. To debug the running searXNG instance use:
  119. .. tabs::
  120. .. group-tab:: root@searx-archlinux
  121. .. code:: sh
  122. $ ./utils/searx.sh inspect service
  123. ...
  124. use [CTRL-C] to stop monitoring the log
  125. ...
  126. Back in the browser on your desktop open the service http://10.174.184.156/searx
  127. and run your application tests while the debug log is shown in the terminal from
  128. above. You can stop monitoring using ``CTRL-C``, this also disables the *"debug
  129. option"* in searXNG's settings file and restarts the searXNG uwsgi application.
  130. To debug services from filtron and morty analogous use:
  131. .. tabs::
  132. .. group-tab:: root@searx-archlinux
  133. .. code:: sh
  134. $ ./utils/filtron.sh inspect service
  135. $ ./utils/morty.sh inspect service
  136. Another point we have to notice is that each service (:ref:`searx <searx.sh>`,
  137. :ref:`filtron <filtron.sh>` and :ref:`morty <morty.sh>`) runs under dedicated
  138. system user account with the same name (compare :ref:`create searx user`). To
  139. get a shell from theses accounts, simply call one of the scripts:
  140. .. tabs::
  141. .. group-tab:: root@searx-archlinux
  142. .. code:: sh
  143. $ ./utils/searx.sh shell
  144. $ ./utils/filtron.sh shell
  145. $ ./utils/morty.sh shell
  146. To get in touch, open a shell from the service user (searx@searx-archlinux):
  147. .. tabs::
  148. .. group-tab:: desktop
  149. .. code:: sh
  150. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  151. ./utils/searx.sh shell
  152. // exit with [CTRL-D]
  153. (searx-pyenv) [searx@searx-archlinux ~]$ ...
  154. The prompt ``[searx@searx-archlinux]`` signals that you are logged in as system
  155. user ``searx`` in the ``searx-archlinux`` container and the python *virtualenv*
  156. ``(searx-pyenv)`` environment is activated.
  157. .. tabs::
  158. .. group-tab:: searx@searx-archlinux
  159. .. code:: sh
  160. (searx-pyenv) [searx@searx-archlinux ~]$ pwd
  161. /usr/local/searx
  162. Wrap production into developer suite
  163. ====================================
  164. In this section we will see how to change the *"Fully functional searXNG suite"*
  165. from a LXC container (which is quite ready for production) into a developer
  166. suite. For this, we have to keep an eye on the :ref:`installation basic`:
  167. - searXNG setup in: ``/etc/searx/settings.yml``
  168. - searXNG user's home: ``/usr/local/searx``
  169. - virtualenv in: ``/usr/local/searx/searx-pyenv``
  170. - searXNG software in: ``/usr/local/searx/searx-src``
  171. The searXNG software is a clone of the ``git_url`` (see :ref:`settings global`)
  172. and the working tree is checked out from the ``git_branch``. With the use of
  173. the :ref:`searx.sh` the searx service was installed as :ref:`uWSGI application
  174. <searx uwsgi>`. To maintain this service, we can use ``systemctl`` (compare
  175. :ref:`service architectures on distributions <uwsgi configuration>`).
  176. .. tabs::
  177. .. group-tab:: desktop
  178. .. code:: sh
  179. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  180. systemctl stop uwsgi@searx
  181. With the command above, we stopped the searx uWSGI-App in the archlinux
  182. container.
  183. The uWSGI-App for the archlinux dsitros is configured in
  184. :origin:`utils/templates/etc/uwsgi/apps-archlinux/searx.ini`, from where at
  185. least you should attend the settings of ``uid``, ``chdir``, ``env`` and
  186. ``http``::
  187. env = SEARX_SETTINGS_PATH=/etc/searx/settings.yml
  188. http = 127.0.0.1:8888
  189. chdir = /usr/local/searx/searx-src/searx
  190. virtualenv = /usr/local/searx/searx-pyenv
  191. pythonpath = /usr/local/searx/searx-src
  192. If you have read the :ref:`"Good to know section" <lxc.sh>` you remember, that
  193. each container shares the root folder of the repository and the command
  194. ``utils/lxc.sh cmd`` handles relative path names **transparent**. To wrap the
  195. searXNG installation into a developer one, we simple have to create a smylink to
  196. the **transparent** reposetory from the desktop. Now lets replace the
  197. repository at ``searx-src`` in the container with the working tree from outside
  198. of the container:
  199. .. tabs::
  200. .. group-tab:: container becomes a developer suite
  201. .. code:: sh
  202. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  203. mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old
  204. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  205. ln -s /share/searx/ /usr/local/searx/searx-src
  206. Now we can develop as usual in the working tree of our desktop system. Every
  207. time the software was changed, you have to restart the searx service (in the
  208. conatiner):
  209. .. tabs::
  210. .. group-tab:: desktop
  211. .. code:: sh
  212. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  213. systemctl restart uwsgi@searx
  214. Remember: :ref:`working in containers` .. here are just some examples from my
  215. daily usage:
  216. .. tabs::
  217. .. group-tab:: desktop
  218. To *inspect* the searXNG instance (already described above):
  219. .. code:: sh
  220. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  221. ./utils/searx.sh inspect service
  222. Run :ref:`makefile`, e.g. to test inside the container:
  223. .. code:: sh
  224. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  225. make test
  226. To install all prerequisites needed for a :ref:`buildhosts`:
  227. .. code:: sh
  228. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  229. ./utils/searx.sh install buildhost
  230. To build the docs on a buildhost :ref:`buildhosts`:
  231. .. code:: sh
  232. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  233. make docs.html
  234. .. _lxcdev summary:
  235. Summary
  236. =======
  237. We build up a fully functional searXNG suite in a archlinux container:
  238. .. code:: sh
  239. $ sudo -H ./utils/lxc.sh install suite searx-archlinux
  240. To access HTTP from the desktop we installed nginx for the services inside the
  241. conatiner:
  242. .. tabs::
  243. .. group-tab:: [root@searx-archlinux]
  244. .. code:: sh
  245. $ ./utils/filtron.sh nginx install
  246. $ ./utils/morty.sh nginx install
  247. To wrap the suite into a developer one, we created a symbolic link to the
  248. repository which is shared **transparent** from the desktop's file system into
  249. the container :
  250. .. tabs::
  251. .. group-tab:: [root@searx-archlinux]
  252. .. code:: sh
  253. $ mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old
  254. $ ln -s /share/searx/ /usr/local/searx/searx-src
  255. $ systemctl restart uwsgi@searx
  256. To get information about the searxNG suite in the archlinux container we can
  257. 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. ...