-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathshioaji-cli.sh
More file actions
343 lines (304 loc) · 8.2 KB
/
Copy pathshioaji-cli.sh
File metadata and controls
343 lines (304 loc) · 8.2 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
#
# Shioaji Auto-Trading CLI
# A user-friendly command-line interface for managing the trading system
#
# Usage: ./shioaji-cli.sh [command]
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Project name (for docker volume names)
PROJECT_NAME="shioaji-api-dashboard"
# Print colored output
print_header() {
echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${CYAN} 📈 Shioaji Auto-Trading System${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
# Check if .env file exists
check_env() {
if [ ! -f ".env" ]; then
print_error ".env file not found!"
echo ""
echo "Please create .env file first:"
echo " cp example.env .env"
echo " # Then edit .env with your API keys"
echo ""
exit 1
fi
}
# Show help
show_help() {
print_header
echo "Usage: ./shioaji-cli.sh [command]"
echo ""
echo "Commands:"
echo -e " ${GREEN}start${NC} Start all services"
echo -e " ${GREEN}stop${NC} Stop all services"
echo -e " ${GREEN}restart${NC} Restart all services"
echo -e " ${GREEN}status${NC} Show service status"
echo -e " ${GREEN}logs${NC} Show all logs (follow mode)"
echo -e " ${GREEN}logs-api${NC} Show API logs only"
echo -e " ${GREEN}logs-worker${NC} Show Trading Worker logs only"
echo -e " ${GREEN}health${NC} Check system health"
echo -e " ${GREEN}dashboard${NC} Open dashboard in browser"
echo -e " ${GREEN}reset${NC} Reset database (DELETE ALL DATA)"
echo -e " ${GREEN}update${NC} Pull latest code and rebuild"
echo -e " ${GREEN}help${NC} Show this help message"
echo ""
echo "Examples:"
echo " ./shioaji-cli.sh start # Start the trading system"
echo " ./shioaji-cli.sh logs # View live logs"
echo " ./shioaji-cli.sh status # Check if services are running"
echo ""
}
# Start services
cmd_start() {
print_header
check_env
print_info "Starting services..."
echo ""
docker compose up -d
echo ""
print_success "Services started!"
echo ""
echo "Dashboard: http://localhost:9879/dashboard"
echo "API Docs: http://localhost:9879/docs"
echo ""
print_info "Waiting for services to be ready..."
sleep 5
cmd_health_quiet
}
# Stop services
cmd_stop() {
print_header
print_info "Stopping services..."
echo ""
docker compose down
echo ""
print_success "Services stopped!"
}
# Restart services
cmd_restart() {
print_header
print_info "Restarting services..."
echo ""
docker compose restart
echo ""
print_success "Services restarted!"
sleep 3
cmd_health_quiet
}
# Show status
cmd_status() {
print_header
print_info "Service Status:"
echo ""
docker compose ps
echo ""
}
# Show logs
cmd_logs() {
print_header
print_info "Showing logs (Ctrl+C to exit)..."
echo ""
docker compose logs -f
}
# Show API logs
cmd_logs_api() {
print_header
print_info "Showing API logs (Ctrl+C to exit)..."
echo ""
docker compose logs -f api
}
# Show worker logs
cmd_logs_worker() {
print_header
print_info "Showing Trading Worker logs (Ctrl+C to exit)..."
echo ""
docker compose logs -f trading-worker
}
# Health check (quiet version)
cmd_health_quiet() {
local health_url="http://localhost:9879/health"
local max_attempts=10
local attempt=1
while [ $attempt -le $max_attempts ]; do
response=$(curl -s "$health_url" 2>/dev/null || echo "")
if [ -n "$response" ]; then
api_status=$(echo "$response" | grep -o '"api":"[^"]*"' | cut -d'"' -f4)
worker_status=$(echo "$response" | grep -o '"trading_worker":"[^"]*"' | cut -d'"' -f4)
redis_status=$(echo "$response" | grep -o '"redis":"[^"]*"' | cut -d'"' -f4)
echo ""
echo "System Health:"
if [ "$api_status" = "healthy" ]; then
echo -e " API: ${GREEN}● healthy${NC}"
else
echo -e " API: ${RED}● $api_status${NC}"
fi
if [ "$worker_status" = "healthy" ]; then
echo -e " Trading Worker: ${GREEN}● healthy${NC}"
else
echo -e " Trading Worker: ${YELLOW}● $worker_status${NC}"
fi
if [ "$redis_status" = "connected" ]; then
echo -e " Redis: ${GREEN}● connected${NC}"
else
echo -e " Redis: ${RED}● $redis_status${NC}"
fi
echo ""
return 0
fi
sleep 2
attempt=$((attempt + 1))
done
print_warning "Could not reach health endpoint. Services may still be starting..."
echo ""
}
# Health check
cmd_health() {
print_header
print_info "Checking system health..."
cmd_health_quiet
}
# Open dashboard
cmd_dashboard() {
local url="http://localhost:9879/dashboard"
print_header
print_info "Opening dashboard..."
echo ""
echo "URL: $url"
echo ""
# Try to open browser
if command -v xdg-open &> /dev/null; then
xdg-open "$url" 2>/dev/null &
elif command -v open &> /dev/null; then
open "$url" 2>/dev/null &
else
print_warning "Could not open browser automatically."
echo "Please open this URL in your browser: $url"
fi
}
# Reset database
cmd_reset() {
print_header
print_warning "DATABASE RESET"
echo ""
echo "This will:"
echo " 1. Stop all services"
echo " 2. Delete all order history"
echo " 3. Delete Redis cache"
echo " 4. Restart with fresh database"
echo ""
print_error "ALL DATA WILL BE PERMANENTLY DELETED!"
echo ""
read -p "Type 'yes' to confirm: " confirm
if [ "$confirm" != "yes" ]; then
echo ""
print_info "Cancelled."
exit 0
fi
echo ""
print_info "Stopping services..."
docker compose down
echo ""
print_info "Removing data volumes..."
docker volume rm ${PROJECT_NAME}_postgres_data 2>/dev/null || true
docker volume rm ${PROJECT_NAME}_redis_data 2>/dev/null || true
echo ""
print_info "Starting services with fresh database..."
docker compose up -d
echo ""
print_info "Waiting for database migration..."
sleep 10
# Check migration logs
echo ""
echo "Migration logs:"
docker compose logs db-migrate | tail -20
echo ""
print_success "Database reset complete!"
echo ""
cmd_health_quiet
}
# Update and rebuild
cmd_update() {
print_header
print_info "Updating system..."
echo ""
echo "Pulling latest code..."
git pull
echo ""
echo "Rebuilding containers..."
docker compose build
echo ""
echo "Restarting services..."
docker compose up -d
echo ""
print_success "Update complete!"
sleep 3
cmd_health_quiet
}
# Main
case "${1:-help}" in
start)
cmd_start
;;
stop)
cmd_stop
;;
restart)
cmd_restart
;;
status)
cmd_status
;;
logs)
cmd_logs
;;
logs-api)
cmd_logs_api
;;
logs-worker)
cmd_logs_worker
;;
health)
cmd_health
;;
dashboard)
cmd_dashboard
;;
reset)
cmd_reset
;;
update)
cmd_update
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac