Files @ f734d107296e
Branch filter:

Location: kallithea/docs/installation_puppet.rst

f734d107296e 6.3 KiB text/prs.fallenstein.rst Show Annotation Show as Raw Download as Raw
Mads Kiilerich
auth: for default permissions, use existing explicit query result values instead of following dot references in ORM result objects

There has been reports of spurious crashes on resolving references like
.repository from Permissions:

File ".../kallithea/lib/auth.py", line 678, in __wrapper
if self.check_permissions(user):
File ".../kallithea/lib/auth.py", line 718, in check_permissions
return user.has_repository_permission_level(repo_name, self.required_perm)
File ".../kallithea/lib/auth.py", line 450, in has_repository_permission_level
actual_perm = self.permissions['repositories'].get(repo_name)
File ".../kallithea/lib/vcs/utils/lazy.py", line 41, in __get__
value = self._func(obj)
File ".../kallithea/lib/auth.py", line 442, in permissions
return self.__get_perms(user=self, cache=False)
File ".../kallithea/lib/auth.py", line 498, in __get_perms
return compute(user_id, user_is_admin)
File ".../kallithea/lib/auth.py", line 190, in _cached_perms_data
r_k = perm.UserRepoToPerm.repository.repo_name
File ".../sqlalchemy/orm/attributes.py", line 285, in __get__
return self.impl.get(instance_state(instance), dict_)
File ".../sqlalchemy/orm/attributes.py", line 721, in get
value = self.callable_(state, passive)
File ".../sqlalchemy/orm/strategies.py", line 710, in _load_for_state
% (orm_util.state_str(state), self.key)

sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <UserRepoToPerm at ...> is not bound to a Session; lazy load operation of attribute 'repository' cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3)

Permissions are cached between requests: SA result records are stored in in
beaker.cache.sql_cache_short and resued in following requests after the initial
session as been removed. References in Permission objects would usually give
lazy lookup ... but not outside the original session, where we would get an
error like this.

Permissions are indeed implemented/used incorrectly. That might explain a part
of the problem. Even if not fully explaining or fixing this problem, it is
still worth fixing:

Permissions are fetched from the database using Session().query with multiple
class/table names (joined together in way that happens to match the references
specified in the table definitions) - including Repository. The results are
thus "structs" with selected objects. If repositories always were retrieved
using this selected repository, everything would be fine. In some places, this
was what we did.

But in some places, the code happened to do what was more intuitive: just use
.repository and rely on "lazy" resolving. SA was not aware that this one
already was present in the result struct, and would try to fetch it again. Best
case, that could be inefficient. Worst case, it would fail as we see here.

Fix this by only querying from one table but use the "joinedload" option to
also fetch other referenced tables in the same select. (This might
inefficiently return the main record multiple times ... but that was already
the case with the previous approach.)

This change is thus doing multiple things with circular dependencies that can't
be split up in minor parts without taking detours:

The existing repository join like:
.join((Repository, UserGroupRepoToPerm.repository_id == Repository.repo_id))
is thus replaced by:
.options(joinedload(UserGroupRepoToPerm.repository))

Since we only are doing Session.query() on one table, the results will be of
that type instead of "structs" with multiple objects. If only querying for
UserRepoToPerm this means:
- perm.UserRepoToPerm.repository becomes perm.repository
- perm.Permission.permission_name looked at the explicitly queried Permission
in the result struct - instead it should look in the the dereferenced
repository as perm.permission.permission_name
.. _installation_puppet:

===================================
Installation and setup using Puppet
===================================

The whole installation and setup process of Kallithea can be simplified by
using Puppet and the `rauch/kallithea
<https://forge.puppetlabs.com/rauch/kallithea>`_ Puppet module. This is
especially useful for getting started quickly, without having to deal with all
the Python specialities.

.. note:: The following instructions assume you are not familiar with Puppet at
          all. If this is not the case, you should probably skip directly to the
          `Kallithea Puppet module documentation
          <https://forge.puppetlabs.com/rauch/kallithea#puppet-kallithea>`_.


Installing Puppet
-----------------

This installation variant requires a Unix/Linux type server with Puppet 3.0+
installed. Many major distributions have Puppet in their standard repositories.
Thus, you will probably be ready to go by running, e.g. ``apt-get install
puppet`` or ``yum install puppet``, depending on your distro's favoured package
manager. Afterwards, check the Puppet version by running ``puppet --version``
and ensure you have at least 3.0.

If your distribution does not provide Puppet packages or you need a
newer version, please see the `Puppet Reference Manual
<https://docs.puppetlabs.com/puppet/4.2/reference/install_linux.html>`_ for
instructions on how to install Puppet on your target platform.


Installing the Puppet module
----------------------------

To install the latest version of the Kallithea Puppet module from the Puppet
Forge, run the following as ``root``:

.. code-block:: bash

    puppet module install rauch/kallithea

This will install both the Kallithea Puppet module and its dependency modules.

.. warning::  Be aware that Puppet can do all kinds of things to your systems.
              Third-party modules (like the ``kallithea`` module) may run
              arbitrary commands on your system (most of the time as the
              ``root`` user), so do not apply them on production machines if
              you don't know what you are doing. Instead, use a test system
              (e.g. a virtual machine) for evaluation purposes.


Applying the module
-------------------

To trigger the actual installation process, we have to *apply* the
``kallithea`` Puppet class, which is provided by the module we have just
installed, to our system. For this, create a file named e.g. ``kallithea.pp``,
a *Puppet manifest*, with the following content:

.. _simple_manifest:
.. code-block:: puppet

    class { 'kallithea':
      seed_db    => true,
      manage_git => true,
    }

To apply the manifest, simply run the following (preferably as root):

.. code-block:: bash

    puppet apply kallithea.pp

This will basically run through the usual Kallithea :ref:`installation` and
:ref:`setup` steps, as documented. Consult the module documentation for details
on `what the module affects
<https://forge.puppetlabs.com/rauch/kallithea#what-kallithea-affects>`_. You
can also do a *dry run* by adding the ``--noop`` option to the command.


Using parameters for customizing the setup process
--------------------------------------------------

The ``kallithea`` Puppet class provides a number of `parameters
<https://forge.puppetlabs.com/rauch/kallithea#class-kallithea>`_ for
customizing the setup process. You have seen the usage of the ``seed_db``
parameter in the :ref:`example above <simple_manifest>`, but there are more.
For example, you can specify the installation directory, the name of the user
under which Kallithea gets installed, the initial admin password, etc.
Notably, you can provide arbitrary modifications to Kallithea's configuration
file by means of the ``config_hash`` parameter.

Parameters, which have not been set explicitly, will be set to default values,
which are defined inside the ``kallithea`` Puppet module. For example, if you
just stick to the defaults as in the :ref:`example above <simple_manifest>`,
you will end up with a Kallithea instance, which

- is installed in ``/srv/kallithea``, owned by the user ``kallithea``
- uses the Kallithea default configuration
- uses the admin user ``admin`` with password ``adminpw``
- is started automatically and enabled on boot

As of Kallithea 0.3.0, this in particular means that Kallithea will use an
SQLite database and listen on ``http://localhost:5000``.

See also the `full parameters list
<https://forge.puppetlabs.com/rauch/kallithea#class-kallithea>`_ for more
information.


Making your Kallithea instance publicly available
-------------------------------------------------

If you followed the instructions above, the Kallithea instance will be
listening on ``http://localhost:5000`` and therefore not publicly available.
There are several ways to change this.

The direct way
^^^^^^^^^^^^^^

The simplest setup is to instruct Kallithea to listen on another IP address
and/or port by using the ``config_hash`` parameter of the Kallithea Puppet
class. For example, assume we want to listen on all interfaces on port 80:

.. code-block:: puppet

    class { 'kallithea':
      seed_db => true,
      config_hash => {
        "server:main" => {
          'host' => '0.0.0.0',
          'port' => '80',
        }
      }
    }

Using Apache as reverse proxy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In a more advanced setup, you might instead want use a full-blown web server
like Apache HTTP Server as the public web server, configured such that requests
are internally forwarded to the local Kallithea instance (a so called *reverse
proxy setup*). This can be easily done with Puppet as well:

First, install the `puppetlabs/apache
<https://forge.puppetlabs.com/puppetlabs/apache>`_ Puppet module as above by running the following as root:

.. code-block:: bash

    puppet module install puppetlabs/apache

Then, append the following to your manifest:

.. code-block:: puppet

    include apache

    apache::vhost { 'kallithea.example.com':
      docroot             => '/var/www/html',
      manage_docroot      => false,
      port                => 80,
      proxy_preserve_host => true,
      proxy_pass          => [
        {
          path => '/',
          url  => 'http://localhost:5000/',
        },
      ],
    }

Applying the resulting manifest will install the Apache web server and setup a
virtual host acting as a reverse proxy for your local Kallithea instance.