| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 | .. _lxcdev:==============================Developing in Linux Containers==============================.. _LXC: https://linuxcontainers.org/lxc/introduction/In this article we will show, how you can make use of Linux Containers (LXC_) in*distributed and heterogeneous development cycles* (TL;DR; jump to the:ref:`lxcdev summary`)... sidebar:: Audience   This blog post is written for experienced admins and developers.  Readers   should have a serious meaning about the terms: *distributed*, *merge* and   *linux container*... contents:: Contents   :depth: 2   :local:   :backlinks: entryMotivation==========Usually in our development cycle, we edit the sources and run some test and/orbuilds by using ``make`` :ref:`[ref] <makefile>` before we commit.  This cycleis simple and perfect but might fail in some aspects we should not overlook.  **The environment in which we run all our development processes matters!**The :ref:`makefile` and the :ref:`make install` encapsulate a lot for us, butthey do not have access to all prerequisites.  For example, there may havedependencies on packages that are installed on the developer's desktop, butusually are not preinstalled on a server or client system.  Another example is;settings have been made to the software on developer's desktop that would neverbe set on a *production* system.  **Linux Containers are isolate environments and not to mix up all the  prerequisites from various projects on developer's desktop is always a good  choice.**The scripts from :ref:`searx_utils` can divide in those to install and maintainsoftware:- :ref:`searx.sh`- :ref:`filtron.sh`- :ref:`morty.sh`and the script :ref:`lxc.sh`, with we can scale our installation, maintenance oreven development tasks over a stack of isolated containers / what we call the:  **SearXNG LXC suite**.. hint::   If you see any problems with the internet connectivity of your   containers read section :ref:`internet connectivity docker`.Gentlemen, start your engines!==============================.. _LXD: https://linuxcontainers.org/lxd/introduction/.. _archlinux: https://www.archlinux.org/Before you can start with containers, you need to install and initiate LXD_once:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ snap install lxd        $ lxd init --autoAnd you need to clone from origin or if you have your own fork, clone from yourfork:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ cd ~/Downloads        $ git clone https://github.com/searxng/searxng.git searxng        $ cd searxngThe :ref:`lxc-searx.env` consists of several images, see ``exportLXC_SUITE=(...`` near by :origin:`utils/lxc-searx.env#L19`.  For this blog postwe exercise on a archlinux_ image.  The container of this image is named``searx-archlinux``.  Lets build the container, but be sure that this containerdoes not already exists, so first lets remove possible old one:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh remove searx-archlinux        $ sudo -H ./utils/lxc.sh build searx-archlinux.. sidebar:: The ``searx-archlinux`` container   is the base of all our exercises here.In this container we install all services :ref:`including searx, morty & filtron<lxc.sh install suite>` in once:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh install suite searx-archlinuxTo proxy HTTP from filtron and morty in the container to the outside of thecontainer, install nginx into the container.  Once for the bot blocker filtron:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          ./utils/filtron.sh nginx install        ...        INFO:  got 429 from http://10.174.184.156/searxand once for the content sanitizer (content proxy morty):.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          ./utils/morty.sh nginx install        ...        INFO:  got 200 from http://10.174.184.156/morty/.. sidebar:: Fully functional SearXNG suite   From here on you have a fully functional SearXNG suite running with bot   blocker (filtron) and WEB content sanitizer (content proxy morty), both are   needed for a *privacy protecting* search engine.On your system, the IP of your ``searx-archlinux`` container differs fromhttp://10.174.184.156/searx, just open the URL reported in your installationprotocol in your WEB browser from the desktop to test the instance from outsideof the container.In such a earXNG suite admins can maintain and access the debug log of thedifferent services quite easy... _working in containers:In containers, work as usual============================Usually you open a root-bash using ``sudo -H bash``.  In case of LXC containersopen the root-bash in the container using ``./utils/lxc.sh cmdsearx-archlinux``:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux bash        INFO:  [searx-archlinux] bash        [root@searx-archlinux searx]# pwd        /share/searxngThe prompt ``[root@searx-archlinux ...]`` signals, that you are the root user inthe searx-container.  To debug the running SearXNG instance use:.. tabs::  .. group-tab:: root@searx-archlinux     .. code:: sh        $ ./utils/searx.sh inspect service        ...        use [CTRL-C] to stop monitoring the log        ...Back in the browser on your desktop open the service http://10.174.184.156/searxand run your application tests while the debug log is shown in the terminal fromabove.  You can stop monitoring using ``CTRL-C``, this also disables the *"debugoption"* in SearXNG's settings file and restarts the SearXNG uwsgi application.To debug services from filtron and morty analogous use:.. tabs::  .. group-tab:: root@searx-archlinux     .. code:: sh        $ ./utils/filtron.sh inspect service        $ ./utils/morty.sh inspect serviceAnother point we have to notice is that each service (:ref:`SearXNG <searx.sh>`,:ref:`filtron <filtron.sh>` and :ref:`morty <morty.sh>`) runs under dedicatedsystem user account with the same name (compare :ref:`create searxng user`).  Toget a shell from theses accounts, simply call one of the scripts:.. tabs::  .. group-tab:: root@searx-archlinux     .. code:: sh        $ ./utils/searx.sh shell        $ ./utils/filtron.sh shell        $ ./utils/morty.sh shellTo get in touch, open a shell from the service user (searx@searx-archlinux):.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \        ./utils/searx.sh shell        // exit with [CTRL-D]        (searx-pyenv) [searx@searx-archlinux ~]$ ...The prompt ``[searx@searx-archlinux]`` signals that you are logged in as systemuser ``searx`` in the ``searx-archlinux`` container and the python *virtualenv*``(searx-pyenv)`` environment is activated... tabs::  .. group-tab:: searx@searx-archlinux     .. code:: sh        (searx-pyenv) [searx@searx-archlinux ~]$ pwd        /usr/local/searxWrap production into developer suite====================================In this section we will see how to change the *"Fully functional SearXNG suite"*from a LXC container (which is quite ready for production) into a developersuite.  For this, we have to keep an eye on the :ref:`installation basic`:- SearXNG setup in: ``/etc/searxng/settings.yml``- SearXNG user's home: ``/usr/local/searx``- virtualenv in: ``/usr/local/searx/searx-pyenv``- SearXNG software in: ``/usr/local/searx/searx-src``With the use of the :ref:`searx.sh` the SearXNG service was installed as:ref:`uWSGI application <searxng uwsgi>`.  To maintain this service, we can use``systemctl`` (compare :ref:`service architectures on distributions <uwsgiconfiguration>`)... tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          systemctl stop uwsgi@searxWith the command above, we stopped the SearXNG uWSGI-App in the archlinuxcontainer.The uWSGI-App for the archlinux dsitros is configured in:origin:`utils/templates/etc/uwsgi/apps-archlinux/searxng.ini`, from where atleast you should attend the settings of ``uid``, ``chdir``, ``env`` and``http``::  env = SEARXNG_SETTINGS_PATH=/etc/searxng/settings.yml  http = 127.0.0.1:8888  chdir = /usr/local/searx/searx-src/searx  virtualenv = /usr/local/searx/searx-pyenv  pythonpath = /usr/local/searx/searx-srcIf you have read the :ref:`"Good to know section" <lxc.sh>` you remember, thateach container shares the root folder of the repository and the command``utils/lxc.sh cmd`` handles relative path names **transparent**.  To wrap theSearXNG installation into a developer one, we simple have to create a smylink tothe **transparent** reposetory from the desktop.  Now lets replace therepository at ``searx-src`` in the container with the working tree from outsideof the container:.. tabs::  .. group-tab:: container becomes a developer suite     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          ln -s /share/searx/ /usr/local/searx/searx-srcNow we can develop as usual in the working tree of our desktop system.  Everytime the software was changed, you have to restart the SearXNG service (in theconatiner):.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          systemctl restart uwsgi@searxRemember: :ref:`working in containers` .. here are just some examples from mydaily usage:.. tabs::  .. group-tab:: desktop     To *inspect* the SearXNG instance (already described above):     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          ./utils/searx.sh inspect service     Run :ref:`makefile`, e.g. to test inside the container:     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          make test     To install all prerequisites needed for a :ref:`buildhosts`:     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          ./utils/searx.sh install buildhost     To build the docs on a buildhost :ref:`buildhosts`:     .. code:: sh        $ sudo -H ./utils/lxc.sh cmd searx-archlinux \          make docs.html.. _lxcdev summary:Summary=======We build up a fully functional SearXNG suite in a archlinux container:.. code:: sh   $ sudo -H ./utils/lxc.sh install suite searx-archlinuxTo access HTTP from the desktop we installed nginx for the services inside theconatiner:.. tabs::  .. group-tab:: [root@searx-archlinux]     .. code:: sh        $ ./utils/filtron.sh nginx install        $ ./utils/morty.sh nginx installTo wrap the suite into a developer one, we created a symbolic link to therepository which is shared **transparent** from the desktop's file system intothe container :.. tabs::  .. group-tab:: [root@searx-archlinux]     .. code:: sh	$ mv /usr/local/searx/searx-src /usr/local/searx/searx-src.old	$ ln -s /share/searx/ /usr/local/searx/searx-src	$ systemctl restart uwsgi@searxTo get information about the searxNG suite in the archlinux container we canuse:.. tabs::  .. group-tab:: desktop     .. code:: sh        $ sudo -H ./utils/lxc.sh show suite searx-archlinux        ...        [searx-archlinux]  INFO:  (eth0) filtron:    http://10.174.184.156:4004/ http://10.174.184.156/searx        [searx-archlinux]  INFO:  (eth0) morty:      http://10.174.184.156:3000/        [searx-archlinux]  INFO:  (eth0) docs.live:  http://10.174.184.156:8080/        [searx-archlinux]  INFO:  (eth0) IPv6:       http://[fd42:573b:e0b3:e97e:216:3eff:fea5:9b65]        ...
 |