-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from the-deep/feature/simple-deployment
Add basic script for deployment
- Loading branch information
Showing
11 changed files
with
389 additions
and
4 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
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,9 @@ | ||
#! /bin/bash | ||
|
||
# /code/deploy/ -> /code/ | ||
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
ROOT_DIR=$(dirname "$BASE_DIR") | ||
|
||
cd $ROOT_DIR | ||
|
||
uwsgi --ini ./deploy/uwsgi.ini # Start uwsgi server |
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,15 @@ | ||
#! /bin/bash | ||
|
||
# /code/deploy/ -> /code/ | ||
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
ROOT_DIR=$(dirname "$BASE_DIR") | ||
|
||
cd $ROOT_DIR | ||
|
||
echo '>> [Running] Django Collectstatic and Migrate' | ||
|
||
./manage.py collectstatic --no-input | ||
./manage.py migrate --no-input | ||
|
||
# Start celery | ||
celery -A main worker -Q CELERY-DEFAULT-QUEUE,CELERY-EXPORT-HEAVY-QUEUE -E --concurrency=2 -l info |
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,12 @@ | ||
[uwsgi] | ||
|
||
chdir = /code/ | ||
module = main.wsgi | ||
|
||
master = true | ||
processes = 10 | ||
http = 0.0.0.0:80 | ||
chmod = 666 | ||
|
||
vacuum = true | ||
harakiri = 60 |
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,71 @@ | ||
import json | ||
import logging | ||
|
||
from django.http import JsonResponse | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
from sns_message_validator import ( | ||
InvalidMessageTypeException, | ||
InvalidCertURLException, | ||
InvalidSignatureVersionException, | ||
SignatureVerificationFailureException, | ||
SNSMessageValidator, | ||
) | ||
|
||
from apps.user.models import User | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
sns_message_validator = SNSMessageValidator() | ||
|
||
|
||
def verify_sns_payload(request) -> tuple[str, int]: | ||
# Validate message type from header without having to parse the request body. | ||
message_type = request.headers.get('x-amz-sns-message-type') | ||
try: | ||
sns_message_validator.validate_message_type(message_type) | ||
message = json.loads(request.body.decode('utf-8')) | ||
sns_message_validator.validate_message(message=message) | ||
except InvalidMessageTypeException: | ||
return 'Invalid message type.', 400 | ||
except json.decoder.JSONDecodeError: | ||
return 'Request body is not in json format.', 400 | ||
except InvalidCertURLException: | ||
return 'Invalid certificate URL.', 400 | ||
except InvalidSignatureVersionException: | ||
return 'Unexpected signature version.', 400 | ||
except SignatureVerificationFailureException: | ||
return 'Failed to verify signature.', 400 | ||
return 'Success', 200 | ||
|
||
|
||
@csrf_exempt | ||
def bounce_handler_view(request): | ||
if request.method != 'POST': | ||
# Return empty error | ||
return JsonResponse({'message': f'{request.method} Method not allowed'}, status=405) | ||
|
||
error_message, status_code = verify_sns_payload(request) | ||
if status_code != 200: | ||
logger.warning(f'Failed to handle bounce request: {error_message}') | ||
return JsonResponse({'message': error_message}, status=status_code) | ||
|
||
body = json.loads(request.body.decode('utf-8')) | ||
if 'SubscribeURL' in body: | ||
logger.warning(f'Verify subscription using this url: {body["SubscribeURL"]}') | ||
return JsonResponse({'message': 'Logged'}, status=200) | ||
|
||
# Do something with the data | ||
message = json.loads(body['Message']) | ||
notification_type = message['notificationType'] | ||
if notification_type == 'Bounce': | ||
recipients = message['bounce']['bouncedRecipients'] | ||
bounce_type = message['bounce']['bounceType'] | ||
if bounce_type == 'Permanent': | ||
for recipient in recipients: | ||
email_address = recipient['emailAddress'] | ||
User.objects.filter(email__iexact=email_address).update(invalid_email=True) | ||
logger.warning(f'Flagged {email_address} as invalid email') | ||
return JsonResponse({'message': 'Success'}, status=200) |
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
Oops, something went wrong.