Skip to content

Commit

Permalink
Adds Callback functions and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Jones committed Jul 30, 2016
1 parent f796b3d commit 8a8d643
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ to change Zappa's behavior. Use these at your own risk!
"assume_policy": "my_assume_policy.json", // optional, IAM assume policy JSON file
"attach_policy": "my_attach_policy.json", // optional, IAM attach policy JSON file
"aws_region": "us-east-1", // AWS Region (default US East),
"callbacks": { // Call custom functions during the Zappa deployment/update process
"settings": "my_app.settings_callback", // After loading the settings
"zip": "my_app.zip_callback", // After creating the package
"post": "my_app.post_callback", // After command has excuted
},
"cache_cluster_enabled": false, // Use APIGW cache cluster (default False)
"cache_cluster_size": .5, // APIGW Cache Cluster size (default 0.5)
"debug": true // Print Zappa configuration errors tracebacks in the 500
Expand Down
5 changes: 5 additions & 0 deletions test_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"ttt888": {
"s3_bucket": "lmbda",
"app_function": "tests.test_app.hello_world",
"callbacks": {
"settings": "test_settings.callback",
"post": "test_settings.callback",
"zip": "test_settings.callback"
},
"delete_zip": true,
"debug": true,
"parameter_depth": 2,
Expand Down
3 changes: 3 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@

def prebuild_me():
print("This is a prebuild script!")

def callback(self):
print("this is a callback")
2 changes: 2 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,11 @@ def test_human_units(self):
zappa.human_size(9999999999999)

def test_detect_dj(self):
# Sanity
settings_modules = detect_django_settings()

def test_detect_flask(self):
# Sanity
settings_modules = detect_flask_apps()

if __name__ == '__main__':
Expand Down
25 changes: 24 additions & 1 deletion zappa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import hjson as json
import inspect
import imp
import importlib
import logging
import os
import pkg_resources
Expand Down Expand Up @@ -148,6 +149,8 @@ def handle(self, argv=None):

# Load our settings
self.load_settings(vargs['settings_file'])
self.callback('settings')

if vargs['app_function'] is not None:
self.app_function = vargs['app_function']

Expand Down Expand Up @@ -202,6 +205,7 @@ def deploy(self):

# Create the Lambda Zip
self.create_package()
self.callback('zip')

# Upload it to S3
success = self.zappa.upload_to_s3(
Expand Down Expand Up @@ -250,6 +254,8 @@ def deploy(self):
# Remove the uploaded zip from S3, because it is now registered..
self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)

self.callback('post')

print("Deployed! {}".format(endpoint_url))


Expand All @@ -267,6 +273,7 @@ def update(self):

# Create the Lambda Zip,
self.create_package()
self.callback('zip')

# Upload it to S3
success = self.zappa.upload_to_s3(self.zip_path, self.s3_bucket_name)
Expand Down Expand Up @@ -295,6 +302,8 @@ def update(self):
else:
endpoint_url = self.zappa.get_api_url(self.lambda_name, self.api_stage)

self.callback('post')

print("Your updated Zappa deployment is live! {}".format(endpoint_url))

return
Expand Down Expand Up @@ -599,6 +608,21 @@ def init(self, settings_file="zappa_settings.json"):
# Utility
##

def callback(self, position):
"""
Allows the execution of custom code between creation of the zip file and deployment to AWS.
:return: None
"""
callbacks = self.zappa_settings[self.api_stage].get('callbacks', {})
callback = callbacks.get(position)

if callback:
(mod_name, cb_func) = callback.rsplit('.', 1)

module_ = importlib.import_module(mod_name)
getattr(module_, cb_func)(self) # Call the function passing self

def load_settings(self, settings_file="zappa_settings.json", session=None):
"""
Load the local zappa_settings.json file.
Expand Down Expand Up @@ -766,7 +790,6 @@ def create_package(self):
temp_settings.close()
lambda_zip.write(temp_settings.name, 'zappa_settings.py')
os.remove(temp_settings.name)
# lambda_zip.close()

def remove_local_zip(self):
"""
Expand Down

0 comments on commit 8a8d643

Please sign in to comment.