-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart-agents.sh
More file actions
executable file
Β·282 lines (237 loc) Β· 8.15 KB
/
Copy pathstart-agents.sh
File metadata and controls
executable file
Β·282 lines (237 loc) Β· 8.15 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
#!/bin/bash
# MAHA Wrapper - Agent Startup Script
# This script starts all available agents in the background
# 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}[MAHA]${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 port is available
check_port() {
local port=$1
if lsof -i :$port >/dev/null 2>&1; then
return 1 # Port is in use
else
return 0 # Port is available
fi
}
# Function to wait for agent to be ready
wait_for_agent() {
local port=$1
local name=$2
local max_attempts=30
local attempt=1
print_status "Waiting for $name to be ready on port $port..."
while [ $attempt -le $max_attempts ]; do
if curl -s http://localhost:$port/health >/dev/null 2>&1; then
print_success "$name is ready on port $port"
return 0
fi
sleep 1
attempt=$((attempt + 1))
done
print_error "$name failed to start on port $port after $max_attempts seconds"
return 1
}
# Function to start an agent
start_agent() {
local agent_dir=$1
local agent_name=$2
local port=$3
local command=$4
print_status "Starting $agent_name..."
# Check if port is available
if ! check_port $port; then
print_warning "Port $port is already in use. $agent_name may already be running."
return 1
fi
# Check if agent directory exists
if [ ! -d "agents/$agent_dir" ]; then
print_error "Agent directory 'agents/$agent_dir' not found"
return 1
fi
# Create logs directory if it doesn't exist
mkdir -p logs
# Start the agent in background
cd agents/$agent_dir
# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
print_status "Installing dependencies for $agent_name..."
npm install >/dev/null 2>&1
fi
# Start the agent
print_status "Launching $agent_name with command: $command"
$command > ../../logs/$agent_dir.log 2>&1 &
local pid=$!
echo $pid > ../../logs/$agent_dir.pid
cd - > /dev/null
# Wait for agent to be ready
if wait_for_agent $port "$agent_name"; then
print_success "$agent_name started successfully (PID: $pid)"
return 0
else
print_error "Failed to start $agent_name"
return 1
fi
}
# Function to stop all agents
stop_agents() {
print_status "Stopping all agents..."
for pidfile in logs/*.pid; do
if [ -f "$pidfile" ]; then
pid=$(cat "$pidfile")
agent_name=$(basename "$pidfile" .pid)
if kill -0 $pid 2>/dev/null; then
print_status "Stopping $agent_name (PID: $pid)..."
kill $pid
rm "$pidfile"
print_success "$agent_name stopped"
else
print_warning "$agent_name (PID: $pid) was not running"
rm "$pidfile"
fi
fi
done
}
# Function to show status of all agents
show_status() {
print_status "Agent Status:"
echo ""
# Check Hello Agent (Port 7029)
# if check_port 7029; then
# echo -e " π€ Hello Agent β ${RED}STOPPED${NC} β Port 7029"
# else
# echo -e " π€ Hello Agent β ${GREEN}RUNNING${NC} β Port 7029 β http://localhost:7029"
# fi
# Check ImageGen Agent (Port 7030)
if check_port 7030; then
echo -e " π¨ ImageGen Agent β ${RED}STOPPED${NC} β Port 7030"
else
echo -e " π¨ ImageGen Agent β ${GREEN}RUNNING${NC} β Port 7030 β http://localhost:7030"
fi
# Check NFT Deployer Agent (Port 7031)
if check_port 7031; then
echo -e " π NFT Deployer β ${RED}STOPPED${NC} β Port 7031"
else
echo -e " π NFT Deployer β ${GREEN}RUNNING${NC} β Port 7031 β http://localhost:7031"
fi
# Check 1inch Wallet Balance Agent (Port 7032)
if check_port 7032; then
echo -e " π° 1inch Agent β ${RED}STOPPED${NC} β Port 7032"
else
echo -e " π° 1inch Agent β ${GREEN}RUNNING${NC} β Port 7032 β http://localhost:7032"
fi
# Check Aave Investor Agent (Port 7033)
if check_port 7033; then
echo -e " π¦ Aave Investor β ${RED}STOPPED${NC} β Port 7033"
else
echo -e " π¦ Aave Investor β ${GREEN}RUNNING${NC} β Port 7033 β http://localhost:7033"
fi
# Check NFT Metadata Creator Agent (Port 7034)
if check_port 7034; then
echo -e " π NFT Metadata β ${RED}STOPPED${NC} β Port 7034"
else
echo -e " π NFT Metadata β ${GREEN}RUNNING${NC} β Port 7034 β http://localhost:7034"
fi
echo ""
}
# Main script logic
case "$1" in
"start")
print_status "Starting MAHA Protocol Agents..."
echo ""
# Start Hello Agent
# start_agent "hello" "Hello Agent" 7029 "npm run dev"
# Start ImageGen Agent
start_agent "imagegen" "ImageGen Agent" 7030 "npm start"
# Start NFT Deployer Agent
start_agent "nft-deployer" "NFT Deployer Agent" 7031 "npm run dev"
# Start 1inch Wallet Balance Agent
start_agent "1inch" "1inch Wallet Balance Agent" 7032 "npm start"
# Start Aave Investor Agent
start_agent "aave-investor" "Aave Investor Agent" 7033 "npm run dev"
# Start NFT Metadata Creator Agent
start_agent "nft-metadata-creator" "NFT Metadata Creator Agent" 7034 "npm run dev"
echo ""
print_success "All agents startup initiated!"
echo ""
show_status
echo ""
print_status "Agent logs are available in the 'logs/' directory"
print_status "Use './start-agents.sh status' to check agent status"
print_status "Use './start-agents.sh stop' to stop all agents"
;;
"stop")
stop_agents
echo ""
show_status
;;
"restart")
print_status "Restarting all agents..."
stop_agents
sleep 2
echo ""
exec "$0" start
;;
"status")
show_status
;;
"logs")
if [ -z "$2" ]; then
print_status "Available log files:"
ls -la logs/*.log 2>/dev/null | awk '{print " " $9}'
echo ""
print_status "Usage: $0 logs <agent>"
print_status "Example: $0 logs imagegen"
print_status "Available agents: imagegen, nft-deployer, 1inch, aave-investor, nft-metadata-creator"
else
if [ -f "logs/$2.log" ]; then
print_status "Showing logs for $2 agent (Press Ctrl+C to exit):"
tail -f "logs/$2.log"
else
print_error "Log file for '$2' not found"
fi
fi
;;
*)
echo ""
echo -e "${BLUE}MAHA Protocol - Agent Management Script${NC}"
echo ""
echo "Usage: $0 {start|stop|restart|status|logs [agent]}"
echo ""
echo "Commands:"
echo " start - Start all agents in background"
echo " stop - Stop all running agents"
echo " restart - Restart all agents"
echo " status - Show status of all agents"
echo " logs - Show available logs or tail specific agent log"
echo ""
echo "Available agents:"
echo " # β’ Hello Agent (Port 7029)"
echo " β’ ImageGen Agent (Port 7030)"
echo " β’ NFT Deployer Agent (Port 7031)"
echo " β’ 1inch Wallet Balance Agent (Port 7032)"
echo " β’ Aave Investor Agent (Port 7033)"
echo " β’ NFT Metadata Creator Agent (Port 7034)"
echo ""
echo "Examples:"
echo " $0 start"
echo " $0 status"
echo " $0 logs nft-metadata-creator"
echo ""
;;
esac