Initial commit

This commit is contained in:
Quentin WEPHRE
2024-06-18 14:59:46 +02:00
parent 13549fe46e
commit fe82b3962f
23 changed files with 1832 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
#!/bin/bash
# Get all devices from an IoT Hub (iothub_name) that have the given tag (tag_key == tag_value)
# Can also filter using tags.number
# Execute an Azure command for each of the devices found
# Here the command will execute a Direct Method for the ThingsPro module on the devices. This Direct Method enable the Discovery Service.
iothub_name="IotHub-CUBE-Prod"
tag_key="site"
tag_value="DANISH"
devices=$(az iot hub query --hub-name $iothub_name --query-command "SELECT * FROM devices WHERE tags.$tag_key = '$tag_value' AND capabilities.iotEdge = true" --output json)
device_ids=$(echo "$devices" | jq -r '.[].deviceId')
for device_id in $device_ids
do
echo "$device_id"
az iot hub invoke-module-method --method-name thingspro-api-v1 --method-payload "{\"method\":\"PUT\",\"path\":\"/system/discovery\",\"requestBody\":{\"enable\": true}""}" --device-id $device_id --module-id thingspro-agent --hub-name $iothub_name
done

View File

@@ -0,0 +1,23 @@
#!/bin/bash
# Get all devices from an IoT Hub (iothub_name) that have the given tag (tag_key == tag_value)
# Can also filter using tags.number
# Execute an Azure command for each of the devices found
# Here the command will configure the modules of all the IoT Edge devices of Danish (as Danish have up to 29 devices) with the given template.
iothub_name="IotHub-CUBE-Prod"
tag_key="site"
tag_value="DANISH"
json_output=$(az iot hub query --hub-name $iothub_name --query-command "SELECT * FROM devices WHERE tags.$tag_key = '$tag_value' AND tags.number != '30' AND capabilities.iotEdge = true" --output json)
hashmap=$(echo "$json_output" | jq -r 'map({key: .tags.number, value: .deviceId}) | from_entries')
for key in $(echo "$hashmap" | jq -r 'keys | map(tonumber) | sort_by(.) | .[]'); do
value=$(jq -r --arg k "$key" '.[$k]' <<< "$hashmap")
az iot edge set-modules --device-id $value --hub-name $iothub_name --content moxa_ac_template_1.5_patch.json
done
echo $hashmap

View File

@@ -0,0 +1,23 @@
#!/bin/bash
# Get all devices from an IoT Hub (iothub_name) that have the given tag (tag_key == tag_value)
# Can also filter using tags.number
# Execute an Azure command for each of the devices found
# Here the command will execute a Direct Method for the ThingsPro module on the devices.
iothub_name="IotHub-CUBE-Prod"
tag_key="site"
tag_value="DANISH"
json_output=$(az iot hub query --hub-name $iothub_name --query-command "SELECT * FROM devices WHERE tags.$tag_key = '$tag_value' AND (tags.number = '6' OR tags.number = '8' OR tags.number = '12' OR tags.number = '13') AND capabilities.iotEdge = true" --output json)
hashmap=$(echo "$json_output" | jq -r 'map({key: .tags.number, value: .deviceId}) | from_entries')
for key in $(echo "$hashmap" | jq -r 'keys | map(tonumber) | sort_by(.) | .[]'); do
value=$(jq -r --arg k "$key" '.[$k]' <<< "$hashmap")
status=$(az iot hub invoke-module-method --timeout 10 --method-name thingspro-api-v1 --method-payload '{"method":"GET","path":"/device/general"}' --device-id $value --module-id thingspro-agent --hub-name $iothub_name | jq .status)
echo "Key: $key, Value: $value, Status: $status"
done
#echo $hashmap

84
Azure/create-moxa-list.py Normal file
View File

