Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slack notification for start/finish #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Optional channel, default to whatever is setup on webhook
# SLACK_CHANNEL=
# Optional webhook URL. If no webhook is provided, the command will not send any messages.
# SLACK_WEBHOOK_URL=
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/.vscode/
/*.egg-info
/.env
/.venv/

/build/
/dist/
/*.egg-info
__pycache__

/output.json
__pycache__
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ _Preview release, please do not use for anything important!_

```bash
$ pip install terraecs
$ cp .env.example .env
```
Edit the `.env` file to your taste for Slack notifications.

## Usage

Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
install_requires=[
'boto3',
'click',
'python-dotenv',
'requests',
],
entry_points={
'console_scripts': [
Expand Down
29 changes: 28 additions & 1 deletion terraecs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
import click
import json
import logging
import os
import requests
import time

from dotenv import load_dotenv
load_dotenv()


SLACK_CHANNEL = os.getenv('SLACK_CHANNEL')
SLACK_WEBHOOK_URL = os.getenv('SLACK_WEBHOOK_URL')


class ContextFilter(logging.Filter):
def filter(self, record):
Expand All @@ -26,6 +35,19 @@ def filter(self, record):

output_file = None

def notify_slack(text):
if not SLACK_WEBHOOK_URL:
return

payload = {
'text': text,
}

if SLACK_CHANNEL:
payload['channel'] = SLACK_CHANNEL

requests.post(SLACK_WEBHOOK_URL, json=payload)

def log(message, level=logging.ERROR, **context):
logger.log(level, message, extra={'context': context})

Expand Down Expand Up @@ -62,6 +84,7 @@ def run(command):
log(f'could not find the {required_key}... Aborting')
exit(1)

notify_slack(f'`terraecs run {" ".join(command)}` started')

ecs_client = boto3.client('ecs')
logs_client = boto3.client('logs')
Expand Down Expand Up @@ -142,7 +165,11 @@ def run(command):

time.sleep(sleep_seconds)

exit(get_exit_code(response['tasks'][0]['containers']))
exit_code = get_exit_code(response['tasks'][0]['containers'])

notify_slack(f'`terraecs run {" ".join(command)}` finished with exit code {exit_code}')

exit(exit_code)

if __name__ == "__main__":
cli(None)