lxcdev.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 searXNG 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:`searx <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. The searXNG software is a clone of the ``git_url`` (see :ref:`settings global`)
  175. and the working tree is checked out from the ``git_branch``. With the use of
  176. the :ref:`searx.sh` the searx service was installed as :ref:`uWSGI application
  177. <searx uwsgi>`. To maintain this service, we can use ``systemctl`` (compare
  178. :ref:`service architectures on distributions <uwsgi configuration>`).
  179. .. tabs::
  180. .. group-tab:: desktop
  181. .. code:: sh
  182. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  183. systemctl stop uwsgi@searx
  184. With the command above, we stopped the searx uWSGI-App in the archlinux
  185. container.
  186. The uWSGI-App for the archlinux dsitros is configured in
  187. :origin:`utils/templates/etc/uwsgi/apps-archlinux/searx.ini`, from where at
  188. least you should attend the settings of ``uid``, ``chdir``, ``env`` and
  189. ``http``::
  190. env = SEARX_SETTINGS_PATH=/etc/searx/settings.yml
  191. http = 127.0.0.1:8888
  192. chdir = /usr/local/searx/searx-src/searx
  193. virtualenv = /usr/local/searx/searx-pyenv
  194. pythonpath = /usr/local/searx/searx-src
  195. If you have read the :ref:`"Good to know section" <lxc.sh>` you remember, that
  196. each container shares the root folder of the repository and the command
  197. ``utils/lxc.sh cmd`` handles relative path names **transparent**. To wrap the
  198. searXNG installation into a developer one, we simple have to create a smylink to
  199. the **transparent** reposetory from the desktop. Now lets replace the
  200. repository at ``searx-src`` in the container with the working tree from outside
  201. of the container:
  202. .. tabs::
  203. .. group-tab:: container becomes a developer suite
  204. .. code:: sh
  205. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  206. mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old
  207. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  208. ln -s /share/searx/ /usr/local/searx/searx-src
  209. Now we can develop as usual in the working tree of our desktop system. Every
  210. time the software was changed, you have to restart the searx service (in the
  211. conatiner):
  212. .. tabs::
  213. .. group-tab:: desktop
  214. .. code:: sh
  215. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  216. systemctl restart uwsgi@searx
  217. Remember: :ref:`working in containers` .. here are just some examples from my
  218. daily usage:
  219. .. tabs::
  220. .. group-tab:: desktop
  221. To *inspect* the searXNG instance (already described above):
  222. .. code:: sh
  223. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  224. ./utils/searx.sh inspect service
  225. Run :ref:`makefile`, e.g. to test inside the container:
  226. .. code:: sh
  227. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  228. make test
  229. To install all prerequisites needed for a :ref:`buildhosts`:
  230. .. code:: sh
  231. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  232. ./utils/searx.sh install buildhost
  233. To build the docs on a buildhost :ref:`buildhosts`:
  234. .. code:: sh
  235. $ sudo -H ./utils/lxc.sh cmd searx-archlinux \
  236. make docs.html
  237. .. _lxcdev summary:
  238. Summary
  239. =======
  240. We build up a fully functional searXNG suite in a archlinux container:
  241. .. code:: sh
  242. $ sudo -H ./utils/lxc.sh install suite searx-archlinux
  243. To access HTTP from the desktop we installed nginx for the services inside the
  244. conatiner:
  245. .. tabs::
  246. .. group-tab:: [root@searx-archlinux]
  247. .. code:: sh
  248. $ ./utils/filtron.sh nginx install
  249. $ ./utils/morty.sh nginx install
  250. To wrap the suite into a developer one, we created a symbolic link to the
  251. repository which is shared **transparent** from the desktop's file system into
  252. the container :
  253. .. tabs::
  254. .. group-tab:: [root@searx-archlinux]
  255. .. code:: sh
  256. $ mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old
  257. $ ln -s /share/searx/ /usr/local/searx/searx-src
  258. $ systemctl restart uwsgi@searx
  259. To get information about the searxNG suite in the archlinux container we can
  260. use:
  261. .. tabs::
  262. .. group-tab:: desktop
  263. .. code:: sh
  264. $ sudo -H ./utils/lxc.sh show suite searx-archlinux
  265. ...
  266. [searx-archlinux] INFO: (eth0) filtron: http://10.174.184.156:4004/ http://10.174.184.156/searx
  267. [searx-archlinux] INFO: (eth0) morty: http://10.174.184.156:3000/
  268. [searx-archlinux] INFO: (eth0) docs.live: http://10.174.184.156:8080/
  269. [searx-archlinux] INFO: (eth0) IPv6: http://[fd42:573b:e0b3:e97e:216:3eff:fea5:9b65]
  270. ...