-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_examples.sh
executable file
·222 lines (192 loc) · 6.83 KB
/
update_examples.sh
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
#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Default values
UPDATE_ENV=false
CLEAN_SVC_KEYS=false
REINSTALL_NPM=false
RUN_SERVICES=false
STOP_SERVICES=false
# Function to display usage information
usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -u, --update-env Update environment variables"
echo " -k, --api-key KEY Use existing API key instead of generating new one"
echo " -w, --webhook URL Webhook URL to use"
echo " -c, --clean-svc-keys Clean service keys"
echo " -i, --install-npm Reinstall npm packages"
echo " -r, --run-services Run services"
echo " -s, --stop-services Stop services"
echo " -h, --help Show this help message"
}
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-u|--update-env) UPDATE_ENV=true ;;
-k|--api-key)
shift
EXTERNAL_API_KEY="$1"
;;
-w|--webhook)
shift
WEBHOOK_URL="$1"
;;
-c|--clean-svc-keys) CLEAN_SVC_KEYS=true ;;
-i|--install-npm) REINSTALL_NPM=true ;;
-r|--run-services) RUN_SERVICES=true ;;
-s|--stop-services) STOP_SERVICES=true ;;
-h|--help)
echo "Usage: $0 [-u|--update-env] [-k|--api-key KEY] [-w|--webhook URL] [-c|--clean-svc-keys] [-i|--install-npm] [-r|--run-services] [-s|--stop-services] [-h|--help]"
echo " -u, --update-env Update environment variables"
echo " -k, --api-key KEY Use existing API key instead of generating new one"
echo " -w, --webhook URL Webhook URL to use"
echo " -c, --clean-svc-keys Clean service keys"
echo " -i, --install-npm Reinstall npm packages"
echo " -r, --run-services Run services"
echo " -s, --stop-services Stop services"
echo " -h, --help Show this help message"
exit 0
;;
*) echo "Unknown parameter passed: $1"; usage; exit 1 ;;
esac
shift
done
# Validate required arguments when updating env
if [ "$UPDATE_ENV" = true ] && [ -z "$WEBHOOK_URL" ]; then
echo "Error: Webhook URL (-w|--webhook) is required when updating environment variables"
usage
exit 1
fi
# Directory containing the examples
EXAMPLES_DIR="./examples"
# Array of example directories
example_dirs=(
"echo-js"
"echo-python"
"ecommerce-agent-app/chat-ui"
"ecommerce-agent-app/customer-svc"
"ecommerce-agent-app/delivery-agent"
"ecommerce-agent-app/inventory-svc"
"crewai-ghostwriters"
)
# Update environment variables
if $UPDATE_ENV; then
echo "Updating environment variables..."
# Only call the API if no external API key is provided
if [ -z "$EXTERNAL_API_KEY" ]; then
echo "Registering new project..."
# Run the curl command and capture the output
CURL_OUTPUT=$(curl -X POST http://localhost:8005/register/project \
-H "Content-Type: application/json" \
-d "{\"webhook\": \"${WEBHOOK_URL}\"}" | gojq .)
# Check if curl command was successful
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "Error: Failed to register project. Curl command failed."
exit 1
fi
# Extract API key from the curl output
API_KEY=$(echo "$CURL_OUTPUT" | gojq -r '.apiKey')
# Check if API_KEY was extracted successfully
if [ -z "$API_KEY" ]; then
echo "Error: Failed to extract API key from the response."
exit 1
fi
else
echo "Using provided API key..."
API_KEY="$EXTERNAL_API_KEY"
fi
for dir in "${example_dirs[@]}"; do
full_dir="$EXAMPLES_DIR/$dir"
echo "Updating environment files in $full_dir"
# Update .env and .env.local files
for env_file in "$full_dir/.env" "$full_dir/.env.local"; do
if [ -f "$env_file" ]; then
# Use a temporary file for sed operations
temp_file=$(mktemp)
sed -e "s/^ORRA_API_KEY=.*/ORRA_API_KEY=$API_KEY/" \
"$env_file" > "$temp_file"
mv "$temp_file" "$env_file"
echo "Updated $env_file"
fi
done
done
fi
# Update environment variables
if $CLEAN_SVC_KEYS; then
echo "Cleaning service key files..."
for dir in "${example_dirs[@]}"; do
full_dir="$EXAMPLES_DIR/$dir"
echo "Attempting to remove service key in $full_dir"
# Clean up orra-service-key.json files
find "$full_dir" -type f \( -name "orra-service-key.json" -o -name "*-orra-service-key.json" \) | while read -r service_key_file; do
echo "Removing $service_key_file"
rm "$service_key_file"
done
if [ $? -ne 0 ]; then
echo "No service key files found in $full_dir"
fi
done
fi
# Reinstall npm packages
if $REINSTALL_NPM; then
echo "Reinstalling npm packages..."
for dir in "${example_dirs[@]}"; do
full_dir="$EXAMPLES_DIR/$dir"
if [ -f "$full_dir/package.json" ]; then
echo "Processing $full_dir"
# Remove old node_modules
rm -rf "$full_dir/node_modules"
# Install packages
(cd "$full_dir" && npm install)
else
echo "Warning: No package.json found in $full_dir"
fi
done
fi
# Run example services
if $RUN_SERVICES; then
echo "Running example services..."
# Run each service in the background
for service in "${example_dirs[@]}"; do
service_dir="$EXAMPLES_DIR/$service"
if [ -d "$service_dir" ]; then
echo "Starting $service"
(cd "$service_dir" && npm run dev) &
else
echo "Warning: $service directory not found"
fi
done
# Wait for all background processes
wait
fi
# Stop example services
if $STOP_SERVICES; then
echo "Stopping example services..."
for service in "${example_dirs[@]}"; do
service_name=$(basename "$service")
pids=$(pgrep -f "node.*$service_name")
if [ -n "$pids" ]; then
echo "Stopping $service_name (PIDs: $pids)"
kill "$pids"
else
echo "No running process found for $service_name"
fi
done
# Wait a moment to allow processes to stop
sleep 2
# Check if any processes are still running
for service in "${example_dirs[@]}"; do
service_name=$(basename "$service")
if pgrep -f "node.*$service_name" > /dev/null; then
echo "Warning: $service_name is still running"
else
echo "$service_name stopped successfully"
fi
done
fi
echo "Script completed."