Browse Source

Hastic api key to config file #23 (#26)

* Add config example

* Make python use config file

* Make node use config file

* Update docs
pull/1/head
rozetko 7 years ago committed by GitHub
parent
commit
ce120b12c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 29
      README.md
  3. 31
      analytics/config.py
  4. 3
      analytics/data_provider.py
  5. 4
      config.example.json
  6. 25
      server/src/config.ts
  7. 7
      server/src/index.ts

1
.gitignore vendored

@ -1,5 +1,6 @@
data data
dist dist
config.json
node_modules/ node_modules/

29
README.md

@ -37,12 +37,6 @@ Possible to install on:
### Linux ### Linux
#### Environment variables
It is possible to export the following environment variables for hastic-server to use:
- HASTIC_API_KEY - (required) API-key of your Grafana instance
- HASTIC_PORT - (optional) port you want to run server on, default: 8000
#### System prerequisites: #### System prerequisites:
* [git](https://git-scm.com/download/linux) * [git](https://git-scm.com/download/linux)
@ -59,11 +53,28 @@ npm install
npm run build npm run build
``` ```
#### Run #### Configuration
You can configure hastic-server using either *environment variables* or *config file*.
> NOTE: environment variables have higher priority than config file.
##### Environment variables
You can export the following environment variables for hastic-server to use:
- HASTIC_API_KEY - (required) API-key of your Grafana instance
- HASTIC_PORT - (optional) port you want to run server on, default: 8000
e.g.
```bash ```bash
export HASTIC_API_KEY=<your_grafana_api_key> export HASTIC_API_KEY=eyJrIjoiVjZqMHY0dHk4UEE3eEN4MzgzRnd2aURlMWlIdXdHNW4iLCJuIjoiaGFzdGljIiwiaWQiOjF9
export HASTIC_PORT=<port_you_want_to_run_server_on> export HASTIC_PORT=8080
```
##### Config file
You can also rename `config.example.json` to `config.json` and set your values there.
#### Run
```bash
cd ./hastic-server/server cd ./hastic-server/server
npm start npm start
``` ```

31
analytics/config.py

@ -1,7 +1,30 @@
import os import os
import json
def get_config_field(field, default_val = None):
val = default_val
config_exists = os.path.isfile(CONFIG_FILE)
if config_exists:
with open(CONFIG_FILE) as f:
config = json.load(f)
if field in os.environ:
val = os.environ[field]
elif config_exists and field in config:
val = config[field]
else:
raise Exception('Please configure {}'.format(field))
return val
DATA_FOLDER = '../data' DATA_FOLDER = '../data'
DATASET_FOLDER = os.path.join(DATA_FOLDER, "datasets/") CONFIG_FILE = '../config.json'
ANOMALIES_FOLDER = os.path.join(DATA_FOLDER, "anomalies/")
MODELS_FOLDER = os.path.join(DATA_FOLDER, "models/") DATASET_FOLDER = os.path.join(DATA_FOLDER, 'datasets/')
METRICS_FOLDER = os.path.join(DATA_FOLDER, "metrics/") ANOMALIES_FOLDER = os.path.join(DATA_FOLDER, 'anomalies/')
MODELS_FOLDER = os.path.join(DATA_FOLDER, 'models/')
METRICS_FOLDER = os.path.join(DATA_FOLDER, 'metrics/')
HASTIC_API_KEY = get_config_field('HASTIC_API_KEY')

3
analytics/data_provider.py

@ -5,6 +5,7 @@ from urllib.parse import urlencode, urlparse
import urllib.request import urllib.request
import json import json
from time import time from time import time
from config import HASTIC_API_KEY
MS_IN_WEEK = 604800000 MS_IN_WEEK = 604800000
@ -138,7 +139,7 @@ class DataProvider:
def __query_grafana(self, params): def __query_grafana(self, params):
headers = { 'Authorization': 'Bearer ' + os.environ['HASTIC_API_KEY'] } headers = { 'Authorization': 'Bearer ' + HASTIC_API_KEY }
url = self.datasource['origin'] + '/' + self.datasource['url'] + '?' + urlencode(params) url = self.datasource['origin'] + '/' + self.datasource['url'] + '?' + urlencode(params)
req = urllib.request.Request(url, headers=headers) req = urllib.request.Request(url, headers=headers)

4
config.example.json

@ -0,0 +1,4 @@
{
"HASTIC_PORT": 8000,
"HASTIC_API_KEY": "eyJrIjoiVjZqMHY0dHk4UEE3eEN4MzgzRnd2aURlMWlIdXdHNW4iLCJuIjoiaGFzdGljIiwiaWQiOjF9"
}

25
server/src/config.ts

@ -1,4 +1,10 @@
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs';
import { getJsonDataSync } from './services/json';
let configFile = path.join(__dirname, '../../config.json');
let configExists = fs.existsSync(configFile);
export const ANALYTICS_PATH = path.join(__dirname, '../../analytics'); export const ANALYTICS_PATH = path.join(__dirname, '../../analytics');
@ -10,3 +16,22 @@ export const MODELS_PATH = path.join(DATA_PATH, 'models');
export const METRICS_PATH = path.join(DATA_PATH, 'metrics'); export const METRICS_PATH = path.join(DATA_PATH, 'metrics');
export const SEGMENTS_PATH = path.join(DATA_PATH, 'segments'); export const SEGMENTS_PATH = path.join(DATA_PATH, 'segments');
export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000');
function getConfigField(field, defaultVal?) {
let val = defaultVal;
if(process.env[field] !== undefined) {
val = process.env[field];
} else if(configExists) {
let config: any = getJsonDataSync(configFile);
if(config[field] !== undefined) {
val = config[field];
}
}
if(val === undefined) {
throw new Error(`Please configure ${field}`)
}
return val;
}

7
server/src/index.ts

@ -9,10 +9,11 @@ import { router as alertsRouter } from './routes/alerts';
import { checkDataFolders } from './services/data'; import { checkDataFolders } from './services/data';
import { HASTIC_PORT } from './config';
checkDataFolders(); checkDataFolders();
var app = new Koa(); var app = new Koa();
const PORT = process.env.HASTIC_PORT || 8000;
app.use(bodyParser()) app.use(bodyParser())
@ -36,7 +37,7 @@ app
.use(rootRouter.routes()) .use(rootRouter.routes())
.use(rootRouter.allowedMethods()) .use(rootRouter.allowedMethods())
app.listen(PORT, () => { app.listen(HASTIC_PORT, () => {
console.log(`Server is running on :${PORT}`) console.log(`Server is running on :${HASTIC_PORT}`)
}); });

Loading…
Cancel
Save