Skip to content

Commit

Permalink
Change user org in Grafana (#4)
Browse files Browse the repository at this point in the history
* Change user org in Grafana
* Expose full configuration of Grafana users in dotenv
* Change default Grafana admin user
* Update `grafana-init` README details about default grafana user env
variables.
* Bump grafana-init version
* Bump version of Grafana-init in env files
* Remove GRAFANA_USERNAME and GRAFANA_PASSWORD from grafana-init

Signed-off-by: Dobroslaw Zybort <[email protected]>
  • Loading branch information
matrixik committed Jan 19, 2018
1 parent 0f8fc3e commit 10f64e6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 18 deletions.
9 changes: 5 additions & 4 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ MON_BASE_PATH=
# - Grafana admin (user name must be different to all OpenStack user names)
# - Grafana user is the OpenStack admin user (see "admin credentials" below)
# (The Monasca data-source is assigned automatically to the Grafana user)
# It need to be provided in JSON format together with project and domain
# Example: [{"user": "mini-mon", "password": "password", "email": "", "project": "mini-mon", "domain": "Default"}]
################################################################################
MON_GRAFANA_ADMIN_USER=admin
MON_GRAFANA_ADMIN_USER=grafana-admin
MON_GRAFANA_ADMIN_PASSWORD=admin
MON_GRAFANA_USERNAME=mini-mon
MON_GRAFANA_PASSWORD=password
MON_GRAFANA_USERS=[{"user": "mini-mon", "password": "password", "email": "", "project": "mini-mon", "domain": "Default"}]

################################################################################
# Set the Keystone credentials
Expand Down Expand Up @@ -115,7 +116,7 @@ MON_KEYSTONE_VERSION=2.0.1
MON_KAFKA_VERSION=2.0.2
MON_KAFKA_INIT_VERSION=2.0.0
MON_GRAFANA_VERSION=2.0.4
MON_GRAFANA_INIT_VERSION=2.0.4
MON_GRAFANA_INIT_VERSION=2.0.5

MON_API_VERSION=2.0.1
MON_PERSISTER_VERSION=2.0.0
Expand Down
5 changes: 3 additions & 2 deletions docker-compose-metric.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ services:
hostname: docker-host
image: fest/grafana-init:${MON_GRAFANA_INIT_VERSION}
environment:
GRAFANA_USERNAME: ${MON_GRAFANA_USERNAME}
GRAFANA_PASSWORD: ${MON_GRAFANA_PASSWORD}
GRAFANA_ADMIN_USERNAME: ${MON_GRAFANA_ADMIN_USER}
GRAFANA_ADMIN_PASSWORD: ${MON_GRAFANA_ADMIN_PASSWORD}
GRAFANA_USERS: ${MON_GRAFANA_USERS}
LOGSTASH_FIELDS: "service=grafana-init"
depends_on:
- grafana
7 changes: 4 additions & 3 deletions dotenv2
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ MON_BASE_PATH=/dashboard/monitoring/logs_proxy
# - Grafana admin (user name must be different to all OpenStack user names)
# - Grafana user is the OpenStack admin user (see "admin credentials" below)
# (The Monasca data-source is assigned automatically to the Grafana user)
# It need to be provided in JSON format together with project and domain
# Example: [{"user": "mini-mon", "password": "password", "email": "", "project": "mini-mon", "domain": "Default"}]
################################################################################
MON_GRAFANA_ADMIN_USER=<grafana admin user name>
MON_GRAFANA_ADMIN_PASSWORD=<grafana admin password>
MON_GRAFANA_USERNAME=<OpenStack admin user name>
MON_GRAFANA_PASSWORD=<OpenStack admin user password>
MON_GRAFANA_USERS=<json with users data>

################################################################################
# Set the Keystone credentials
Expand Down Expand Up @@ -100,7 +101,7 @@ ZOOKEEPER_VERSION=3.4
MON_KAFKA_VERSION=2.0.2
MON_KAFKA_INIT_VERSION=2.0.0
MON_GRAFANA_VERSION=2.0.4
MON_GRAFANA_INIT_VERSION=2.0.4
MON_GRAFANA_INIT_VERSION=2.0.5

MON_API_VERSION=2.0.1
MON_PERSISTER_VERSION=2.0.0
Expand Down
4 changes: 2 additions & 2 deletions grafana-init/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ from alpine:3.5

env LOG_LEVEL=INFO \
GRAFANA_URL=http://grafana:3000 \
GRAFANA_USERNAME=mini-mon \
GRAFANA_PASSWORD=password \
GRAFANA_ADMIN_USERNAME=admin \
GRAFANA_ADMIN_PASSWORD=password \
DATASOURCE_TYPE=monasca \
DATASOURCE_URL=http://monasca:8070/ \
DATASOURCE_ACCESS_MODE=proxy \
Expand Down
25 changes: 22 additions & 3 deletions grafana-init/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Configuration
|--------------------|------------------------|---------------------------------|
| `LOG_LEVEL` | `INFO` | Logging level, e.g. `DEBUG` |
| `GRAFANA_URL` | `http://grafana:3000` | Location of Grafana server |
| `GRAFANA_USERNAME` | `mini-mon` | Agent Keystone username |
| `GRAFANA_PASSWORD` | `password` | Agent Keystone password |
| `GRAFANA_USERS` | `'[{"user": GRAFANA_USERNAME, "password": GRAFANA_PASSWORD, "email": ""}]'` | Agent Keystone users. Default datasource and dashboard are created for multiple users if this variable is set as proper JSON format. e.g. `'[{"user": "mini-mon", "password": "password", "email": ""}, {"user": "username", "password": "password", "email": ""}]'`. Default value is overwritten by `GRAFANA_USERNAME` and `GRAFANA_PASSWORD`. **NOTE: Set this variable in String type** |
| `GRAFANA_USERS` | `'[{"user": "mini-mon", "password": "password", "email": "", "project": "mini-mon", "domain": "Default"}]'` | Agent Keystone users configuration. Read more in [Grafana users](#grafana-users) **NOTE: Set this variable as String type** |
| `DATASOURCE_TYPE` | `monasca` | Agent Keystone user domain |
| `GRAFANA_ADMIN_USERNAME` | `admin` | Agent Keystone admin username |
| `GRAFANA_ADMIN_PASSWORD` | `password` | Agent Keystone admin username |
| `DATASOURCE_URL` | `http://monasca:8070/` | Agent Keystone project name |
| `DATASOURCE_ACCESS_MODE` | `proxy` | Grafana access mode string |
| `DASHBOARDS_DIR` | `/dashboards.d` | Directory to scan for .json dashboards |
Expand All @@ -52,6 +52,25 @@ Note that the only datasource type supported at the moment is `monasca`. Other
datasources should be simple to implement as needed by adding logic to
`create_datasource_payload()` in [`grafana.py`][6].

### Grafana users

`GRAFANA_USERS`

Default data source and dashboard are created for multiple
users if this variable is set as proper JSON format. e.g.
```
'[{"user": "mini-mon", "password": "password", "email": "", "project": "mini-mon", "domain": "Default"}, {"user": "other-user", "password": "password", "email": "", "project": "monasca", "domain": "Default"}]'
```
In the same place you could provide user project and domain that will be used
to set proper organization in Grafana. If you does not provide them dashboards
will be uploaded to random organization if user is assigned to more than one
(that's how Grafana API is doing by default).

`GRAFANA_ADMIN_USERNAME` and `GRAFANA_ADMIN_PASSWORD`

Grafan admin user is need to set proper organization for each user.


### Custom Dashboards

This image comes with a default set of Monasca-specific dashboards, but these
Expand Down
2 changes: 1 addition & 1 deletion grafana-init/build.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repository: fest/grafana-init
variants:
- tag: 2.0.4
- tag: 2.0.5
aliases:
- :pike-{date}-{time}
52 changes: 49 additions & 3 deletions grafana-init/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
import os
import time
import urllib

from requests import Session, RequestException

Expand All @@ -28,9 +29,15 @@
logger = logging.getLogger(__name__)

GRAFANA_URL = os.environ.get('GRAFANA_URL', 'http://grafana:3000')
GRAFANA_USERNAME = os.environ.get('GRAFANA_USERNAME', 'mini-mon')
GRAFANA_PASSWORD = os.environ.get('GRAFANA_PASSWORD', 'password')
GRAFANA_USERS = [{'user': GRAFANA_USERNAME, 'password': GRAFANA_PASSWORD, 'email': ''}]
GRAFANA_ADMIN_USERNAME = os.environ.get('GRAFANA_ADMIN_USERNAME', 'admin')
GRAFANA_ADMIN_PASSWORD = os.environ.get('GRAFANA_ADMIN_PASSWORD', 'password')
GRAFANA_USERS = [{
'user': "mini-mon",
'password': "password",
'email': '',
'project': 'mini-mon',
'domain': 'Default',
}]

DATASOURCE_NAME = os.environ.get('DATASOURCE_NAME', 'monasca')
DATASOURCE_URL = os.environ.get('DATASOURCE_URL', 'http://monasca:8070/')
Expand Down Expand Up @@ -73,6 +80,14 @@ def create_login_payload():
return grafana_users


def create_admin_login_payload():
return {
'user': GRAFANA_ADMIN_USERNAME,
'password': GRAFANA_ADMIN_PASSWORD,
'email': ''
}


@retry(retries=24, delay=5.0)
def login(session, user):
r = session.post('{url}/login'.format(url=GRAFANA_URL),
Expand All @@ -81,6 +96,22 @@ def login(session, user):
r.raise_for_status()


@retry(retries=12, delay=5.0)
def change_user_context(admin_session, user_session, organisation):
org = admin_session.get('{url}/api/orgs/name/{org_name}'.format(
url=GRAFANA_URL, org_name=urllib.quote(organisation.encode('utf8'))
), timeout=5)
org.raise_for_status()

org_id = json.loads(org.text)['id']
logging.debug('Organisation "%s" id = %r', organisation, org_id)

r = user_session.post('{url}/api/user/using/{org}'.
format(url=GRAFANA_URL, org=org_id),
timeout=5)
r.raise_for_status()


@retry(retries=12, delay=5.0)
def check_initialized(session):
r = session.get('{url}/api/datasources'.format(url=GRAFANA_URL), timeout=5)
Expand Down Expand Up @@ -127,6 +158,10 @@ def create_dashboard_payload(json_path):


def main():
admin_session = Session()
admin_user = create_admin_login_payload()
login(admin_session, admin_user)

for user in create_login_payload():
logging.info('Opening a Grafana session...')
session = Session()
Expand All @@ -136,6 +171,13 @@ def main():
logging.info('Grafana has already been initialized, skipping!')
return

if (user['project'] != '') and (user['domain'] != ''):
# Grafana org name is created from Kestone project+"@"+domain
org_name = user['project'] + '@' + user['domain']
logging.info('Setting user "%s" organisation to "%s"',
user['user'], org_name)
change_user_context(admin_session, session, org_name)

logging.info('Attempting to add configured datasource...')
r = session.post('{url}/api/datasources'.format(url=GRAFANA_URL),
json=create_datasource_payload())
Expand All @@ -152,7 +194,11 @@ def main():
logging.info('Ending %r session...', user.get('user'))
session.get('{url}/logout'.format(url=GRAFANA_URL))

logging.info('Ending %r session...', admin_user.get('user'))
admin_session.get('{url}/logout'.format(url=GRAFANA_URL))

logging.info('Finished successfully.')


if __name__ == '__main__':
main()

0 comments on commit 10f64e6

Please sign in to comment.