@@ -0,0 +1,84 @@
import pandas as pd
import subprocess
import argparse
import sys
import json
# This Python script will read an Excel file containing a list of devices for a site (using --serials *file name*)
# It will then create a IoT Edge device for each of the line in the file
# The user need to precise which IoT Hub is used (using --env *PROD or DEV*)
# The user need provide the site name, which will be added as a tag (using --site *site name*)
# The user need to precise which configuration of the modules should be used, according to the running firmware on the devices (using --version *version number*)
# Example:
# python create_devices_list.py --serials DANISH.xlsx --env PROD --site DANISH --version 1.5_patch
# will create all the IoT Edge devices using the serial number found in the DANISH.xlsx file
# on the PROD IoT Hub
# tag each new device with a site = DANISH
# configure the modules for each devices with the corresponding version found in moxa_ac_template_1.5_patch.json
# each device will have also be given a number = i where i is incremented automatically according to the number of device in the Excel file
# the provided Excel file will be modified, adding a column connection_string for each of the devices created with their corresponding connection string
# the output is therefore SENSITIVE
def generate_commands(serials_file, env, site, version):
# Read serial numbers from Excel file
df = pd.read_excel(serials_file)
df = df[df['device_name'].notnull()]
serials = df['device_name'].tolist()
# Initialize number
number = 1
# List to store connection strings
connection_strings = []
# Generate commands for each serial number
for serial in serials:
# Replace placeholders with actual values
print(serial, end=" ")
device_id = f"DIGIT-{serial}"
print(device_id, end=" ")
tags = f'{{"deviceId":"{device_id}","site":"{site}","number":"{number}"}}'
content = f"moxa_ac_template_{version}.json"
# Construct command strings
create_command = f"az iot hub device-identity create --device-id {device_id} --hub-name IotHub-CUBE-{env} --edge-enabled"
twin_update_command = f"az iot hub device-twin update --device-id {device_id} --hub-name IotHub-CUBE-{env} --set tags='{tags}'"
set_modules_command = f"az iot edge set-modules --device-id {device_id} --hub-name IotHub-CUBE-{env} --content {content}"
# Execute create command and get primary key
create_output = subprocess.check_output(create_command, shell=True)
create_output_json = json.loads(create_output.decode('utf-8'))
primary_key = create_output_json['authentication']['symmetricKey']['primaryKey']
print(primary_key, end=" ")
# Generate connection string
connection_string = f"HostName=IotHub-CUBE-{env}.azure-devices.net;DeviceId={device_id};SharedAccessKey={primary_key}"
print(connection_string)
connection_strings.append(connection_string)
tags_output = subprocess.run(twin_update_command, shell=True)
modules_output = subprocess.run(set_modules_command, shell=True)
# Increment number
number += 1
# Add connection strings to DataFrame
df['connection_string'] = connection_strings
# Save DataFrame to Excel file
df.to_excel(serials_file, index=False)
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(description='Create devices list')
parser.add_argument('--serials', required=True, help='Excel file containing serial numbers')
parser.add_argument('--env', required=True, help='Environment (PROD or DEV)')
parser.add_argument('--site', required=True, help='Site name')
parser.add_argument('--version', required=True, help='Version number')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# Generate commands
generate_commands(args.serials, args.env, args.site, args.version)

48
Azure/create-moxa.py Normal file
View File

