Skip to content

Commit

Permalink
Add lambda builder
Browse files Browse the repository at this point in the history
  • Loading branch information
brwyatt committed Dec 1, 2019
1 parent 259bad2 commit 1aa9920
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
url="https://github.com/brwyatt/Ophiuchus",
packages=find_packages(where="src"),
package_dir={"": "src"},
package_data={},
package_data={"ophiuchus": ["templates/lambdas/*"]},
python_requires="~=3.6",
include_package_data=False,
entry_points={
Expand All @@ -23,6 +23,6 @@
"build = ophiuchus.cli.build:Build",
],
},
install_requires=["boto3>=1.10.0,<1.11.0"],
install_requires=["boto3>=1.10.0,<1.11.0", "Jinja2>=2.10.0,<2.11.0"],
tests_require=["aiohttp>=3.6.0,<3.7.0"],
)
34 changes: 34 additions & 0 deletions src/ophiuchus/cli/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from shutil import rmtree
from typing import List

import jinja2
import pkg_resources
from ophiuchus.cli.subcommands import EntryPointBuilderSubcommand
from ophiuchus.framework import GlobalConfig
from ophiuchus.framework import Handler
Expand Down Expand Up @@ -105,6 +107,12 @@ def build_site_group(
requirements_file=requirements_file,
)

self.build_lambdas(
site_group=site_group,
site_group_lambda_dir=site_group_lambda_dir,
site_group_packages_dir=site_group_packages_dir,
)

def install_package(
self,
site_group_packages_dir: str,
Expand All @@ -128,3 +136,29 @@ def install_package(
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

def build_lambdas(
self,
site_group: str,
site_group_lambda_dir: str,
site_group_packages_dir: str,
):
self.log.debug("Creating site group lambda directory")
os.makedirs(site_group_lambda_dir, exist_ok=True)

working_set = pkg_resources.WorkingSet(
entries=[site_group_packages_dir],
)

env = jinja2.Environment(
loader=jinja2.PackageLoader("ophiuchus", "templates"),
)
template = env.get_template("lambdas/handler.py.template")
for name, ep in load_entry_points(
site_group, Handler, working_set,
).items():
file_name = os.path.join(site_group_lambda_dir, f"{name}.py")
with open(file_name, "w") as f:
f.write(
template.render(module=ep.__module__, name=ep.__name__),
)
18 changes: 18 additions & 0 deletions src/ophiuchus/templates/lambdas/handler.py.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Auto-generated by Ophiuchus Build
import json
import logging

from ophiuchus.framework import GlobalConfig

from {{ module }} import {{ name }}

log = logging.getLogger(__name__)

config = GlobalConfig.from_file("/opt/config.json")

real_handler = {{ name }}(config)

def handler(event, context):
method = event.get('httpMethod', '').upper()
if hasattr(real_handler, method):
return getattr(real_handler, method)(event, context)

0 comments on commit 1aa9920

Please sign in to comment.