-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·267 lines (231 loc) · 6.64 KB
/
run.sh
File metadata and controls
executable file
·267 lines (231 loc) · 6.64 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
# DevOps Portfolio Project - Setup and Run Script
# This script automates the setup and running of the DevOps portfolio project
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check prerequisites
check_prerequisites() {
print_status "Checking prerequisites..."
local missing_tools=()
if ! command_exists node; then
missing_tools+=("node")
fi
if ! command_exists npm; then
missing_tools+=("npm")
fi
if ! command_exists docker; then
missing_tools+=("docker")
fi
if ! command_exists docker-compose; then
missing_tools+=("docker-compose")
fi
if [ ${#missing_tools[@]} -ne 0 ]; then
print_error "Missing required tools: ${missing_tools[*]}"
echo "Please install the missing tools and run this script again."
exit 1
fi
print_success "All prerequisites are met!"
}
# Function to install npm dependencies
install_dependencies() {
print_status "Installing Node.js dependencies..."
npm install
print_success "Dependencies installed successfully!"
}
# Function to run tests
run_tests() {
print_status "Running tests..."
npm run lint
npm test
print_success "All tests passed!"
}
# Function to build Docker image
build_docker() {
print_status "Building Docker image..."
docker build -t devops-portfolio-app .
print_success "Docker image built successfully!"
}
# Function to start the application
start_app() {
local mode=$1
case $mode in
"local")
print_status "Starting application locally..."
npm start &
LOCAL_PID=$!
sleep 3
;;
"docker")
print_status "Starting application with Docker..."
docker run -d -p 3000:3000 --name devops-portfolio-app devops-portfolio-app
sleep 3
;;
"compose")
print_status "Starting application with Docker Compose..."
docker-compose up -d
sleep 5
;;
*)
print_error "Invalid mode: $mode"
exit 1
;;
esac
}
# Function to test the application
test_app() {
print_status "Testing application endpoints..."
# Test health endpoint
if curl -f -s http://localhost:3000/health > /dev/null; then
print_success "Health endpoint is working!"
else
print_error "Health endpoint failed!"
return 1
fi
# Test main page
if curl -f -s http://localhost:3000 > /dev/null; then
print_success "Main page is accessible!"
else
print_error "Main page failed!"
return 1
fi
# Test API endpoint
if curl -f -s http://localhost:3000/api/info > /dev/null; then
print_success "API endpoint is working!"
else
print_error "API endpoint failed!"
return 1
fi
print_success "All endpoints are working correctly!"
}
# Function to cleanup
cleanup() {
print_status "Cleaning up..."
# Kill local process if it exists
if [ ! -z "$LOCAL_PID" ]; then
kill $LOCAL_PID 2>/dev/null || true
fi
# Stop and remove Docker containers
docker-compose down 2>/dev/null || true
docker stop devops-portfolio-app 2>/dev/null || true
docker rm devops-portfolio-app 2>/dev/null || true
print_success "Cleanup completed!"
}
# Function to show help
show_help() {
echo "DevOps Portfolio Project - Setup and Run Script"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " setup - Install dependencies and run tests"
echo " start-local - Start application locally with Node.js"
echo " start-docker - Start application with Docker"
echo " start-compose - Start application with Docker Compose"
echo " test - Run all tests (lint + unit tests)"
echo " build - Build Docker image"
echo " clean - Clean up running containers and processes"
echo " full - Run full pipeline (setup + build + test + start)"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 setup # Install dependencies and run tests"
echo " $0 start-local # Start the app locally"
echo " $0 full # Complete setup and start"
echo ""
}
# Function to run full pipeline
run_full_pipeline() {
print_status "Running full DevOps pipeline..."
check_prerequisites
install_dependencies
run_tests
build_docker
start_app "compose"
test_app
print_success "Full pipeline completed successfully!"
echo ""
echo "Application is running at: http://localhost:3000"
echo "Health check: http://localhost:3000/health"
echo "API info: http://localhost:3000/api/info"
echo ""
echo "To stop the application, run: $0 clean"
}
# Trap to cleanup on exit
trap cleanup EXIT
# Main script logic
case ${1:-help} in
"setup")
check_prerequisites
install_dependencies
run_tests
print_success "Setup completed!"
;;
"start-local")
check_prerequisites
start_app "local"
test_app
print_success "Application is running locally at http://localhost:3000"
echo "Press Ctrl+C to stop..."
wait
;;
"start-docker")
check_prerequisites
build_docker
start_app "docker"
test_app
print_success "Application is running with Docker at http://localhost:3000"
echo "To stop: docker stop devops-portfolio-app"
;;
"start-compose")
check_prerequisites
start_app "compose"
test_app
print_success "Application is running with Docker Compose at http://localhost:3000"
echo "To stop: docker-compose down"
;;
"test")
check_prerequisites
install_dependencies
run_tests
;;
"build")
check_prerequisites
build_docker
;;
"clean")
cleanup
;;
"full")
run_full_pipeline
;;
"help"|"--help"|"-h")
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac