Skip to content

Commit b0ebfc5

Browse files
authored
fix(start-stack): copy docker folder to user's home directory (DEV-1581) (#274)
1 parent 56b9e30 commit b0ebfc5

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ make install
2626
```
2727

2828

29+
30+
## User data in the folder `.dsp-tools`
31+
32+
DSP-TOOLS saves user data in the user's home directory, in the folder `.dsp-tools`. Here is an overview of its
33+
structure:
34+
35+
| folder | command using it | description |
36+
|:-----------|:-----------------|:---------------------------------------------|
37+
| xmluploads | `xmlupload` | saves id2iri mappings and error reports |
38+
| docker | `stack-up` | files necessary to startup Docker containers |
39+
40+
41+
42+
## The `start-stack` command
43+
44+
This command starts Docker containers of DSP-API and DSP-APP, in the version that is running on [https://admin.dasch.
45+
swiss](https://admin.dasch.swiss/help). In addition to the containers, a number of files from the DSP-API GitHub
46+
repository is necessary. The version of the docker images and these files must be the same. The version is hardcoded at the
47+
following places in the code:
48+
49+
- `knora/dsplib/docker/docker-compose.yml`: The 4 variables `services/{app,db,sipi,api}/image` must point to the
50+
DockerHub image of the last deployed version
51+
- `knora/dsplib/utils/stack_handling.py`: The variable `commit_of_used_api_version` must be the commit hash of DSP-API
52+
of the version that is running on [https://admin.dasch.swiss](https://admin.dasch.swiss/help).
53+
54+
55+
2956
## Git submodules
3057

3158
This repository embeds [https://github.com/dasch-swiss/0123-import-scripts](https://github.com/dasch-swiss/0123-import-scripts)

‎knora/dsplib/utils/stack_handling.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import importlib.resources
12
import re
3+
import shutil
24
import subprocess
35
import time
46
from pathlib import Path
@@ -8,8 +10,8 @@
810

911
from knora.dsplib.models.helpers import BaseError
1012

11-
# relative path to "knora/dsplib/docker", to make it accessible when dsp-tools is called from another working directory
12-
docker_path = Path(__file__).parent / Path("../docker")
13+
docker_path_of_user = Path.home() / Path(".dsp-tools/docker")
14+
docker_path_of_user.mkdir(parents=True, exist_ok=True)
1315

1416

1517
def start_stack(
@@ -33,6 +35,14 @@ def start_stack(
3335
if enforce_docker_system_prune and suppress_docker_system_prune:
3436
raise BaseError('The arguments "--prune" and "--no-prune" are mutually exclusive')
3537

38+
# copy contents of knora/dsplib/docker to ~/.dsp-tools/docker
39+
# rationale to use importlib.resources: https://setuptools.pypa.io/en/latest/userguide/datafiles.html#accessing-data-files-at-runtime
40+
docker_path_of_distribution = importlib.resources.files("knora").joinpath("dsplib").joinpath("docker")
41+
for file in docker_path_of_distribution.iterdir():
42+
dst = docker_path_of_user / file.name
43+
if not dst.is_file():
44+
shutil.copy(file, dst)
45+
3646
# get sipi.docker-config.lua
3747
commit_of_used_api_version = "3f44354df"
3848
url_prefix = f"https://github.com/dasch-swiss/dsp-api/raw/{commit_of_used_api_version}/"
@@ -42,11 +52,11 @@ def start_stack(
4252
if not re.search(max_post_size_regex, docker_config_lua_text):
4353
raise BaseError("Unable to set max_file_size. Please try again without this flag.")
4454
docker_config_lua_text = re.sub(max_post_size_regex, f"max_post_size = '{max_file_size}M'", docker_config_lua_text)
45-
with open(docker_path / "sipi.docker-config.lua", "w") as f:
55+
with open(docker_path_of_user / "sipi.docker-config.lua", "w") as f:
4656
f.write(docker_config_lua_text)
4757

4858
# start up the fuseki database
49-
completed_process = subprocess.run("docker compose up db -d", shell=True, cwd=docker_path)
59+
completed_process = subprocess.run("docker compose up db -d", shell=True, cwd=docker_path_of_user)
5060
if not completed_process or completed_process.returncode != 0:
5161
raise BaseError("Cannot start the API: Error while executing 'docker compose up db -d'")
5262

@@ -94,7 +104,7 @@ def start_stack(
94104
raise BaseError(f"Cannot start DSP-API: Error when creating graph '{graph}'")
95105

96106
# startup all other components
97-
subprocess.run("docker compose up -d", shell=True, cwd=docker_path)
107+
subprocess.run("docker compose up -d", shell=True, cwd=docker_path_of_user)
98108
print("DSP-API is now running on http://0.0.0.0:3333/ and DSP-APP on http://0.0.0.0:4200/")
99109

100110
# docker system prune
@@ -108,11 +118,11 @@ def start_stack(
108118
prune_docker = input("Allow dsp-tools to execute 'docker system prune'? This is necessary to keep your "
109119
"Docker clean. If you are unsure what that means, just type y and press Enter. [y/n]")
110120
if prune_docker == "y":
111-
subprocess.run("docker system prune -f", shell=True, cwd=docker_path)
121+
subprocess.run("docker system prune -f", shell=True, cwd=docker_path_of_user)
112122

113123

114124
def stop_stack() -> None:
115125
"""
116126
Shut down the Docker containers of your local DSP stack and delete all data that is in it.
117127
"""
118-
subprocess.run("docker compose down --volumes", shell=True, cwd=docker_path)
128+
subprocess.run("docker compose down --volumes", shell=True, cwd=docker_path_of_user)

0 commit comments

Comments
 (0)