deployment.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. .. _deployment:
  2. How to deploy FlexMeasures
  3. ===========================
  4. Here you can learn how to get FlexMeasures onto a server.
  5. .. note:: FlexMeasures can be deployed via Docker, where the solver is already installed and there are cloud infrastructures like Kubernetes you'd use. Read more at :ref:`docker-image`. You need other components (e.g. postgres and redis) which are not handled here. See :ref:`docker-compose` for inspiration.
  6. WSGI configuration
  7. ------------------
  8. On your own computer, ``flexmeasures run`` is a nice way to start FlexMeasures. On a production web server, you want it done the :abbr:`WSGI (Web Server Gateway Interface)` way.
  9. Here, you'd want to hand FlexMeasures' ``app`` object to a WSGI process, as your platform of choice describes.
  10. Often, that requires a WSGI script. Below is a minimal example.
  11. .. code-block:: python
  12. # use this if you run from source, not needed if you pip-installed FlexMeasures
  13. project_home = u'/path/to/your/code/flexmeasures'
  14. if project_home not in sys.path:
  15. sys.path = [project_home] + sys.path
  16. # create flask app - the name "application" has to be passed to the WSGI server
  17. from flexmeasures.app import create as create_app
  18. application = create_app()
  19. The web server is told about the WSGI script, but also about the object that represents the application.
  20. For instance, if this script is called wsgi.py, then the relevant argument to the gunicorn server is `wsgi:application`.
  21. A more nuanced one from our practice is this:
  22. .. code-block:: python
  23. # This file contains the WSGI configuration required to serve up your
  24. # web application.
  25. # It works by setting the variable 'application' to a WSGI handler of some description.
  26. # The crucial part are the last two lines. We add some ideas for possible other logic.
  27. import os
  28. project_home = u'/path/to/your/code/flexmeasures'
  29. # use this if you want to load your own ``.env`` file.
  30. from dotenv import load_dotenv
  31. load_dotenv(os.path.join(project_home, '.env'))
  32. # use this if you run from source
  33. if project_home not in sys.path:
  34. sys.path = [project_home] + sys.path
  35. # adapt PATH to find our LP solver if it is installed from source
  36. os.environ["PATH"] = os.environ.get("PATH") + ":/home/seita/Cbc-2.9/bin"
  37. # create flask app - the name "application" has to be passed to the WSGI server
  38. from flexmeasures.app import create as create_app
  39. application = create_app()
  40. Keep in mind that FlexMeasures is based on `Flask <https://flask.palletsprojects.com/>`_, so almost all knowledge on the web on how to deploy a Flask app also helps with deploying FlexMeasures.
  41. .. _installing-a-solver:
  42. Install the linear solver on the server
  43. ---------------------------------------
  44. To compute schedules, FlexMeasures uses the `HiGHS <https://highs.dev/>`_ mixed integer linear optimization solver (FlexMeasures solver by default) or `Cbc <https://github.com/coin-or/Cbc>`_.
  45. Solvers are used through `Pyomo <http://www.pyomo.org>`_\ , so in principle supporting a `different solver <https://pyomo.readthedocs.io/en/stable/solving_pyomo_models.html#supported-solvers>`_ would be possible.
  46. You tell FlexMeasures with the config setting :ref:`solver-config` which solver to use.
  47. However, the solver also needs to be installed - in addition to FlexMeasures (the Docker image already has it). Here is advice on how to install the two solvers we test internally:
  48. .. note:: We default to HiGHS, as it seems more powerful
  49. HiGHS can be installed using pip:
  50. .. code-block:: bash
  51. $ pip install highspy
  52. More information on `the HiGHS website <https://highs.dev/>`_.
  53. Cbc needs to be present on the server where FlexMeasures runs, under the ``cbc`` command.
  54. You can install it on Debian like this:
  55. .. code-block:: bash
  56. $ apt-get install coinor-cbc
  57. (also available in different popular package managers).
  58. More information is on `the CBC website <https://projects.coin-or.org/Cbc>`_.
  59. If you can't use the package manager on your host, the solver has to be installed from source.
  60. We provide an example script in ``ci/install-cbc-from-source.sh`` to do that, where you can also
  61. pass a directory for the installation.
  62. In case you want to install a later version, adapt the version in the script.