.. _couchdb-head:

CouchDB for BOSS
################

`CouchDB <https://couchdb.apache.org/>`_ is a document based database, which can provide JSON strings for BOSS devices.

To get started, install an instance of CouchDB, for example in a `docker container <https://docs.couchdb.org/en/stable/install/docker.html>`_.

After creating a database (in this example, we created a database `my-boss`) as described in the official CouchDB documentation,
we can now create a CouchDB document with the following content:

.. code-block:: json

  {
    "_id": "http-test",
    "classname": "http.client.HTTPConnection",
    "arguments": {
      "host": "gitlab.com"
    }
  }

.. hint::

   It is also possible to include multiple devices in a single document like:

   .. code-block:: json

        {
          "_id": "multi-devices",
          "rows": [
            { "<device definition>" },
            { "<device definition>" },
          ]
        }


This example launches a simple HTTP Connection to gitlab. You can simply use a different class in the :code:`classname` entry to
use your device driver or any other arbitrary class you want to expose as HERO.


To use BOSS to start the HERO defined above, run

.. code-block:: bash

   uv run boss -u http://<user>:<pw>@<couchdb_host>:5984/my-boss/http-test

.. warning::

   Passing the password in the URL :ref:`is insecure <couchdb-credentials>` and should only be used for local test setups.


If you want to run all documents/devices in the database you can run

.. note::

   This is not recommended in production. Use a view (see below) instead to avoid loading invalid documents and the ability to
   switch off/on devices quickly.


.. code-block:: bash

   uv run boss -u http://<user>:<pw>@<couchdb_host>:5984/my-boss/_all_docs?include_docs=true


.. _couchdb-view:

Using Views
-----------

To make a view which includes all active devices, add a `Design Document` view entry with the following map function:

.. code-block::

   function(doc) {
    if(doc.active) {
      emit()
    }
   }

With the following boss command, you can than start all devices that have the key :code:`active` set to :code:`true` in their json definition:

.. code-block:: bash

   uv run boss -u http://<user>:<pw>@<couchdb_host>:5984/my-boss/_design/active/_view/active?include_docs=true


.. hint::

   If you want to directly see if your settings worked, you can use our rudimentary `GUI <https://gitlab.com/atomiq-project/hero-monitor>`_.


.. _couchdb-credentials:

Passing Credentials
-------------------

BOSS supports multiple secure ways to pass credentials to your CouchDB instance.
For security reasons, avoid passing passwords directly in URLs as they may be visible in logs, shell history, or process listings.

.. tab-set::

   .. tab-item:: As File

      Store your password in a file and reference it via the environment variables ``BOSS_SOURCE_PW_FILE_<username>`` and ``BOSS_SOURCE_PW_FILE``. This is the most secure method.

      Therefore create a password file with secure permissions:

      .. code-block:: bash

         echo "my_secure_password" > /path/to/password.txt
         chmod 600 /path/to/password.txt


      .. tab-set::

        .. tab-item:: Run BOSS in Docker

          To use the file in docker compose, you can pass it `as a secret <https://docs.docker.com/compose/how-tos/use-secrets/>`_ as in the following example:

          .. code-block:: yaml

             services:
               boss:
                 image: registry.gitlab.com/atomiq-project/boss
                 restart: always
                 command: python -m boss.starter -u http://admin@couchdb:5984/my-boss/_design/active/_view/active?include_docs=true
                 environment:
                   - BOSS_SOURCE_PW_FILE_admin=/run/secrets/couchdb_password
                 secrets:
                   - couchdb_password

             secrets:
               couchdb_password:
                 file: ./couchdb_password.txt

        .. tab-item:: Run BOSS locally

          .. code-block:: bash

             export BOSS_SOURCE_PW_FILE=/path/to/password.txt

          to use the password for all users if multiple urls with different users are given or

          .. code-block:: bash

             export BOSS_SOURCE_PW_FILE_<username>=/path/to/password.txt

          to only provide the password for a specific user.

          Run BOSS with the username in the URL:

          .. code-block:: bash

             uv run boss -u http://<username>@<couchdb_host>:5984/my-boss/_design/active/_view/active?include_docs=true



   .. tab-item:: As Environment Variable

      Store your password directly in the ``BOSS_SOURCE_PW_<username>`` and ``BOSS_SOURCE_PW`` environment variables.

      .. tab-set::

        .. tab-item:: Run BOSS in Podman

            This method is especially useful for production deployments using `Podman <https://podman.io/>`_:

            * Create the secret:

              .. code-block:: bash

                 printf 'YOUR_PASSWORD' | podman secret create couchdb-password -

            * Create a container `quadlet file <https://www.redhat.com/en/blog/quadlet-podman>`_ (for example at ``/etc/containers/systemd/boss.container``):

              .. code-block:: systemd

                 [Unit]
                 Description=BOSS Service
                 After=network-online.target
                 Wants=network-online.target

                 [Container]
                 Image=registry.gitlab.com/atomiq-project/boss
                 ContainerName=boss

                 PublishPort=127.0.0.1:8000:8000

                 Secret=couchdb-password,type=env,target=BOSS_SOURCE_PW

                 Command=python -m boss.starter -u http://admin@couchdb:5984/my-boss/_design/active/_view/active?include_docs=true

                 [Service]
                 Restart=always
                 RestartSec=10

                 [Install]
                 WantedBy=default.target

            * Manage the container:

              .. code-block:: bash

                 systemctl daemon-reload
                 systemctl start boss.service
                 systemctl enable boss.service
                 systemctl status boss.service


        .. tab-item:: Run BOSS in Docker

          .. code-block:: yaml

             services:
               boss:
                 image: registry.gitlab.com/atomiq-project/boss
                 restart: always
                 command: python -m boss.starter -u http://admin@couchdb:5984/my-boss/_design/active/_view/active?include_docs=true
                 environment:
                   - BOSS_SOURCE_PW_admin=my_secure_password

        .. tab-item:: Run BOSS locally

            1. Set the environment variable:

              .. code-block:: bash

                 export BOSS_SOURCE_PW_<username>="my_secure_password"

            2. Run BOSS:

            .. code-block:: bash

               uv run boss -u http://<username>@<couchdb_host>:5984/my-boss/_design/active/_view/active?include_docs=true

