Skip to content

Commit 89d43bc

Browse files
Merge pull request #77 from blackdotraven/feat_new_confighelper
feat: new confighelper with env support
2 parents 2de754e + f5352e9 commit 89d43bc

25 files changed

Lines changed: 250 additions & 137 deletions

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ We decided to opensource it as an easy-to-setup software after some requests.
1616
- dect-wip-ommsync checks for newly paired handsets and creates temporary voip accounts for calling the self-registration code.
1717
- dect-wip-mitel-phonebook provides an endpoint for mitel dect handsets to lookup phonebook entries
1818

19+
## Configuration and Environment Variables
20+
The main config `dect-wip.ini` is in `/etc/dect-wip.ini` by default.
21+
22+
Config location can be overridden with env-variable `CONFIG_PATH`(WSGI/Dev-Mode) or commandline argument `--config-file` (DEV-Mode).
23+
24+
All settings in `dect-wip.ini` can be overridden via env-variable (in all caps) in the following order:
25+
- `FROM_FILE_{SECTION}_{KEY}`
26+
- `{SECTION}_{KEY}`
27+
- Config-File
28+
29+
For example:
30+
- `FROM_FILE_EVENT_TOKEN_PREFIX`
31+
- `EVENT_TOKEN_PREFIX`
32+
```
33+
[event]
34+
token_prefix = 01990
35+
```
36+
37+
The prefix `FROM_FILE_` will read the contents of that file and uses it as variable value.
38+
39+
Values specified via env-variables can be omitted in `dect-wip.ini`. A config-file need to be present, but it can be empty.
40+
1941
## how to contribute
2042
- This is a fun-project! We don't get paid for this and we might have higher response times
2143
- Fetch a unassigned issue, ask if you don't know anything or are unsure
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
if [ ! -d "venv/" ]; then
4+
python3 -mvenv venv
5+
source venv/bin/activate
6+
pip3 install --upgrade pip
7+
pip3 install -r requirements.txt
8+
fi
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
echo "working in $(pwd)"
44

5-
if [ ! -d "venv/" ]; then
6-
python3 -mvenv venv
7-
source venv/bin/activate
8-
pip3 install --upgrade pip
9-
pip3 install -r requirements.txt
10-
fi
5+
./_setup_venv.sh
116

127
source venv/bin/activate
13-
python3 phonebook.py
8+
gunicorn phonebook:init_wsgi\(\)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
echo "working in $(pwd)"
4+
5+
./_setup_venv.sh
6+
7+
source venv/bin/activate
8+
python3 phonebook.py
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from tools.confighelper import DectWIPConfig
2+
from os import environ
3+
4+
_config = DectWIPConfig(config_path=environ.get('CONFIG_PATH', '/etc/dect-wip.ini'))
5+
6+
# starting gunicorn config here
7+
8+
bind = f'{_config.mitelphonebook_listen_ip}:{_config.mitelphonebook_port}'
9+
accesslog = _config.gunicorn_accesslogfile
10+
errorlog = _config.gunicorn_errorlogfile

services/dect-wip-mitel-phonebook/phonebook.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
import os
12
from flask import Flask, request, render_template_string
23
import requests
3-
import os
4-
import configparser
54
import click
5+
from tools.confighelper import DectWIPConfig
66

7-
dect_wip_ip = os.getenv('DECT_WIP_URL', '127.0.0.1:8080')
8-
port = int(os.getenv('DECT_WIP_PHONEBOOK_PORT', '8082'))
97
app = Flask(__name__)
10-
config = configparser.ConfigParser()
118

129
xsi_template = """<?xml version="1.0" encoding="UTF-8"?>
1310
<Personal xmlns="http://schema.broadsoft.com/xsi">
@@ -27,7 +24,7 @@ def return_phonebook(caller):
2724
search_index = int(search_index)-1 # let search_index start at 0 not 1
2825
search_results_count = int(request.args.get('results'))
2926

30-
if always_search_with_contains:
27+
if config.mitelphonebook_always_search_with_contains:
3128
if not search_string.startswith('*'):
3229
print(f'config.ini phonebook always_search_with_contains is true --> converting {search_string} to *{search_string}')
3330
search_string = f'*{search_string}'
@@ -38,9 +35,9 @@ def return_phonebook(caller):
3835
search_string = search_string.replace('*','%')
3936

4037
if search_string == '':
41-
names_and_extensions = requests.get(f"http://{dect_wip_ip}/api/v1/phonebook").json()
38+
names_and_extensions = requests.get(f"http://{config.dect_wip_internal_ip}:{config.dect_wip_port}/api/v1/phonebook").json()
4239
else:
43-
names_and_extensions = requests.get(f"http://{dect_wip_ip}/api/v1/phonebook?search={search_string}").json()
40+
names_and_extensions = requests.get(f"http://{config.dect_wip_internal_ip}:{config.dect_wip_port}/api/v1/phonebook?search={search_string}").json()
4441

4542
print(f"{caller} searched for {search_string}, index={search_index}, results_count={search_results_count}")
4643

@@ -55,21 +52,18 @@ def init(config_path):
5552

5653
# setup global config
5754

58-
global always_search_with_contains
55+
global config
5956

60-
config.read(config_path)
61-
always_search_with_contains = config.getboolean('phonebook', 'always_search_with_contains')
57+
config = DectWIPConfig(config_path=config_path)
6258

6359
@click.command()
64-
@click.option('--config', 'config_path', envvar='CONFIG', default='/etc/dect-wip.ini', help='optional config location')
60+
@click.option('--config', 'config_path', envvar='CONFIG_PATH', default='/etc/dect-wip.ini', help='optional config location')
6561
def init_dev(config_path):
66-
# TODO: refactor config.ini and ENV
6762
init(config_path)
68-
app.run(host='0.0.0.0', port=port, debug=False, use_reloader=True)
63+
app.run(host=config.mitelphonebook_listen_ip, port=config.mitelphonebook_port, debug=False, use_reloader=True)
6964

7065
def init_wsgi():
71-
config_path = os.getenv('CONFIG', '/etc/dect-wip.ini')
72-
init(config_path)
66+
init(os.environ.get('CONFIG_PATH', '/etc/dect-wip.ini'))
7367
return app
7468

7569
if __name__ == '__main__':
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
flask
22
requests
3+
gunicorn
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../tools
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
if [ ! -d "venv/" ]; then
4+
python3 -mvenv venv
5+
source venv/bin/activate
6+
pip3 install --upgrade pip
7+
pip3 install -r requirements.txt
8+
fi
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
echo "working in $(pwd)"
44

5-
if [ ! -d "venv/" ]; then
6-
python3 -mvenv venv
7-
source venv/bin/activate
8-
pip3 install --upgrade pip
9-
pip3 install -r requirements.txt
10-
fi
5+
./_setup_venv.sh
116

127
source venv/bin/activate
13-
python3 dect-wip-ommsync.py
8+
gunicorn dect-wip-ommsync:init_wsgi\(\)

0 commit comments

Comments
 (0)