-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdmtools.sh
More file actions
executable file
·456 lines (406 loc) · 16.2 KB
/
Copy pathdmtools.sh
File metadata and controls
executable file
·456 lines (406 loc) · 16.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/bin/bash
# DMTools CLI Wrapper
# Usage: ./dmtools.sh [command] [args...]
# Supports: STDIN, heredoc, file input, inline JSON, and direct job-name execution for `run`
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
get_install_dir_candidates() {
local script_parent_dir=""
script_parent_dir="$(cd "$SCRIPT_DIR/.." 2>/dev/null && pwd)"
if [ -n "${DMTOOLS_INSTALL_DIR:-}" ]; then
printf '%s\n' "$DMTOOLS_INSTALL_DIR"
fi
if [ -n "$script_parent_dir" ] && [ "$script_parent_dir" != "${DMTOOLS_INSTALL_DIR:-}" ]; then
printf '%s\n' "$script_parent_dir"
fi
if [ "$HOME/.dmtools" != "${DMTOOLS_INSTALL_DIR:-}" ] && [ "$HOME/.dmtools" != "$script_parent_dir" ]; then
printf '%s\n' "$HOME/.dmtools"
fi
}
# Find Java command (bundled or system)
find_java_command() {
local install_dir=""
while IFS= read -r install_dir; do
[ -n "$install_dir" ] || continue
# macOS has different JRE structure: Contents/Home/bin/java
local bundled_java_macos="$install_dir/jre/Contents/Home/bin/java"
local bundled_java="$install_dir/jre/bin/java"
local bundled_java_exe="$install_dir/jre/bin/java.exe"
# Check for bundled Java (order matters: macOS, Windows, Linux)
if [ -x "$bundled_java_macos" ]; then
echo "$bundled_java_macos"
return 0
elif [ -x "$bundled_java_exe" ]; then
echo "$bundled_java_exe"
return 0
elif [ -x "$bundled_java" ]; then
echo "$bundled_java"
return 0
fi
done < <(get_install_dir_candidates)
# Fall back to system Java
if command -v java >/dev/null 2>&1; then
echo "java"
return 0
fi
# No Java found
return 1
}
# Get Java command or error
JAVA_CMD=$(find_java_command 2>/dev/null) || {
echo "Error: Java 17+ is required but not found." >&2
echo "Please install DMTools first:" >&2
echo " curl -fsSL https://github.com/epam/dm.ai/releases/latest/download/install.sh | bash" >&2
exit 1
}
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Brand colors (teal/cyan theme)
# Detect terminal capabilities and use appropriate color mode
# macOS Terminal often doesn't support 24-bit colors, so we check TERM_PROGRAM
USE_24BIT=false
if [ -n "$COLORTERM" ] && [[ "$COLORTERM" == *"truecolor"* ]]; then
USE_24BIT=true
elif [ -n "$TERM_PROGRAM" ] && [[ "$TERM_PROGRAM" != "Apple_Terminal" ]]; then
# For non-Apple terminals, check if 256color is supported
if [ -n "$TERM" ] && [[ "$TERM" == *"256color"* ]]; then
USE_24BIT=true
fi
fi
if [ "$USE_24BIT" = true ]; then
TEAL_LIGHT='\033[38;2;102;208;232m' # #66D0E8 - Light teal
TEAL_DARK='\033[38;2;51;153;204m' # #3399CC - Darker blue-green
else
# Use 256-color palette for better compatibility, or fallback to ANSI
# For macOS Terminal, use bright cyan which looks good
TEAL_LIGHT='\033[1;36m' # Bright cyan (works well in macOS Terminal)
TEAL_DARK='\033[0;36m' # Cyan
fi
CYAN='\033[0;36m'
CYAN_BRIGHT='\033[1;36m'
WHITE='\033[1;37m'
BOLD='\033[1m'
RESET='\033[0m'
# Helper functions
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
info() {
echo -e "${GREEN}Info: $1${NC}" >&2
}
# Execute java command with proper stderr handling
# Usage: execute_java_command arg1 arg2 ...
execute_java_command() {
if [ "$DEBUG" = true ]; then
"$@"
else
"$@" 2>/dev/null
fi
}
# Load environment variables from various .env files
# Usage: load_env_files [quiet]
# quiet: if set, don't print info messages
load_env_files() {
local quiet_mode="${1:-false}"
local env_files=(
# Current working directory (highest priority)
".env"
"dmtools.env"
"dmtools-installer.env"
"dmtools-local.env"
# Script directory (lower priority)
"$SCRIPT_DIR/.env"
"$SCRIPT_DIR/dmtools.env"
"$SCRIPT_DIR/dmtools-installer.env"
"$SCRIPT_DIR/dmtools-runtime.env"
"$SCRIPT_DIR/dmtools-local.env"
)
for env_file in "${env_files[@]}"; do
if [ -f "$env_file" ] && [ -r "$env_file" ]; then
# Check file permissions for security (should not be world-readable for sensitive data)
if [ -f "$env_file" ]; then
# Load the environment file, ignoring comments and empty lines
while IFS= read -r line || [ -n "$line" ]; do
# Skip empty lines and comments
if [[ -n "$line" && ! "$line" =~ ^[[:space:]]*# ]]; then
# Export the variable if it's in KEY=VALUE format
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
# Remove surrounding quotes if present
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//")
export "$key"="$value"
fi
fi
done < "$env_file"
if [ "$quiet_mode" != "quiet" ]; then
info "Loaded environment variables from: $env_file"
fi
fi
fi
done
}
# Load environment files silently (will be reloaded with messages when command is executed)
load_env_files quiet
# Try to find JAR file in multiple locations
find_jar_file() {
local jar_file=""
local install_dir=""
while IFS= read -r install_dir; do
[ -n "$install_dir" ] || continue
if [ -f "$install_dir/dmtools.jar" ]; then
echo "$install_dir/dmtools.jar"
return 0
fi
done < <(get_install_dir_candidates)
# Check local build directory (development)
if ls "$SCRIPT_DIR/build/libs"/*.jar >/dev/null 2>&1; then
jar_file=$(find "$SCRIPT_DIR/build/libs" -name "*-all.jar" | head -1)
# Check for any JAR file in the script directory
elif ls "$SCRIPT_DIR"/*.jar >/dev/null 2>&1; then
jar_file=$(find "$SCRIPT_DIR" -maxdepth 1 \( -name "dmtools*.jar" -o -name "*-all.jar" \) | head -1)
# Check parent directory build folder (if script is in subdirectory)
elif ls "$SCRIPT_DIR/../build/libs"/*.jar >/dev/null 2>&1; then
jar_file=$(find "$SCRIPT_DIR/../build/libs" -name "*-all.jar" | head -1)
fi
echo "$jar_file"
}
JAR_FILE=$(find_jar_file)
usage() {
# Check if JAR file exists before calling JobRunner
if [ -z "$JAR_FILE" ] || [ ! -f "$JAR_FILE" ]; then
error "DMTools JAR file not found. Please install DMTools first:
curl -fsSL https://github.com/epam/dm.ai/releases/latest/download/install.sh | bash
Or if you're developing locally, build the project first:
./gradlew build
Note: Java 17+ is required for DMTools to run."
fi
execute_java_command "$JAVA_CMD" -Dlog4j2.configurationFile=classpath:log4j2-cli.xml -Dlog4j.configuration=log4j2-cli.xml -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true -Djava.rmi.server.hostname=127.0.0.1 --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner
exit 0
}
# Parse arguments
if [ $# -eq 0 ]; then
if [ -t 0 ] && [ -t 1 ]; then
chosen=$("$JAVA_CMD" -Dlog4j2.configurationFile=classpath:log4j2-cli.xml -Dlog4j.configuration=log4j2-cli.xml -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner interactive)
if [ -n "$chosen" ]; then
if [[ "$chosen" == run:* ]]; then
target="${chosen#run:}"
exec "$0" run "$target"
else
# chosen is "tool_name\tjson_params\toutput_format"
tool_name="${chosen%%$'\t'*}"
rest="${chosen#*$'\t'}"
params="${rest%%$'\t'*}"
output_format="${rest#*$'\t'}"
if [ "$params" = "$rest" ]; then
output_format=""
fi
output_args=()
if [ -n "$output_format" ]; then
output_args=("--output" "$output_format")
fi
if [ -n "$params" ] && [ "$tool_name" != "$params" ]; then
exec "$0" "$tool_name" --data "$params" "${output_args[@]}"
else
exec "$0" "$tool_name" "${output_args[@]}"
fi
fi
fi
exit 0
else
usage
fi
exit 0
fi
# Initialize variables for argument parsing
ARGS=()
DATA=""
FILE=""
QUIET=true
DEBUG=false
COMMAND=""
OUTPUT_FORMAT=""
# Parse all arguments first to handle flags
while [[ $# -gt 0 ]]; do
case $1 in
--data)
DATA="$2"
shift 2
;;
--file)
FILE="$2"
shift 2
;;
--verbose)
QUIET=false
shift
;;
--debug)
QUIET=false
DEBUG=true
shift
;;
--quiet)
QUIET=true
shift
;;
--toon)
OUTPUT_FORMAT="toon"
shift
;;
--mini)
OUTPUT_FORMAT="mini"
shift
;;
--output)
OUTPUT_FORMAT="$2"
shift 2
;;
--output=*)
OUTPUT_FORMAT="${1#--output=}"
shift
;;
*)
if [ -z "$COMMAND" ]; then
COMMAND="$1"
else
ARGS+=("$1")
fi
shift
;;
esac
done
# Validate command exists
if [ -z "$COMMAND" ]; then
echo "DMTools CLI - Use 'dmtools help' or 'dmtools --help' for usage information"
exit 0
fi
# Determine log configuration based on debug mode (must be before case block)
if [ "$DEBUG" = true ]; then
LOG_CONFIG="log4j2-debug.xml"
else
LOG_CONFIG="log4j2-cli.xml"
fi
# Handle special commands
case "$COMMAND" in
"help"|"-h"|"--help")
usage
;;
"interactive"|"i")
chosen=$("$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner interactive)
if [ -n "$chosen" ]; then
if [[ "$chosen" == run:* ]]; then
target="${chosen#run:}"
exec "$0" run "$target"
else
tool_name="${chosen%%$'\t'*}"
rest="${chosen#*$'\t'}"
params="${rest%%$'\t'*}"
output_format="${rest#*$'\t'}"
if [ "$params" = "$rest" ]; then
output_format=""
fi
output_args=()
if [ -n "$output_format" ]; then
output_args=("--output" "$output_format")
fi
if [ -n "$params" ] && [ "$tool_name" != "$params" ]; then
exec "$0" "$tool_name" --data "$params" "${output_args[@]}"
else
exec "$0" "$tool_name" "${output_args[@]}"
fi
fi
fi
exit 0
;;
"doctor")
if [ -z "$JAR_FILE" ] || [ ! -f "$JAR_FILE" ]; then
error "DMTools JAR file not found. Please install DMTools first or build the project."
fi
"$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner doctor
exit $?
;;
"run")
# Handle run command with JSON file, JS file, or direct job name + optional encoded parameter
if [ ${#ARGS[@]} -lt 1 ]; then
error "Run command requires at least one argument: json-file-path or job-name"
fi
JSON_FILE="${ARGS[0]}"
# Extra args after ARGS[0] (file) — includes optional encoded param and --key value pairs (e.g. --ciRunUrl)
EXTRA_RUN_ARGS=("${ARGS[@]:1}")
info "Executing job with file: $JSON_FILE"
# Run is not a tool — stderr must NOT be suppressed so errors are visible in CI logs
"$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner run "$JSON_FILE" "${EXTRA_RUN_ARGS[@]}"
exit $?
;;
esac
# Check if JAR file exists
if [ -z "$JAR_FILE" ] || [ ! -f "$JAR_FILE" ]; then
error "DMTools JAR file not found. Please install DMTools first:
curl -fsSL https://github.com/epam/dm.ai/releases/latest/download/install.sh | bash
Or if you're developing locally, build the project first:
./gradlew build
Note: Java 17+ is required for DMTools to run."
fi
# Check if command starts with - or --, then proxy directly to JobRunner
if [[ "$COMMAND" == -* ]]; then
# Direct proxy to JobRunner for flags like --version, --help, etc.
CMD_ARGS=("$COMMAND")
if [ ${#ARGS[@]} -gt 0 ]; then
CMD_ARGS+=("${ARGS[@]}")
fi
# Execute directly without mcp prefix
if [ "$DEBUG" = true ]; then
"$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner "${CMD_ARGS[@]}"
else
"$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner "${CMD_ARGS[@]}" 2>/dev/null
fi
exit $?
fi
# Proxy all other commands to JobRunner as MCP tools
# Build command arguments array
CMD_ARGS=("$COMMAND")
if [ ${#ARGS[@]} -gt 0 ]; then
CMD_ARGS+=("${ARGS[@]}")
fi
# Check if STDIN has data (heredoc/pipe)
if [ ! -t 0 ]; then
STDIN_DATA=$(cat)
if [ -n "$STDIN_DATA" ]; then
[ "$QUIET" = false ] && info "Reading JSON from STDIN (${#STDIN_DATA} characters)"
CMD_ARGS+=("--stdin-data" "$STDIN_DATA")
fi
fi
# Add --data or --file flags if provided
if [ -n "$DATA" ]; then
[ "$QUIET" = false ] && info "Using inline data"
CMD_ARGS+=("--data" "$DATA")
elif [ -n "$FILE" ]; then
if [ ! -f "$FILE" ]; then
error "File not found: $FILE"
fi
[ "$QUIET" = false ] && info "Using file data: $FILE"
FILE_DATA=$(cat "$FILE")
CMD_ARGS+=("--data" "$FILE_DATA")
fi
# Add verbose/debug flags
if [ "$QUIET" = false ]; then
CMD_ARGS+=("--verbose")
fi
if [ "$DEBUG" = true ]; then
CMD_ARGS+=("--debug")
fi
# Add output format flag if provided
if [ -n "$OUTPUT_FORMAT" ]; then
CMD_ARGS+=("--output" "$OUTPUT_FORMAT")
fi
# Execute command via JobRunner as MCP tool
if [ "$DEBUG" = true ]; then
"$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner mcp "${CMD_ARGS[@]}"
else
"$JAVA_CMD" -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG -Dlog4j2.disable.jmx=true -Djava.net.preferIPv4Stack=true --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -Dpolyglot.engine.WarnInterpreterOnly=false -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner mcp "${CMD_ARGS[@]}" 2>/dev/null
fi