This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_devices.sh
78 lines (65 loc) · 1.98 KB
/
check_devices.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
#!/bin/bash
echo "$(date -u) - Running check_devices.sh" >>"$(pwd)"/logs/scripts.log
echo "-- $(date -u) - Running check_devices.sh" >>"$(pwd)"/logs/log.txt
. functions.sh
. sp.config
get_ports
# Loop each device
change_something=0
# shellcheck disable=SC2154
echo "Return Ports: $return_ports" >>"$(pwd)"/logs/log.txt
declare -A newDevices
# shellcheck disable=SC2154
for ii in "${!spDevices[@]}"; do
val=${spDevices[$ii]}
# shellcheck disable=SC2206
arrIN=(${val//,/ })
id=${arrIN[0]}
lastPort=${arrIN[1]}
found="0"
# Check current devices
for x in $return_ports; do
dev_id=$(bash get_device_id.sh "$x")
if [ "$dev_id" == "$id" ]; then
# Found it!
found="1"
if [ "$x" != "$lastPort" ]; then
echo "spDevices[$ii] - New port: $x - ${spDevices[$ii]}" >>"$(pwd)"/logs/log.txt
# Is a new port!
lastPort=$x
change_something=1
break
else
echo "spDevices[$ii] - Same port: $x - ${spDevices[$ii]}" >>"$(pwd)"/logs/log.txt
break
fi
fi
done # end loop current ports
# Check if it was found
if [ "$found" == "0" ]; then
echo "spDevices[$ii] - Port not found - ${spDevices[$ii]}" >>"$(pwd)"/logs/log.txt
# This device is no longer plugged in, consider it a change
if [[ -n $lastPort ]]; then
lastPort=""
change_something=1
fi
fi
newDevices[$ii]=$lastPort
done
# Proceed
if [ "$change_something" == "1" ]; then
# A device has changed port!
echo "Generating docker-compose.yaml file - $(date -u)" >>"$(pwd)"/logs/device.log
for index in "${!newDevices[@]}"; do
val=${spDevices[$index]}
# shellcheck disable=SC2206
arrIN=(${val//,/ })
{
echo "Changing from - to;"
echo "spDevices\[$index\]=${val}"
echo "spDevices\[$index\]=${arrIN[0]},${newDevices[$index]}"
} >>"$(pwd)"/logs/device.log
sed -i "s-spDevices\[$index\]=${val}-spDevices\[$index\]=${arrIN[0]},${newDevices[$index]}-gI" sp.config
done
bash generate_yaml.sh
fi