Skip to content

Commit 15c6e98

Browse files
guiwritehtessaro
andauthored
docs: Deployment and self hosting (#5911)
Co-authored-by: htessaro <[email protected]>
1 parent f838589 commit 15c6e98

28 files changed

+1989
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"label": "Deployment and Self-hosting",
3+
"collapsed": false,
4+
"position": 20
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Administration and Maintenance",
3+
"position": 5
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Troubleshooting
3+
sidebar_label: Troubleshooting
4+
sidebar_position: 90
5+
---
6+
7+
Here are some common issues encountered when trying to set up Flagsmith in a self-hosted environment.
8+
9+
## Health Checks
10+
11+
If you are using health checks, make sure to use `/health` as the health-check endpoint for both the API and the frontend.
12+
13+
## API and Database Connectivity
14+
15+
The most common cause of issues when setting things up in AWS with an RDS database is missing Security Group permissions between the API application and the RDS database. You need to ensure that the attached security groups for ECS/Fargate/EC2 allow access to the RDS database. [AWS provide more detail about this here](https://aws.amazon.com/premiumsupport/knowledge-center/ecs-task-connect-rds-database/) and [here](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html).
16+
17+
Make sure you have a `DATABASE_URL` environment variable set within the API application.
18+
19+
## Frontend > API DNS Setup
20+
21+
If you are running the API and the frontend as separate applications, you need to make sure that the frontend is pointing to the API. Check the [Frontend environment variables](/deployment/hosting/locally-frontend#environment-variables), particularly `API_URL`.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
sidebar_label: Upgrades and Rollbacks
3+
title: Upgrades and Rollbacks
4+
sidebar_position: 2
5+
---
6+
7+
# Rolling Back to a Previous Version of Flagsmith
8+
9+
:::warning
10+
11+
These steps may result in data loss in the scenario where new models or fields have been added to the database. We recommend taking a full backup of the database before completing the rollback.
12+
13+
:::
14+
15+
This page covers the process of rolling back to a previous version of Flagsmith. If you need to roll back to a previous version, you will need to ensure that the database is also rolled back to the correct state. In order to do this, you will need to unapply all the migrations that happened between the version that you want to roll back to and the one that you are rolling back from. The following steps explain how to do that.
16+
17+
## Steps for v2.151.0 and Later
18+
19+
1. Identify the date and time that you deployed the version that you want to roll back to.
20+
21+
:::tip
22+
23+
If you are unsure about when you completed the previous deployment, you can use the `django_migrations` table as a guide. If you query the table using the following query, you should see the migrations that have been applied (in descending order), grouped in batches corresponding to each deployment.
24+
25+
```sql
26+
SELECT *
27+
FROM django_migrations
28+
ORDER BY applied DESC
29+
```
30+
31+
:::
32+
33+
2. Run the rollback command inside a Flagsmith API container running the _current_ version of Flagsmith:
34+
35+
```bash
36+
python manage.py rollbackmigrationsafter "<datetime from step 1>"
37+
```
38+
39+
3. Roll back the Flagsmith API to the desired version.
40+
41+
## Steps for Versions Earlier Than v2.151.0
42+
43+
If you are rolling back from a version earlier than v2.151.0, you will need to replace step 2 above with the following two steps.
44+
45+
### Step 1: Generate Rollback Commands
46+
47+
Replace the datetime in the query below with a datetime after the deployment of the version you want to roll back to, and before any subsequent deployments. Execute the subsequent query against the Flagsmith database.
48+
49+
```sql {14} showLineNumbers
50+
select
51+
concat('python manage.py migrate ',
52+
app,
53+
' ',
54+
case
55+
when substring(name, 1, 4)::integer = 1 then 'zero'
56+
else lpad((substring(name, 1, 4)::integer - 1)::text, 4, '0')
57+
end
58+
) as "python_commands"
59+
from django_migrations
60+
where id in (
61+
select min(id)
62+
from django_migrations
63+
where applied >= 'yyyy-MM-dd HH:mm:ss'
64+
group by app
65+
);
66+
```
67+
68+
Example output:
69+
70+
```
71+
python_commands
72+
-----------------------------------------------
73+
python manage.py migrate multivariate 0007
74+
python manage.py migrate segment 0004
75+
python manage.py migrate environments 0034
76+
python manage.py migrate features 0064
77+
python manage.py migrate token_blacklist zero
78+
```
79+
80+
### Step 2: Execute the Rollback Commands
81+
82+
Run the generated commands inside a Flagsmith API container running the _current_ version of Flagsmith.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
sidebar_label: Django Admin
3+
title: Django Admin
4+
sidebar_position: 100
5+
---
6+
7+
# Django Admin
8+
9+
The Flagsmith API is a Django application. As such, certain administrative tasks can be performed using [Django's built-in admin interface](https://docs.djangoproject.com/en/4.2/ref/contrib/admin/), which we refer to as Django Admin.
10+
11+
:::danger
12+
13+
Improper use of Django Admin can cause data loss and make your Flagsmith instance unusable. Make sure to control who has access, and only perform tasks as directed by Flagsmith staff.
14+
15+
:::
16+
17+
## Accessing Django Admin
18+
19+
Django Admin can be accessed from the `/admin/` route on the Flagsmith API. Note that the trailing slash is important.
20+
21+
Accessing Django Admin requires a user with [`is_staff`](https://docs.djangoproject.com/en/4.2/ref/contrib/auth/#django.contrib.auth.models.User.is_staff) set. This does not grant any additional permissions beyond accessing Django Admin itself.
22+
23+
A user with [`is_superuser`](https://docs.djangoproject.com/en/4.2/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser) is granted all permissions. Note that superusers still require `is_staff` to access Django Admin.
24+
25+
You can obtain a user with these permissions using any of these methods:
26+
27+
* Use the [`createsuperuser` management command](/deployment/hosting/locally-api#locally) from a Flagsmith API shell.
28+
* If no users exist yet, [visit the Initialise Config page](/deployment/hosting/locally-api#environments-with-no-direct-console-access-eg-heroku-ecs).
29+
* Manually set the `is_staff` and `is_superuser` database fields for your user in the `users_ffadminuser` table.
30+
31+
## Authentication
32+
33+
You can log in to Django Admin using the same email and password you use to log in to Flagsmith, or using Google login.
34+
35+
### Email and Password
36+
37+
To log in to Django Admin with a password, make sure the Flagsmith API has the `ENABLE_ADMIN_ACCESS_USER_PASS` environment variable set to `true`.
38+
39+
If your Flagsmith account does not have a password, you can create one using any of these methods:
40+
41+
* From the Flagsmith login page, click "Forgot password". Make sure your Flagsmith API is [configured to send emails](/deployment/hosting/locally-api#email).
42+
* From a Flagsmith API shell, run `python manage.py changepassword [email protected]` and type a password.
43+
44+
### Google
45+
46+
Google accounts use OAuth 2.0, which requires TLS.
47+
48+
To set up Google authentication for Django Admin, create an OAuth client ID and secret from the [Google Developer Console](https://console.developers.google.com/project). The redirect URI should point to `/admin/admin_sso/assignment/end/` on your API domain.
49+
50+
Set your Google OAuth client ID and secret in the following Flagsmith API environment variables:
51+
52+
* `OAUTH_CLIENT_ID`
53+
* `OAUTH_CLIENT_SECRET`
54+
55+
To log in with Google, click "Log in using SSO" from the Django Admin login page.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Core Configuration",
3+
"position": 30,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: "Caching Strategies"
3+
description: "How to configure caching for API endpoints and the environment document."
4+
sidebar_position: 40
5+
---
6+
7+
The application utilises an in-memory cache for a number of different API endpoints to improve performance. The main things that are cached are listed below:
8+
9+
1. Environment flags - the application utilises an in memory cache for the flags returned when calling /flags. The number of seconds this is cached for is configurable using the environment variable `"CACHE_FLAGS_SECONDS"`
10+
2. Project segments - the application utilises an in memory cache for returning the segments for a given project. The number of seconds this is cached for is configurable using the environment variable `"CACHE_PROJECT_SEGMENTS_SECONDS"`.
11+
3. Flags and identities endpoint caching - the application provides the ability to cache the responses to the GET /flags and GET /identities endpoints. The application exposes the configuration to allow the caching to be handled in a manner chosen by the developer. The configuration options are explained in more detail below.
12+
4. Environment document - when making heavy use of the environment document, it is often wise to utilise caching to reduce the load on the database. Details are provided below.
13+
14+
## Flags & Identities Endpoint Caching
15+
16+
To enable caching on the flags and identities endpoints (GET requests only), you must set the following environment variables:
17+
18+
| Environment Variable | Description | Example value | Default |
19+
| ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------ | --------------------------------------------- |
20+
| `FLAG_IDENTITY_CACHE_LOCATION` | The location to cache the flags and identities endpoints. One of `default` or `redis`. | `redis` | |
21+
| `FLAG_IDENTITY_CACHE_TTL_SECONDS` | The number of seconds to cache the flags and identities endpoints for. | `60` | `0` (i.e. don't cache) |
22+
23+
## Environment Document Caching
24+
25+
To enable caching on the environment document endpoint, you must set the following environment variables:
26+
27+
:::caution
28+
29+
Persistent cache should only be used with cache backends that offer a centralised cache. It should not be used with e.g. LocMemCache.
30+
31+
:::
32+
33+
:::info
34+
35+
When using a persistent cache, a change can take a few seconds to update the cache. This can also be optimised by increasing the performance of your task processor.
36+
37+
:::
38+
39+
| Environment Variable | Description | Example value | Default |
40+
|---------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------|-----------------------------------------------|
41+
| `CACHE_ENVIRONMENT_DOCUMENT_MODE` | The caching mode. One of `PERSISTENT` or `EXPIRING`. Note that although the default is `EXPIRING` there is no caching by default due to the default value of `CACHE_ENVIRONMENT_DOCUMENT_SECONDS` | `PERSISTENT` | `EXPIRING` |
42+
| `CACHE_ENVIRONMENT_DOCUMENT_SECONDS` | Number of seconds to cache the environment for (only relevant when `CACHE_ENVIRONMENT_DOCUMENT_MODE=EXPIRING`) | `60` | `0` ( = don't cache) |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: "Email Setup"
3+
description: "How to configure Flagsmith to send emails using SMTP or SendGrid."
4+
sidebar_position: 30
5+
---
6+
7+
:::note
8+
9+
You can self-host Flagsmith without setting up an email server/gateway. You can invite additional users to the platform using invitation links, and the platform will run fine without email.
10+
11+
:::
12+
13+
:::tip
14+
15+
Flagsmith makes use of the `django_site` table to provide the domain name for email template links. You will need to configure the record in this table to point to your domain for email links to work.
16+
17+
:::
18+
19+
## Required Environment Variables
20+
21+
- `SENDER_EMAIL`: Email address from which emails are sent
22+
- `EMAIL_BACKEND`: One of:
23+
- `django.core.mail.backends.smtp.EmailBackend`
24+
- `sgbackend.SendGridBackend`
25+
- `django_ses.SESBackend`
26+
27+
## SMTP Configuration
28+
29+
If using `django.core.mail.backends.smtp.EmailBackend`, you will need to configure:
30+
31+
- `EMAIL_HOST` = env("EMAIL_HOST", default='localhost')
32+
- `EMAIL_HOST_USER` = env("EMAIL_HOST_USER", default=None)
33+
- `EMAIL_HOST_PASSWORD` = env("EMAIL_HOST_PASSWORD", default=None)
34+
- `EMAIL_PORT` = env("EMAIL_PORT", default=587)
35+
- `EMAIL_USE_TLS` = env.bool("EMAIL_USE_TLS", default=True)
36+
37+
## SendGrid Configuration
38+
39+
If using `sgbackend.SendGridBackend`, you will need to configure:
40+
41+
- `SENDGRID_API_KEY`: API key for the SendGrid account
42+
43+
## AWS SES Configuration
44+
45+
If using AWS SES, you will need to configure:
46+
47+
- `AWS_SES_REGION_NAME`: If using Amazon SES as the email provider, specify the region (e.g. eu-central-1) that contains your verified sender email address. Defaults to us-east-1
48+
- `AWS_SES_REGION_ENDPOINT`: SES region endpoint, e.g. email.eu-central-1.amazonaws.com. Required when using SES.
49+
- `AWS_ACCESS_KEY_ID`: If using Amazon SES, these form part of your SES credentials.
50+
- `AWS_SECRET_ACCESS_KEY`: If using Amazon SES, these form part of your SES credentials.

0 commit comments

Comments
 (0)