forked from IbcAlpha/IBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_run.sh
280 lines (223 loc) · 7.76 KB
/
_run.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
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
#!/bin/bash
#################""" TTTTTTTTTOOOOOOOOOOOODDDDDDDDDDOOOOOOOOOO "###############
# DEPENDENCY PACKAGES : "libxtst6", "libxi6"
#########################################
# #
# Global variables: definitions #
# #
#########################################
entry_point=ibcalpha.ibc.IbcGateway
ibg_path="$g_path/modules/ibg"
tws_settings_path="$ibg_path/Jts"
mkdir -p "$tws_settings_path" # Make sure the settings dir exists
ibc_ini="$l_dir/_IBconfig.ini"
java_path="$ibg_path/jre/bin"
jars="$ibg_path/jars"
install4j="$ibg_path/.install4j"
vmoptions_source="$ibg_path/ibgateway.vmoptions"
# errorlevel set by IBC if second factor authentication dialog times out and
# ExitAfterSecondFactorAuthenticationTimeout setting is true
let E_2FA_DIALOG_TIMED_OUT=$((1111 % 256))
# errorlevel set by IBC if login dialog is not displayed within the time
# specified in the LoginDialogDisplayTimeout setting
E_LOGIN_DIALOG_DISPLAY_TIMEOUT=$((1112 % 256))
generate_ini() {
#########################################
# #
# Generate the IBC.ini config file #
# #
#########################################
json_file="$l_dir/IBconfig.json"
# Check if the JSON file exists
if [ ! -f "$json_file" ]; then
echo "Error: IBC JSON config file not found at $json_file"
exit 1
fi
# Extract customizable ("internal"=false) setting-value pairs and write them in the .init file
jq -r '
def extract_pairs:
to_entries
| map(
if .value | type == "object" and has("value") and (.internal == false) then
"\(.key)=\(.value.value)"
elif .value | type == "object" or type == "array" then
.value | extract_pairs
else
empty
end
)
| .[];
extract_pairs
' "$json_file" > "$ibc_ini"
# Check proper generation of .ini file
if [ ! -f "$ibc_ini" ]; then
echo "Error: $ibc_ini file not generated properly."
exit 1
fi
# Implement the internal settings
jq -r '
def extract_internal_settings:
to_entries
| map(
if .value | type == "object" and has("default") and (.internal == true) then
"\(.key)=\(.value.default)"
elif .value | type == "object" or type == "array" then
.value | extract_internal_settings
else
empty
end
)
| .[];
extract_internal_settings
' "$json_file" | while IFS='=' read -r key default_value; do
eval "value=\"$default_value\""
echo "$key=$value" >> "$ibc_ini"
done
}
run_ibg() {
generate_ini
#########################################
# #
# Construct the classpath #
# #
#########################################
declare ibc_classpath
for jar in "${jars}"/*.jar; do
if [[ -n "${ibc_classpath}" ]]; then
ibc_classpath="${ibc_classpath}:"
fi
ibc_classpath="${ibc_classpath}${jar}"
done
ibc_classpath="${ibc_classpath}:$install4j/i4jruntime.jar:${l_dir}/IBC.jar"
########################################
# #
# Generate the JAVA VM options #
# #
########################################
declare -a vm_options
index=0
while read line; do
if [[ -n ${line} && ! "${line:0:1}" = "#" && ! "${line:0:2}" = "-D" ]]; then
vm_options[$index]="$line"
((index++))
fi
done <<< $(cat ${vmoptions_source})
java_vm_options=${vm_options[*]}
java_vm_options="$java_vm_options -Dtwslaunch.autoupdate.serviceImpl=com.ib.tws.twslaunch.install4j.Install4jAutoUpdateService"
java_vm_options="$java_vm_options -Dchannel=latest"
java_vm_options="$java_vm_options -Dexe4j.isInstall4j=true"
java_vm_options="$java_vm_options -Dinstall4jType=standalone"
java_vm_options="$java_vm_options -DjtsConfigDir=${tws_settings_path}"
java_vm_options="$java_vm_options -DinstallDir=${ibg_path}"
ibc_session_id=$(mktemp -u XXXXXXXX)
java_vm_options="$java_vm_options -Dibcsessionid=$ibc_session_id"
########################################
# #
# Auto-restart handling #
# #
########################################
find_auto_restart() {
local autorestart_path=""
local f=""
restarted_needed=
for i in $(find $tws_settings_path -type f -name "autorestart"); do
local x=${i/$tws_settings_path/}
local y=$(echo $x | xargs dirname)/.
local e=$(echo "$y" | cut -d/ -f3)
if [[ "$e" = "." ]]; then
if [[ -z $f ]]; then
f="$i"
echo "autorestart file found at $f"
autorestart_path=$(echo "$y" | cut -d/ -f2)
else
autorestart_path=
echo "WARNING: deleting extra autorestart file found at $i"
rm $i
echo "WARNING: deleting first autorestart file found"
rm $f
fi
fi
done
if [[ -z $autorestart_path ]]; then
if [[ -n $f ]]; then
echo "*******************************************************************************"
echo "WARNING: More than one autorestart file was found. IBC can't determine which is"
echo " the right one, so they've all been deleted. Full authentication will"
echo " be required."
echo
echo " If you have two or more TWS/Gateway instances with the same setting"
echo " for TWS_SETTINGS_PATH, you should ensure that they are configured with"
echo " different autorestart times, to avoid creation of multiple autorestart"
echo " files."
echo "*******************************************************************************"
echo
restarted_needed=yes
else
echo "autorestart file not found"
echo
restarted_needed=
fi
else
echo "AUTORESTART_OPTION is -Drestart=${autorestart_path}"
autorestart_option=" -Drestart=${autorestart_path}"
restarted_needed=yes
fi
}
find_auto_restart
########################################
# #
# Main loop #
# #
########################################
_run_ibg() {
while :; do
echo -e "\n\n\n\n\n -------------------------------------------- \n ---------- MAIN LOOP ---------- \n ---------------------------------------------------------- \n\n\n\n\n"
# forward signals (see https://veithen.github.io/2014/11/16/sigterm-propagation.html)
trap 'kill -TERM $PID' TERM INT
"$java_path/java" -cp "$ibc_classpath" $java_vm_options$autorestart_option $entry_point $ibc_ini ${mode} &
PID=$!
wait $PID
trap - TERM INT
wait $PID
exit_code=$(($? % 256))
echo "IBC returned exit status $exit_code"
if [[ $exit_code -eq $E_LOGIN_DIALOG_DISPLAY_TIMEOUT ]]; then
:
elif [[ -e "${tws_settings_path}/COLDRESTART$ibc_session_id" ]]; then
rm "${tws_settings_path}/COLDRESTART$ibc_session_id"
autorestart_option=
echo "IBC will cold-restart shortly"
else
find_auto_restart
if [[ -n $restarted_needed ]]; then
restarted_needed=
# restart using the TWS/Gateway-generated autorestart file
:
elif [[ $exit_code -ne $E_2FA_DIALOG_TIMED_OUT ]]; then
break;
elif [[ ${twofa_to_action_upper} != "RESTART" ]]; then
break;
fi
fi
# wait a few seconds before restarting
echo "IBC will restart shortly"
echo sleep 2
done
}
# Global variable storing the xvfb display number
_current_xvfb_display_=
source "$l_dir/scripts/xvfb-functions.sh"
start_xvfb
export DISPLAY=":$_current_xvfb_display_"
echo "[DEBUG] DISPLAY was set to $DISPLAY"
# prevent other Java tools interfering with IBC
_JAVA_TOOL_OPTIONS=$JAVA_TOOL_OPTIONS
JAVA_TOOL_OPTIONS=
pushd "$tws_settings_path" > /dev/null
_run_ibg
popd > /dev/null
JAVA_TOOL_OPTIONS=$_JAVA_TOOL_OPTIONS
stop_xvfb $_current_xvfb_display_
exit $exit_code
}
run_ibg