-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathcode.py
50 lines (37 loc) · 1.66 KB
/
code.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
#!/usr/bin/env python3
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
import time
import os
import http.server
import socketserver
from awsiot.greengrasscoreipc.clientv2 import GreengrassCoreIPCClientV2
client = GreengrassCoreIPCClientV2()
thing_name = os.environ["AWS_IOT_THING_NAME"]
def main():
# https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-component-configuration.html
config = client.get_configuration().value
print("This component was deployed with the following configuration:", config)
# example use case that takes component configuration as arguments for a webserver
host = config["Webserver"]["Host"]
port = config["Webserver"]["Port"]
directory = config["Webserver"]["Directory"]
os.chdir(directory)
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer((host, port), Handler) as httpd:
print(f"serving at {host}:{port} ...")
httpd.serve_forever()
if __name__ == "__main__":
# Once we enter here, we know:
# * all dependencies are available (imports succeeded)
# * IPC Client created
# * AWS_IOT_THING_NAME environment variable is available
# This should be sufficient to consider this component `running` and the deployment will be completed.
# If any of these failed, the component will be `broken`, and the deployment might roll-back or report the error.
# Once the component is `running`, we need to try as hard as possible to keep it alive and running.
while True:
try:
main()
except Exception as e:
print("ERROR", e)
time.sleep(5)