Completed
Push — master ( 9acf22...b513d0 )
by Andrea
01:06
created

send_slack()   A

Complexity

Conditions 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
#################################################################
2
# MET v2 Metadate Explorer Tool
3
#
4
# This Software is Open Source. See License: https://github.com/TERENA/met/blob/master/LICENSE.md
5
# Copyright (c) 2012, TERENA All rights reserved.
6
#
7
# This Software is based on MET v1 developed for TERENA by Yaco Sistemas, http://www.yaco.es/
8
# MET v2 was developed for TERENA by Tamim Ziai, DAASI International GmbH, http://www.daasi.de
9
# Current version of MET has been revised for performance improvements by Andrea Biancini,
10
# Consortium GARR, http://www.garr.it
11
#########################################################################################
12
13
import hashlib, smtplib
14
from email.mime.text import MIMEText
15
from django.conf import settings
16
from slackclient import SlackClient
17
18
def compare_filecontents(a, b):
19
    if a is None:
20
        return b is None
21
    if b is None:
22
        return a is None
23
24
    md5_a = hashlib.md5(a).hexdigest()
25
    md5_b = hashlib.md5(b).hexdigest()
26
    return md5_a == md5_b
27
28
def _connect_to_smtp(server, port=25, login_type=None, username=None, password=None):
29
    smtp_send = smtplib.SMTP(server, port)
30
    smtp_send.ehlo()
31
32
    if smtp_send.has_extn('STARTTLS'):
33
        smtp_send.starttls()
34
        smtp_send.ehlo()
35
36
    if username and password:
37
        try:
38
            if login_type:
39
                smtp_send.esmtp_features['auth'] = login_type
40
            smtp_send.login(username, password)
41
        except Exception, errorMessage:
42
            print('Error occurred while trying to login to the email server with user %s: %s' % (username, errorMessage))
43
            raise
44
45
    return smtp_send
46
47
def send_slack(message):
48
    slack_config_dict = getattr(settings, "SLACK_CONFIG")
49
    if slack_config_dict and 'token' in slack_config_dict:
50
        slack_token = slack_config_dict['token']
51
        slack_channel = slack_config_dict['channel'] if 'channel' in slack_config_dict else '#devops'
52
        sc = SlackClient(slack_token)
53
54
        sc.api_call(
55
          "chat.postMessage",
56
          channel=slack_channel,
57
          text=message
58
        )
59
60
def send_mail(from_email_address, subject, message):
61
    mail_config_dict = getattr(settings, "MAIL_CONFIG")
62
    server = mail_config_dict['email_server']
63
    smtp_send = None
64
65
    if server is None:
66
        return
67
    
68
    smtp_send = _connect_to_smtp(server, mail_config_dict['email_server_port'], mail_config_dict['login_type'], mail_config_dict['username'], mail_config_dict['password'])
69
        
70
    try:
71
        message = MIMEText(message.encode("utf-8"), "plain", _charset = "UTF-8")
72
        message['From'] = from_email_address
73
        message['To'] = ",".join(mail_config_dict['to_email_address'])
74
        message['Subject'] = subject
75
            
76
        smtp_send.sendmail(
77
            from_email_address, 
78
            mail_config_dict['to_email_address'],
79
            message.as_string()
80
        )
81
    except Exception, errorMessage:
82
        print('Error occurred while trying to send an email to %s: %s' % (mail_config_dict['to_email_address'], errorMessage))
83
        raise
84
    finally:
85
        if smtp_send:
86
            smtp_send.quit()
87