Skip to content

Commit ea24b7a

Browse files
committed
2 parents 55ed486 + 1708f23 commit ea24b7a

47 files changed

Lines changed: 4354 additions & 170 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Dockerfile

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,37 @@ ENV PYTHONUNBUFFERED 1
77
# Set work directory
88
WORKDIR /app
99

10-
# Install system dependencies
10+
# Install system dependencies including development tools
1111
RUN apt-get update && apt-get install -y --no-install-recommends \
1212
build-essential \
13+
curl \
14+
vim \
15+
git \
16+
postgresql-client \
1317
&& rm -rf /var/lib/apt/lists/*
1418

1519
# Install Python dependencies
1620
COPY requirements.txt .
1721
RUN pip install --upgrade pip
1822
RUN pip install --no-cache-dir -r requirements.txt
1923

20-
# Copy project
21-
COPY . .
24+
# Install additional development dependencies
25+
RUN pip install --no-cache-dir \
26+
ipython \
27+
ipdb \
28+
django-debug-toolbar \
29+
django-extensions
2230

23-
# Create necessary directories
31+
# Create necessary directories first
2432
RUN mkdir -p /app/static /app/media
2533

26-
# Run gunicorn
27-
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "core.wsgi"]
34+
# Database population is handled by Django management command in docker-compose
35+
36+
# Copy project (this will be overridden by volume mount in development)
37+
COPY . .
38+
39+
# Ensure .env file exists for SECRET_KEY persistence
40+
RUN touch /app/.env
41+
42+
# Development command - will be overridden by docker-compose
43+
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000", "--insecure"]

Dockerfile.prod

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM python:3.9
2+
3+
# set environment variables
4+
ENV PYTHONDONTWRITEBYTECODE 1
5+
ENV PYTHONUNBUFFERED 1
6+
7+
# Set work directory
8+
WORKDIR /app
9+
10+
# Install system dependencies
11+
RUN apt-get update && apt-get install -y --no-install-recommends \
12+
build-essential \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
# Install Python dependencies
16+
COPY requirements.txt .
17+
RUN pip install --upgrade pip
18+
RUN pip install --no-cache-dir -r requirements.txt
19+
20+
# Copy project
21+
COPY . .
22+
23+
# Create necessary directories
24+
RUN mkdir -p /app/static /app/media
25+
26+
# Run gunicorn
27+
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "core.wsgi"]

Scripts/Auto-Setup.sh

100644100755
Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
APP_NAME="Hardhat Enterprises Web App"
44
ENV_FILE=".env"
55
ENV_SAMPLE_FILE="env.sample"
6+
7+
# Default to development mode
8+
MODE="dev"
69
COMPOSE_FILE="docker-compose.yml"
7-
HEALTHCHECK_URL="http://localhost:80/health"
10+
HEALTHCHECK_URLS=("http://localhost:8000" "http://127.0.0.1:8000")
811

912
GREEN="\033[0;32m"
1013
RED="\033[0;31m"
@@ -15,16 +18,43 @@ show_help() {
1518
echo "Usage: $0 [OPTION]"
1619
echo
1720
echo " --dry-run Only perform environment checks"
18-
echo " --setup Run checks and start dockerized app"
21+
echo " --dev Run checks and start development environment (default)"
22+
echo " --prod Run checks and start production environment"
23+
echo " --setup Run checks and start dockerized app (legacy - uses dev mode)"
1924
echo " --help Show this help message"
25+
echo
26+
echo "Development mode (--dev):"
27+
echo " - Uses docker-compose.yml"
28+
echo " - Django dev server on port 8000"
29+
echo " - Nginx proxy on port 8080"
30+
echo " - PostgreSQL on port 5433"
31+
echo " - Health checks: http://localhost:8000, http://127.0.0.1:8000"
32+
echo
33+
echo "Production mode (--prod):"
34+
echo " - Uses docker-compose-prod.yml"
35+
echo " - Gunicorn server behind Nginx on port 80"
36+
echo " - PostgreSQL on port 5432"
37+
echo " - Health check: http://localhost"
2038
}
2139

2240
TICK="${GREEN}Passed${NC}"
2341
CROSS="${RED}Failed${NC}"
2442

43+
set_mode_config() {
44+
if [ "$MODE" = "prod" ]; then
45+
COMPOSE_FILE="docker-compose-prod.yml"
46+
HEALTHCHECK_URLS=("http://localhost")
47+
echo -e "${YELLOW}Production mode selected${NC}"
48+
else
49+
COMPOSE_FILE="docker-compose.yml"
50+
HEALTHCHECK_URLS=("http://localhost:8000" "http://127.0.0.1:8000")
51+
echo -e "${YELLOW}Development mode selected${NC}"
52+
fi
53+
}
54+
2555
show_table_header() {
2656
echo -e "\n======================================================"
27-
echo -e "${YELLOW} Running checks for \"$APP_NAME\"${NC}"
57+
echo -e "${YELLOW} Running checks for \"$APP_NAME\" ($MODE mode)${NC}"
2858
echo -e "======================================================\n"
2959
}
3060

@@ -58,13 +88,24 @@ check_file() {
5888
}
5989

6090
check_health() {
61-
if curl -s --head --request GET "$HEALTHCHECK_URL" | grep "200 OK" >/dev/null; then
62-
echo -e "Healthcheck endpoint ($HEALTHCHECK_URL) : $TICK"
63-
return 0
64-
else
65-
echo -e "Healthcheck endpoint ($HEALTHCHECK_URL) : $CROSS"
91+
local health_found=false
92+
local active_url=""
93+
94+
for url in "${HEALTHCHECK_URLS[@]}"; do
95+
if curl -s --head --request GET "$url" | grep "200 OK" >/dev/null; then
96+
echo -e "Health endpoint ($url) : $TICK"
97+
health_found=true
98+
active_url="$url"
99+
break
100+
fi
101+
done
102+
103+
if [ "$health_found" = false ]; then
104+
echo -e "Health endpoints (${HEALTHCHECK_URLS[*]}) : $CROSS"
66105
return 1
67106
fi
107+
108+
return 0
68109
}
69110

70111
run_checks() {
@@ -79,33 +120,78 @@ run_checks() {
79120

80121
check_file "$ENV_SAMPLE_FILE" "env.sample present"
81122
check_file "$ENV_FILE" ".env present"
82-
check_file "$COMPOSE_FILE" "docker-compose.yml present"
123+
124+
if [ "$MODE" = "prod" ]; then
125+
check_file "$COMPOSE_FILE" "docker-compose-prod.yml present"
126+
else
127+
check_file "$COMPOSE_FILE" "docker-compose.yml present"
128+
fi
83129

84130
check_health
85131
}
86132

87133
run_setup() {
88-
echo -e "\n ${YELLOW}Starting Docker containers...\n${NC}"
89-
docker-compose up --build -d
134+
echo -e "\n ${YELLOW}Starting Docker containers ($MODE mode)...\n${NC}"
135+
136+
if [ "$MODE" = "prod" ]; then
137+
docker-compose -f docker-compose-prod.yml up --build -d
138+
else
139+
docker-compose up --build -d
140+
fi
90141

91142
echo -e "\n ${YELLOW}Waiting for health endpoint...${NC}"
92-
sleep 5
143+
sleep 10
93144

94145
if check_health; then
95-
echo -e "\n${GREEN}Setup complete. Visit your app at http://localhost:80/health\n"
146+
if [ "$MODE" = "prod" ]; then
147+
echo -e "\n${GREEN}✅ Production setup complete!${NC}"
148+
echo -e "${GREEN}🌐 Application available at: http://localhost${NC}"
149+
echo -e "${GREEN}📊 Health check: http://localhost${NC}\n"
150+
else
151+
echo -e "\n${GREEN}✅ Development setup complete!${NC}"
152+
echo -e "${GREEN}🌐 Django app: http://localhost:8000 or http://127.0.0.1:8000${NC}"
153+
echo -e "${GREEN}🌐 Nginx proxy: http://localhost:8080${NC}"
154+
echo -e "${GREEN}📊 Health checks: http://localhost:8000, http://127.0.0.1:8000${NC}\n"
155+
fi
96156
else
97-
echo -e "\n${RED}Setup ran but app is not healthy. Please check logs using 'docker-compose logs'.${NC}\n"
157+
echo -e "\n${RED}❌ Setup ran but app is not healthy.${NC}"
158+
if [ "$MODE" = "prod" ]; then
159+
echo -e "${RED}Please check logs using 'docker-compose -f docker-compose-prod.yml logs'.${NC}\n"
160+
else
161+
echo -e "${RED}Please check logs using 'docker-compose logs'.${NC}\n"
162+
fi
98163
fi
99164
}
100165

166+
# Parse arguments
101167
case "$1" in
102-
--dry-run)
168+
--prod)
169+
MODE="prod"
170+
set_mode_config
171+
run_checks
172+
run_setup
173+
;;
174+
--dev)
175+
MODE="dev"
176+
set_mode_config
103177
run_checks
178+
run_setup
104179
;;
105180
--setup)
181+
# Legacy option - defaults to dev mode
182+
MODE="dev"
183+
set_mode_config
106184
run_checks
107185
run_setup
108186
;;
187+
--dry-run)
188+
# Use default mode for dry run, but allow override with second argument
189+
if [ "$2" = "--prod" ]; then
190+
MODE="prod"
191+
fi
192+
set_mode_config
193+
run_checks
194+
;;
109195
--help | *)
110196
show_help
111197
;;

Scripts/README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,26 @@ The Hardhat Enterprises Web App's environment setup and verification are automat
1616
### Commands and Usage
1717

1818
- **`--dry-run`**
19-
- Performs the checks related to enviornment without even starting the application.
20-
- Check and Verify the presence of `Git`, `Python 3`, `pip3`, `Docker`, `Docker Compose` (i.e. `.env`,`docker-compose.yaml` etc)
21-
- Checks the health endpoint located at `http://localhost:80/health`.
22-
- **Example**: `./Auto-setup.sh --dry-run`
19+
- Performs environment checks without starting the application.
20+
- Checks presence of `Git`, `Python 3`, `pip3`, `Docker`, `Docker Compose`, `.env`, and compose files.
21+
- Tests health endpoints based on selected mode.
22+
- **Example**: `./Auto-Setup.sh --dry-run` (development mode)
23+
- **Example**: `./Auto-Setup.sh --dry-run --prod` (production mode)
24+
- **`--dev`**
25+
- Runs checks and starts development environment (default mode).
26+
- Uses `docker-compose.yml` with Django dev server on port 8000 and Nginx on port 8080.
27+
- Includes development tools, debugging capabilities, and auto-population of test data.
28+
- **Example**: `./Auto-Setup.sh --dev`
29+
- **`--prod`**
30+
- Runs checks and starts production environment.
31+
- Uses `docker-compose-prod.yml` with Gunicorn server behind Nginx on port 80.
32+
- Optimised for performance with production-grade configuration.
33+
- **Example**: `./Auto-Setup.sh --prod`
2334
- **`--setup`**
24-
- Uses `docker-compose up --build -d` to launch the Docker containers and performs all environment checks.
25-
- Awaits confirmation from the health endpoint that the application is operating properly.
26-
- Feedback on setup success or failure is given, along with recommendations for log checks if necessary.
35+
- Legacy command that defaults to development mode for backwards compatibility.
2736
- **Example**: `./Auto-Setup.sh --setup`
2837
- **`--help`**
29-
- Displays a help message with the instructions and options available.
38+
- Displays help message with all available options and mode descriptions.
3039
- **Example**: `./Auto-Setup.sh --help`
3140

3241
## precommit.sh
@@ -83,10 +92,13 @@ The `secrets-scan.sh` script incorporates TruffleHog to detect secrets within fi
8392
- **Example**: `./secrets-scan.sh`
8493

8594
## harden_docker_script.sh
86-
The `harden_docker_script.sh` script evaluates Docker settings for security compliance, verifying adherence to best practices like minimal base images, non-root users, and appropriate cleanup procedures.
95+
The `harden_docker_script.sh` script evaluates Docker settings for security compliance, verifying adherence to best practices like minimal base images, non-root users, and appropriate cleanup procedures.
8796

8897
- **`compliance`**
89-
- Checks `Dockerfile`, `.dockerignore`, and `docker-compose.yml` for security compliance. - Validates the use of minimal base images (`python:3.12-slim`, `nginx:alpine`), non-root user configurations, dependency cleanup, and read-only > - Generates a compliance report with pass/fail results for each check.
98+
- Checks `Dockerfile`, `Dockerfile.prod`, `.dockerignore`, `docker-compose.yml`, and `docker-compose-prod.yml` for security compliance.
99+
- Validates development and production configurations separately.
100+
- Ensures proper environment separation between dev and prod modes.
101+
- Generates a compliance report with pass/fail results for each check.
90102
- **Example**: `./harden_docker_script.sh compliance`
91103
- **`compliance --images`**
92104
- Expands the `complianc` directive to encompass the examination of constructed Docker images (`django_app` and `nginx`).

0 commit comments

Comments
 (0)