-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathzebrunner.sh
executable file
·323 lines (273 loc) · 8.81 KB
/
zebrunner.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
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
#!/bin/bash
## shellcheck disable=SC1091
#source patch/utility.sh
replace() {
#TODO: https://github.com/zebrunner/zebrunner/issues/328 organize debug logging for setup/replace
file=$1
#echo "file: $file"
content=$(< $file) # read the file's content into
#echo "content: $content"
old=$2
#echo "old: $old"
new=$3
#echo "new: $new"
content=${content//"$old"/$new}
#echo "content: $content"
printf '%s' "$content" > $file # write new content to disk
}
setup() {
if [ -f roles/devices/vars/main.yml ]; then
echo "roles/devices/vars/main.yml already exists, making a backup roles/devices/vars/main.yml.bak"
cp roles/devices/vars/main.yml roles/devices/vars/main.yml.bak
fi
cp roles/devices/vars/main.yml.original roles/devices/vars/main.yml
if [ -f roles/mac-devices/vars/main.yml ]; then
echo "roles/mac-devices/vars/main.yml already exists, making a backup roles/mac-devices/vars/main.yml.bak"
cp roles/mac-devices/vars/main.yml roles/mac-devices/vars/main.yml.bak
fi
cp roles/mac-devices/vars/main.yml.original roles/mac-devices/vars/main.yml
if [ -d $HOME/Library/LaunchAgents ] && [ ! -f $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist ]; then
# register devices manager to manage attach/reboot actions
cp roles/mac-devices/templates/ZebrunnerDevicesListener.plist $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist
replace $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist "working_dir_value" "${BASEDIR}"
replace $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist "user_value" "$USER"
fi
if [ -d $HOME/Library/LaunchAgents ] && [ ! -f $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist ]; then
# register socat listener for usbmuxd service
cp roles/mac-devices/templates/ZebrunnerUsbmuxd.plist $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist
replace $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist "working_dir_value" "${BASEDIR}"
replace $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist "user_value" "$USER"
launchctl load $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist
fi
#TODO: switch to master branch after oficial release and merge
echo "Follow https://github.com/zebrunner/mcloud-agent/tree/master#run-ansible-playbook to deploy MCloud agent services!"
}
shutdown() {
if [ ! -f /usr/local/bin/zebrunner-farm ]; then
echo_warning "You have to setup services in advance using: ./zebrunner.sh setup"
echo_telegram
exit -1
fi
echo_warning "Shutdown will erase all settings and data for \"${BASEDIR}\"!"
confirm "" " Do you want to continue?" "n"
if [[ $? -eq 0 ]]; then
exit
fi
if [ -f $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist ]; then
launchctl unload $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist
rm -f $HOME/Library/LaunchAgents/ZebrunnerDevicesListener.plist
fi
if [ -f $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist ]; then
launchctl unload $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist
rm -f $HOME/Library/LaunchAgents/ZebrunnerUsbmuxd.plist
fi
down
sudo rm -f /usr/local/bin/zebrunner-farm
sudo rm -f /usr/local/bin/mcloud-devices.txt
sudo rm -f /etc/udev/rules.d/90_mcloud.rules
# restore original main.yml
rm -f roles/devices/vars/main.yml
rm -f roles/mac-devices/vars/main.yml
docker volume rm appium-storage-volume
docker volume rm mcloud-mitm-volume
}
status() {
if [[ ! -f /usr/local/bin/zebrunner-farm ]]; then
echo_warning "MCloud agent is not configured yet! Use: ./zebrunner.sh setup"
echo_telegram
exit -1
fi
/usr/local/bin/zebrunner-farm status $1
}
start() {
if [[ ! -f /usr/local/bin/zebrunner-farm ]]; then
echo_warning "You have to setup services in advance using: ./zebrunner.sh setup"
echo_telegram
exit -1
fi
/usr/local/bin/zebrunner-farm start $1
}
stop() {
/usr/local/bin/zebrunner-farm stop $1
}
down() {
/usr/local/bin/zebrunner-farm down $1
}
backup() {
confirm "" " Do you want to do a backup now?" "n"
if [[ $? -eq 0 ]]; then
exit
fi
# copy udev related files into ./backup folder
cp /usr/local/bin/zebrunner-farm backup/
cp /usr/local/bin/mcloud-devices.txt backup/
cp /etc/udev/rules.d/90_mcloud.rules backup/
cp roles/devices/vars/main.yml roles/devices/vars/main.yml.bak
cp roles/mac-devices/vars/main.yml roles/mac-devices/vars/main.yml.bak
if [ -f backup/zebrunner-farm ] && [ -f backup/mcloud-devices.txt ] && [ -f backup/90_mcloud.rules ] && [ -f roles/devices/vars/main.yml.bak ] && [ -f roles/mac-devices/vars/main.yml.bak ]; then
echo "MCloud backup succeed."
else
echo_warning "MCloud backup failed!"
echo_telegram
fi
}
restore() {
confirm "" " Your services will be stopped and current data might be lost. Do you want to do a restore now?" "n"
if [[ $? -eq 0 ]]; then
exit
fi
sudo cp backup/zebrunner-farm /usr/local/bin/zebrunner-farm
sudo cp backup/mcloud-devices.txt /usr/local/bin/mcloud-devices.txt
sudo cp backup/90_mcloud.rules /etc/udev/rules.d/90_mcloud.rules
cp roles/devices/vars/main.yml.bak roles/devices/vars/main.yml
cp roles/mac-devices/vars/main.yml.bak roles/mac-devices/vars/main.yml
# reload udevadm rules
sudo udevadm control --reload-rules
if [ -f /usr/local/bin/zebrunner-farm ] && [ -f /usr/local/bin/mcloud-devices.txt ] && [ -f /etc/udev/rules.d/90_mcloud.rules ] && [ -f roles/devices/vars/main.yml ] && [ -f roles/mac-devices/vars/main.yml ]; then
echo "MCloud restore succeed."
else
echo_warning "MCloud restore failed!"
echo_telegram
fi
down
echo_warning "Your services needs to be started after restore."
confirm "" " Start now?" "y"
if [[ $? -eq 1 ]]; then
start
fi
}
confirm() {
local message=$1
local question=$2
local isEnabled=$3
if [[ "$isEnabled" == "1" ]]; then
isEnabled="y"
fi
if [[ "$isEnabled" == "0" ]]; then
isEnabled="n"
fi
while true; do
if [[ ! -z $message ]]; then
echo "$message"
fi
read -r -p "$question y/n [$isEnabled]:" response
if [[ -z $response ]]; then
if [[ "$isEnabled" == "y" ]]; then
return 1
fi
if [[ "$isEnabled" == "n" ]]; then
return 0
fi
fi
if [[ "$response" == "y" || "$response" == "Y" ]]; then
return 1
fi
if [[ "$response" == "n" || "$response" == "N" ]]; then
return 0
fi
echo "Please answer y (yes) or n (no)."
echo
done
}
version() {
echo "Zebrunner MCloud Agent"
device_version=$(cat defaults/main.yml | grep DEVICE_VERSION | cut -d ":" -f 2)
echo "zebrunner/mcloud-device:${device_version}"
appium_version=$(cat defaults/main.yml | grep APPIUM_VERSION | cut -d ":" -f 2)
echo "public.ecr.aws/zebrunner/appium:${appium_version}"
}
# IMPORTANT! In case of any changes please copy them in zebrunner-farm !
ansible() {
# Check if the operating system is Linux or macOS
if [[ "$(uname)" == "Linux" ]]; then
file="devices.yml"
elif [[ "$(uname)" == "Darwin" ]]; then
file="mac-devices.yml"
else
echo "This script is not running on a Linux or macOS system. Run ansible manually."
exit 1
fi
# Make a list of arguments
if [[ "$1" == "" ]]; then
arg="$file"
elif [[ "$1" == "devices" ]]; then
arg="$file --tag registerDevices"
else
arg="$@ $file"
fi
# Run ansible with arguments
echo "ansible-playbook -vvv -i hosts $arg"
echo "*******************************************************************"
ansible-playbook -vvv -i hosts $arg
}
echo_warning() {
echo "
WARNING! $1"
}
echo_telegram() {
echo "
For more help join telegram channel: https://t.me/zebrunner
"
}
echo_help() {
echo "
Usage: ./zebrunner.sh [option]
Arguments:
status [udid] Status of MCloud Agent whitelisted devices or exact device by udid
start [udid] Start devices containers or exact device by udid
stop [udid] Stop and keep devices containers or exact device by udid
restart [udid] Restart all devices containers or exact device by udid
down [udid] Stop and remove devices containers
ansible ['devices'] Run ansible-playbook script with custom or predefined args
shutdown Stop and remove devices containers, clear volumes
backup Backup MCloud agent setup
restore Restore MCloud agent setup
version Version of MCloud"
echo_telegram
exit 0
}
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${BASEDIR}" || exit
case "$1" in
status)
status $2
;;
setup)
setup
;;
start)
start $2
;;
stop)
stop $2
;;
restart)
down $2
start $2
;;
down)
down $2
;;
ansible)
ansible "${@:2}"
;;
shutdown)
shutdown
;;
backup)
backup
;;
restore)
restore
;;
version)
version
;;
--help | -h)
echo_help
;;
*)
echo_help
exit 1
;;
esac