-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrewst_remote_agent.py
executable file
·95 lines (73 loc) · 2.49 KB
/
rewst_remote_agent.py
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
""" Module for executing the remote agent """
import asyncio
import logging
import logging.handlers
import platform
import signal
import sys
from __version__ import __version__
from config_module.config_io import (
load_configuration,
get_org_id_from_executable_name,
setup_file_logging,
)
from iot_hub_module.connection_management import iot_hub_connection_loop
os_type = platform.system().lower()
stop_event = asyncio.Event()
class ConfigurationError(Exception):
""" Configuration error class """
pass
def signal_handler() -> None:
"""
Signal handler used in the application.
"""
logging.info("Shutting down gracefully.")
stop_event.set()
# Main function
async def main() -> None:
"""
Main entry point of the program
Raises:
ConfigurationError: if the configuration failed.
"""
logging.info(f"Version: {__version__}")
logging.info(f"Running on {os_type}")
config_file = None
try:
logging.info("Loading Configuration")
if config_file:
logging.info(f"Using config file {config_file}.")
config_data = load_configuration(None, config_file)
org_id = config_data["rewst_org_id"]
else:
org_id = get_org_id_from_executable_name(sys.argv)
if org_id:
logging.info(f"Found Org ID {org_id} via executable name.")
config_data = load_configuration(org_id)
else:
logging.warning(f"Did not find guid in executable name.")
config_data = None
# Exit if no configuration was found
if not config_data:
raise ConfigurationError("No configuration was found.")
except ConfigurationError as e:
logging.error(str(e))
return
except Exception as e:
logging.exception(f"Exception Caught during self-configuration: {str(e)}")
return
logging.info(f"Running for Org ID {org_id}")
logging.info("Setting up file logging")
try:
setup_file_logging(org_id)
except Exception as e:
logging.exception(f"Exception occurred setting up file-based logging: {e}.")
if os_type != "windows":
# Register signal handlers for Unix-based systems
loop = asyncio.get_running_loop()
for sig in (signal.SIGTERM, signal.SIGINT):
loop.add_signal_handler(sig, signal_handler)
await iot_hub_connection_loop(config_data, stop_event)
# Entry point
if __name__ == "__main__":
asyncio.run(main())