Blame view

INSTALL.multitenancy.md 5.32 KB
af25d9e0   Aurélio A. Heckert   correct markdown ...
1
2
Multitenancy support
====================
afb05ea7   Daniel Cunha   Adding multitenan...
3

af25d9e0   Aurélio A. Heckert   correct markdown ...
4
Multitenancy refers to a principle in software architecture where a single instance of the software runs on a server, serving multiple client organizations (tenants). Multitenancy is contrasted with a multi-instance architecture where separate software instances (or hardware systems) are set up for different client organizations. With a multitenant architecture, a software application is designed to virtually partition its data and configuration, and each client organization works with a customized virtual application instance.
afb05ea7   Daniel Cunha   Adding multitenan...
5
6
7

Today this feature is available only for PostgreSQL databases.

af25d9e0   Aurélio A. Heckert   correct markdown ...
8
This document assumes that you have a new fully PostgresSQL default Noosfero installation as explained at the `INSTALL.md` file.
afb05ea7   Daniel Cunha   Adding multitenan...
9

af25d9e0   Aurélio A. Heckert   correct markdown ...
10
11
Separated data
--------------
afb05ea7   Daniel Cunha   Adding multitenan...
12
13
14
15
16

The items below are separated for each hosted environment:

* Uploaded files
* Database
4197c870   Braulio Bhavamitra   Remove/replace ol...
17
* Solr index
afb05ea7   Daniel Cunha   Adding multitenan...
18
19
20
21
* ActiveRecord#cache_key
* Feed updater
* Delayed Job Workers

af25d9e0   Aurélio A. Heckert   correct markdown ...
22
23
Database configuration file
---------------------------
afb05ea7   Daniel Cunha   Adding multitenan...
24

af25d9e0   Aurélio A. Heckert   correct markdown ...
25
The file config/database.yml must follow a structure in order to achieve multitenancy support. In this example, we will set 3 different environments: env1, env2 and env3.
afb05ea7   Daniel Cunha   Adding multitenan...
26
27
28

Each "hosted" environment must have an entry like this:

0531c6a2   Leandro Santos   merging with master
29
    env1_production: &DEFAULT
af25d9e0   Aurélio A. Heckert   correct markdown ...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
      adapter: postgresql
      encoding: unicode
      database: noosfero
      schema_search_path: public
      username: noosfero
      domains:
        - env1.com
        - env1.org

    env2_production:
      adapter: postgresql
      encoding: unicode
      database: noosfero
      schema_search_path: env2
      username: noosfero
      domains:
        - env2.com
        - env2.org

    env3_production:
      adapter: postgresql
      encoding: unicode
      database: noosfero
      schema_search_path: env3
      username: noosfero
      domains:
        - env3.com
        - env3.net

The "hosted" environments define, besides the `schema_search_path`, a list of domains that, when accessed, tells which database the application should use. Also, the environment name must end with "`_<hosting>`", where `<hosting>` is the name of the hosting environment.
afb05ea7   Daniel Cunha   Adding multitenan...
60
61
62

You must also tell the application which is the default environment.

af25d9e0   Aurélio A. Heckert   correct markdown ...
63
    production:
0531c6a2   Leandro Santos   merging with master
64
      <<: *DEFAULT
afb05ea7   Daniel Cunha   Adding multitenan...
65

af25d9e0   Aurélio A. Heckert   correct markdown ...
66
On the example above there are only three hosted environments, but it can be more than three. The schemas `env2` and `env3` must already exist in the same database of the hosting environment. As postgres user, you can create them typing:
afb05ea7   Daniel Cunha   Adding multitenan...
67

af25d9e0   Aurélio A. Heckert   correct markdown ...
68
69
    $ psql database_name -c "CREATE SCHEMA env2 AUTHORIZATION database_user"
    $ psql database_name -c "CREATE SCHEMA env3 AUTHORIZATION database_user"
afb05ea7   Daniel Cunha   Adding multitenan...
70

af25d9e0   Aurélio A. Heckert   correct markdown ...
71
Replace `database_name` and `database_user` above with your stuff.
afb05ea7   Daniel Cunha   Adding multitenan...
72

af25d9e0   Aurélio A. Heckert   correct markdown ...
73
So, yet on this same example, when a user accesses http://env2.com or http://env2.org, the Noosfero application running on production will turn the database schema to `env2`. When the access is from domains http://env3.com or http://env3.net, the schema to be loaded will be `env3`.
afb05ea7   Daniel Cunha   Adding multitenan...
74

af25d9e0   Aurélio A. Heckert   correct markdown ...
75
There is an example of this file in `config/database.yml.multitenancy`
afb05ea7   Daniel Cunha   Adding multitenan...
76

af25d9e0   Aurélio A. Heckert   correct markdown ...
77
78
Preparing the database
----------------------
afb05ea7   Daniel Cunha   Adding multitenan...
79
80
81

Now create the environments:

af25d9e0   Aurélio A. Heckert   correct markdown ...
82
    $ RAILS_ENV=production rake multitenancy:create
afb05ea7   Daniel Cunha   Adding multitenan...
83

af25d9e0   Aurélio A. Heckert   correct markdown ...
84
This command above will create the hosted environment files equal to their hosting environment, here called 'production'.
afb05ea7   Daniel Cunha   Adding multitenan...
85
86
87

Run db:schema:load for each other environment:

af25d9e0   Aurélio A. Heckert   correct markdown ...
88
89
    $ RAILS_ENV=env2_production rake db:schema:load
    $ RAILS_ENV=env3_production rake db:schema:load
afb05ea7   Daniel Cunha   Adding multitenan...
90

af25d9e0   Aurélio A. Heckert   correct markdown ...
91
Then run the migrations for the hosting environment, and it will run for each of its hosted environments:
afb05ea7   Daniel Cunha   Adding multitenan...
92

af25d9e0   Aurélio A. Heckert   correct markdown ...
93
    RAILS_ENV=production rake db:migrate
afb05ea7   Daniel Cunha   Adding multitenan...
94

af25d9e0   Aurélio A. Heckert   correct markdown ...
95
96
Start Noosfero
--------------
afb05ea7   Daniel Cunha   Adding multitenan...
97
98
99

Run Noosfero init file as root:

af25d9e0   Aurélio A. Heckert   correct markdown ...
100
    # invoke-rc.d noosfero start
afb05ea7   Daniel Cunha   Adding multitenan...
101

af25d9e0   Aurélio A. Heckert   correct markdown ...
102
103
Feed updater & Delayed job
--------------------------
afb05ea7   Daniel Cunha   Adding multitenan...
104

af25d9e0   Aurélio A. Heckert   correct markdown ...
105
Just for your information, a daemon of `feed-updater` and `delayed_job` must be running for each environment. Noosfero initializer do this, relax.
afb05ea7   Daniel Cunha   Adding multitenan...
106

af25d9e0   Aurélio A. Heckert   correct markdown ...
107
108
Uploaded files
--------------
afb05ea7   Daniel Cunha   Adding multitenan...
109

af25d9e0   Aurélio A. Heckert   correct markdown ...
110
When running with PostgreSQL, Noosfero uploads stuff to a folder named the same way as the running schema. Inside the upload folder root, for example, will be `public/image_uploads/env2` and `public/image_uploads/env3`.
afb05ea7   Daniel Cunha   Adding multitenan...
111

af25d9e0   Aurélio A. Heckert   correct markdown ...
112
113
Adding multitenancy support to an existing Noosfero environment
---------------------------------------------------------------
afb05ea7   Daniel Cunha   Adding multitenan...
114

af25d9e0   Aurélio A. Heckert   correct markdown ...
115
If you already have a Noosfero environment, you can turn it multitenant by following the steps below in addition to the previous steps:
b1b3e471   Caio SBA   Multitenancy docu...
116

af25d9e0   Aurélio A. Heckert   correct markdown ...
117
### 1. Reindex your database
b1b3e471   Caio SBA   Multitenancy docu...
118

af25d9e0   Aurélio A. Heckert   correct markdown ...
119
Rebuild the Solr index by running the following task just for your hosting environment, do this as noosfero user:
b1b3e471   Caio SBA   Multitenancy docu...
120

af25d9e0   Aurélio A. Heckert   correct markdown ...
121
    $ RAILS_ENV=production rake multitenancy:reindex
b1b3e471   Caio SBA   Multitenancy docu...
122

af25d9e0   Aurélio A. Heckert   correct markdown ...
123
### 2. Move the uploaded files to the right place
b1b3e471   Caio SBA   Multitenancy docu...
124

af25d9e0   Aurélio A. Heckert   correct markdown ...
125
Add a directory with the same name as your schema name (by default this name is `public`) in the root of each upload directory, for example, `public/articles/0000` will be moved to `public/articles/public/0000`. Do this with the directories `public/image_uploads`, `public/articles` and `public/thumbnails`.
b1b3e471   Caio SBA   Multitenancy docu...
126

af25d9e0   Aurélio A. Heckert   correct markdown ...
127
### 3. Fix paths on activities
b1b3e471   Caio SBA   Multitenancy docu...
128

af25d9e0   Aurélio A. Heckert   correct markdown ...
129
The profile activities store static paths to the images, so it's necessary to fix these paths. You can do this easily by setting an alias on your webserver. On Apache you can add the three rules below, where 'public' is the schema name:
b1b3e471   Caio SBA   Multitenancy docu...
130

af25d9e0   Aurélio A. Heckert   correct markdown ...
131
132
133
    RewriteRule ^/articles(.+) /articles/public$1
    RewriteRule ^/image_uploads(.+) /image_uploads/public$1
    RewriteRule ^/thumbnails(.+) /thumbnails/public$1