-
Notifications
You must be signed in to change notification settings - Fork 8
/
aws_lambda.py
executable file
·125 lines (92 loc) · 3.61 KB
/
aws_lambda.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import glob
import importlib
import os
import sys
import sentry_sdk
from decouple import config
SENTRY_DSN = config("SENTRY_DSN", default=None)
SENTRY_ENV = config("SENTRY_ENV", default=None)
SERVER_URL = os.getenv("SERVER", "http://localhost:8888/v1")
if SENTRY_DSN:
# Note! If you don't do `sentry_sdk.init(DSN)` it will still work
# to do things like calling `sentry_sdk.capture_exception(exception)`
# It just means it's a noop.
env_option = {}
if SENTRY_ENV:
env_option = {"environment": SENTRY_ENV}
if os.getenv("AWS_LAMBDA_FUNCTION_NAME"):
# We're running in AWS. See https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
integrations = [AwsLambdaIntegration()]
elif os.getenv("FUNCTION_TARGET", os.getenv("GOOGLE_CLOUD_PROJECT")):
# We're running in Google Cloud. See https://cloud.google.com/functions/docs/configuring/env-var
from sentry_sdk.integrations.gcp import GcpIntegration
integrations = [GcpIntegration()]
else:
raise RuntimeError("Could not determine Cloud environment for Sentry")
sentry_sdk.init(SENTRY_DSN, integrations=integrations, **env_option)
def help_(**kwargs):
"""Show this help."""
def white_bold(s):
return f"\033[1m\x1b[37m{s}\033[0;0m"
entrypoints = [
os.path.splitext(os.path.basename(f))[0] for f in glob.glob("./commands/[a-z]*.py")
]
commands = [
getattr(importlib.import_module(f"commands.{entrypoint}"), entrypoint)
for entrypoint in entrypoints
]
func_listed = "\n - ".join([f"{white_bold(f.__name__)}: {f.__doc__}" for f in commands])
print(
f"""
Remote Settings lambdas.
Available commands:
- {func_listed}
"""
)
def run(command, event=None, context=None):
if event is None:
event = {"server": SERVER_URL}
if context is None:
context = {"sentry_sdk": sentry_sdk}
if isinstance(command, (str,)):
# Import the command module and returns its main function.
mod = importlib.import_module(f"commands.{command}")
command = getattr(mod, command)
# Note! If the sentry_sdk was initialized with the platform integration,
# it is now ready to automatically capture all and any unexpected exceptions.
# See https://docs.sentry.io/platforms/python/guides/aws-lambda/
# See https://docs.sentry.io/platforms/python/guides/gcp-functions/
# Option to test failure to test Sentry integration.
if event.get("force_fail") or os.getenv("FORCE_FAIL"):
raise Exception("Found forced failure flag")
command(event, context)
def backport_records(*args, **kwargs):
return run("backport_records", *args, **kwargs)
def blockpages_generator(*args, **kwargs):
return run("blockpages_generator", *args, **kwargs)
def refresh_signature(*args, **kwargs):
return run("refresh_signature", *args, **kwargs)
def sync_megaphone(*args, **kwargs):
return run("sync_megaphone", *args, **kwargs)
def build_bundles(*args, **kwargs):
return run("build_bundles", *args, **kwargs)
def main(*args):
# Run the function specified in CLI arg.
#
# $ AUTH=user:pass python aws_lambda.py refresh_signature
#
if not args or args[0] in ("help", "--help"):
help_()
return
entrypoint = args[0]
try:
command = globals()[entrypoint]
except KeyError:
print(f"Unknown function {entrypoint!r}", file=sys.stderr)
help_()
return 1
command()
if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))