forked from Azure/iot-hub-device-update
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig-integration.sh
More file actions
executable file
·138 lines (123 loc) · 3.67 KB
/
config-integration.sh
File metadata and controls
executable file
·138 lines (123 loc) · 3.67 KB
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
#!/bin/bash
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Ensure we dont end the user's terminal session if invoked from source (".").
if [[ $0 != "${BASH_SOURCE[0]}" ]]; then
ret=return
else
ret=exit
fi
error() { echo -e "\033[1;31mError:\033[0m $*" >&2; }
warn() { echo -e "\033[1;33mWarning:\033[0m $*" >&2; }
current_schema_version="1.0"
conf_file=$1
new_conf_file="/etc/adu/du-config.json"
if [ ! -f "$conf_file" ]; then
error "Could not find the existing conf file"
$ret 1
fi
connection_string=$(grep "^connection_string\s*=\s*" "$conf_file" | sed -e "s/^connection_string\s*=\s*//")
if [ ! "$connection_string" ]; then
error "File does not contain connection_string"
$ret 1
fi
aduc_manufacturer=$(grep "^aduc_manufacturer\s*=\s*" "$conf_file" | sed -e "s/^aduc_manufacturer\s*=\s*//")
aduc_model=$(grep "^aduc_model\s*=\s*" "$conf_file" | sed -e "s/^aduc_model\s*=\s*//")
manufacturer=$(grep "^manufacturer\s*=\s*" "$conf_file" | sed -e "s/^manufacturer\s*=\s*//")
model=$(grep "^model\s*=\s*" "$conf_file" | sed -e "s/^model\s*=\s*//")
json_content=$(
cat << END_OF_JSON
{
"schemaVersion": "$current_schema_version",
"aduShellTrustedUsers": [
"adu",
"do"
],
<% MANUFACTURER %>
<% MODEL %>
"agents": [
{
"name": "host-update",
"runas": "adu",
"connectionSource": {
"connectionType": "string",
"connectionData": "$connection_string"
}<% ADUC_MANUFACTURER %><% ADUC_MODEL %>
}
]
}
END_OF_JSON
)
if [[ "$manufacturer" ]]; then
manufacturer_json=$(
cat << EOM
"manufacturer": "$manufacturer",
EOM
)
json_content=${json_content/<% MANUFACTURER %>/$manufacturer_json}
else
warn "DeviceInfo manufacturer is mandatory, please add it in $new_conf_file"
no_manufacturer_json=$(
cat << EOM
"manufacturer": <INSERT DEVICE INFO MANUFACTURER>,
EOM
)
json_content=${json_content/<% MANUFACTURER %>/$no_manufacturer_json}
fi
if [[ "$model" ]]; then
model_json=$(
cat << EOM
"model": "$model",
EOM
)
json_content=${json_content/<% MODEL %>/$model_json}
else
warn "DeviceInfo model is mandatory, please add it in $new_conf_file"
no_model_json=$(
cat << EOM
"model": <INSERT DEVICE INFO MODEL>,
EOM
)
json_content=${json_content/<% MODEL %>/$no_model_json}
fi
if [[ "$aduc_manufacturer" ]]; then
aduc_manufacturer_json=$(
cat << EOM
,
"manufacturer": "$aduc_manufacturer"
EOM
)
processed_aduc_manufacturer_json=$(echo "$aduc_manufacturer_json" | tr '\n' '#' | sed -e 's/#$//g')
json_content=$(echo "$json_content" | sed -e "s/<% ADUC_MANUFACTURER %>/$processed_aduc_manufacturer_json/g;s/#/\n/g")
else
warn "DeviceProperty manufacturer (aduc_manufacturer) is mandatory, please add it in $new_conf_file"
no_aduc_manufacturer_json=$(
cat << EOM
,
"manufacturer": <INSERT DEVICE PROPERTY MANUFACTURER>
EOM
)
json_content=${json_content/<% ADUC_MANUFACTURER %>/$no_aduc_manufacturer_json}
fi
if [[ "$aduc_model" ]]; then
aduc_model_json=$(
cat << EOM
,
"model": "$aduc_model"
EOM
)
processed_aduc_model_json=$(echo "$aduc_model_json" | tr '\n' '#' | sed -e 's/#$//g')
json_content=$(echo "$json_content" | sed -e "s/<% ADUC_MODEL %>/$processed_aduc_model_json/g;s/#/\n/g")
else
warn "DeviceProperty model (aduc_model) is mandatory, please add it in $new_conf_file"
no_aduc_model_json=$(
cat << EOM
,
"model": <INSERT DEVICE PROPERTY MODEL>
EOM
)
json_content=${json_content/<% ADUC_MODEL %>/$no_aduc_model_json}
fi
echo "$json_content" | sudo tee $new_conf_file || {
error "Failed to write to the new configuration file"
}