adding code and build files for grafana tools

This commit is contained in:
Sheldan
2023-08-27 18:53:56 +02:00
parent 7bbe33bca4
commit 4fc43d1aa7
12 changed files with 180 additions and 0 deletions

2
.env Normal file
View File

@@ -0,0 +1,2 @@
REGISTRY_PREFIX=harbor.sheldan.dev/grafana-tools/
VERSION=0.0.1

20
.github/workflows/release.yaml vendored Normal file
View File

@@ -0,0 +1,20 @@
name: Publish package to GitHub Packages
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Login to Harbor
uses: docker/login-action@v2
with:
registry: harbor.sheldan.dev
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_TOKEN }}
- name: Push container
run: docker-compose build && docker-compose push
env:
REGISTRY_PREFIX: harbor.sheldan.dev/grafana-tools/
VERSION: ${{ env.version }}

View File

View File

@@ -0,0 +1,7 @@
FROM python:3.10-alpine
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
COPY . /opt/app
ENTRYPOINT [ "/usr/local/bin/python", "deploy.py" ]

View File

@@ -0,0 +1,69 @@
import glob
import json
import sys
import requests
import yaml
if len(sys.argv) != 2:
print('First parameter must be the absolute path to the folder containing the .json files.')
path = sys.argv[1]
with open(path + '/config.yaml') as config_file:
grafana_config = yaml.safe_load(config_file)
if 'base_url' not in grafana_config:
print('Missing base_url in config.yaml')
exit(1)
base_url = grafana_config['base_url']
folder_name = None
if 'folder_name' in grafana_config:
folder_name = grafana_config['folder_name']
use_auth = False
if 'username' in grafana_config and 'password' in grafana_config:
use_auth = True
user_name = grafana_config['username']
password = grafana_config['password']
print(f'Using basic auth.')
print(f'Using grafana at {base_url}')
session = requests.Session()
if use_auth:
session.auth = (user_name, password)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
folder_id = None
if folder_name is not None:
existing_folders_str = session.get(f'{base_url}/api/folders', headers=headers)
existing_folders = json.loads(existing_folders_str.text)
for existing_folder in existing_folders:
if existing_folder['title'] == folder_name:
folder_id = existing_folder['id']
if folder_id is None and folder_name is not None:
folder_config = {
"title": folder_name
}
folder_response_str = session.post(f'{base_url}/api/folders', json=folder_config, headers=headers)
folder_id = json.loads(folder_response_str.text)['id']
files = glob.glob(path + '/*.json')
dashboard_counter = 0
for dashboard in files:
with open(dashboard) as dashboard_file:
dashboard_config = json.loads(dashboard_file.read())
if folder_id is not None:
dashboard_config['folderId'] = folder_id
response = session.post(f'{base_url}/api/dashboards/db', json=dashboard_config, headers=headers)
if response.status_code != 200:
print(f'Failed to update/create dashboard - {response.status_code} - body "{response.text}"')
exit(1)
print(f'Uploaded {dashboard} json.')
dashboard_counter += 1
print(f'Done deploying {dashboard_counter}')

View File

@@ -0,0 +1,2 @@
requests==2.22.0
PyYAML==5.3.1

9
docker-compose.yaml Normal file
View File

@@ -0,0 +1,9 @@
version: "3.7"
services:
dashboard_deployment:
build: dashboard-deployment
image: ${REGISTRY_PREFIX}grafana-dashboard-deployment:${VERSION:-latest}
user_deployment:
build: user-creation
image: ${REGISTRY_PREFIX}grafana-user-deployment:${VERSION:-latest}

View File

7
user-creation/Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM python:3.10-alpine
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
COPY . /opt/app
ENTRYPOINT [ "/usr/local/bin/python", "deploy.py" ]

View File

@@ -0,0 +1,4 @@
---
base_url: "{{ $.Values.userDeployment.grafana.host }}"
username: "{{ $.Values.userDeployment.grafana.username }}"
password: "{{ $.Values.userDeployment.grafana.password }}"

58
user-creation/deploy.py Normal file
View File

@@ -0,0 +1,58 @@
import glob
import sys
import requests
import yaml
import json
if len(sys.argv) != 2:
print('First parameter must be the absolute path to the folder containing the .json files.')
path = sys.argv[1]
with open(path + '/config.yaml') as config_file:
grafana_config = yaml.safe_load(config_file)
if 'base_url' not in grafana_config:
print('Missing base_url in config.yaml')
exit(1)
base_url = grafana_config['base_url']
use_auth = False
if 'username' in grafana_config and 'password' in grafana_config:
use_auth = True
user_name = grafana_config['username']
password = grafana_config['password']
print(f'Using basic auth.')
print(f'Using grafana at {base_url}')
session = requests.Session()
if use_auth:
session.auth = (user_name, password)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
files = glob.glob(path + '/*.json')
user_counter = 0
for user_path in files:
with open(user_path) as user_file:
user_config = json.load(user_file)
login_name = user_config['login']
user_exists = session.get(f'{base_url}/api/users/lookup?loginOrEmail={login_name}')
if user_exists.status_code == 404:
print(f'User {login_name} does not exist yet - creating')
creation_response = session.post(f'{base_url}/api/admin/users', json=user_config, headers=headers)
creation_response_json = json.loads(creation_response)
user_id = creation_response_json['id']
else:
user_id = json.loads(user_exists.text)['id']
user_response = session.get(f'{base_url}/api/users/{user_id}')
user_response_json = json.loads(user_response.text)
org_id = user_response_json['orgId']
org_role = {
"role": "Editor"
}
session.patch(f'{base_url}/api/orgs/{org_id}/users/{user_id}', json=org_role, headers=headers)
user_counter += 1
print(f'Done deploying {user_counter} users')

View File

@@ -0,0 +1,2 @@
requests==2.22.0
PyYAML==5.3.1