-
Notifications
You must be signed in to change notification settings - Fork 5
/
pre-command
executable file
·171 lines (125 loc) · 3.4 KB
/
pre-command
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
#!/bin/bash
set -euo pipefail
PLUGIN_NAME=lambdatest-buildkite-plugin
# Allow passing TMP_DIR for testing purposes
if [[ -z "${TMP_DIR:-}" ]]; then
TMP_DIR="${TMPDIR:-/tmp}/${PLUGIN_NAME}"
mkdir -p "${TMP_DIR}"
export BUILDKITE_PLUGIN_LAMBDATEST_TMP_DIR="${TMP_DIR}"
fi
# shellcheck disable=SC2034
DEFAULT_LAMBDATEST_VERSION="1.0.0"
error() {
local message="$1"
echo "lambdatest: [lambdatest-buildkite-plugin] Error: ${message}" >&2
exit 1
}
check_set() {
local name="$1"
if [[ -z "${!name:-}" ]]; then
error "${name} not set"
fi
}
maybe_download_lt_archive() {
local lt_binary="$1"
local lt_archive_path="${TMP_DIR}/${lt_binary}"
if [[ ! -e "${lt_archive_path}" ]]; then
# shellcheck disable=SC2034
local tmp_archive_path
curl -fsSL --create-dirs --retry 5 -o "LT_binary.zip" "https://downloads.lambdatest.com/tunnel/v3/${lt_binary}"
unzip "LT_binary.zip"
fi
}
maybe_download_lt() {
if [[ -e "${TMP_DIR}/LT_binary.zip" ]]; then
echo "Binary already exists. Skipping download."
return
fi
pushd "${TMP_DIR}" >/dev/null || return
local os=""
local lt_binary=""
case "${OSTYPE}" in
linux*)
os="linux"; lt_binary="linux/64bit/LT_Linux.zip"
;;
darwin*)
os="osx"; lt_binary="mac/64bit/LT_Mac.zip"
;;
cygwin*|msys*|win32*|win64*)
# shellcheck disable=SC2034
os="Windows"; lt_binary="windows/64bit/LT_Windows.zip"
;;
*) error "Unknown OS: ${OSTYPE}";;
esac
local lt_zip_path
# shellcheck disable=SC2034
lt_zip_path=$(maybe_download_lt_archive "${lt_binary}")
popd >/dev/null || return
}
main() {
check_set LT_USERNAME
check_set LT_ACCESS_KEY
[[ -z "${BUILDKITE_PLUGIN_LAMBDATEST_TUNNEL_IDENTIFIER:-}" ]] && check_set BUILDKITE_JOB_ID
# shellcheck disable=SC2034
local tunnel_identifier="${BUILDKITE_PLUGIN_LAMBDATEST_TUNNEL_IDENTIFIER:-${BUILDKITE_JOB_ID}}"
maybe_download_lt
case "${OSTYPE}" in
linux*)
export LT_TUNNEL_NAME="LT-${BUILDKITE_JOB_ID}"
;;
darwin*)
export LT_TUNNEL_NAME="LT-${BUILDKITE_JOB_ID}"
;;
cygwin*|msys*|win32*|win64*)
setx LT_TUNNEL_NAME "LT-${BUILDKITE_JOB_ID}" /M
;;
*)
error "Unknown OS: ${OSTYPE}"
;;
esac
# shellcheck disable=SC2153
start \
"LT-${BUILDKITE_JOB_ID}" \
"${LT_USERNAME}" \
"${LT_ACCESS_KEY}"
}
start() {
local tunnel_name="$1"
local lt_username="$2"
local lt_access_key="$3"
echo "LambdaTest: Starting Tunnel"
local pidfile="${TMP_DIR}/pid.${BUILDKITE_JOB_ID}"
local logfile="${TMP_DIR}/LT-${BUILDKITE_JOB_ID}.log"
pushd "${TMP_DIR}" >/dev/null
if [[ -z "${IS_UNDER_TEST:-}" ]]; then
export PATH="${TMP_DIR}:${PATH}"
fi
./LT \
-u "${lt_username}" \
-k "${lt_access_key}" \
--tunnelName "${tunnel_name}" \
--pidfile "${pidfile}" \
--logFile "${logfile}" \
--verbose \
&
# shellcheck disable=SC2034
local pid="$!"
local last_size=0
while true; do
sleep 10
# Check the current size of the logfile
local current_size
if [[ "${OSTYPE}" == "cygwin" || "${OSTYPE}" == "msys" ]]; then
current_size=$(cmd.exe /c "for %I in ($logfile) do @echo %~zI")
else
current_size=$(wc -c < "${logfile}" | tr -d ' ')
fi
if [ "$current_size" -eq "$last_size" ]; then
echo "No more output from ./LT process for 10 seconds. Moving on."
break
fi
last_size=$current_size
done
popd >/dev/null
}
main