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

Added Support for Slack notifications on artifact purge #37

Open
wants to merge 3 commits 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
5 changes: 5 additions & 0 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Lavatory looks for several environment variables in order to authenticate:
These will be loaded in at the beginning of a run and raise an exception
if these environment variables are missing.

In addition, Lavatory supports the ability to send Slack notifications with optional environment variables (both of which must be set):

``SLACK_API_TOKEN`` - Slack API Token

``SLACK_CHANNEL`` - Slack Channel to Post Notification to

Purging Artifacts
-----------------
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ requests
humanfriendly
party
pluginbase
slacker
17 changes: 17 additions & 0 deletions src/lavatory/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@ def load_credentials():
raise MissingEnvironmentVariable(key.upper())

return credentials

def load_slack_credentials():
"""Get slack credentials from environment variables

Returns False if no credentials set.

"""

slack_credentials = {
"api_token": os.getenv('SLACK_API_TOKEN', False),
jvasallo marked this conversation as resolved.
Show resolved Hide resolved
"slack_channel": os.getenv('SLACK_CHANNEL', False)
}

if not slack_credentials['api_token'] and not slack_credentials['slack_channel']:
slack_credentials = False

return slack_credentials
7 changes: 6 additions & 1 deletion src/lavatory/utils/artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# pylint: disable=redefined-builtin
from requests.exceptions import (BaseHTTPError, ConnectionError, HTTPError, InvalidURL, RequestException)

from ..credentials import load_credentials
from ..credentials import load_credentials, load_slack_credentials
from .slack import post_slack_message

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,6 +85,10 @@ def purge(self, dry_run, artifacts):
purged += 1
except (BaseHTTPError, HTTPError, InvalidURL, RequestException, ConnectionError) as error:
LOG.error(str(error))

if load_slack_credentials():
slack_message = "Purged Artifact: {}".format(full_artifact_url)
post_slack_message(message=slack_message, username="lavatory", icon_emoji=":name_badge:")

return purged

Expand Down
30 changes: 30 additions & 0 deletions src/lavatory/utils/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Post a message to slack."""
import logging

import slacker

from ..credentials import load_slack_credentials

LOG = logging.getLogger(__name__)


def post_slack_message(message=None, username=None, icon_emoji=None):
"""Format the message and post to the appropriate slack channel.

Args:
message (str): Message to post to slack
channel (str): Desired channel. Must start with #

"""
LOG.debug('Loading Slack Credentials')
slack_credentials = load_slack_credentials()

channel = slack_credentials['slack_channel']
LOG.debug('Slack Channel: %s\nSlack Message: %s', channel, message)

slack = slacker.Slacker(slack_credentials['api_token'])
try:
slack.chat.post_message(channel=channel, text=message, username=username, icon_emoji=icon_emoji)
LOG.info('Message posted to %s', channel)
except slacker.Error:
LOG.info("error posted message to %s", channel)