Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #21 from medyagh/emailfeature
Browse files Browse the repository at this point in the history
added a feature to email the screenshots in the end if SMTP config is…
  • Loading branch information
rmb938 authored Jun 19, 2017
2 parents 63a06fe + 2cc59d0 commit a6069b3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ WINNAKER_MAX_WAIT_PIPELINE=100
WINNAKER_HIPCHAT_POSTURL=REPLACE_DEFAULT
WINNAKER_NUMBER_OF_STAGES_TO_CHECK=2

WINNAKER_EMAIL_FROM=
WINNAKER_EMAIL_TO=
WINNAKER_EMAIL_SMTP=

# Internal Config
# only change if you know what you are doing
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ build-docker-nocache:
build-docker:
docker build -t winnaker .


clean-output:
rm winnaker-screenshots/*.png || true

clean:
rm *.pyc
rm winnaker/*.pyc


run-docker:
docker run --env-file .env -it -v $(CURDIR)/winnaker-screenshots:/winnaker-screenshots/ winnaker

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setup(name='winnaker',
description='An audit tool that tests the whole system functionality of Spinnaker',
author='Target Corporation',
version='0.6.0',
version='0.7.0',
license='MIT',
packages=find_packages(),
install_requires=[
Expand Down
4 changes: 4 additions & 0 deletions winnaker/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export WINNAKER_OUTPUTPATH="./winnaker-screenshots"
export WINNAKER_MAX_WAIT_PIPELINE="100"
export WINNAKER_HIPCHAT_POSTURL="REPLACE_DEFAULT"
export WINNAKER_NUMBER_OF_STAGES_TO_CHECK="2"
export WINNAKER_EMAIL_FROM="REPLACE_DEFAULT"
export WINNAKER_EMAIL_TO="REPLACE_DEFAULT"
export WINNAKER_EMAIL_SMTP="REPLACE_DEFAULT"


# Internal Config
# only change if you know what you are doing
Expand Down
13 changes: 13 additions & 0 deletions winnaker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@
import time, logging, os
from datetime import datetime
from retrying import retry
from os import listdir
from os.path import isfile, join
from os.path import basename

# from selenium.common.exceptions import ElementNotVisibleException

def getScreenshotFiles():
return [
os.environ["WINNAKER_OUTPUTPATH"] +
"/" +
f for f in listdir(
os.environ["WINNAKER_OUTPUTPATH"]) if isfile(
join(
os.environ["WINNAKER_OUTPUTPATH"],
f))]

def get_env(env_key, default):
value = os.getenv(env_key)
if value is None or len(value) == 0:
Expand Down
9 changes: 8 additions & 1 deletion winnaker/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import argparse, logging, os, sys
from winnaker.models import *
from winnaker.notify import *
from selenium import webdriver
import pkg_resources # part of setuptools

import atexit
from datetime import datetime

def main():
print ("""
Expand Down Expand Up @@ -54,6 +56,11 @@ def main():
logging.info("Winnaker Version: {}".format(version))
logging.info("Current Config: {}".format(args))


if os.environ.get('WINNAKER_EMAIL_SMTP') is not None:
atexit.register(send_mail,os.environ["WINNAKER_EMAIL_FROM"],os.environ["WINNAKER_EMAIL_TO"],"Winnaker Screenshots "+str(datetime.utcnow()),"Here are the screenshots of the spinnaker's last run at "+str(datetime.utcnow())+" UTC Time",files=getScreenshotFiles(),server=os.environ["WINNAKER_EMAIL_SMTP"])


if args.headless:
logging.debug("Starting virtual display")
from pyvirtualdisplay import Display
Expand Down
32 changes: 32 additions & 0 deletions winnaker/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate


def send_mail(send_from, send_to, subject, text, files=None,
server="localhost"):
print ("Sending email")
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

msg.attach(MIMEText(text))

for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(
f)
msg.attach(part)

smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()

0 comments on commit a6069b3

Please sign in to comment.