-
Notifications
You must be signed in to change notification settings - Fork 0
/
fah-cli
executable file
·113 lines (87 loc) · 2.26 KB
/
fah-cli
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
#!/bin/bash
IP='localhost'
PORT="36330"
connect_to_server ()
{
nc "$IP" "$PORT"
}
get_slot_info()
{
status=$(printf "slot-info\nexit\n" | connect_to_server | tail -n +4 | head -n -3 | sed -e 's/\(\W\?\)\(False\|True\)\(\W\?\)/\1"\2"\3/g')
if [ "$1" == "-s" ]
then
status=$(echo "$status" | jq -c '.[]')
fi
echo "$status"
}
pause()
{
slot_id="$1"
printf "pause %s\nexit\n" "$slot_id" | connect_to_server > /dev/null
}
function get_simulation_info()
{
slot_id="$1"
status=$(printf "simulation-info %s\nexit\n" "$slot_id"| connect_to_server | head -n -3 | tail -n +4 | jq)
if [ "$2" == "-s" ]
then
status=$(echo "$status" | jq -c '.[]')
fi
echo "$status"
}
get_progress()
{
total_progress=""
while read -r slot_info
do
slot_id=$(echo "$slot_info" | jq '.id' | sed 's/\"//g')
slot_progress=$(get_simulation_info "$slot_id" | jq '.progress')
slot_progress=$(echo "$slot_progress * 100" | bc)
status="($(get_slot_info | jq ".[$slot_id].status" | sed 's/"//g'))"
total_progress="$total_progress, Slot #$slot_id: $slot_progress% $status"
done < <(get_slot_info -s)
total_progress=$(echo "$total_progress" | tail -c +3)
echo "$total_progress"
}
start()
{
slot_id="$1"
printf "unpause %s\nexit\n" "$slot_id" | connect_to_server >/dev/null
}
toggle_slot()
{
slot_id="$1"
if [ -z "$slot_id" ]
then
echo 'Slot id missing'
exit 1
fi
if [ "$(get_slot_info | jq ".[$slot_id].status" | sed 's/"//g')" == 'RUNNING' ]
then
pause "$slot_id"
else
start "$slot_id"
fi
}
toggle()
{
if [ -n "$1" ]
then
toggle_slot "$1"
fi
while read -r slot_info
do
slot_id=$(echo "$slot_info" | jq '.id' | sed 's/\"//g')
toggle_slot "$slot_id"
done < <(get_slot_info -s)
}
options=("pause" "progress" "start" "status" "toggle" "toggle-slot")
case "$1" in
${options[0]}) pause "$2" ;;
"${options[1]}") get_progress ;;
"${options[2]}") start "$2" ;;
"${options[3]}") get_slot_info | jq ;;
"${options[4]}") toggle "$2" ;;
"${options[5]}") toggle_slot "$2" ;;
*) echo "Usage: $(basename "$0") [$(echo "${options[@]}" | tr ' ' '|')]" && exit 1
esac