Skip to content

Commit

Permalink
Adapt qlever ui command to new QLever UI config command
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannah Bast committed Feb 22, 2025
1 parent 165b46f commit e56ae29
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "qlever"
description = "Script for using the QLever SPARQL engine."
version = "0.5.18"
version = "0.5.19"
authors = [
{ name = "Hannah Bast", email = "[email protected]" }
]
Expand All @@ -20,7 +20,7 @@ classifiers = [
"Topic :: Database :: Front-Ends"
]

dependencies = [ "psutil", "termcolor", "argcomplete" ]
dependencies = [ "psutil", "termcolor", "argcomplete", "pyyaml" ]

[project.urls]
Github = "https://github.com/ad-freiburg/qlever"
Expand Down
91 changes: 78 additions & 13 deletions src/qlever/commands/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import subprocess
from os import environ
from pathlib import Path

import yaml

from qlever.command import QleverCommand
from qlever.containerize import Containerize
from qlever.log import log
from qlever.util import is_port_used
from qlever.util import is_port_used, run_command


class UiCommand(QleverCommand):
Expand Down Expand Up @@ -37,7 +40,18 @@ def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
}

def additional_arguments(self, subparser) -> None:
pass
subparser.add_argument(
"--ui-config-file",
help="Name of the config file for the QLever UI "
"(default: <name>.ui-config.yml)",
)
subparser.add_argument(
"--pull-latest",
type=str,
choices=["yes", "no"],
default="yes",
help="Pull the latest image of the QLever UI",
)

def execute(self, args) -> bool:
# If QLEVER_OVERRIDE_DISABLE_UI is set, this command is disabled.
Expand All @@ -59,24 +73,37 @@ def execute(self, args) -> bool:
log.info("")

# Construct commands and show them.
ui_config_name = args.name
ui_config_file = args.ui_config_file or f"{args.name}.ui-config.yml"
server_url = f"http://{args.host_name}:{args.port}"
ui_url = f"http://{args.host_name}:{args.ui_port}"
pull_cmd = f"{args.ui_system} pull -q {args.ui_image}"
run_cmd = (
get_config_cmd = (
f"{args.ui_system} exec -it "
f"{args.ui_image} "
f"bash -c \"python manage.py config {ui_config_name}\""
)
start_ui_cmd = (
f"{args.ui_system} run -d "
f"--publish {args.ui_port}:7000 "
f"--name {args.ui_container} "
f"{args.ui_image}"
)
exec_cmd = (
set_config_cmd = (
f"{args.ui_system} exec -it "
f"{args.ui_container} "
f'bash -c "python manage.py configure '
f'{args.ui_config} {server_url}"'
f"bash -c \"python manage.py config {ui_config_name} "
f"{ui_config_file} --hide-all-other-backends\""
)
self.show(
"\n".join(
["Stop running containers", pull_cmd, run_cmd, exec_cmd]
[
"Stop running containers",
pull_cmd,
get_config_cmd,
start_ui_cmd,
set_config_cmd,
]
),
only_show=args.show,
)
Expand All @@ -94,19 +121,57 @@ def execute(self, args) -> bool:
# Check if the UI port is already being used.
if is_port_used(args.ui_port):
log.warning(
f"It looks like the specified port for the UI ({args.ui_port}) is already in use. You can set another port in the Qleverfile in the [ui] section with the UI_PORT variable."
f"It looks like port {args.ui_port} for the QLever UI "
f"is already in use. You can set another port in the "
f" Qleverfile in the [ui] section with the UI_PORT variable."
)

# Try to start the QLever UI.
# Start the QLever UI.
try:
subprocess.run(pull_cmd, shell=True, stdout=subprocess.DEVNULL)
subprocess.run(run_cmd, shell=True, stdout=subprocess.DEVNULL)
subprocess.run(exec_cmd, shell=True, stdout=subprocess.DEVNULL)
if args.pull_latest == "yes":
run_command(pull_cmd)
run_command(start_ui_cmd)
except subprocess.CalledProcessError as e:
log.error(f"Failed to start the QLever UI ({e})")
return False

# Success.
# Check if config file with name `ui_config_file` exists. If not, try
# to obtain it via `get_config_cmd` and set it as default.
if Path(ui_config_file).exists():
log.info(f"Found config file `{ui_config_file}` for QLever UI ...")
else:
log.info(
f"Trying to obtain default config for `{ui_config_name}` ..."
)
try:
config_yaml = run_command(get_config_cmd, return_output=True)
config_dict = yaml.safe_load(config_yaml)
config_dict["config"]["backend"]["isDefault"] = True
config_dict["config"]["backend"]["baseUrl"] = server_url
config_dict["config"]["backend"]["sortKey"] = 1
except Exception as e:
log.error(
f"Neither found config file `{ui_config_file}` nor "
f"could obtain default config for `{ui_config_name}`"
)
log.info("")
log.info(f"Error message: {e}")
log.info("")
log.info(
"TODO: provide further instructions for this case "
"(obtain `default` config file, edit it, rename it, "
"then try again)"
)
return False

# Configure the QLever UI.
try:
run_command(set_config_cmd)
except subprocess.CalledProcessError as e:
log.error(f"Failed to configure the QLever UI ({e})")
return False

# If we come this far, everything should work.
log.info(
f"The QLever UI should now be up at {ui_url} ..."
f"You can log in as QLever UI admin with username and "
Expand Down

0 comments on commit e56ae29

Please sign in to comment.