From d8df4365c3264588c74818edb8a579552e822784 Mon Sep 17 00:00:00 2001 From: Quentin WEPHRE Date: Fri, 27 Jun 2025 08:40:36 +0200 Subject: [PATCH] New tools for restarting services and fixes --- Azure/bash_direct_method_2.sh | 4 +- Python/azure_iot_hub_list_devices.py | 2 +- Python/azure_iot_hub_restart_iotedge.py | 51 +++++++++ Python/cube_activate_ssh.py | 20 ++-- Python/cube_ssh_batch.py | 25 ++++- Python/cube_ssh_batch_passive.py | 131 ++++++++++++++++++++++++ Python/requirements.txt | 7 +- 7 files changed, 222 insertions(+), 18 deletions(-) create mode 100644 Python/azure_iot_hub_restart_iotedge.py create mode 100644 Python/cube_ssh_batch_passive.py diff --git a/Azure/bash_direct_method_2.sh b/Azure/bash_direct_method_2.sh index 9ecef9a..dacf243 100644 --- a/Azure/bash_direct_method_2.sh +++ b/Azure/bash_direct_method_2.sh @@ -16,8 +16,8 @@ hashmap=$(echo "$json_output" | jq -r 'map({key: .tags.number, value: .deviceId} 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 + print(value) + # az iot edge set-modules --device-id $value --hub-name $iothub_name --content moxa_ac_template_1.5_patch.json done echo $hashmap \ No newline at end of file diff --git a/Python/azure_iot_hub_list_devices.py b/Python/azure_iot_hub_list_devices.py index db5fc84..b518542 100644 --- a/Python/azure_iot_hub_list_devices.py +++ b/Python/azure_iot_hub_list_devices.py @@ -25,7 +25,7 @@ if CONNECTION_STRING == "": registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING) -query_spec = QuerySpecification(query="SELECT * FROM devices WHERE IS_DEFINED(tags.site) AND capabilities.iotEdge = true ") +query_spec = QuerySpecification(query="SELECT * FROM devices WHERE IS_DEFINED(tags.site) AND tags.site = 'DANISH' AND capabilities.iotEdge = true ") query_result = registry_manager.query_iot_hub(query_spec) diff --git a/Python/azure_iot_hub_restart_iotedge.py b/Python/azure_iot_hub_restart_iotedge.py new file mode 100644 index 0000000..4479ae5 --- /dev/null +++ b/Python/azure_iot_hub_restart_iotedge.py @@ -0,0 +1,51 @@ +from azure.iot.hub import IoTHubRegistryManager +from azure.iot.hub.protocol.models import QuerySpecification, Module +from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult +from dotenv import load_dotenv +from isight_device import iSightDevice + +import json +import os + +load_dotenv() + +module_id = "$edgeAgent" +method_name = "RestartModule" +module = "edgeHub" +payload = '{"schemaVersion":"1.0", "id":"' + module + '"}' + +# Install the Azure IoT Hub SDK: +# pip install azure-iot-hub + +# Authenticate to your Azure account +# CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_SAFT_PROD")) +CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD")) +if CONNECTION_STRING == "": + print("Provide a connection string for the Iot Hub before running the script!") + exit(13) + + +registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING) +query_spec = QuerySpecification(query="SELECT * FROM devices WHERE IS_DEFINED(tags.site) AND tags.site = 'DANISH' AND capabilities.iotEdge = true ") + +query_result = registry_manager.query_iot_hub(query_spec) + +devices = [] +for item in query_result.items: + # currentDevice = iSightDevice(str(item.device_id), str(item.tags['site']), int(item.tags['number']), str(item.tags['version'])) + devices.append(iSightDevice(str(item.device_id), str(item.tags['site']), int(item.tags['number']), str(item.tags['version']))) + +devices.sort(key = lambda d: (d.site, d.number)) + +for device in devices: + print(device, end="\t") + current_device_modules = registry_manager.get_modules(device.deviceId) + for module in current_device_modules: + if (module.module_id == "thingspro-agent"): + device.setModule(module) + try: + direct_method = CloudToDeviceMethod(method_name=method_name, payload=json.loads(payload)) + response = registry_manager.invoke_device_module_method(device_id=device.deviceId, module_id=module_id, direct_method_request=direct_method) + print(response) + except: + print("ERROR") \ No newline at end of file diff --git a/Python/cube_activate_ssh.py b/Python/cube_activate_ssh.py index a44f233..ee18b63 100644 --- a/Python/cube_activate_ssh.py +++ b/Python/cube_activate_ssh.py @@ -6,6 +6,7 @@ import os import urllib3 from dotenv import load_dotenv import io +import time load_dotenv(override=True) @@ -31,8 +32,7 @@ def authenticate(base_url): } try: - print(f"Authenticating as {username}...") - response = requests.post(auth_url, files=files, verify=False) + response = requests.post(auth_url, files=files, verify=False, timeout=10) response.raise_for_status() # Raise exception for 4XX/5XX responses # Extract token from response @@ -40,9 +40,9 @@ def authenticate(base_url): token = auth_data.get("token") if not token: - print("Error: No token received in authentication response") + print("Authentication failure!") - print("Authentication successful.") + print("Authentication success!", end = " ") return token except requests.exceptions.RequestException as e: @@ -66,16 +66,16 @@ def set_ssh_status(base_url, token): payload = { "currentStatus": True } try: - print(f"Sending request to enable SSH...") - response = requests.post(ssh_url, headers=headers, json=payload, verify=False) + response = requests.post(ssh_url, headers=headers, json=payload, verify=False, timeout=10) response.raise_for_status() - print(f"SSH enabled successfully!") + print(f"SSH activation success!") return True except requests.exceptions.RequestException as e: - print(f"SSH activation failed: {e}") + print("SSH activation failure!") + print(f"Exception: {e}") if hasattr(e, 'response') and e.response: print(f"Response: {e.response.text}") return False @@ -88,10 +88,8 @@ def activate_ssh(ip_address): # Convert http:// to https:// or add https:// if no protocol specified if url.startswith("http://"): url = "https://" + url[7:] - print(f"Converting to HTTPS: {url}") else: url = "https://" + url - print(f"Adding HTTPS protocol: {url}") if not url.endswith(":9080"): url = url + ":9080" @@ -102,5 +100,5 @@ def activate_ssh(ip_address): token = authenticate(url) if not token: return - + time.sleep(3) set_ssh_status(url, token) \ No newline at end of file diff --git a/Python/cube_ssh_batch.py b/Python/cube_ssh_batch.py index 6088566..3691af0 100644 --- a/Python/cube_ssh_batch.py +++ b/Python/cube_ssh_batch.py @@ -13,7 +13,7 @@ load_dotenv(override=True) ip_address_prefix = "10.188.11." ssh_command = "hostname" -csv_filename = "hoohana6.csv" +csv_filename = "hoohana7.csv" SITE_NAME = "HOOHANA" ssh_username = os.getenv("DEFAULT_CUBE_LINUX_ADMIN_USER") @@ -52,6 +52,24 @@ def update_cloud_config(ip, new_content): finally: client.close() +def restart_cloudagent(ip): + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + client.connect(ip, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False) + stdin, stdout, stderr = client.exec_command(f'sudo -S bash -c \'systemctl restart cube-web-cloudagent << EOF\n\nEOF\'\n') + stdin.write(ssh_password + "\n") + stdin.flush() + stdoutput = [line for line in stdout] + stderroutput = [line for line in stderr] + for output in stdoutput: + print(output.strip()) + except Exception as e: + print(f"SSH Error: {str(e)}") + raise + finally: + client.close() + def main(): print("Starting...") @@ -64,7 +82,8 @@ def main(): results = [] - for i in range (54, 55): + numbers = [16, 18, 34, 35] + for i in numbers: ip_address = f"{ip_address_prefix}{i}" print(f"Activating SSH for {ip_address}:", end=" ") @@ -162,6 +181,8 @@ def main(): continue print("Done!") + restart_cloudagent(ip_address) + writer.writerow([i, ip_address, cube_id, migration, status]) file.flush() diff --git a/Python/cube_ssh_batch_passive.py b/Python/cube_ssh_batch_passive.py new file mode 100644 index 0000000..3e5a7a6 --- /dev/null +++ b/Python/cube_ssh_batch_passive.py @@ -0,0 +1,131 @@ +import csv +import paramiko +import time +from cube_activate_ssh import activate_ssh +from dotenv import load_dotenv +import os +import re +from azure.iot.hub import IoTHubRegistryManager +from azure.iot.hub.models import Twin, TwinProperties + +load_dotenv(override=True) + +ssh_username = os.getenv("DEFAULT_CUBE_LINUX_ADMIN_USER") +ssh_password = os.getenv("DEFAULT_CUBE_LINUX_ADMIN_PASSWORD") +CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD")) + +def execute_ssh_command(ip, command): + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + client.connect(ip, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False) + stdin, stdout, stderr = client.exec_command(command) + result = stdout.read().decode().lower().strip() + return result + except Exception as e: + print(f"SSH Error: {str(e)}") + raise + finally: + client.close() + +def update_cloud_config(ip, new_content): + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + client.connect(ip, port=11022, username=ssh_username, password=ssh_password, allow_agent=False, look_for_keys=False) + stdin, stdout, stderr = client.exec_command(f'sudo -S bash -c \'cat > /etc/cube/config-azure.properties << EOF\n{new_content}\nEOF\'\n') + stdin.write(ssh_password + "\n") + stdin.flush() + stdoutput = [line for line in stdout] + stderroutput = [line for line in stderr] + for output in stdoutput: + print(output.strip()) + except Exception as e: + print(f"SSH Error: {str(e)}") + raise + finally: + client.close() + +def main(): + + ip_address_prefix = "10.84.171." + start_ip = 1 + end_ip = 5 + SITE_NAME = "MYRTLE" + + print(f"Site: {SITE_NAME}") + print(f"From {ip_address_prefix}{str(start_ip)} to {ip_address_prefix}{str(end_ip)}") + + file_numbering = 0 + + while os.path.exists(SITE_NAME + "_" + str(file_numbering) + ".csv"): + file_numbering = file_numbering + 1 + + csv_filename = SITE_NAME + "_" + str(file_numbering) + ".csv" + + print(f"Logging results to {csv_filename}") + + with open(csv_filename, mode="w", newline="") as file: + writer = csv.writer(file) + writer.writerow(["Number", "IP address", "Cube ID", "Environment", "Correct configuration"]) + + registry_manager = IoTHubRegistryManager.from_connection_string(CONNECTION_STRING) + + results = [] + + for i in range (start_ip, end_ip): + ip_address = f"{ip_address_prefix}{i}" + print(f"Activating SSH for {ip_address}:", end=" ") + + try: + activate_ssh(ip_address) + except Exception as e: + writer.writerow([i, ip_address, "UNREACHABLE", "NA", "NA"]) + file.flush() + continue + + ssh_command = "hostname" + print(f"Executing {ssh_command} for {ip_address}:", end=" ") + try: + cube_id = execute_ssh_command(ip_address, ssh_command) + except Exception as e: + print("Failed!") + writer.writerow([i, ip_address, "UNREACHABLE", "NA", "NA"]) + file.flush() + continue + print(cube_id) + + ssh_command = "grep \"connection-string\" /etc/cube/config-azure.properties" + print(f"Getting configured Connection String for {ip_address}:", end=" ") + try: + connection_string = execute_ssh_command(ip_address, ssh_command) + if connection_string == "": + raise Exception("No Connection String was extracted!") + + iothub_match = re.search(r"hostname\\=(.*?);", connection_string, re.IGNORECASE) + iothub = iothub_match.group(1) if iothub_match else None + if iothub.lower() == "IotHub-CUBE-PROD.azure-devices.net".lower(): + migration = "SAFT" + elif iothub.lower() == "iot-ingest-ess-prod.azure-devices.net".lower(): + migration = "INOX" + else: + migration = "NONE" + + device_id_match = re.search(r"deviceid\\=(.*?);", connection_string, re.IGNORECASE) + cloud_cube_id = device_id_match.group(1) if device_id_match else None + if cloud_cube_id.lower() == cube_id.lower(): + status = "CORRECT" + else: + status = "INCORRECT" + except Exception as e: + print(e) + migration = "NONE" + status = "INCORRECT" + finally: + print(f"{migration} {status}") + + writer.writerow([i, ip_address, cube_id, migration, status]) + file.flush() + +if __name__ == "__main__": + main() diff --git a/Python/requirements.txt b/Python/requirements.txt index 839ba48..5c564c0 100644 --- a/Python/requirements.txt +++ b/Python/requirements.txt @@ -1,2 +1,5 @@ -requests -dotenv-python \ No newline at end of file +azure-core +azure-iot-hub +python-dotenv +paramiko +requests \ No newline at end of file