Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- also: rename definition file to greengo.
- Loading branch information
Dmitri
committed
Mar 28, 2018
1 parent
47811f1
commit f6a9c6c
Showing
35 changed files
with
1,117 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Greeengrass Group definition file | ||
Group: | ||
name: ml_take2_group | ||
Cores: | ||
- name: ml_take2_core_1 | ||
key_path: ./certs | ||
config_path: ./config | ||
SyncShadow: False | ||
|
||
Lambdas: | ||
- name: GreengrassHelloWorld | ||
handler: function.handler | ||
package: lambdas/GreengrassHelloWorld | ||
role: 'arn:aws:iam::012345678901:role/ExistingLambdaRole' | ||
environment: | ||
foo: bar | ||
|
||
Subscriptions: # not implemented | ||
- Id: 1 | ||
Source: "" | ||
Subject: "" | ||
Target: "" | ||
|
||
Devices: # not implemented | ||
- name: ml_take2_thing_1 | ||
key_path: ./certs | ||
|
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import sys | ||
import platform | ||
from threading import Timer | ||
|
||
sys.path.append(os.path.dirname(os.path.realpath(__file__))) | ||
|
||
import greengrasssdk | ||
|
||
INTERVAL = 5 | ||
|
||
# Creating a greengrass core sdk client | ||
client = greengrasssdk.client('iot-data') | ||
|
||
# Retrieving platform information to send from Greengrass Core | ||
my_platform = platform.platform() | ||
|
||
|
||
def run(): | ||
print "Executing run..." | ||
if not my_platform: | ||
client.publish(topic='hello/world', payload='Hello from Greengrass Core.') | ||
else: | ||
client.publish( | ||
topic='hello/world', | ||
payload='Hello from Greengrass Core running on platform: {}'.format(my_platform)) | ||
|
||
# Asynchronously schedule this function to be run again in 5 seconds | ||
Timer(INTERVAL, run).start() | ||
|
||
# Start executing the function above | ||
run() | ||
|
||
|
||
# This is a dummy handler and will not be invoked | ||
# Instead the code above will be executed in an infinite loop for our example | ||
def handler(event=None, context=None): | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# | ||
# Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
|
||
from .function_arn_fields import FunctionArnFields | ||
from .greengrass_message import GreengrassMessage |
Binary file not shown.
16 changes: 16 additions & 0 deletions
16
lambdas/GreengrassHelloWorld/greengrass_common/common_log_appender.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# | ||
# Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# this log appender is shared among all components of python lambda runtime, including: | ||
# greengrass_common/greengrass_message.py, greengrass_ipc_python_sdk/ipc_client.py, | ||
# greengrass_ipc_python_sdk/utils/exponential_backoff.py, lambda_runtime/lambda_runtime.py. | ||
# so that all log records will be emitted to local Cloudwatch. | ||
import logging.handlers | ||
|
||
from greengrass_common.local_cloudwatch_handler import LocalCloudwatchLogHandler | ||
|
||
# https://docs.python.org/2/library/logging.html#logrecord-attributes | ||
LOCAL_CLOUDWATCH_FORMAT = '[%(levelname)s]-%(filename)s:%(lineno)d,%(message)s' | ||
|
||
local_cloudwatch_handler = LocalCloudwatchLogHandler('GreengrassSystem', 'python_runtime') | ||
local_cloudwatch_handler.setFormatter(logging.Formatter(LOCAL_CLOUDWATCH_FORMAT)) |
Binary file added
BIN
+583 Bytes
lambdas/GreengrassHelloWorld/greengrass_common/common_log_appender.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
lambdas/GreengrassHelloWorld/greengrass_common/env_vars.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# | ||
# Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
|
||
import os | ||
|
||
AUTH_TOKEN = os.getenv('AWS_CONTAINER_AUTHORIZATION_TOKEN') | ||
MY_FUNCTION_ARN = os.getenv('MY_FUNCTION_ARN') | ||
SHADOW_FUNCTION_ARN = os.getenv('SHADOW_FUNCTION_ARN') | ||
ROUTER_FUNCTION_ARN = os.getenv('ROUTER_FUNCTION_ARN') |
Binary file not shown.
46 changes: 46 additions & 0 deletions
46
lambdas/GreengrassHelloWorld/greengrass_common/function_arn_fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# | ||
# Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
|
||
import re | ||
|
||
ARN_FIELD_REGEX = \ | ||
'arn:aws:lambda:([a-z]{2}-[a-z]+-\d{1}):(\d{12}):function:([a-zA-Z0-9-_]+)(?::(\$LATEST|[a-zA-Z0-9-_]+))?' | ||
|
||
|
||
class FunctionArnFields: | ||
""" | ||
This class takes in a string representing a Lambda function's ARN (the qualifier is optional), parses that string | ||
into individual fields for region, account_id, name and qualifier. It also has a static method for creating a | ||
Function ARN string from those subfields. | ||
""" | ||
@staticmethod | ||
def build_arn_string(region, account_id, name, qualifier): | ||
if qualifier: | ||
return 'arn:aws:lambda:{region}:{account_id}:function:{name}:{qualifier}'.format( | ||
region=region, account_id=account_id, name=name, qualifier=qualifier | ||
) | ||
else: | ||
return 'arn:aws:lambda:{region}:{account_id}:function:{name}'.format( | ||
region=region, account_id=account_id, name=name | ||
) | ||
|
||
def __init__(self, function_arn_string): | ||
self.parse_function_arn(function_arn_string) | ||
|
||
def parse_function_arn(self, function_arn_string): | ||
regex_match = re.match(ARN_FIELD_REGEX, function_arn_string) | ||
if regex_match: | ||
region, account_id, name, qualifier = map( | ||
lambda s: s.replace(':', '') if s else s, regex_match.groups() | ||
) | ||
else: | ||
raise ValueError('Cannot parse given string as a function ARN.') | ||
|
||
self.region = region | ||
self.account_id = account_id | ||
self.name = name | ||
self.qualifier = qualifier | ||
|
||
def to_arn_string(self): | ||
return FunctionArnFields.build_arn_string(self.region, self.account_id, self.name, self.qualifier) |
Binary file added
BIN
+2.66 KB
lambdas/GreengrassHelloWorld/greengrass_common/function_arn_fields.pyc
Binary file not shown.
76 changes: 76 additions & 0 deletions
76
lambdas/GreengrassHelloWorld/greengrass_common/greengrass_message.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# | ||
# Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
|
||
import base64 | ||
import json | ||
import logging | ||
from greengrass_common.common_log_appender import local_cloudwatch_handler | ||
|
||
# Log messages here are not part of customer's log because anything that | ||
# goes wrong here has nothing to do with customer's lambda code. Since we configured | ||
# the root logger to log to customer's log, we need to turn off propagation. | ||
runtime_logger = logging.getLogger(__name__) | ||
runtime_logger.addHandler(local_cloudwatch_handler) | ||
runtime_logger.propagate = False | ||
# set to the lowest possible level so all log messages will be sent to local cloudwatch handler | ||
runtime_logger.setLevel(logging.DEBUG) | ||
|
||
|
||
class GreengrassMessage: | ||
""" | ||
Holds the payload and extension_map fields making up messages exchanged over the IPC. Provides methods for encoding | ||
and decoding to/from strings. | ||
""" | ||
|
||
def __init__(self, payload=b'', **extension_map): | ||
self.payload = payload | ||
self.extension_map = extension_map | ||
|
||
@classmethod | ||
def decode(cls, encoded_string): | ||
if encoded_string: | ||
try: | ||
data_map = json.loads(encoded_string) | ||
except (ValueError, TypeError) as e: | ||
runtime_logger.exception(e) | ||
raise ValueError('Could not load provided encoded string "{}" as JSON due to exception: {}'.format( | ||
repr(encoded_string), str(e) | ||
)) | ||
|
||
try: | ||
payload = base64.b64decode(data_map['Payload']) | ||
except (ValueError, TypeError) as e: | ||
runtime_logger.exception(e) | ||
raise ValueError( | ||
'Could not decode payload of Greengrass Message data' | ||
'"{}" from base64 due to exception: {}'.format(repr(data_map), str(e)) | ||
) | ||
|
||
extension_map = data_map['ExtensionMap_'] | ||
else: | ||
payload = None | ||
extension_map = {} | ||
|
||
return cls(payload, **extension_map) | ||
|
||
def encode(self): | ||
try: | ||
# .decode to convert bytes -> string | ||
payload = base64.b64encode(self.payload).decode() | ||
except (ValueError, TypeError) as e: | ||
runtime_logger.exception(e) | ||
raise ValueError('Could not encode Greengrass Message payload "{}" as base64 due to exception: {}'.format( | ||
repr(self.payload), str(e) | ||
)) | ||
|
||
try: | ||
return json.dumps({'Payload': payload, 'ExtensionMap_': self.extension_map}) | ||
except (ValueError, TypeError) as e: | ||
runtime_logger.exception(e) | ||
raise ValueError('Could not encode Greengrass Message fields "{}" as JSON due to exception: {}'.format( | ||
str(self), str(e) | ||
)) | ||
|
||
def __str__(self): | ||
return str({'Payload': self.payload, 'ExtensionMap_': self.extension_map}) |
Binary file added
BIN
+3.03 KB
lambdas/GreengrassHelloWorld/greengrass_common/greengrass_message.pyc
Binary file not shown.
Oops, something went wrong.