-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot-domain-create
executable file
·298 lines (234 loc) · 6.06 KB
/
iot-domain-create
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
#!/bin/bash -e
CPWD="$( cd "$(dirname "$0")" ; pwd -P )"
#----------------------------------------------------#
# COMMON ATTRIBUTES #
#----------------------------------------------------#
source $CPWD/scripts/iot-utils
#----------------------------------------------------#
# DEFINES / MACROS #
#----------------------------------------------------#
LOG=$LOGS_DIR/iot-domain-create.log
#----------------------------------------------------#
# CMDLINE OPTIONS #
#----------------------------------------------------#
ARGC=$#
ARGV=("$@")
VERBOSE=false
HELP=false
# Clear args
set --
COUNT=0
while [ $COUNT -lt $ARGC ]; do
STRIP=${ARGV[COUNT]}
ID=0
LONGFORMAT=`echo $STRIP | cut -b 1-2`
if [ "x$LONGFORMAT" == "x--" ]; then
STRIP=`echo ${ARGV[COUNT]} | cut -b 2-3`
else
ID=`echo $STRIP | cut -b 3-`
STRIP=`echo $STRIP | cut -b 1-2`
fi
if [ "x$STRIP" == "x-v" ]; then
VERBOSE=true
elif [ "x$STRIP" == "x-h" ]; then
HELP=true
else
pl
pr "Error: Unknown argument"
COUNT=$ARGC
HELP=true
fi
COUNT=$((COUNT+1))
done
log "Started"
pl
pg "iot-domain-create ($VERSION)"
if [ "$HELP" == "true" ]; then
pl
pn "Usage:"
pl
pn "Create new domain with latest settings and payload."
pl
pn "iot-domain-create [-v] [-h] ..."
pl
pn "Help:"
pl
pn "-v (--verbose) Verbose output for operations"
pn "-h (--help) Print this"
pl
exit 1
fi
#----------------------------------------------------#
# PREPARE #
#----------------------------------------------------#
pn
pg ":: Setup & Checks ::"
pn
common_setup
log "Common Setup OK"
pnn "- Admin permissions enabled : "
admin_check
pg "Yes"
log "Admin OK"
#----------------------------------------------------#
# BODY #
#----------------------------------------------------#
pl
pg ":: Add Domain Confirmation ::"
pl
pnn "Type 'ok' and Enter to continue : "
read SELECT
if [ "x$SELECT" != "xok" ]; then
err "User aborted."
fi
log "Confirmation granted"
pl
pg ":: Add Domain ::"
pl
pn "A domain consists of a Customer ID and a Site ID. It is"
pn "possible for multiple Site IDs to be associated with a"
pn "single Customer ID, not the other way around. Do you"
pn "want to create a new Customer ID first, or do you want"
pn "to add a new Site ID to an existing Customer ID?"
pl
pnn "Create new Customer ID? [y, N] "
read -n1 -s SELECT
pl
if [ "x$SELECT" == "xY" ] || [ "x$SELECT" == "xy" ]; then
log "Creating Customer ID"
pnn "Specify Customer ID : cust-"
read CUST
CUST=cust-$CUST
if [ "x$CUST" == "xcust-" ]; then
err "Empty Customer ID suffix is not allowed"
fi
if [ -d $MASTER_DB/$CUST ]; then
err "Customer ID cannot be created, it already exists ($CUST)"
fi
else
log "Selecting Customer ID"
mkdir -p $MASTER_DB
CUST_LIST=($(ls $MASTER_DB | grep cust- || true))
CUST_LIST_MAX=${#CUST_LIST[@]}
SELECT=$CUST_LIST_MAX
ITER=0
while [ $SELECT -lt 0 ] || [ $SELECT -ge $CUST_LIST_MAX ] ; do
if [ $CUST_LIST_MAX -eq 0 ]; then
break
fi
if [ $ITER -ne 0 ]; then
pl
pr "Error: Your choice is invalid!"
pl
fi
pn "Please select the Customer ID from the list:"
COUNT=0
while [ $COUNT -lt $CUST_LIST_MAX ]; do
pn "[$(printf "%2d" $((COUNT + 1)))] ${CUST_LIST[$COUNT]}"
COUNT=$((COUNT + 1))
done
pl
pnn "Choice? : "
read SELECT
SELECT=$((SELECT - 1))
ITER=$((ITER + 1))
done
CUST=${CUST_LIST[$SELECT]}
fi
if [ -z "$CUST" ]; then
err "No valid Customer ID found! Please create a new Customer ID"
fi
pl
pn "If you select an existing Site ID, all the settings for"
pn "the domain will be updates to match the latest settings"
pn "and payload."
pl
pnn "Create new Site ID? [y, N] "
read -n1 -s SELECT
pl
if [ "x$SELECT" == "xY" ] || [ "x$SELECT" == "xy" ]; then
log "Creating Site ID"
pnn "Specify Site ID : site-"
read SITE
SITE=site-$SITE
if [ "x$SITE" == "xsite-" ]; then
err "Empty Site ID suffix is not allowed"
fi
if [ -d $MASTER_DB/$CUST/$SITE ]; then
err "Site ID cannot be created, it already exists ($SITE)"
fi
else
log "Selecting Site ID"
mkdir -p $MASTER_DB/$CUST
SITE_LIST=($(ls $MASTER_DB/$CUST | grep site- || true))
SITE_LIST_MAX=${#SITE_LIST[@]}
SELECT=$SITE_LIST_MAX
ITER=0
while [ $SELECT -lt 0 ] || [ $SELECT -ge $SITE_LIST_MAX ]; do
if [ $SITE_LIST_MAX -eq 0 ]; then
break
fi
if [ $ITER -ne 0 ]; then
pl
pr "Error: Your choice is invalid!"
pl
fi
pn "Please select the Site ID from the list:"
COUNT=0
while [ $COUNT -lt $SITE_LIST_MAX ]; do
pn "[$(printf "%2d" $((COUNT + 1)))] ${SITE_LIST[$COUNT]}"
COUNT=$((COUNT + 1))
done
pl
pnn "Choice? : "
read SELECT
SELECT=$((SELECT - 1))
ITER=$((ITER + 1))
done
SITE=${SITE_LIST[$SELECT]}
fi
if [ -z "$SITE" ]; then
err "No valid Site ID found! Please create a new Site ID"
fi
pl
pg ":: Final Confirmation ::"
pl
if [ -d $MASTER_DB/$CUST/$SITE ]; then
pn "The following domain already exist and will be updated:"
log "Domain exist and will be updated"
else
pn "The following domain will be created:"
log "Domain will be created"
fi
pl
pnn "Customer ID : "
pg $CUST
pnn "Site ID : "
pg $SITE
pl
pnn "Type 'ok' and Enter to continue : "
read SELECT
if [ "x$SELECT" != "xok" ]; then
err "User aborted."
fi
log "Creating Customer ID: $CUST, Site ID: $SITE"
pl
pg ":: Performing Actions ::"
pl
pnn "- Creating domain directory structure : "
mkdir -p $MASTER_DB/$CUST/$SITE/payload
mkdir -p $MASTER_DB/$CUST/$SITE/settings
pg "Done"
pnn "- Copying latest settings : "
run cp -Rf $REPO_ROOT/settings/* $MASTER_DB/$CUST/$SITE/settings
if [ ! -f $MASTER_DB/$CUST/$SITE/settings/version.cfg ]; then
echo "0" > $MASTER_DB/$CUST/$SITE/settings/version.cfg
fi
pg "Done"
pnn "- Copying latest payload : "
run cp -Rf $REPO_ROOT/payload/* $MASTER_DB/$CUST/$SITE/payload
if [ ! -f $MASTER_DB/$CUST/$SITE/payload/version.cfg ]; then
echo "0" > $MASTER_DB/$CUST/$SITE/payload/version.cfg
fi
pg "Done"
pl