From 2271e3f154824286401b5bdf74be931477f13d49 Mon Sep 17 00:00:00 2001
From: James Kirk <james.kirk@noc.ac.uk>
Date: Thu, 9 Feb 2023 15:06:19 +0000
Subject: [PATCH] refactor: updated how run_compose rebuilds and the container
 mount fs

---
 docker/Dockerfile          |  3 ++-
 docker/docker-compose.yaml |  8 ++++++--
 endpoints/auth_resource.py |  2 +-
 endpoints/clients.py       |  2 +-
 endpoints/token.py         |  2 +-
 run-compose.sh             | 20 +++++++++++++++++++-
 6 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/docker/Dockerfile b/docker/Dockerfile
index 72e3dbc..606b939 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -1,7 +1,8 @@
 FROM python:alpine3.17
 
+WORKDIR /app
+
 COPY requirements.txt requirements.txt
 RUN pip install -r requirements.txt
-COPY . .
 
 ENTRYPOINT [ "python" ]
\ No newline at end of file
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index 8e282ab..a76b1f7 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -19,7 +19,10 @@ services:
       - rabbitmq__local
     environment:
       - MQ_HOST=rmq
+    volumes:
+      - ../:/app
     command: "soar_bus.py"
+    container_name: soar_bus
 
   soar_api:
     build:
@@ -35,5 +38,6 @@ services:
     environment:
       - MQ_HOST=rmq
     volumes:
-      - ${DATA_DIR}/data:${DATA_DIR}/data
-    command: "api.py"
\ No newline at end of file
+      - ../:/app
+    command: "api.py"
+    container_name: soar_api
\ No newline at end of file
diff --git a/endpoints/auth_resource.py b/endpoints/auth_resource.py
index ef1bbf9..6429a20 100644
--- a/endpoints/auth_resource.py
+++ b/endpoints/auth_resource.py
@@ -10,7 +10,7 @@ class AuthResource(Resource):
 
     def __init__(self): 
         self.token = TokenModel()
-        with open("/data/clients.json", "r") as clients_file:
+        with open("./data/clients.json", "r") as clients_file:
             self.clients = json.load(clients_file)
 
     def auth(self, request): 
diff --git a/endpoints/clients.py b/endpoints/clients.py
index 7698b8b..659080b 100644
--- a/endpoints/clients.py
+++ b/endpoints/clients.py
@@ -13,7 +13,7 @@ class ClientSchema(Schema):
 
 
 class ClientsFile:
-    file = "/data/clients.json"
+    file = "./data/clients.json"
     mtime = 0
     clients = {}
     parser = None
diff --git a/endpoints/token.py b/endpoints/token.py
index 9448bf8..077ce5a 100644
--- a/endpoints/token.py
+++ b/endpoints/token.py
@@ -19,7 +19,7 @@ class Token(Resource):
         self.schema = TokenQuerySchema()
         self.model = TokenModel()
         data_dir = os.getenv("DATA_DIR", "/data")
-        with open(data_dir + "/clients.json", "r") as clients_file:
+        with open("./data/clients.json", "r") as clients_file:
             self.clients = json.load(clients_file)
     
     def get(self):
diff --git a/run-compose.sh b/run-compose.sh
index a5b16a0..6b8bfad 100755
--- a/run-compose.sh
+++ b/run-compose.sh
@@ -3,8 +3,26 @@
 set -a
 source .env
 
+usage() {
+    echo "usage: ./run-compose.sh [<rebuild>]"
+    echo "    rebuild - will force docker-compose to rebuild the images before spinning them up."
+}
+
 if [[ -z "${DATA_DIR}" ]]; then
     DATA_DIR=$(pwd)
 fi
 
-docker-compose -f docker/docker-compose.yaml up --build
\ No newline at end of file
+while [ -n "$1" ]; do
+    case $1 in
+    rebuild)
+        EXTRA_ARGS="--build"
+        ;;
+    *)
+        usage
+        exit 0
+        ;;
+    esac
+    shift
+done
+
+docker-compose -f docker/docker-compose.yaml up $EXTRA_ARGS
\ No newline at end of file
-- 
GitLab