-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.sh
More file actions
executable file
Β·198 lines (182 loc) Β· 6.91 KB
/
vm.sh
File metadata and controls
executable file
Β·198 lines (182 loc) Β· 6.91 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
#!/bin/bash
# VM Management Script
set -e
COMPOSE_FILE="docker-compose.vm.yml"
show_help() {
echo "π₯οΈ Three-Tier Application Ubuntu VM Manager"
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " start Start the Ubuntu VM"
echo " stop Stop the Ubuntu VM"
echo " restart Restart the Ubuntu VM"
echo " rebuild Rebuild and restart the VM (fresh start)"
echo " logs Show VM logs"
echo " shell Open shell inside the VM"
echo " exec Execute a command inside the VM"
echo " ssh SSH into the VM as developer user"
echo " status Show VM and service status"
echo " fix-db Fix database connection issues"
echo " clean Stop VM and remove volumes (DESTRUCTIVE)"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 start # Start the VM"
echo " $0 logs -f # Follow logs"
echo " $0 shell # Get VM shell"
echo " $0 exec 'ps aux' # Execute command in VM"
echo " $0 ssh # SSH as developer"
echo " $0 fix-db # Fix database issues"
}
case "${1:-help}" in
start)
# Determine API URL
BACKEND_IP="${2}"
if [ -z "$BACKEND_IP" ]; then
# Try environment variable first
if [ -n "$REACT_APP_API_URL" ]; then
API_URL="$REACT_APP_API_URL"
# Then try .env file
elif [ -f ".env" ] && grep -q "REACT_APP_API_URL" .env; then
API_URL=$(grep "REACT_APP_API_URL" .env | cut -d'=' -f2)
echo "π Using API URL from .env file: $API_URL"
else
# Cross-platform IP detection
if [[ "$(uname)" == "Linux" ]]; then
DETECTED_IP=$(hostname -I | awk '{print $1}')
elif [[ "$(uname)" == "Darwin" ]]; then
DETECTED_IP=$(ipconfig getifaddr en0)
if [ -z "$DETECTED_IP" ]; then
DETECTED_IP=$(ipconfig getifaddr en1)
fi
else
DETECTED_IP=""
fi
if [ -z "$DETECTED_IP" ]; then
echo "β οΈ Could not auto-detect IP address. Please provide it as an argument or set REACT_APP_API_URL."
exit 1
fi
API_URL="http://$DETECTED_IP:3000"
fi
else
API_URL="http://$BACKEND_IP:3000"
fi
# For nginx proxy setup, API and frontend use the same port (3000)
HOST_IP=$(echo "$API_URL" | sed 's|http://||' | sed 's|:.*||')
API_URL="http://$HOST_IP:3000"
echo "π Using unified URL (nginx proxy): $API_URL"
echo "π Starting Ubuntu VM..."
export HOST_IP=$HOST_IP
export REACT_APP_API_URL="$API_URL"
export FRONTEND_URL="$API_URL"
echo "π All traffic via: $API_URL"
./start-vm.sh
;;
stop)
echo "βΉοΈ Stopping Ubuntu VM..."
docker compose -f $COMPOSE_FILE down
echo "β
VM stopped"
;;
restart)
echo "π Restarting Ubuntu VM..."
docker compose -f $COMPOSE_FILE down
./start-vm.sh
;;
rebuild)
echo "ποΈ Rebuilding Ubuntu VM (fresh start)..."
docker compose -f $COMPOSE_FILE down -v
docker compose -f $COMPOSE_FILE build --no-cache
# Run the same logic as start to ensure frontend is built with correct API URL
shift
$0 start "$@"
;;
logs)
shift
echo "π VM Logs:"
docker compose -f $COMPOSE_FILE logs "$@"
;;
shell|bash)
echo "π» Opening VM shell..."
docker compose -f $COMPOSE_FILE exec ubuntu-vm bash -l
;;
exec)
shift
echo "π§ Executing command in VM: $*"
docker compose -f $COMPOSE_FILE exec ubuntu-vm "$@"
;;
fix-db)
echo "π§ Fixing database issues..."
# Check if VM is running
if ! docker compose -f docker-compose.vm.yml ps --format json | grep -q "running"; then
echo "β VM is not running. Start it first with: ./vm.sh start"
exit 1
fi
echo "π¦ Setting up PostgreSQL database..."
# Start PostgreSQL if it's not running
$0 exec bash -c "sudo supervisorctl start postgresql"
sleep 3
# Create database user and database
echo "Creating database user and database..."
$0 exec bash -c "sudo -u postgres psql -c \"DROP USER IF EXISTS taskuser;\"" || true
$0 exec bash -c "sudo -u postgres psql -c \"CREATE USER taskuser WITH SUPERUSER PASSWORD 'taskpass123';\""
$0 exec bash -c "sudo -u postgres psql -c \"DROP DATABASE IF EXISTS taskmanager;\"" || true
$0 exec bash -c "sudo -u postgres psql -c \"CREATE DATABASE taskmanager OWNER taskuser;\""
# Load initial data
echo "Loading initial database schema and data..."
$0 exec bash -c "sudo -u postgres psql -d taskmanager -f /app/database/init.sql"
# Create initialization marker
$0 exec bash -c "touch /var/lib/postgresql/14/main/.db_initialized"
# Restart backend to reconnect
echo "Restarting backend service..."
$0 exec bash -c "sudo supervisorctl restart backend"
echo "β
Database setup complete!"
# Test the connection
echo "π Testing database connection..."
sleep 3
if curl -f http://localhost:3001/health >/dev/null 2>&1; then
echo "β
Database connection successful!"
curl http://localhost:3001/health
echo ""
else
echo "β Still having issues. Check logs with: $0 logs"
fi
;;
ssh)
echo "π Connecting via SSH..."
echo "Password: developer123"
ssh developer@localhost -p 2222
;;
status)
echo "π VM Status:"
echo ""
docker compose -f $COMPOSE_FILE ps
echo ""
echo "π Service Status (inside VM):"
if docker compose -f $COMPOSE_FILE exec -T ubuntu-vm supervisorctl status 2>/dev/null; then
echo "β
VM is running"
else
echo "β VM is not running or not ready"
fi
;;
clean)
echo "π§Ή Cleaning up VM and data..."
read -p "This will remove all VM data. Continue? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
docker compose -f $COMPOSE_FILE down -v --remove-orphans
docker image rm three-ubuntu-vm 2>/dev/null || true
echo "β
VM and data cleaned"
else
echo "β Cancelled"
fi
;;
help|--help|-h)
show_help
;;
*)
echo "β Unknown command: $1"
echo ""
show_help
exit 1
;;
esac