@@ -0,0 +1,48 @@
import pandas as pd
import subprocess
import argparse
import sys
import json
# This Python script will create an IoT Edge device according to the provided parameters
# It will create the device with the name DIGIT-*serial number* (using --serial *serial number*)
# The user need to precise the number of the gateway on the site (using --number *number*)
# The user need to precise which IoT Hub is used (using --env *PROD or DEV*)
# The user need provide the site name, which will be added as a tag (using --site *site name*)
# The user need to precise which configuration of the modules should be used, according to the running firmware on the device (using --version *version number*)
# Example:
# python create_moxa.py --serial TBBHB1044382 --number 5 --env PROD --site DANISH --version 1.5_patch
# will create the IoT Edge device using the serial number provided (TBBHB1044382)
# on the PROD IoT Hub
# tag the device with number = 5
# tag the device with site = DANISH
# configure the modules for the device with the corresponding version found in moxa_ac_template_1.5_patch.json
def generate_commands(serial, number, env, site, version):
device_id = f"DIGIT-{serial}"
tags = f'{{"deviceId":"{device_id}","site":"{site}","number":"{number}"}}'
content = f"moxa_ac_template_{version}.json"
create_device_command = f"az iot hub device-identity create --device-id {device_id} --hub-name IotHub-CUBE-{env} --edge-enabled"
twin_update_command = f"az iot hub device-twin update --device-id {device_id} --hub-name IotHub-CUBE-{env} --set tags='{tags}'"
set_modules_command = f"az iot edge set-modules --device-id {device_id} --hub-name IotHub-CUBE-{env} --content {content}"
subprocess.run(create_device_command, shell=True)
subprocess.run(twin_update_command, shell=True)
subprocess.run(set_modules_command, shell=True)
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(description='Create devices list')
parser.add_argument('--serial', required=True, help='Serial number of the gateway')
parser.add_argument('--number', required=True, help='Gateway on-site number')
parser.add_argument('--env', required=True, help='Environment (PROD or DEV)')
parser.add_argument('--site', required=True, help='Site name')
parser.add_argument('--version', required=True, help='Version number')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# Generate commands
generate_commands(args.serial, args.number, args.env, args.site, args.version)

5
Azure/create_moxa.txt Normal file
View File

@@ -0,0 +1,5 @@
az iot hub device-identity create --device-id DIGIT-TBBHB1044382 --hub-name IotHub-CUBE-PROD --edge-enabled
az iot hub device-twin update --device-id DIGIT-TBBHB1044382 --hub-name IotHub-CUBE-PROD --set tags='{"deviceId":"DIGIT-TBBHB1044382","site":"SASK","number":"1"}'
az iot edge set-modules --device-id DIGIT-TBBHB1044382 --hub-name IotHub-CUBE-DEV --content moxa_ac_template.json
This file is a just a note for usual Azure commands involved to create a device.

View File

@@ -0,0 +1,71 @@
{
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.1",
"runtime": {
"type": "docker",
"settings": {}
},
"systemModules": {
"edgeAgent": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
},
"SendRuntimeQualityTelemetry": {
"value": false
}
},
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.0.10",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}}}}"
},
"type": "docker"
},
"edgeHub": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
}
},
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.0.10",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"PortBindings\":{\"443/tcp\":[{\"HostPort\":\"443\"}],\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}]}}}"
},
"status": "running",
"type": "docker"
}
},
"modules": {
"thingspro-agent": {
"restartPolicy": "always",
"settings": {
"image": "moxa2019/thingspro-agent:2.1.1-armhf",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"Binds\":[\"/var/thingspro/apps/cloud/data/setting/:/var/thingspro/cloud/setting/\",\"/run/:/host/run/\",\"/var/thingspro/data/:/var/thingspro/data/\"]}}"
},
"status": "running",
"type": "docker"
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.1",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 86400
},
"routes": {
"route": {
"route": "FROM /messages/* INTO $upstream"
}
}
}
},
"thingspro-agent": {
"properties.desired": {}
}
}
}

View File

@@ -0,0 +1,71 @@
{
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.1",
"runtime": {
"type": "docker",
"settings": {}
},
"systemModules": {
"edgeAgent": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
},
"SendRuntimeQualityTelemetry": {
"value": false
}
},
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.1.4",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}}}}"
},
"type": "docker"
},
"edgeHub": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
}
},
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.1.4",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"PortBindings\":{\"443/tcp\":[{\"HostPort\":\"443\"}],\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}]}}}"
},
"status": "running",
"type": "docker"
}
},
"modules": {
"thingspro-agent": {
"restartPolicy": "always",
"settings": {
"image": "moxa2019/thingspro-agent:2.2.3-armhf",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"Binds\":[\"/var/thingspro/apps/cloud/data/setting/:/var/thingspro/cloud/setting/\",\"/run/:/host/run/\",\"/var/thingspro/data/:/var/thingspro/data/\"]}}"
},
"status": "running",
"type": "docker"
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.1",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 86400
},
"routes": {
"route": {
"route": "FROM /messages/* INTO $upstream"
}
}
}
},
"thingspro-agent": {
"properties.desired": {}
}
}
}

View File

@@ -0,0 +1,71 @@
{
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.1",
"runtime": {
"type": "docker",
"settings": {}
},
"systemModules": {
"edgeAgent": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
},
"SendRuntimeQualityTelemetry": {
"value": false
}
},
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.2.7",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}}}}"
},
"type": "docker"
},
"edgeHub": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
}
},
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.2.7",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"PortBindings\":{\"443/tcp\":[{\"HostPort\":\"443\"}],\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}]}}}"
},
"status": "running",
"type": "docker"
}
},
"modules": {
"thingspro-agent": {
"restartPolicy": "always",
"settings": {
"image": "moxa2019/thingspro-agent:2.2.3-armhf",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"Binds\":[\"/var/thingspro/apps/cloud/data/setting/:/var/thingspro/cloud/setting/\",\"/run/:/host/run/\",\"/var/thingspro/data/:/var/thingspro/data/\"]}}"
},
"status": "running",
"type": "docker"
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.1",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 86400
},
"routes": {
"route": {
"route": "FROM /messages/* INTO $upstream"
}
}
}
},
"thingspro-agent": {
"properties.desired": {}
}
}
}

View File

@@ -0,0 +1,71 @@
{
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.1",
"runtime": {
"type": "docker",
"settings": {}
},
"systemModules": {
"edgeAgent": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
},
"SendRuntimeQualityTelemetry": {
"value": false
}
},
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.4.10",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}}}}"
},
"type": "docker"
},
"edgeHub": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
}
},
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.4.10",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"PortBindings\":{\"443/tcp\":[{\"HostPort\":\"443\"}],\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}]}}}"
},
"status": "running",
"type": "docker"
}
},
"modules": {
"thingspro-agent": {
"restartPolicy": "always",
"settings": {
"image": "moxa2019/thingspro-agent:2.2.3-armhf",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"Binds\":[\"/var/thingspro/apps/cloud/data/setting/:/var/thingspro/cloud/setting/\",\"/run/:/host/run/\",\"/var/thingspro/data/:/var/thingspro/data/\"]}}"
},
"status": "running",
"type": "docker"
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.1",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 86400
},
"routes": {
"route": {
"route": "FROM /messages/* INTO $upstream"
}
}
}
},
"thingspro-agent": {
"properties.desired": {}
}
}
}

View File

@@ -0,0 +1,71 @@
{
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.1",
"runtime": {
"type": "docker",
"settings": {}
},
"systemModules": {
"edgeAgent": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
},
"SendRuntimeQualityTelemetry": {
"value": false
}
},
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.4.27",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}}}}"
},
"type": "docker"
},
"edgeHub": {
"env": {
"UpstreamProtocol": {
"value": "AMQPWS"
}
},
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.4.27",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"PortBindings\":{\"443/tcp\":[{\"HostPort\":\"443\"}],\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}]}}}"
},
"status": "running",
"type": "docker"
}
},
"modules": {
"thingspro-agent": {
"restartPolicy": "always",
"settings": {
"image": "moxa2019/thingspro-agent:2.2.5-armhf",
"createOptions": "{\"HostConfig\":{\"LogConfig\":{\"Type\":\"json-file\",\"Config\":{\"max-size\":\"10m\",\"max-file\":\"3\"}},\"Binds\":[\"/var/thingspro/apps/azureiotedge/data/setting/:/var/thingspro/cloud/setting/\",\"/run/:/host/run/\",\"/var/thingspro/data/:/var/thingspro/data/\"]}}"
},
"status": "running",
"type": "docker"
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.1",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 86400
},
"routes": {
"route": {
"route": "FROM /messages/* INTO $upstream"
}
}
}
},
"thingspro-agent": {
"properties.desired": {}
}
}
}