This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
factory-ai-vision-install.sh
435 lines (376 loc) · 14.7 KB
/
factory-ai-vision-install.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/bin/bash
# @ECHO OFF
AZURE_CORE_NO_COLOR=
AZURE_CORE_ONLY_SHOW_ERRORS=True
# Many of the az commands return output containing carriage returns that must be removed
CR=$'\r'
# ARM deployment script for Custom Vison solution (Free SKU)
customVisionArm=deploy-custom-vision-arm.json
# edge-deployment-json is the template,
#edgeDeploymentJson=deployment.amd64.json
# edge-deploy-json is the deployment description with keys and endpoints added
edgeDeployJson=deploy.modules.json
# the solution resource group name
rgName=visiononedge-rg
now=`date +"%Y_%m_%d_%H_%M_%S"`
if [ -d "factoryai_configs/factoryai_cfgs" ]; then
while true; do
read -p "Do you want to use the existing config files? (y or n): " -n 1 -r; echo
case $REPLY in
[Yy]* ) isCfg=true; break;;
[Nn]* ) isCfg=false; break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
if [ "$isCfg" = true ]; then
PS3='Choose the number corresponding to the Azure IoT Edge device: '
configs=`ls factoryai_configs/factoryai_cfgs`
select opt in $configs
do
echo "you chose: " $opt
if [ "$opt" != "" ]; then
break
fi
done
source factoryai_configs/factoryai_cfgs/$opt
echo Read from config ...
echo '################################################'
cat factoryai_configs/factoryai_cfgs/$opt
# FOR Backwar Compatibility
if [ "$platform" = "" ]; then
platform="amd64"
fi
echo '################################################'
fi
# azSubscriptonName = The friendly name of the Azure subscription
# iotHubName = The IoT Hub that corresponds to the ASE device
# edgeDeviceId = The device id of the ASE device
# cvTrainingApiKey = The Custom Vision service training key
# cvTrainingEndpoint = The Custom Vision service end point
# cpuGpu = CPU or GPU deployment
# SETLOCAL ENABLEDELAYEDEXPANSION
# ############################## Install Prereqs ##############################
echo Checking the az command
az version --output none
if [ ! $? -eq 0 ]; then
# Azure CLI is not installed. It has an MSI installer on Windows, or is available over REST.
echo
echo It looks like Azure CLI is not installed. Please install it from:
echo https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
echo and try again
read -p "Press any key to exit..."
exit 1
fi
echo Installing / updating the IoT extension
az extension add --name azure-iot
az extension update --name azure-iot
echo Deleting conflict Extension
(az version | grep azure-cli-iot-ext) && az extension remove --name azure-cli-iot-ext
################################ Get Tenant ###################################
# remove the header and ---- from output list - start good var data at var1
#COLUMNS=1
#outputarrten=()
#echo Logging on to Azure...
#output=$(az login -o table --query [].name --only-show-errors)
#let cnt=0
#while IFS=' ' read -r line
#do
# if [ $cnt -gt 1 ]; then
# outputarrten+=("$line")
# fi
# let cnt++
#done <<< "$output"
#
## get length of an array
#tLen=${#outputarrten[@]}
#
#PS3='Choose the number corresponding to your tenant: '
#select opt in "${outputarrten[@]}"
#do
# # remove carriage return
# azSubscriptonName=${opt%$CR}
# echo you chose: $azSubscriptonName
# break
#done
#az account set --subscription "$azSubscriptonName" --only-show-errors
if [ "$isCfg" != true ]; then
################################ Install Custom Vision ###########################
echo You can use your existing Custom Vision service, or create a new one
while true; do
read -p "Would you like to use an existing Custom Vision Service? (y or n): " -n 1 -r; echo
case $REPLY in
[Yy]* ) read -p "Please enter your Custom Vision endpoint: " cvTrainingEndpoint; echo
read -p "Please enter your Custom Vision Key: " cvTrainingApiKey; echo
if [[ -z $cvTrainingEndpoint ]]; then
cvTrainingEndpoint='<Training_Endpoint>'
fi
if [[ -z $cvTrainingApiKey ]]; then
cvTrainingApiKey='<Training_API_Key>'
fi
break;;
[Nn]* ) cvTrainingEndpoint=""; break;;
* ) echo "Please answer yes or no.";;
esac
done
if [ "$cvTrainingEndpoint" == "" ]; then
echo Installing the Custom Vision Service
echo
loc=()
loc+=("eastus")
loc+=("westus2")
loc+=("southcentralus")
loc+=("northcentralus")
PS3='Choose the location: '
select opt in "${loc[@]}"
do
echo "you chose: " $opt
location=${opt%$CR}
break
done
echo Creating resource group - $rgName
output=$(az group create -l $location -n $rgName)
echo Creating Custom Vision Service
outputarrcv=()
# Need to note in the documentation that only one free service per subscription can be created. An existing one results in an error.
output="$(az deployment group create --resource-group $rgName --template-file $customVisionArm --query properties.outputs.*.value -o table --parameters "{ 'location': { 'value': '$location' } }")"
let cnt=0
while read -r line
do
if [ $cnt -gt 1 ]; then
outputarrcv+=("$line")
fi
let cnt++
done <<< "$output"
# get length of an array
tLen=${#outputarrcv[@]}
if [ $tLen -eq 0 ]; then
echo
echo Deployment failed. Please check if you already have a free version of Custom Vision installed.
read -p "Press <Enter> key to exit..."
exit 1
fi
# the Custom Vision variables
cvTrainingApiKey=${outputarrcv[0]}
cvTrainingEndpoint=${outputarrcv[1]}
echo API Key: $cvTrainingApiKey
echo Endpoint: $cvTrainingEndpoint
fi
# ############################## Get Streaming Type #####################################
while true; do
read -p "Do you want to use Azure Video Analyzer? (y or n): " -n 1 -r; echo
case $REPLY in
[Yy]* ) streaming="ava"; break;;
[Nn]* ) streaming="opencv"; break;;
* ) echo "Please answer yes or no.";;
esac
done
if [ "$streaming" == "ava" ]; then
read -p "Please enter your AVA Provision Token: " avaProvisioningToken; echo
fi
# ############################## Get IoT Hub #####################################
echo listing IoT Hubs
outputhubs=$(az iot hub list --only-show-errors -o table --query [].name)
outputarrhubs=()
let cnt=0
while read -r line
do
if [ $cnt -gt 1 ]; then
outputarrhubs+=("$line")
fi
let cnt++
done <<< "$outputhubs"
# get length of an array
tLen=${#outputarrhubs[@]}
if [ $tLen -le 0 ]; then
echo IoTHub not found
echo Sorry, this demo requires that you have an existing IoTHub and registered Azure Iot Edge Device
read -p "Press <Enter> key to exit..."; echo
exit 1
fi
# Only one option so no need to prompt for choice
if [ $tLen -le 1 ]; then
while true; do
read -p "please confirm install to ${outputarrhubs[0]%$CR} hub (y or n): " -n 1 -r;echo
case $REPLY in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
iotHubName=${outputarrhubs[0]%$CR}
else
PS3='Choose the number corresponding to the IoTHub managing your target edge device: '
select opt in "${outputarrhubs[@]}"
do
echo "you chose: " $opt
iotHubName=${opt%$CR}
break
done
fi
iotHubConnectionString=$(az iot hub connection-string show --hub-name $iotHubName -o tsv)
echo "Got IoTHub Connection: " $iotHubConnectionString
# ############################## Get Device #####################################
echo getting devices
# query parameter retrieves only edge devices
output=$(az iot hub device-identity list -n $iotHubName -o table --query [?capabilities.iotEdge].[deviceId])
let cnt=0
outputarrdevs=()
while read -r line
do
# strip off column name and -------
if [ $cnt -gt 1 ]; then
outputarrdevs+=("$line")
fi
let cnt++
done <<< "$output"
# get length of an array
tLen=${#outputarrdevs[@]}
if [ $tLen -le 0 ]; then
echo No edge device found
echo Sorry, this demo requires that you have an existing IoTHub and registered Azure IoT Edge Device
read -p "Press any key to exit..."; echo
exit 1
fi
# Only one option so no need to prompt for choice
if [ $tLen -le 1 ]; then
while true; do
read -p "please confirm install to ${outputarrdevs[0]%$CR} device (y or n): " -n 1 -r;echo
case $REPLY in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
edgeDeviceId=${outputarrdevs[0]%$CR}
else
PS3='Choose the number corresponding to the Azure Iot Edge device: '
select opt in "${outputarrdevs[@]}"
do
echo "you chose: " $opt
edgeDeviceId=${opt%$CR}
break
done
fi
################################ Check for Platform ###########################################
#echo 1 amd64
#echo 2 arm64v8
#read -p "Choose the platform you're going to deploy: "
#if [ "$REPLY" == "1" ]; then
#platform="amd64"
#elif [ "$REPLY" == "2" ]; then
#platform="arm64v8"
#else
#echo "Unknown Platform"
#exit
#fi
platform="amd64"
echo Platform: amd64
################################ Check for Device ###########################################
#if [ "$platform" == "amd64" ]; then
# PS3='Choose the number corresponding to the Azure Stack Edge device: '
# deviceOptions="cpu gpu vpu"
# select cpuGpu in $deviceOptions
# do
# echo "you chose: " $cpuGpu
# if [ "$cpuGpu" != "" ]; then
# break
# fi
# done
# if [ "$cpuGpu" == "cpu" ]; then
# runtime="runc"
# fi
# if [ "$cpuGpu" == "gpu" ]; then
# runtime="nvidia"
# fi
# if [ "$cpuGpu" == "vpu" ]; then
# runtime="runc"
# fi
#elif [ "$platform" == "arm64v8" ]; then
# PS3='Choose the number corresponding to the Azure Stack Edge device: '
# deviceOptions="cpu jetson"
# select cpuGpu in $deviceOptions
# do
# echo "you chose: " $cpuGpu
# if [ "$cpuGpu" != "" ]; then
# break
# fi
# done
# if [ "$cpuGpu" == "cpu" ]; then
# runtime="runc"
# fi
# if [ "$cpuGpu" == "jetson" ]; then
# runtime="nvidia"
# fi
#fi
PS3='Choose the number corresponding to the Azure IoT Edge device: '
deviceOptions="cpu gpu vpu"
select cpuGpu in $deviceOptions
do
echo "you chose: " $cpuGpu
if [ "$cpuGpu" != "" ]; then
break
fi
done
if [ "$cpuGpu" == "cpu" ]; then
runtime="runc"
fi
if [ "$cpuGpu" == "gpu" ]; then
runtime="nvidia"
fi
if [ "$cpuGpu" == "vpu" ]; then
runtime="runc"
fi
fi #if [ $isCfg != true ]; then
################################ Check for Platform ###########################################
if [ "$streaming" == "ava" ]; then
edgeDeploymentJson=deployment.ava.json
else
edgeDeploymentJson=deployment.opencv.json
fi
if [ "$cpuGpu" == "vpu" ]; then
createOptions='"createOptions": "{\"ExposedPorts\":{\"9001/tcp\":{},\"8001/tcp\":{}},\"Cmd\":[\"/ovms/bin/ovms --config_path /workspace/config.json --port 9001 --rest_port 8001 --log_level DEBUG\"],\"Devices\":[{\"PathOnHost\":\"/dev/ion\",\"PathInContainer\":\"/dev/ion\"}],\"Entrypoint\":[\"sh\",\"-c\"],\"HostConfig\":{\"Mounts\":[{\"Target\":\"/workspace\",\"Source\":\"ovmsworkspace\",\"Type\":\"volume\"},{\"Target\":\"/var/tmp\",\"Source\":\"hddldaemon\",\"Type\":\"volume\"}],\"PortBindings\":{\"9001/tcp\":[{\"HostPort\":\"9001\"}],\"8001/tcp\":[{\"HostPort\":\"8001\"}]},\"LogConfig\":{\"Type\":\"\",\"Config\":",'
createOptions01='"createOptions01": "{\"max-size\":\"10m\",\"max-file\":\"10\"}}}}"'
else
createOptions='"createOptions": "{\"ExposedPorts\":{\"9001/tcp\":{},\"8001/tcp\":{}},\"Cmd\":[\"/ovms/bin/ovms --config_path /workspace/config.json --port 9001 --rest_port 8001 --log_level DEBUG\"],\"Entrypoint\":[\"sh\",\"-c\"],\"HostConfig\":{\"Mounts\":[{\"Target\":\"/workspace\",\"Source\":\"ovmsworkspace\",\"Type\":\"volume\"}],\"PortBindings\":{\"9001/tcp\":[{\"HostPort\":\"9001\"}],\"8001/tcp\":[{\"HostPort\":\"8001\"}]},\"LogConfig\":{\"Type\":\"\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"10\"}}}}"'
createOptions01=''
fi
################################ Write Config ############################################
# Will overwrite file if it already exists
input="./$edgeDeploymentJson"
while read -r line
do
prtline=${line//'<Training Endpoint>'/$cvTrainingEndpoint}
prtline=${prtline//'<Training API Key>'/$cvTrainingApiKey}
prtline=${prtline//'<cpu or gpu>'/$cpuGpu}
#prtline=${prtline//'<cpu or gpu>'/cpu}
prtline=${prtline//'<Docker Runtime>'/$runtime}
prtline=${prtline//'<platform>'/$platform}
prtline=${prtline//'<create option>'/$createOptions}
prtline=${prtline//'<create option 01>'/$createOptions01}
prtline=${prtline//'$IOTHUB_CONNECTION_STRING'/$iotHubConnectionString}
prtline=${prtline//'$AVA_PROVISIONING_TOKEN'/$avaProvisioningToken}
echo $prtline
done < "$input" > ./$edgeDeployJson
if [ "$isCfg" != true ]; then
mkdir -p factoryai_configs
mkdir -p factoryai_configs/factoryai_cfgs
factoryaiConfigName=factoryai_configs/factoryai_cfgs/factoryai_"$edgeDeviceId"_"$cpuGpu"_"$streaming"_"$now".cfg
echo cvTrainingEndpoint='"'$cvTrainingEndpoint'"' > $factoryaiConfigName
echo cvTrainingApiKey='"'$cvTrainingApiKey'"' >> $factoryaiConfigName
echo cpuGpu='"'$cpuGpu'"' >> $factoryaiConfigName
echo runtime='"'$runtime'"' >> $factoryaiConfigName
echo platform='"'$platform'"' >> $factoryaiConfigName
echo streaming='"'$streaming'"' >> $factoryaiConfigName
echo iotHubName='"'$iotHubName'"' >> $factoryaiConfigName
echo iotHubConnectionString='"'$iotHubConnectionString'"' >> $factoryaiConfigName
echo avaProvisioningToken='"'$avaProvisioningToken'"' >> $factoryaiConfigName
echo edgeDeviceId='"'$edgeDeviceId'"' >> $factoryaiConfigName
fi
# ############################## Deploy Edge Modules #####################################
echo Deploying containers to Azure IoT Edge Device
echo This will take more than 10 min at normal connection speeds. Status can be checked on the Azure IoT Edge device
#echo az iot edge set-modules --device-id $edgeDeviceId --hub-name $iotHubName --content $edgeDeployJson
output=$(az iot edge set-modules --device-id $edgeDeviceId --hub-name $iotHubName --content $edgeDeployJson)
echo "installation complete"
echo solution scheduled to deploy on the $edgeDeviceId device, from the $iotHubName hub