forked from rhastie/build-nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·139 lines (107 loc) · 4.59 KB
/
entrypoint.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
#!/bin/bash
# Use Bash as default command shell
# Config file processing procedures
sed_escape() {
sed -e 's/[]\/$*.^[]/\\&/g'
}
cfg_write() { # path, key, value
cfg_delete "$1"
echo "$1=$2" >> "$config_file"
}
cfg_read() { # path, key -> value
test -f "$config_file" && grep "^$(echo "$1" | sed_escape)=" "$config_file" | sed "s/^$(echo "$1" | sed_escape)=//" | tail -1
}
cfg_delete() { # path, key
test -f "$config_file" && sed -i "/^$(echo "$1" | sed_escape).*$/d" "$config_file"
}
cfg_haskey() { # path, key
test -f "$config_file" && grep "^$(echo "$1" | sed_escape)=" "$config_file" > /dev/null
}
do_params() { # get global parameters from config file and set alternative defaults
echo -e "\nReading global parameters and setting defaults"
if cfg_haskey node_json; then
node_json=$(cfg_read node_json)
echo -e "Using Node JSON file $node_json"
else
node_json="/home/node.json"
echo -e "Default to Registry JSON file /home/node.json"
fi
}
###
# Main body of entrypoint script starts here
###
echo -e "\nStart of container entrypoint.sh BASH script..."
# If we were given arguments, override the default command and interpret in /bin/bash
if [ $# -gt 0 ]; then
exec $@
exit $? # Make sure we really exit
fi
# Define path to configuration file globally
config_file="/home/container-config"
# Get global parameters and set defaults
do_params
# Adjust node.json to update/add "label" with relevant "$(hostname)" data
echo -e "\nChecking for update_label parameter"
if cfg_haskey update_label && [ "$(cfg_read update_label)" = "TRUE" ]; then
# Update label field in node.json
echo -e "Insert/Replace label: with $(hostname) hostname in $node_json file"
jq --arg key "$(hostname)-node" '. + {label: $key}' "$node_json" > /home/node.json.tmp
mv /home/node.json.tmp $node_json
fi
# Adjust registry.json to update/add "ptp_domain_number" with relevant PTP Domain data if on a Mellanox switch
echo -e "\nChecking for update_ptp_domain parameter"
if cfg_haskey update_ptp_domain && [ "$(cfg_read update_ptp_domain)" = "TRUE" ]; then
# Retrieve switch username and password from config file
username=$(cfg_read switch_username)
password=$(cfg_read switch_password)
# Test for being on a Mellanox switch
json_data="{\"username\": \""$username"\", \"password\": \""$password"\", \"cmd\": \"show ptp\", \"execution_type\": \"sync\"}"
echo "Sending JSON: "$json_data
curl -k -L -X POST -d "$json_data" -c /dev/null --connect-timeout 5 \
"https://localhost/admin/launch?script=rh&template=json-request&action=json-login" > /home/ptp_data
# If Error Code is 0 - Then we must be running on a Mellanox switch
if [ $? -eq 0 ]; then
# Test for OK response from switch in returned JSON output
if [[ $(jq -r '.status' /home/ptp_data) == "OK" ]]; then
# Recover PTP domain number from JSON output
ptp_domain=$(jq -r '.data[0].Domain' /home/ptp_data)
echo -e "BC PTP Domain on Mellanox Switch is set to: $ptp_domain"
echo -e "Insert/Replace ptp_domain_number: with $ptp_domain in $registry_json file"
jq --argjson key "$ptp_domain" '. + {ptp_domain_number: $key}' "$registry_json" > /home/registry.json.tmp
mv /home/registry.json.tmp $registry_json
else
# We got an ERROR from the Switch report why we had error
echo -e "Switch did not return valid PTP data. Error response from Mellanox switch is:"
cat /home/ptp_data
fi
else
# Not running on a Mellanox switch so report error
echo -e "updata_ptp_domain set but not running on a Mellanox switch - not updating ptp_domain_number in $registry_json"
fi
# Clean up /home/ptp_data file
rm /home/ptp_data
fi
# You should use either Avahi or Apple mDNS - DO NOT use both
#
# mDNSResponder 878.30.4
echo -e "\nStarting mDNSResponder service"
/etc/init.d/mdns start
# Avahi
#echo -e "\nStarting dbus and avahi services"
#/etc/init.d/dbus start
#/etc/init.d/avahi-daemon start
# Start Sony Registry, Sony Node and/or MQTT Broker inside correct directory with logging on or off
sleep 1
# Start Sony Node Application
echo -e "\nStarting Sony Node Application with following config"
cat $node_json
if cfg_haskey log_output && [ "$(cfg_read log_output)" = "TRUE" ]; then
echo -e "\nStarting with file logging"
/home/nmos-cpp-node $node_json >>/home/lognode-err.txt 2>/home/lognode-out.txt
else
echo -e "\nStarting with output to console"
/home/nmos-cpp-node $node_json
fi
ret=$?
echo -e "\nEnd of script..."
exit $ret # Make sure we really exit