Skip to content

Commit ce25a42

Browse files
author
Mike Storey
committed
Containerization working!
1 parent d91ed75 commit ce25a42

33 files changed

+365
-833
lines changed

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ Thumbs.db
5151

5252
# Project specific
5353
tests/
54-
docs/
5554
*.egg-info/
5655
.pytest_cache/
5756
.coverage

.github/workflows/docker-push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
context: .
3636
platforms: linux/amd64,linux/arm64
3737
push: true
38-
tags: ghcr.io/agile-learning-institute/stage0_mongodb_api:latest
38+
tags: ghcr.io/agile-learning-institute/mongodb_configurator_api:latest

Dockerfile

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
1-
# Stage 1: Build stage
2-
FROM python:3.12-slim as build
1+
# Use Python 3.12 slim image
2+
FROM python:3.12-slim
33

4-
# Set the working directory in the container
5-
WORKDIR /app
4+
# Set the working directory
5+
WORKDIR /opt/mongo_configurator
66

7-
# Copy the entire context to the container
8-
COPY . .
7+
# Install system dependencies
8+
RUN apt-get update && apt-get install -y --no-install-recommends \
9+
gcc \
10+
&& rm -rf /var/lib/apt/lists/*
911

10-
# Get the current Git branch and build time
11-
RUN DATE=$(date +'%Y%m%d-%H%M%S') && \
12-
echo "${DATE}" > /app/BUILT_AT
12+
# Copy dependency files first for better caching
13+
COPY Pipfile Pipfile.lock ./
1314

14-
# Stage 2: Production stage
15-
FROM python:3.12-slim
15+
# Install pipenv
16+
RUN pip install pipenv
1617

17-
# Set the working directory in the container
18-
WORKDIR /opt/stage0_mongodb_api
18+
# Copy application code
19+
COPY . .
1920

20-
# Copy the entire source code and the BUILT_AT file from the build stage
21-
COPY --from=build /app/ /opt/stage0_mongodb_api/
21+
# Install dependencies
22+
RUN pipenv install --deploy --system
2223

23-
# Install pipenv and dependencies
24-
COPY Pipfile Pipfile.lock /opt/stage0_mongodb_api/
25-
RUN pip install pipenv && pipenv install --deploy --system
24+
# Create build timestamp
25+
RUN echo $(date +'%Y%m%d-%H%M%S') > /opt/mongo_configurator/BUILT_AT
2626

27-
# Install Gunicorn for running the Flask app in production
27+
# Install Gunicorn for production
2828
RUN pip install gunicorn
2929

30-
# Expose the port the app will run on
30+
# Create non-root user for security
31+
RUN useradd --create-home --shell /bin/bash app && \
32+
chown -R app:app /opt/mongo_configurator
33+
34+
# Switch to non-root user
35+
USER app
36+
37+
# Expose the port
3138
EXPOSE 8081
3239

33-
# Set Environment Variables
34-
ENV PYTHONPATH=/opt/stage0_mongodb_api/stage0_mongodb_api
35-
ENV MONGODB_API_PORT=8081
40+
# Set environment variables
41+
ENV PYTHONPATH=/opt/mongo_configurator/configurator
42+
ENV API_PORT=8081
3643

37-
# Command to run the application using Gunicorn
38-
CMD exec gunicorn --bind 0.0.0.0:${MONGODB_API_PORT} --timeout 120 --preload stage0_mongodb_api.server:app
44+
# Command to run the application
45+
CMD ["gunicorn", "--bind", "0.0.0.0:8081", "--timeout", "10", "--preload", "configurator.server:app"]

Pipfile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ local = "sh -c 'PYTHONPATH=$(pwd)/configurator BUILT_AT=Local LOAD_TEST_DATA=Tru
88
debug = "sh -c 'PYTHONPATH=$(pwd)/configurator BUILT_AT=Local LOAD_TEST_DATA=True LOGGING_LEVEL=DEBUG python3 -m server'"
99
batch = "sh -c 'PYTHONPATH=$(pwd)/configurator AUTO_PROCESS=True EXIT_AFTER_PROCESSING=True LOAD_TEST_DATA=True python3 -m server'"
1010
test = "sh -c 'PYTHONPATH=$(pwd)/configurator LOGGING_LEVEL=CRITICAL coverage run -m unittest discover -s ./tests -p \"test_*.py\" -v && echo \"\\n=== COVERAGE REPORT ===\" && coverage report && coverage html'"
11-
stepci-observability = "stepci run ./tests/stepci/observability.yaml"
12-
stepci-small = "stepci run ./tests/stepci/small_sample.yaml"
13-
stepci-large = "stepci run ./tests/stepci/large_sample.yaml"
14-
build = "docker build --tag ghcr.io/agile-learning-institute/mongodb_configurator_api:latest ."
15-
service = "sh -c 'pipenv run down && docker compose --profile configurator up --detach && echo `Visit http://localhost:8082/`'"
11+
stepci = "stepci run ./tests/stepci/workflow.yaml"
12+
container = "docker build --tag ghcr.io/agile-learning-institute/mongodb_configurator_api:latest ."
1613
database = "sh -c 'pipenv run down && docker compose --profile mongodb up --detach'"
14+
api = "sh -c 'pipenv run down && docker compose --profile configurator-api up --detach'"
15+
service = "sh -c 'pipenv run down && docker compose --profile configurator up --detach && echo `Visit http://localhost:8082/`'"
1716
down = "docker compose down mongodb configurator_api configurator_spa"
1817

1918
[packages]
@@ -22,7 +21,6 @@ flask = "*"
2221
prometheus-flask-exporter = "*"
2322
pymongo = "*"
2423
pyyaml = ">=6.0.1"
25-
configurator = {file = ".", editable = true}
2624

2725
[dev-packages]
2826
coverage = "*"

Pipfile.lock

Lines changed: 1 addition & 784 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This project builds a the [MongoDB Configurator](https://github.com/agile-learni
99
- [Python](https://www.python.org/downloads/) 3.12 or later
1010
- [Pipenv](https://pipenv.pypa.io/en/latest/installation.html)
1111
- [Docker Desktop](https://github.com/agile-learning-institute/stage0/tree/main/developer_edition)
12-
- [MongoDB Compass]() *optional*
12+
- [MongoDB Compass](https://www.mongodb.com/products/compass) *optional*
1313

1414
### Quick Start
1515
```bash
@@ -47,16 +47,16 @@ pipenv install --dev
4747
# Run Unit Tests and generate coverage report
4848
pipenv run test
4949

50-
# Drop the Testing Database - Live - Real Drop Database!!!
51-
pipenv run drop
52-
5350
#####################
5451
# Running test server - uses INPUT_FOLDER setting#
5552
pipenv run database # Start the backing mongo database
5653
pipenv run local # Start the server locally
5754
pipenv run debug # Start locally with DEBUG logging
5855
pipenv run batch # Run locally in Batch mode (process and exit)
5956

57+
# Drop the Testing Database - Live - Real Drop Database!!!
58+
pipenv run drop
59+
6060
#####################
6161
# Building and Testing the container (before a PR)
6262
pipenv run build # Build the container
@@ -65,10 +65,11 @@ pipenv run service # Run the DB, API, and SPA containers
6565

6666
pipenv run down # Stops all testing containers
6767

68-
#####################
69-
# Black Box Testing #
70-
pipenv run stepci-observe # Observability endpoints
71-
pipenv run stepci-<type> # [configurations, dictionaries, types, test_data, enumerators]
68+
################################
69+
# Black Box Testing with StepCI
70+
# https://github.com/stepci/stepci/blob/main/README.md
71+
export INPUT_FOLDER=./tests/test_cases/stepci
72+
pipenv run stepci
7273

7374
```
7475

@@ -116,16 +117,17 @@ tests/
116117
├── test_cases/ # Test data
117118
│ ├── small_sample/ # Simple test configuration
118119
│ ├── large_sample/ # Complex test configuration
119-
│ ├── empty_input/ # Load Error testing
120+
│ ├── stepci/ # For step ci testing
120121
│ ├── sample_template/ # Configuration for Template
121-
│ ├── playground/ # Served with Stack for UI testing
122+
│ ├── playground/ # Served with Stack for interactive UI testing
122123
│ ├── .../ # Additional test cases
123124
```
124-
the unit tests TestConfigurationIntegration and TestTypeRendering are integration tests that use the input folders in test_cases.
125+
the unit tests TestConfigurationIntegration and TestTypeRendering are integration tests that use the small_sample and large_sample input folders in test_cases.
125126

126127
## API Documentation
127128

128129
The complete API documentation with interactive testing is available:
130+
- [API Server docs/index.html](http://localhost:8081/docs/index.html) if the API is running
129131
- GoLive on [index.html](./docs/index.html)
130132

131133
The Swagger UI provides:
@@ -149,7 +151,7 @@ curl -X GET http://localhost:8081/api/configurations/
149151
# Process all configurations
150152
curl -X POST http://localhost:8081/api/configurations/
151153

152-
# Clean all types
154+
# Lock all types
153155
curl -X PATCH http://localhost:8081/api/types/
154156
```
155157
---

docker-compose.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ services:
1111
extra_hosts:
1212
- "mongodb:127.0.0.1"
1313
healthcheck:
14-
test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'mongodb:27017'}]}) }" | mongosh --port 27017 --quiet
14+
test: echo "db.runCommand('ping')" | mongosh --port 27017 --quiet
1515
interval: 5s
1616
timeout: 30s
1717
start_period: 0s
18-
retries: 30
19-
command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
18+
retries: 3
19+
command: ["--bind_ip_all", "--port", "27017"]
2020
profiles:
2121
- mongodb
2222
- configurator
@@ -38,7 +38,7 @@ services:
3838
API_PORT: 8081
3939
SPA_PORT: 8082
4040
volumes:
41-
- ${INPUT_FOLDER:-./tests/test_cases/large_sample}:/input
41+
- ${INPUT_FOLDER:-./tests/test_cases/playground}:/input
4242
depends_on:
4343
mongodb:
4444
condition: service_healthy
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Local
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

0 commit comments

Comments
 (0)