Test config setup

This commit is contained in:
Jordan Bradford
2024-02-02 22:52:16 -05:00
parent d292457803
commit 78d4b7fbc4
3 changed files with 158 additions and 12 deletions

View File

@@ -1 +1,142 @@
file:///home/bradfojb/Desktop/Personal-Repos/the-littlest-jupyterhub/schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Littlest JupyterHub YAML config file",
"definitions": {
"Users": {
"type": "object",
"properties": {
"extra_user_groups": {
"type": "object",
"items": {
"type": "string"
}
},
"allowed": {
"type": "array",
"items": {
"type": "string"
}
},
"banned": {
"type": "array",
"items": {
"type": "string"
}
},
"admin": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"HTTP": {
"type": "object",
"additionalProperties": false,
"properties": {
"address": {
"type": "string",
"format": "ipv4"
},
"port": {
"type": "integer"
}
}
},
"HTTPS": {
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean"
},
"address": {
"type": "string",
"format": "ipv4"
},
"port": {
"type": "integer"
},
"tls": {
"$ref": "#/definitions/TLS"
},
"letsencrypt": {
"$ref": "#/definitions/LetsEncrypt"
}
}
},
"LetsEncrypt": {
"type": "object",
"additionalProperties": false,
"properties": {
"email": {
"type": "string",
"format": "email"
},
"domains": {
"type": "array",
"items": {
"type": "string",
"format": "hostname"
}
}
}
},
"TLS": {
"type": "object",
"additionalProperties": false,
"properties": {
"key": {
"type": "string"
},
"cert": {
"type": "string"
}
},
"required": ["key", "cert"]
},
"Limits": {
"description": "User CPU and memory limits.",
"type": "object",
"additionalProperties": false,
"properties": {
"memory": {
"type": "string"
},
"cpu": {
"type": "integer"
}
}
},
"UserEnvironment": {
"type": "object",
"additionalProperties": false,
"properties": {
"default_app": {
"type": "string",
"enum": ["jupyterlab", "classic"],
"default": "jupyterlab"
}
}
}
},
"properties": {
"additionalProperties": false,
"user_environment": {
"$ref": "#/definitions/UserEnvironment"
},
"users": {
"$ref": "#/definitions/Users"
},
"limits": {
"$ref": "#/definitions/Limits"
},
"https": {
"$ref": "#/definitions/HTTPS"
},
"http": {
"$ref": "#/definitions/HTTP"
}
}
}

View File

@@ -155,10 +155,18 @@ def remove_item_from_config(config, property_path, value):
def validate_config(config):
import json
import jsonschema
config_schema = json.load("config-schema.json")
jsonschema.validate(instance=config, schema=config_schema)
import json, jsonschema
cwd = os.getcwd()
config_schema_file = os.path.join(cwd, "config-schema.json")
with open(config_schema_file) as f:
config_schema = json.load(f)
try:
jsonschema.validate(instance=config, schema=config_schema)
except jsonschema.exceptions.ValidationError as e:
print(e)
exit()
def show_config(config_path):
@@ -179,14 +187,13 @@ def set_config_value(config_path, key_path, value):
Set key at key_path in config_path to value
"""
# FIXME: Have a file lock here
# FIXME: Validate schema here
try:
with open(config_path) as f:
config = yaml.load(f)
except FileNotFoundError:
config = {}
config = set_item_in_config(config, key_path, value)
validate_config(config)
with open(config_path, "w") as f:
@@ -198,7 +205,6 @@ def unset_config_value(config_path, key_path):
Unset key at key_path in config_path
"""
# FIXME: Have a file lock here
# FIXME: Validate schema here
try:
with open(config_path) as f:
config = yaml.load(f)
@@ -206,6 +212,7 @@ def unset_config_value(config_path, key_path):
config = {}
config = unset_item_from_config(config, key_path)
validate_config(config)
with open(config_path, "w") as f:
yaml.dump(config, f)
@@ -216,7 +223,6 @@ def add_config_value(config_path, key_path, value):
Add value to list at key_path
"""
# FIXME: Have a file lock here
# FIXME: Validate schema here
try:
with open(config_path) as f:
config = yaml.load(f)
@@ -224,6 +230,7 @@ def add_config_value(config_path, key_path, value):
config = {}
config = add_item_to_config(config, key_path, value)
validate_config(config)
with open(config_path, "w") as f:
yaml.dump(config, f)
@@ -234,7 +241,6 @@ def remove_config_value(config_path, key_path, value):
Remove value from list at key_path
"""
# FIXME: Have a file lock here
# FIXME: Validate schema here
try:
with open(config_path) as f:
config = yaml.load(f)
@@ -242,6 +248,7 @@ def remove_config_value(config_path, key_path, value):
config = {}
config = remove_item_from_config(config, key_path, value)
validate_config(config)
with open(config_path, "w") as f:
yaml.dump(config, f)

View File

@@ -17,8 +17,6 @@ jupyterhub-tmpauthenticator>=1.0.0,<2
oauthenticator[azuread]>=16.0.4,<17
jupyterhub-idle-culler>=1.2.1,<2
jsonschema
# pycurl is installed to improve reliability and performance for when JupyterHub
# makes web requests. JupyterHub will use tornado's CurlAsyncHTTPClient when
# making requests over tornado's SimpleHTTPClient automatically if pycurl is