lxcdev.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. **hint**
  14. If you have issues with the internet connectivity of your containers read
  15. section :ref:`internet connectivity docker`.
  16. .. contents::
  17. :depth: 2
  18. :local:
  19. :backlinks: entry
  20. Motivation
  21. ==========
  22. Most often in our development cycle, we edit the sources and run some test
  23. and/or builds by using ``make`` :ref:`[ref] <makefile>` before we commit. This
  24. cycle is simple and perfect but might fail in some aspects we should not
  25. overlook.
  26. **The environment in which we run all our development processes matters!**
  27. The :ref:`makefile` and the :ref:`make install` encapsulate a lot for us, but
  28. these tools do not have access to all prerequisites. For example, there may
  29. have dependencies on packages that are installed on developer's desktop, but
  30. usually are not preinstalled on a server or client system. Another example is;
  31. settings have been made to the software on developer's desktop that would never
  32. be set on a *production* system.
  33. **Linux Containers are isolate environments**, we use them to not mix up all
  34. the prerequisites from various projects on developer's desktop.
  35. The scripts from :ref:`searx_utils` can divide in those to install and maintain
  36. software
  37. - :ref:`searxng.sh`
  38. and the script
  39. - :ref:`lxc.sh`
  40. with we can scale our installation, maintenance or even development tasks over a
  41. stack of isolated containers / what we call the:
  42. - :ref:`searxng lxc suite`
  43. .. _lxcdev install searxng:
  44. Gentlemen, start your engines!
  45. ==============================
  46. .. _LXD: https://linuxcontainers.org/lxd/introduction/
  47. .. _archlinux: https://www.archlinux.org/
  48. Before you can start with containers, you need to install and initiate LXD_
  49. once:
  50. .. tabs::
  51. .. group-tab:: desktop (HOST)
  52. .. code:: bash
  53. $ snap install lxd
  54. $ lxd init --auto
  55. And you need to clone from origin or if you have your own fork, clone from your
  56. fork:
  57. .. tabs::
  58. .. group-tab:: desktop (HOST)
  59. .. code:: bash
  60. $ cd ~/Downloads
  61. $ git clone https://github.com/searxng/searxng.git searxng
  62. $ cd searxng
  63. .. sidebar:: The ``searxng-archlinux`` container
  64. is the base of all our exercises here.
  65. The :ref:`lxc-searxng.env` consists of several images, see ``export
  66. LXC_SUITE=(...`` near by :origin:`utils/lxc-searxng.env#L19`.
  67. For this blog post we exercise on a archlinux_ image. The container of this
  68. image is named ``searxng-archlinux``.
  69. Lets build the container, but be sure that this container does not already
  70. exists, so first lets remove possible old one:
  71. .. tabs::
  72. .. group-tab:: desktop (HOST)
  73. .. code:: bash
  74. $ sudo -H ./utils/lxc.sh remove searxng-archlinux
  75. $ sudo -H ./utils/lxc.sh build searxng-archlinux
  76. .. sidebar:: further read
  77. - :ref:`lxc.sh install suite`
  78. - :ref:`installation nginx`
  79. To install the complete :ref:`SearXNG suite <searxng lxc suite>` and the HTTP
  80. proxy :ref:`installation nginx` into the archlinux container run:
  81. .. tabs::
  82. .. group-tab:: desktop (HOST)
  83. .. code:: bash
  84. $ sudo -H ./utils/lxc.sh install suite searxng-archlinux
  85. $ sudo -H ./utils/lxc.sh cmd -- FORCE_TIMEOUT=0 ./utils/searxng.sh install nginx
  86. $ sudo ./utils/lxc.sh show suite | grep SEARXNG_URL
  87. ...
  88. [searxng-archlinux] SEARXNG_URL : http://n.n.n.140/searxng
  89. .. sidebar:: Fully functional SearXNG suite
  90. From here on you have a fully functional SearXNG suite (including a
  91. :ref:`redis db`).
  92. In such a SearXNG suite admins can maintain and access the debug log of the
  93. services quite easy.
  94. In the example above the SearXNG instance in the container is wrapped to
  95. ``http://n.n.n.140/searxng`` to the HOST system. Note, on your HOST system, the
  96. IP of your ``searxng-archlinux`` container is different to this example. To
  97. test the instance in the container from outside of the container, in your WEB
  98. browser on your desktop just open the URL reported in your installation
  99. .. _working in containers:
  100. In containers, work as usual
  101. ============================
  102. Usually you open a root-bash using ``sudo -H bash``. In case of LXC containers
  103. open the root-bash in the container is done by the ``./utils/lxc.sh cmd
  104. searxng-archlinux`` command:
  105. .. tabs::
  106. .. group-tab:: desktop (HOST)
  107. .. code:: bash
  108. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux bash
  109. INFO: [searxng-archlinux] bash
  110. [root@searxng-archlinux SearXNG]$
  111. The prompt ``[root@searxng-archlinux ...]`` signals, that you are the root user
  112. in the container (GUEST). To debug the running SearXNG instance use:
  113. .. tabs::
  114. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  115. .. code:: bash
  116. $ ./utils/searxng.sh instance inspect
  117. ...
  118. use [CTRL-C] to stop monitoring the log
  119. ...
  120. .. group-tab:: desktop (HOST)
  121. .. code:: bash
  122. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux ./utils/searxng.sh instance inspect
  123. ...
  124. use [CTRL-C] to stop monitoring the log
  125. ...
  126. Back in the browser on your desktop open the service http://n.n.n.140/searxng
  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. Another point we have to notice is that the service :ref:`SearXNG <searxng.sh>`
  131. runs under dedicated system user account with the same name (compare
  132. :ref:`create searxng user`). To get a login shell from these accounts, simply
  133. call:
  134. .. tabs::
  135. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  136. .. code:: bash
  137. $ ./utils/searxng.sh instance cmd bash -l
  138. (searx-pyenv) [searxng@searxng-archlinux ~]$ pwd
  139. /usr/local/searxng
  140. .. group-tab:: desktop (HOST)
  141. .. code:: bash
  142. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux ./utils/searxng.sh instance cmd bash -l
  143. INFO: [searxng-archlinux] ./utils/searxng.sh instance cmd bash -l
  144. (searx-pyenv) [searxng@searxng-archlinux ~]$ pwd
  145. /usr/local/searxng
  146. The prompt ``[searxng@searxng-archlinux]`` signals that you are logged in as system
  147. user ``searxng`` in the ``searxng-archlinux`` container and the python *virtualenv*
  148. ``(searxng-pyenv)`` environment is activated.
  149. Wrap production into developer suite
  150. ====================================
  151. In this section we will see how to change the *"Fully functional SearXNG suite"*
  152. from a LXC container (which is quite ready for production) into a developer
  153. suite. For this, we have to keep an eye on the :ref:`installation basic`:
  154. - SearXNG setup in: ``/etc/searxng/settings.yml``
  155. - SearXNG user's home: ``/usr/local/searxng``
  156. - virtualenv in: ``/usr/local/searxng/searxng-pyenv``
  157. - SearXNG software in: ``/usr/local/searxng/searxng-src``
  158. With the use of the :ref:`searxng.sh` the SearXNG service was installed as
  159. :ref:`uWSGI application <searxng uwsgi>`. To maintain this service, we can use
  160. ``systemctl`` (compare :ref:`uWSGI maintenance`).
  161. .. tabs::
  162. .. group-tab:: uwsgi@searxng
  163. .. code:: bash
  164. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux systemctl stop uwsgi@searxng
  165. With the command above, we stopped the SearXNG uWSGI-App in the archlinux
  166. container.
  167. The uWSGI-App for the archlinux distros is configured in
  168. :origin:`utils/templates/etc/uwsgi/apps-archlinux/searxng.ini`, from where at
  169. least you should attend the settings of ``uid``, ``chdir``, ``env`` and
  170. ``http``::
  171. env = SEARXNG_SETTINGS_PATH=/etc/searxng/settings.yml
  172. http = 127.0.0.1:8888
  173. chdir = /usr/local/searxng/searxng-src/searx
  174. virtualenv = /usr/local/searxng/searxng-pyenv
  175. pythonpath = /usr/local/searxng/searxng-src
  176. If you have read the :ref:`Good to know` you remember, that each container
  177. shares the root folder of the repository and the command ``utils/lxc.sh cmd``
  178. handles relative path names **transparent**.
  179. To wrap the SearXNG installation in the container into a developer one, we
  180. simple have to create a symlink to the **transparent** repository from the
  181. desktop. Now lets replace the repository at ``searxng-src`` in the container
  182. with the working tree from outside of the container:
  183. .. tabs::
  184. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  185. .. code:: bash
  186. $ mv /usr/local/searxng/searxng-src /usr/local/searxng/searxng-src.old
  187. $ ln -s /share/SearXNG/ /usr/local/searxng/searxng-src
  188. .. group-tab:: desktop (HOST)
  189. .. code:: bash
  190. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  191. mv /usr/local/searxng/searxng-src /usr/local/searxng/searxng-src.old
  192. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  193. ln -s /share/SearXNG/ /usr/local/searxng/searxng-src
  194. Now we can develop as usual in the working tree of our desktop system. Every
  195. time the software was changed, you have to restart the SearXNG service (in the
  196. container):
  197. .. tabs::
  198. .. group-tab:: uwsgi@searxng
  199. .. code:: bash
  200. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux systemctl restart uwsgi@searxng
  201. Remember: :ref:`working in containers` .. here are just some examples from my
  202. daily usage:
  203. To *inspect* the SearXNG instance (already described above):
  204. .. tabs::
  205. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  206. .. code:: bash
  207. $ ./utils/searx.sh inspect service
  208. .. group-tab:: desktop (HOST)
  209. .. code:: bash
  210. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux ./utils/searx.sh inspect service
  211. Run :ref:`makefile`, e.g. to test inside the container:
  212. .. tabs::
  213. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  214. .. code:: bash
  215. $ make test
  216. .. group-tab:: desktop (HOST)
  217. .. code:: bash
  218. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux make test
  219. To install all prerequisites needed for a :ref:`buildhosts`:
  220. .. tabs::
  221. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  222. .. code:: bash
  223. $ ./utils/searxng.sh install buildhost
  224. .. group-tab:: desktop (HOST)
  225. .. code:: bash
  226. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux ./utils/searxng.sh install buildhost
  227. To build the docs on a buildhost :ref:`buildhosts`:
  228. .. tabs::
  229. .. group-tab:: ``[root@searxng-archlinux SearXNG]`` (GUEST)
  230. .. code:: bash
  231. $ make docs.html
  232. .. group-tab:: desktop (HOST)
  233. .. code:: bash
  234. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux make docs.html
  235. .. _lxcdev summary:
  236. Summary
  237. =======
  238. We build up a fully functional SearXNG suite in a archlinux container:
  239. .. code:: bash
  240. $ sudo -H ./utils/lxc.sh build searxng-archlinux
  241. $ sudo -H ./utils/lxc.sh install suite searxng-archlinux
  242. ...
  243. Developer install? (wraps source from HOST into the running instance) [YES/no]
  244. To wrap the suite into a developer one answer ``YES`` (or press Enter).
  245. .. code:: text
  246. link SearXNG's sources to: /share/SearXNG
  247. =========================================
  248. mv -f "/usr/local/searxng/searxng-src" "/usr/local/searxng/searxng-src.backup"
  249. ln -s "/share/SearXNG" "/usr/local/searxng/searxng-src"
  250. ls -ld /usr/local/searxng/searxng-src
  251. |searxng| lrwxrwxrwx 1 searxng searxng ... /usr/local/searxng/searxng-src -> /share/SearXNG
  252. On code modification the instance has to be restarted (see :ref:`uWSGI
  253. maintenance`):
  254. .. code:: bash
  255. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux systemctl restart uwsgi@searxng
  256. To access HTTP from the desktop we installed nginx for the services inside the
  257. container:
  258. .. code:: bash
  259. $ sudo -H ./utils/lxc.sh cmd -- FORCE_TIMEOUT=0 ./utils/searxng.sh install nginx
  260. To get information about the SearxNG suite in the archlinux container we can
  261. use:
  262. .. code:: text
  263. $ sudo -H ./utils/lxc.sh show suite searxng-archlinux
  264. [searxng-archlinux] INFO: (eth0) docs-live: http:///n.n.n.140:8080/
  265. [searxng-archlinux] INFO: (eth0) IPv6: http://[fd42:555b:2af9:e121:216:3eff:fe5b:1744]
  266. [searxng-archlinux] uWSGI:
  267. [searxng-archlinux] SEARXNG_UWSGI_SOCKET : /usr/local/searxng/run/socket
  268. [searxng-archlinux] environment /usr/local/searxng/searxng-src/utils/brand.env:
  269. [searxng-archlinux] GIT_URL : https://github.com/searxng/searxng
  270. [searxng-archlinux] GIT_BRANCH : master
  271. [searxng-archlinux] SEARXNG_URL : http:///n.n.n.140/searxng
  272. [searxng-archlinux] SEARXNG_PORT : 8888
  273. [searxng-archlinux] SEARXNG_BIND_ADDRESS : 127.0.0.1