aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Reyna <David.Reyna@windriver.com>2019-01-29 16:11:48 -0800
committerDavid Reyna <David.Reyna@windriver.com>2019-01-29 16:11:48 -0800
commit2feba03263ea000b2985cd80b76868beaf025459 (patch)
tree812a83ecea39259e58d501ad07e2748f8d848787
parent1cd553d6f2afee77e65207ef34e73c450879380b (diff)
downloadsrtool-2feba03263ea000b2985cd80b76868beaf025459.tar.gz
srtool-2feba03263ea000b2985cd80b76868beaf025459.tar.bz2
srtool-2feba03263ea000b2985cd80b76868beaf025459.zip
srtool_email: update email values, error handling
Various updates and fixes: * Use the new SRT_EMAIL_* variable names * Fix hardcoded value for 'from' address * Add additional error handling * Allow the email settings to be defined in SrtSetting values, and provide example in the ACME datasource file Signed-off-by: David Reyna <David.Reyna@windriver.com>
-rwxr-xr-xbin/acme/datasource.json_sample23
-rwxr-xr-xbin/common/srtool_email.py46
-rwxr-xr-xbin/dev_tools/srt_env.sh6
-rw-r--r--lib/orm/models.py8
-rw-r--r--lib/srtgui/api.py4
-rw-r--r--lib/srtgui/tables.py2
-rw-r--r--lib/srtgui/views.py12
7 files changed, 75 insertions, 26 deletions
diff --git a/bin/acme/datasource.json_sample b/bin/acme/datasource.json_sample
index 48e3838e..dc1d0188 100755
--- a/bin/acme/datasource.json_sample
+++ b/bin/acme/datasource.json_sample
@@ -38,11 +38,32 @@
"helptext" : "The registered script to manage defects",
"value" : "bin/acme/srtool_defect.py"
},
-
{
"name" : "SRTOOL_DEFECT_URLBASE",
"helptext" : "The url base lookup for the external defect tool",
"value" : "https://bugzilla.acme.org/show_bug.cgi?id="
+ },
+
+ {
+ "name" : "SRT_EMAIL_SMTP",
+ "helptext" : "The ACME SMTP server",
+ "value" : "smtp.acme.com"
+ },
+ {
+ "name" : "SRT_EMAIL_USER",
+ "helptext" : "The ACME email user account",
+ "value" : "srt_manager"
+ },
+ {
+ "_comment_" : "Fetch from environment",
+ "name" : "SRT_EMAIL_PASSWD",
+ "helptext" : "The ACME email user password",
+ "value" : ""
+ },
+ {
+ "name" : "SRT_EMAIL_FROM",
+ "helptext" : "The ACME email 'from' address",
+ "value" : "srt.manager@acme.com"
}
],
"datasource" : [
diff --git a/bin/common/srtool_email.py b/bin/common/srtool_email.py
index 103c8be0..c57fa9ab 100755
--- a/bin/common/srtool_email.py
+++ b/bin/common/srtool_email.py
@@ -44,6 +44,8 @@ msg = ''
verbose = False
test = False
+srtErrorLog = 'srt_errors.txt'
+
#################################
# Send the email
#
@@ -67,6 +69,11 @@ def enter_message():
print("Message length is", len(msg))
+def srt_error_log(msg):
+ f1=open(srtErrorLog, 'a')
+ f1.write("|" + msg + "|\n" )
+ f1.close()
+
#################################
# Send the email
#
@@ -135,28 +142,17 @@ def main(argv):
args = parser.parse_args()
# Resolve the arguments
- if args.smtpfrom:
- fromaddr = args.smtpfrom
- else:
- print("ERROR: missing 'from' address")
- exit(1)
if args.smtpto:
toaddrs = args.smtpto
toaddrs = toaddrs.split(',')
else:
- print("ERROR: missing 'to' address")
+ print("ERROR: missing 'to' address", file=sys.stderr)
exit(1)
if args.subject:
subject = args.subject
else:
- print("ERROR: missing 'subject'")
+ print("ERROR: missing 'subject'", file=sys.stderr)
exit(1)
- if args.smtpserver:
- smtpserver = args.smtpserver
- else:
- smtpserver = os.environ.get('SRT_SMTP')
- if not smtpserver:
- smtpserver = 'localhost'
verbose = args.verbose
test = args.test
@@ -164,11 +160,25 @@ def main(argv):
if args.user:
srt_user = args.user
else:
- srt_user = os.environ.get('SRT_USER')
+ srt_user = os.getenv('SRT_EMAIL_USER','')
if args.passwd:
srt_passwd = args.passwd
else:
- srt_passwd = os.environ.get('SRT_PASSWD')
+ srt_passwd = os.getenv('SRT_EMAIL_PASSWD','')
+ # Server settings
+ if args.smtpserver:
+ smtpserver = args.smtpserver
+ else:
+ smtpserver = os.getenv('SRT_EMAIL_SMTP','')
+ if args.smtpfrom:
+ fromaddr = args.smtpfrom
+ else:
+ fromaddr = os.getenv('SRT_EMAIL_FROM','')
+ if not smtpserver or not srt_user or not srt_passwd or not fromaddr:
+ msg = "FATAL ERROR: Missing SMTP('%s'), User('%s'), Password('%s'), and/or From('%s') for email access" % (smtpserver,srt_user,'*******' if srt_passwd else '',fromaddr)
+ print(msg, file=sys.stderr)
+ srt_error_log(msg)
+ exit(1)
# Encryption
if args.tls:
@@ -187,9 +197,13 @@ def main(argv):
elif args.prompt:
enter_message()
else:
- print("ERROR: missing message source")
+ print("ERROR: missing message source", file=sys.stderr)
exit(1)
+ # Debug
+ if verbose:
+ srt_error_log("EMAIL: SMTP('%s'), User('%s'), Password('%s'), From('%s'), args('%s')" % (smtpserver,srt_user,srt_passwd,fromaddr,str(args)))
+
# Send the email
send_email()
diff --git a/bin/dev_tools/srt_env.sh b/bin/dev_tools/srt_env.sh
index 1cceb1c5..f36a2329 100755
--- a/bin/dev_tools/srt_env.sh
+++ b/bin/dev_tools/srt_env.sh
@@ -22,9 +22,10 @@ if [ "debug" != "$mode" ] ; then
export SRTDBG_SKIP_CPE_IMPORT=0
export SRT_SKIP_AUTOUPDATE=0
# Email credentials
- export SRT_SMTP=temp_smnp_server
+ export SRT_EMAIL_SMTP=smtp.org.com
export SRT_EMAIL_PASSWD=temp_password
export SRT_EMAIL_USER=temp_user
+ export SRT_EMAIL_FROM=temp_user@org.com
# Defect (e.g. Jira) credentials
export SRT_DEFECT_PASSWD=temp_password
export SRT_DEFECT_USER=temp_user
@@ -42,9 +43,10 @@ else
export SRTDBG_SKIP_CPE_IMPORT=0
export SRT_SKIP_AUTOUPDATE=1
# Email credentials
- export SRT_SMTP=temp_smnp_server
+ export SRT_EMAIL_SMTP=smtp.org.com
export SRT_EMAIL_PASSWD=temp_password
export SRT_EMAIL_USER=temp_user
+ export SRT_EMAIL_FROM=temp_user@org.com
# Defect (e.g. Jira) credentials
export SRT_DEFECT_PASSWD=temp_password
export SRT_DEFECT_USER=temp_user
diff --git a/lib/orm/models.py b/lib/orm/models.py
index fd0fb0e9..2a3a1368 100644
--- a/lib/orm/models.py
+++ b/lib/orm/models.py
@@ -122,6 +122,14 @@ class SrtSetting(models.Model):
def __str__(self):
return "Setting %s = %s" % (self.name, self.value)
+ @staticmethod
+ def get_setting(key,default):
+ try:
+ return(SrtSetting.objects.get(name=key).value)
+ except:
+ return(default)
+
+
class HelpText(models.Model):
VARIABLE = 0
HELPTEXT_AREA = ((VARIABLE, 'variable'), )
diff --git a/lib/srtgui/api.py b/lib/srtgui/api.py
index c16b30cc..84543789 100644
--- a/lib/srtgui/api.py
+++ b/lib/srtgui/api.py
@@ -64,10 +64,10 @@ def execute_process(*args):
process.wait()
raise
retcode = process.poll()
- return retcode, stdout, stderr
+ return retcode, stdout.decode('UTF-8'), stderr.decode('UTF-8')
else:
result = subprocess.run(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- return result.returncode,result.stdout,result.stderr
+ return result.returncode,result.stdout.decode('UTF-8'),result.stderr.decode('UTF-8')
#
# Extract Upstream CVE record details
diff --git a/lib/srtgui/tables.py b/lib/srtgui/tables.py
index 4da603b9..7cbce339 100644
--- a/lib/srtgui/tables.py
+++ b/lib/srtgui/tables.py
@@ -1958,7 +1958,7 @@ class NotificationsTable(ToasterTable):
users_link_template = '''
{% for tu in data.todo2user.all %}
- {% if not forloop.first %}, {% endif %}{{tu.user.name}}</a>
+ {% if not forloop.first %}, {% endif %}{{tu.user.username}}</a>
{% endfor %}
'''
self.add_column(title="Users",
diff --git a/lib/srtgui/views.py b/lib/srtgui/views.py
index 088a716d..dc8f7642 100644
--- a/lib/srtgui/views.py
+++ b/lib/srtgui/views.py
@@ -1539,24 +1539,28 @@ def xhr_notifications(request):
i,notify.category,notify.get_priority_text,"%s://%s%s" % (request.scheme,request.get_host(),notify.url),notify.description,)
# Get email addresses
for nu in notify.todo2user.all():
- if 'All' == nu.user.name:
+ if 'All' == nu.user.username:
continue
if not nu.user.email:
continue
to_emails[nu.user.email] = 1
_log("NOTIFICATION EMAILS:\n%s\n%s\n" % (msg,to_emails.keys()))
if len(to_emails.keys()):
+ # If SrtSetting pass user,password,fromaddr,server else expect from environment
result_returncode,result_stdout,result_stderr = execute_process(
- 'bin/srtool_email.py',
+ 'bin/common/srtool_email.py',
'--to=%s' % ','.join(to_emails.keys()),
- '--from=%s' % 'david.reyna@windriver.com',
+ '--from=%s' % SrtSetting.get_setting('SRT_EMAIL_FROM',''),
+ '--user=%s' % SrtSetting.get_setting('SRT_EMAIL_USER',''),
+ '--passwd=%s' % SrtSetting.get_setting('SRT_EMAIL_PASSWD',''),
+ '--server=%s' % SrtSetting.get_setting('SRT_EMAIL_SMTP',''),
'--tls',
'-m=%s' % msg,
'-s=%s' % 'Message from SRTool',
)
_log("SRT_EMAIL=%s|%s|%s" % (result_returncode,result_stdout,result_stderr))
if 0 != result_returncode:
- results_msg = "ERROR:email:%s,%s" % (result_returncode,result_stderr)
+ results_msg = "ERROR:email:%s,'%s','%s'" % (result_returncode,result_stderr,result_stdout)
else:
results_msg = "Emails sent to %s" % ','.join(to_emails.keys())
else: