Skip to content

Commit 48b5cc8

Browse files
authored
Merge pull request #313 from mapswipe/dev
v2.1.10
2 parents 7e13aa9 + 3c5dfe1 commit 48b5cc8

File tree

11 files changed

+181
-169
lines changed

11 files changed

+181
-169
lines changed

docs/source/cli.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Command Line Interface
22

3+
THIS DOCUMENT IS OUTDATED: Please use the `--help` flag of the CLI.
4+
5+
---
6+
37
This document describes how to use the command line interface of MapSwipe Worker.
48

59
In our current deployment setup the commands of the MapSwipe Workers CLI are hard-coded in the Docker-Compose File.
610

711
You can run these commands also using docker-compose:
12+
813
```
914
docker-compose run mapswipe_workers mapswipe_workers --help
1015
```

docs/source/configuration.md

Lines changed: 48 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,79 @@
1-
<!--
2-
3-
Then set up a Service Account Key file:
4-
1. Open [Google Cloud Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts)
5-
2. Create a new Service Account Key file:
6-
* set name (e.g. *dev-mapswipe-workers*)
7-
* add roles, (e.g. `Storage Admin` and `Firebase Admin`) or use pre-defined role instead (e.g. `Custom Firebase Developer`)
8-
3. Download Key as file:
9-
* select `.json` and save
10-
1+
# Configuration Reference
112

3+
Most of the configuration is stored in environment variables.
4+
At the root of the GitHub repository an example file (`example.env`) with all possible configuration variables exists. To get started copy this file to `.env` and fill in missing variables. Once done source this file to make variables accessible as environment variables: `source .env` to either be used by docker-compose during deployment setup or by MapSwipe Workers directly.
125

6+
In following chapters configuration values and keys are discussed for each part of the MapSwipe Back-end.
137

148

15-
## Firebase
9+
## MapSwipe Workers
1610

17-
Firebase is a central part of MapSwipe. In our setup we use *Firebase Database*, *Firebase Database Rules* and *Firebase Functions*. In the documentation we will refer to two elements:
18-
1. `your_project_id`: This is the name of your Firebase project (e.g. *dev-mapswipe*)
19-
2. `your_database_name`: This is the name of your Firebase database. It is very likely that this will be the same as your Firebase project name as well.)
11+
All configuration values for MapSwipe Workers are stored in environment variables.
2012

21-
The `mapswipe_workers` module uses the [Firebase Python SDK](https://firebase.google.com/docs/reference/admin/python) to access *Firebase Database* services as administrator, you must generate a Service Account Key file in JSON format. For this we use the previously generated Service Account Key. (Check the *Google APIs and Services Credentials* section again if you don't have it.) Copy the file to `mapswipe_workers/config/serviceAccountKey.json`.
13+
Required environment variables are:
14+
- FIREBASE_API_KEY
15+
- FIREBASE_DB
16+
- GOOGLE_APPLICATION_CREDENTIALS
17+
- POSTGRES_DB
18+
- POSTGRES_HOST
19+
- POSTGRES_PASSWORD
20+
- POSTGRES_PORT
21+
- POSTGRES_USER
2222

23-
The `mapswipe_workers` module further uses the [Firebase Database REST API](https://firebase.google.com/docs/reference/rest/database) to access *Firebase Database* either as a normal user or project manager.
23+
Mandatory environment variables are:
24+
- SLACK_CHANNEL
25+
- SLACK_TOKEN
26+
- SENTRY_DSN
2427

25-
For both things to work you need to add your `database_name` in the configuration file. For the the REST API add also the previously generated *mapswipe_workers* api key. (Check the *Google APIs & Services Credentials* section again if you don't have it.) The firebase section in `mapswipe_workers/config/configuration.json` should look like this now:
28+
For satellite imagery access to at least one provider is needed. Define the API key as environment variable:
29+
- IMAGE_BING_API_KEY
30+
- IMAGE_MAPBOX_API_KEY
31+
- IMAGE_MAXA_PREMUIM_API_KEY
32+
- IMAGE_MAXAR_STANDARD_API_KEY
33+
- IMAGE_ESRI_API_KEY
34+
- IMAGE_ESRI_BETA_API_KEY
2635

27-
```json
28-
"firebase": {
29-
"database_name": "your_database_name",
30-
"api_key": "mapswipe_workers_api_key"
31-
}
32-
```
36+
In addition to get access to Firebase a Service Account Key is required.
37+
The path the Service Account Key is defined in:
38+
- GOOGLE_APPLICATION_CREDENTIALS
3339

34-
The `manager_dashboard` module uses the [Firebase JavaScript client SDK](https://firebase.google.com/docs/database/web/start) to access *Firebase Database* service as authenticated as MapSwipe user with project manager credentials. Add the previously generated *manager-dashboard* api key. (Check the *Google APIs & Services Credentials* section again if you don't have it.) Project-id refers to the name of your Firebase project (e.g. dev-mapswipe). The firebaseConfig in `mapswipe_dashboard/js/app.js` should look like this now:
40+
> Notes: When deploying using `docker` or `docker-compose` `POSTGRES_HOST` should have the value `postgres` and the Service Account Key (`serviceAccountKey.json`) should be copied to `mapswipe_workers/serviceAccountKey.json` as described in detail in [Deployment](deployment.md).
3541
36-
```javascript
37-
var firebaseConfig = {
38-
apiKey: "manager_dashboard_api_key",
39-
authDomain: "your_project_id.firebaseapp.com",
40-
databaseURL: "https://your_project_id.firebaseio.com",
41-
storageBucket: "your_project_id.appspot.com"
42-
};
43-
```
4442

45-
The `firebase` module uses the [Firebase Command Line Interface (CLI) Tools](https://github.com/firebase/firebase-tools) to access *Firebase Database Rules* and *Firebase Functions*. You need a firebase token. Here's how you generate it:
46-
1. On a PC with a browser install the Firebase Command Line Tools ([https://firebase.google.com/docs/cli/](https://firebase.google.com/docs/cli/#install_the_firebase_cli))
47-
2. Run `firebase login:ci` to generate a Firebase Token.
48-
3. Save the Firebase Token to `.env` at the root of the cloned MapSwipe Backend repository: `echo "FIREBASE_TOKEN=your_token" >> .env`
49-
4. You should have an entry for the firebase token in your `.env` now:
50-
51-
```bash
52-
FIREBASE_TOKEN="your_token"
53-
```
54-
55-
-->
56-
57-
# Configuration Reference
43+
## Postgres
5844

59-
This document provides details on all required configuration files:
45+
Required environment variables are:
46+
- POSTGRES_DB
47+
- POSTGRES_HOST
48+
- POSTGRES_PASSWORD
49+
- POSTGRES_PORT
50+
- POSTGRES_USER
6051

61-
- `.env file`
62-
* postgres password
63-
* firebase token
64-
* wal-g google storage prefix
65-
- `mapswipe_workers/config/configuration.json`
66-
* firebase: api-key, databaseName
67-
* postgres: host, port, database, username, password
68-
* imagery: urls, api keys
69-
* slack: token, username, channel
70-
* sentry: dsn value
71-
- `mapswipe_workers/config/serviceAccountKey.json`
72-
* check if file exists
73-
- `manager_dashboard/manager_dashboard/js/app.js`
74-
* firebase: authDomain, apiKey, databaseUrl, storageBucket
75-
- `nginx/nginx.conf`
76-
* server name
77-
* ssl certificates, ssl certificates key
7852

79-
You can run the script `test_config.py` to check if you set all the needed variables and file. The script will test the following files:
53+
### Postgres Backup
8054

55+
On details of how the back-up works please refer to [Postgres Backup](backup.md).
8156

82-
## .env
57+
Required environment variables are:
58+
- WALG_GS_PREFIX
8359

84-
The environment file (`.env`) contains all variables needed by services running in Docker container.
60+
To gain access to Google Cloud Storage another Service Account Key is needed. Again refer to [Postgres Backup](backup.md) on how to create this file.
61+
The Service Account Key (`serviceAccountKey.json`) should be saved to `postgres/serviceAccountKey.json`
8562

86-
```.env
87-
POSTGRES_PASSWORD=password
8863

89-
# Google Cloud Storage path for backups of Postgres
90-
WALG_GS_PREFIX=gs://x4m-test-bucket/walg-folder
64+
### Manager Dashboard
9165

92-
# Token for deployment of Firebase Rules and Functions
93-
FIREBASE_TOKEN=firebase_token
66+
`manager_dashboard/manager_dashboard/js/app.js`
9467

95-
GOOGLE_APPLICATION_CREDENTIALS=google_application_credentials
9668
```
97-
98-
99-
## MapSwipe Workers - Configuration
100-
101-
`mapswipe_workers/config/configuration.json`:
102-
103-
```json
104-
{
105-
"postgres": {
106-
"host": "postgres",
107-
"port": "5432",
108-
"database": "mapswipe",
109-
"username": "mapswipe_workers",
110-
"password": "your_mapswipe_db_password"
111-
},
112-
"firebase": {
113-
"database_name": "your_firebase_database_name",
114-
"api_key": "your_firebase_api_key"
115-
},
116-
"imagery":{
117-
"bing": {
118-
"api_key": "your_bing_api_key",
119-
"url": "http://t0.tiles.virtualearth.net/tiles/a{quad_key}.jpeg?g=854&mkt=en-US&token={key}"
120-
},
121-
"digital_globe": {
122-
"api_key": "your_digital_globe_api_key",
123-
"url": "https://api.mapbox.com/v4/digitalglobe.nal0g75k/{z}/{x}/{y}.png?access_token={key}"
124-
},
125-
"sinergise": {
126-
"api_key": "your_sinergise_api_key",
127-
"url": "https://services.sentinel-hub.com/ogc/wmts/{key}?request=getTile&tilematrixset=PopularWebMercator256&tilematrix={z}&tilecol={x}&tilerow={y}&layer={layer}"
128-
}
129-
},
130-
"slack": {
131-
"token": "your_slack_token",
132-
"channel": "your_slack_channel",
133-
"username": "your_slack_username"
134-
},
135-
"sentry": {
136-
"dsn": "your_sentry_dsn_value"
137-
}
138-
}
69+
TODO
13970
```
14071

14172

14273
## NGINX
14374

75+
`nginx/nginx.conf`:
76+
14477
```
14578
server {
14679
listen 80;
@@ -181,19 +114,3 @@ server {
181114
}
182115
}
183116
```
184-
185-
## Postgres Backup
186-
We back up the Postgres database using [Wal-G](https://github.com/wal-g/wal-g) and [Google Cloud Storage](https://console.cloud.google.com/storage). You could also set it up using another cloud storage service.
187-
188-
First, create a new cloud storage bucket:
189-
1. Google Cloud Platform > Storage > Create Bucket
190-
2. Choose a bucket name, e.g. `your_project_id_postgres_backup`
191-
3. Select storage location > `Multi-Region` > `eu`
192-
4. Select storage class > `Coldline`
193-
194-
We need to access Google Cloud Storage. For this we use the previously generated Service Account Key. (Check the *Google APIs and Services Credentials* section again if you don't have it.) Copy the file to `postges/serviceAccountKey.json`.
195-
196-
```bash
197-
GCS_Link_URL=https://console.cloud.google.com/storage/browser/your_project_id_postgres_backup
198-
GCS_Link_for_gsutil=gs://your_project_id_postgres_backup
199-
```

docs/source/deployment_overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ MapSwipe utilizes a bunch of Google Cloud services:
1313
- Create a database: `> Develop > Database > Create Database`
1414
- [Google Cloud Storage](https://cloud.google.com/storage/) for backup of Postgres
1515

16-
[Configuration and Credentials]() describes all needed configuration and credentials.
16+
[Configuration](configuration.md) describes all needed configuration and credentials.
1717

18-
[Installation]() describes step-by-step how to setup the backend for the first time.
18+
[Installation](installation.md) describes step-by-step how to setup the backend for the first time.

docs/source/dev_setup.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Development Setup
22

3-
In this document some tips and workflows for development are loosely collected. Those are independend of the production setup using Docker. A working Firebase Project (including Firebase Functions and Database Rules) is presupposed.
3+
In this document some tips and workflows for development are loosely collected. Those are independent of the production setup using Docker. A working Firebase Project (Including Firebase Functions and Database Rules) is presupposed. Get in touch to get access to an existing Firebase Project for development purposes.
44

55

66
## Installation
@@ -21,22 +21,28 @@ git checkout dev
2121

2222
### Configuration
2323

24-
MapSwipe Workers looks for configuration in `~/.config/mapswipe_workers`. (XDG Base Directory Specification is respected). It expects three files:
24+
All configurations values are stored in environment variables.
2525

26-
- `configuration.json`
27-
- `serviceAccountKey.json`
28-
- `logging.cfg`
26+
Please refer to the documentation on [Configuration](configuration.md) for further details.
2927

30-
Please refer to the [configuration](configuration.md) and [setup](setup.md) documentation for further details.
3128

32-
In addition the data directory for MapSwipe Workers needs to be created:
33-
`mkdir --parents .local/share/mapswipe_workers`
29+
### Keys
30+
31+
To gain access to Firebase MapSwipe workers needs a Service Account Key (`serviceAccountKey.json`). Create on in Firebase and store it where ever you want. Make sure to store the path to the key in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
32+
33+
34+
### Directories
35+
36+
MapSwipe Workers needs access to a directory for data and logs.
37+
To create the directory run `mkdir --parents ~/.local/share/mapswipe_workers`.
38+
39+
> Note: XDG Base Directory Specification is respected
3440
3541

3642
### Install MapSwipe Workers Python Package
3743

3844
1. Create a Python virtual environment with `system-site-packages` option enabled to get access to GDAL/OGR Python packages
39-
2. Activate the vitrual environment.
45+
2. Activate the virtual environment.
4046
3. Install MapSwipe Workers using pip.
4147
4. Run it.
4248

@@ -50,11 +56,11 @@ mapswipe_workers --help
5056

5157
## Postgres
5258

53-
Setup a local Postgres instance for MapSwipe Workers using the provided Dockerfile.
59+
Setup a local Postgres instance for MapSwipe Workers using the for development purposes provided Dockerfile.
5460

5561
```bash
5662
cd postgres/`
57-
docker build -t mapswipe_postgres .
63+
docker build -t mapswipe_postgres Dockerfile-dev
5864
docker run -d -p 5432:5432 --name mapswipe_postgres -e POSTGRES_DB=mapswipe -e POSTGRES_USER=mapswipe_workers -e POSTGRES_PASSWORD=your_password mapswipe_postgres
5965
```
6066

@@ -98,9 +104,9 @@ Firebase functions are used by Mapswipe Workers to increment/decrement or calcul
98104
- user.timeSpentMapping
99105
- user.contibutions{.projectId.groupId.{timestamp, startTime, endTime}}
100106
101-
The functions will be triggert by incoming results from the Mapswipe App.
107+
Those functions will be directly or indirectly triggered by incoming results from the MapSwipe App.
102108
103-
By using firebase functions those attributes can be calculated in real-time and be accessed by users immediately. The use of those functions also reduces the data-transfer between the Firebase Realtime Database and Mapswipe Workers.
109+
By using Firebase functions those attributes can be calculated in real-time and be accessed by users immediately. The use of those functions also reduces the data-transfer between the Firebase Realtime Database and Mapswipe Workers.
104110
105111
On how to setup development enviroment and how to deploy functions to the Firebase instance please refer to the official [Guide on Cloud Function for Firebase](https://firebase.google.com/docs/functions/get-started).
106112
For more information refer to the official [Reference on Cloud Function for Firebase](https://firebase.google.com/docs/reference/functions/). For example function take a look at this [GitHub repository](https://github.com/firebase/functions-samples).

docs/source/installation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Installation
22

3-
This document describes how to setup all the parts of the MapSwipe back-end for the first time.
3+
This document describes how to setup all the parts of the MapSwipe back-end production.
44

5-
Please take also a look at our [Configuration Reference](https://mapswipe-workers.readthedocs.io/en/dev/configuration.html) which is a summary of all configurations and keys needed for deployment.
5+
Please take also a look at our [Configuration Reference](configuration.md) which is a summary of all configurations and keys needed for deployment.
66

77
1. Firebase
88
2. Postgres
99
3. MapSwipe Workers
1010
4. API
1111
5. Manager Dashboard
12-
6. Lets Encrypt and NEGIX as proxy
12+
6. Lets Encrypt and NGINX as proxy
1313

1414
For all those setups our main repository is required:
1515

docs/source/notes.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Then set up a Service Account Key file:
2+
1. Open [Google Cloud Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts)
3+
2. Create a new Service Account Key file:
4+
* set name (e.g. *dev-mapswipe-workers*)
5+
* add roles, (e.g. `Storage Admin` and `Firebase Admin`) or use pre-defined role instead (e.g. `Custom Firebase Developer`)
6+
3. Download Key as file:
7+
* select `.json` and save
8+
9+
10+
## Firebase
11+
12+
Firebase is a central part of MapSwipe. In our setup we use *Firebase Database*, *Firebase Database Rules* and *Firebase Functions*. In the documentation we will refer to two elements:
13+
1. `your_project_id`: This is the name of your Firebase project (e.g. *dev-mapswipe*)
14+
2. `your_database_name`: This is the name of your Firebase database. It is very likely that this will be the same as your Firebase project name as well.)
15+
16+
The `mapswipe_workers` module uses the [Firebase Python SDK](https://firebase.google.com/docs/reference/admin/python) to access *Firebase Database* services as administrator, you must generate a Service Account Key file in JSON format. For this we use the previously generated Service Account Key. (Check the *Google APIs and Services Credentials* section again if you don't have it.) Copy the file to `mapswipe_workers/config/serviceAccountKey.json`.
17+
18+
The `mapswipe_workers` module further uses the [Firebase Database REST API](https://firebase.google.com/docs/reference/rest/database) to access *Firebase Database* either as a normal user or project manager.
19+
20+
For both things to work you need to add your `database_name` in the configuration file. For the the REST API add also the previously generated *mapswipe_workers* api key. (Check the *Google APIs & Services Credentials* section again if you don't have it.) The firebase section in `mapswipe_workers/config/configuration.json` should look like this now:
21+
22+
```json
23+
"firebase": {
24+
"database_name": "your_database_name",
25+
"api_key": "mapswipe_workers_api_key"
26+
}
27+
```
28+
29+
The `manager_dashboard` module uses the [Firebase JavaScript client SDK](https://firebase.google.com/docs/database/web/start) to access *Firebase Database* service as authenticated as MapSwipe user with project manager credentials. Add the previously generated *manager-dashboard* api key. (Check the *Google APIs & Services Credentials* section again if you don't have it.) Project-id refers to the name of your Firebase project (e.g. dev-mapswipe). The firebaseConfig in `mapswipe_dashboard/js/app.js` should look like this now:
30+
31+
```javascript
32+
var firebaseConfig = {
33+
apiKey: "manager_dashboard_api_key",
34+
authDomain: "your_project_id.firebaseapp.com",
35+
databaseURL: "https://your_project_id.firebaseio.com",
36+
storageBucket: "your_project_id.appspot.com"
37+
};
38+
```
39+
40+
The `firebase` module uses the [Firebase Command Line Interface (CLI) Tools](https://github.com/firebase/firebase-tools) to access *Firebase Database Rules* and *Firebase Functions*. You need a firebase token. Here's how you generate it:
41+
1. On a PC with a browser install the Firebase Command Line Tools ([https://firebase.google.com/docs/cli/](https://firebase.google.com/docs/cli/#install_the_firebase_cli))
42+
2. Run `firebase login:ci` to generate a Firebase Token.
43+
3. Save the Firebase Token to `.env` at the root of the cloned MapSwipe Backend repository: `echo "FIREBASE_TOKEN=your_token" >> .env`
44+
4. You should have an entry for the firebase token in your `.env` now:
45+
46+
```bash
47+
FIREBASE_TOKEN="your_token"
48+
```

mapswipe_workers/mapswipe_workers/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def query(self, query, data=None):
6060
self._db_connection.commit()
6161
self._db_cur.close()
6262

63-
def copy_from(self, f, table, columns):
63+
def copy_from(self, f, table, columns=None):
6464
self._db_cur = self._db_connection.cursor()
6565
self._db_cur.copy_from(f, table, columns=columns)
6666
self._db_connection.commit()

0 commit comments

Comments
 (0)