Commit 29880dc9 authored by David Huss's avatar David Huss 💬

Move stuff to config.py, Add --new option

parent 50364499
......@@ -43,9 +43,13 @@ poetry run bbbmon
# Configuration
Just run `bbbmon` and it will tell you what it needs.
Run `bbbmon config --new` to create a new default configuration file. bbbmon will always ask you before it creates or overwrites anything.
You can define one or more endpoints in the `bbbmon.properties` file at the path bbbmon tells you about. In this file you can specify multiple bbb servers each with it's secret and bigbluebutton-URL. You can find the secret on your server in it's config-file via `cat /usr/share/bbb-web/WEB-INF/classes/bigbluebutton.properties | grep securitySalt=`
Within the config you can define one or more endpoints with running bbb instances – each with it's secret and bigbluebutton-URL. You can find the secret on your server in it's config-file via
```bash
cat /usr/share/bbb-web/WEB-INF/classes/bigbluebutton.properties | grep securitySalt=
```
A example configuration file could look like this:
```toml
......@@ -53,17 +57,23 @@ A example configuration file could look like this:
securitySalt=MY_SUPER_SECRET_SECRET
bigbluebutton.web.serverURL=https://bbb.example.com/
[Foo's private bbb-server]
[Föö]
securitySalt=MY_SUPER_SECRET_SECRET2
bigbluebutton.web.serverURL=https://bbb.foo.com/
```
The section names in the square brackets will be used as display names (these support utf-8)
The section names in the square brackets can be chosen arbitrarily (as long as they are unique) and will be used as display names (they support utf-8). It makes sense to keep them short as they can be used for filtering and/or ordering:
```bash
bbbmon meetings -e Föö
```
# Usage
For help run:
```bash
bbbmon --help
bbbmon -h
```
......@@ -11,7 +11,7 @@ from typing import NewType, Optional, Tuple, Iterable, List
# Local module imports
from bbbmon.xmldict import XmlListConfig, XmlDictConfig
from bbbmon.configuration import Config, Endpoint, SERVER_PROPERTIES_FILE, Url, Secret, get_user_config_path
from bbbmon.configuration import Config, Endpoint, SERVER_PROPERTIES_FILE, Url, Secret, get_user_config_path, init_config, new_config
......@@ -23,17 +23,6 @@ FRIENDLY_KEYNAMES = {
"moderatorCount": "Number of Moderators"
}
EXAMPLE_CONFIG = """[myservername]
securitySalt=YOURSUPERSECRETSECRET
bigbluebutton.web.serverURL=https://bbb.example.com/
; You can add, however, multiple Endpoints:
; [mysecondservername]
; securitySalt=ANOTHERSECRET
; bigbluebutton.web.serverURL=https://bbb.test.com/
"""
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
......@@ -265,51 +254,6 @@ def print_overview(config: Config, leaderboards: bool, participants: bool, prese
print_duration_leaderboard(meeting, endpoint.name, presenter, presenter_id, fancy)
def init_config() -> Optional[Config]:
"""
Read the config either from the servers bigbluebutton.properties-file or from
the user config path. Display a message if neither of these files exist.
"""
# Get OS dependend properties file
user_config_path = get_user_config_path()
# Check if we are on the server and try to read that properties file first
if os.path.isfile(SERVER_PROPERTIES_FILE):
return Config().from_server()
elif os.path.isfile(user_config_path):
return Config().from_config(user_config_path)
else:
new_config(user_config_path)
def new_config(user_config_path: str):
click.echo("{} There was no config file found. Make sure it exists and is readable at either location:".format(click.style('Error:', fg='red', bold=True), click.style("Error:", fg='red', bold=True)))
print(" [0] {}".format(SERVER_PROPERTIES_FILE))
print(" [1] {}".format(user_config_path))
print()
print("For now the file just needs to contain three lines:")
for line in EXAMPLE_CONFIG.splitlines():
click.echo(click.style((line), fg="bright_black"))
print()
if click.confirm(click.style('Do you want to create a config file at {}?'.format(user_config_path), fg="green"), abort=True):
# Create all directories in the path to the config, if they don't exist yet
try:
os.makedirs(user_config_path.rstrip("bbbmon.properties"))
except FileExistsError:
pass
# Write default config
with open(user_config_path, "w") as f:
for line in EXAMPLE_CONFIG.splitlines():
f.write("{}\n".format(line))
# Open with standard editor
click.edit(filename=user_config_path)
exit()
@click.group(context_settings=CONTEXT_SETTINGS)
def main():
"""BBBMON is a small CLI utility to monitor bbb usage
......@@ -318,7 +262,7 @@ def main():
Examples: bbbmon config --edit
bbbmon meetings --watch 20 --endpoint bbb
Internally bbbmon relies on the offical bbb-API, which means you need to have the server's secret in order to create a valid request.
Internally bbbmon relies on the offical bbb-API, which means you need to have the server's secret in order to create a valid request. Create a new configuration with: bbbmon config --new
"""
pass
......@@ -347,20 +291,37 @@ def meetings(ctx, leaderboards, participants, presenter, watch, presenter_id, me
@main.command(context_settings=CONTEXT_SETTINGS)
@click.pass_context
@click.option('--new', is_flag=True, help="Create a new default config and open it in the default editor")
@click.option('--edit', is_flag=True, help="Open the config in the default editor")
@click.option('--print', 'print_', is_flag=True, help="Print the config to stdout")
@click.option('--path', is_flag=True, help="Print the path to the config")
def config(ctx, edit, path, print_):
"""Print, show or edit the config"""
def config(ctx, new, edit, path, print_):
"""Print, show or edit the config
Example: bbbmon config --new
"""
user_config_path = get_user_config_path()
if edit:
click.edit(filename=user_config_path)
if os.path.isfile(user_config_path):
click.edit(filename=user_config_path)
else:
new_config(user_config_path)
elif path:
print(get_user_config_path())
elif print_:
with open(get_user_config_path(), "r") as f:
print(f.read())
elif new:
if os.path.isfile(user_config_path):
click.echo("{} There is already a config file at {} !".format(click.style(" WARNING ", bg="bright_red", fg="black", bold=True), user_config_path))
if click.confirm(click.style('Do you want to edit it with your default editor instead of overwriting it?', fg="yellow")):
click.edit(filename=user_config_path)
else:
if click.confirm(click.style('Do you want to overwrite it with the default config instead?', fg="red"), abort=True):
new_config(user_config_path, skip_prompt=True)
else:
new_config(user_config_path)
else:
ctx = click.get_current_context()
click.echo(ctx.get_help())
......
......@@ -3,6 +3,7 @@
import configparser
from typing import NewType, Optional, Tuple, Iterable, List
import click
import os
# Default path
SERVER_PROPERTIES_FILE = "/usr/share/bbb-web/WEB-INF/classes/bigbluebutton.properties"
......@@ -13,13 +14,16 @@ Secret = NewType('Secret', str)
Url = NewType('Url', str)
def get_user_config_path() -> str:
"""
Return the user config path
"""
user_config_path = click.get_app_dir("bbbmon")
user_config_path = "{}.properties".format(user_config_path)
return user_config_path
EXAMPLE_CONFIG = """[myservername]
securitySalt=YOURSUPERSECRETSECRET
bigbluebutton.web.serverURL=https://bbb.example.com/
; You can add, however, multiple Endpoints:
; [mysecondservername]
; securitySalt=ANOTHERSECRET
; bigbluebutton.web.serverURL=https://bbb.test.com/
"""
class Config():
......@@ -118,4 +122,64 @@ class Endpoint():
.lstrip("https://")\
.rstrip("/bigbluebutton")
else:
self.name = name
\ No newline at end of file
self.name = name
def init_config() -> Optional[Config]:
"""
Read the config either from the servers bigbluebutton.properties-file or from
the user config path. Display a message if neither of these files exist.
"""
# Get OS dependend properties file
user_config_path = get_user_config_path()
# Check if we are on the server and try to read that properties file first
if os.path.isfile(SERVER_PROPERTIES_FILE):
return Config().from_server()
elif os.path.isfile(user_config_path):
return Config().from_config(user_config_path)
else:
new_config(user_config_path)
def new_config(user_config_path: str, skip_prompt: bool=True):
"""
Warn users that there was no config file found, and then ask them if they want to create one.
The skip_prompt flag can be used to invoke this without asking or printing (e.g.
if asking and printing should be done elsewhere)
"""
if not skip_prompt:
click.echo("{} There was no config file found. Make sure it exists and is readable at either location:".format(click.style('Error:', fg='red', bold=True), click.style("Error:", fg='red', bold=True)))
print(" [0] {}".format(SERVER_PROPERTIES_FILE))
print(" [1] {}".format(user_config_path))
print()
print("For now the file just needs to contain three lines:")
for line in EXAMPLE_CONFIG.splitlines():
click.echo(click.style((line), fg="bright_black"))
print()
if skip_prompt or click.confirm(click.style('Do you want to create a config file at {}?'.format(user_config_path), fg="green"), abort=True):
# Create all directories in the path to the config, if they don't exist yet
try:
os.makedirs(user_config_path.rstrip("bbbmon.properties"))
except FileExistsError:
pass
# Write default config
with open(user_config_path, "w") as f:
for line in EXAMPLE_CONFIG.splitlines():
f.write("{}\n".format(line))
# Open with standard editor
click.edit(filename=user_config_path)
exit()
def get_user_config_path() -> str:
"""
Return the user config path
"""
user_config_path = click.get_app_dir("bbbmon")
user_config_path = "{}.properties".format(user_config_path)
return user_config_path
\ No newline at end of file
[tool.poetry]
name = "bbbmon"
version = "0.1.11"
version = "0.1.12"
description = "A small CLI utility to monitor bbb usage"
authors = ["David Huss <david.huss@hfbk-hamburg.de>"]
maintainers = ["David Huss <david.huss@hfbk-hamburg.de>"]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment