Repository navigation
Expand file tree
/
Copy pathstartup.sh
More file actions
151 lines (126 loc) · 4.96 KB
/
Copy pathstartup.sh
File metadata and controls
151 lines (126 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
set -euo pipefail
# Environment setup
source .env
# Project root directory
PROJECT_ROOT=$(dirname $(readlink -f "$0"))
# Log file location
LOG_FILE="${PROJECT_ROOT}/logs/startup.log"
# PID file location
PID_FILE="${PROJECT_ROOT}/logs/app.pid"
# Service timeouts
TIMEOUT_SECONDS=30
# Health check intervals
CHECK_INTERVAL_SECONDS=1
# Utility functions
log_info() {
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "${timestamp} - INFO - $@"
}
log_error() {
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "${timestamp} - ERROR - $@" >&2
}
cleanup() {
log_info "Cleaning up..."
if [ -f "${PID_FILE}" ]; then
kill -9 $(cat "${PID_FILE}")
fi
rm -f "${PID_FILE}"
}
check_dependencies() {
log_info "Checking dependencies..."
# Add dependency checks here, for example:
# command -v python >/dev/null 2>&1 || log_error "Python not found"
}
# Health checks
check_port() {
local port="$1"
local host="localhost"
nc -z "$host" "$port" >/dev/null 2>&1
}
wait_for_service() {
local port="$1"
local timeout="$2"
local elapsed=0
log_info "Waiting for service on port $port..."
while [[ "$elapsed" -lt "$timeout" ]]; do
if check_port "$port"; then
log_info "Service started on port $port."
return 0
fi
sleep "$CHECK_INTERVAL_SECONDS"
elapsed=$((elapsed + CHECK_INTERVAL_SECONDS))
done
log_error "Timeout waiting for service on port $port."
return 1
}
verify_service() {
# Add service health checks here, for example:
# curl -s "http://localhost:8000/healthcheck" >/dev/null 2>&1
}
# Service management
start_database() {
log_info "Starting database..."
# Replace with your database start command:
sudo pg_ctl -D /var/lib/postgresql/data -l logfile start
wait_for_service "5432" "$TIMEOUT_SECONDS" || exit 1
verify_service
}
start_backend() {
log_info "Starting backend server..."
# Replace with your backend server start command:
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
store_pid
wait_for_service "8000" "$TIMEOUT_SECONDS" || exit 1
verify_service
}
start_frontend() {
log_info "Starting frontend server..."
# Replace with your frontend server start command:
# This assumes you are using a Node.js application.
# Adjust the command if you are using a different technology.
npm run dev
store_pid
wait_for_service "3000" "$TIMEOUT_SECONDS" || exit 1
verify_service
}
store_pid() {
log_info "Saving process ID..."
echo $$ > "${PID_FILE}"
}
# Main execution flow
check_dependencies
start_database
start_backend
start_frontend
log_info "Startup complete."
# Cleanup on exit
trap cleanup EXIT ERR
```
**Explanation:**
1. **Shebang Line:** The script begins with `#!/bin/bash`, indicating that it's a Bash script.
2. **Strict Error Handling:** `set -euo pipefail` sets strict error handling to ensure the script exits immediately on any errors.
3. **Environment Setup:**
- The script sources the `.env` file to load environment variables.
- The `PROJECT_ROOT` variable is set to the script's directory for easy file path management.
- Log file and PID file locations are defined.
- Service timeouts and health check intervals are set.
4. **Functions:**
- `log_info` and `log_error` are defined to handle logging messages with timestamps.
- `cleanup` is defined to remove PID files and stop services on script exit.
- `check_dependencies` is defined to check for required dependencies (you should add your specific dependency checks here).
- `check_port` checks if a given port is available on the host machine.
- `wait_for_service` polls a specific port until a service becomes available or a timeout is reached.
- `verify_service` is defined to perform service health checks (add your specific health checks here).
- `start_database` starts the database service (replace the command with your actual database startup command).
- `start_backend` starts the backend server (replace the command with your actual backend startup command).
- `start_frontend` starts the frontend server (replace the command with your actual frontend startup command).
- `store_pid` saves the process ID of the currently running service to a PID file.
5. **Main Execution Flow:**
- The `check_dependencies` function is called to verify that all required tools are present.
- The `start_database`, `start_backend`, and `start_frontend` functions are called in sequence to start the services.
- After the services are started, a "Startup complete." message is logged.
6. **Cleanup on Exit:**
- The `trap` command sets cleanup handlers for the `EXIT` and `ERR` signals. This ensures that the `cleanup` function is called when the script exits normally or due to an error.
This script provides a robust and flexible framework for starting your MVP project. You can modify it to include specific dependencies, service start commands, and health check procedures based on your actual project requirements. Remember to test the script thoroughly in your development environment before deploying it to production.