##// END OF EJS Templates
admin.py: Login to email server added
Miguel Valdez -
r683:e5270acfc266
parent child
Show More
@@ -1,346 +1,369
1 """The admin module contains all administrative classes relating to the schain python api.
1 """The admin module contains all administrative classes relating to the schain python api.
2
2
3 The main role of this module is to send some reports. It contains a
3 The main role of this module is to send some reports. It contains a
4 notification class and a standard error handing class.
4 notification class and a standard error handing class.
5
5
6 $Id: admin.py 3966 2015-12-01 14:32:29Z miguel.urco $
6 $Id: admin.py 3966 2015-12-01 14:32:29Z miguel.urco $
7 """
7 """
8 import os
8 import os
9 import smtplib
9 import smtplib
10 import ConfigParser
10 import ConfigParser
11 import StringIO
11 import StringIO
12
12
13 from email.mime.text import MIMEText
13 from email.mime.text import MIMEText
14 from email.mime.application import MIMEApplication
14 from email.mime.application import MIMEApplication
15 from email.mime.multipart import MIMEMultipart
15 from email.mime.multipart import MIMEMultipart
16
16
17 class SchainConfigure():
17 class SchainConfigure():
18
18
19 __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe"
20 __DEFAULT_ADMINISTRATOR_EMAIL = "miguel.urco@jro.igp.gob.pe"
19 __DEFAULT_ADMINISTRATOR_EMAIL = "miguel.urco@jro.igp.gob.pe"
21 __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe"
20 __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe"
21 __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe"
22 __DEFAULT_SENDER_PASS = ""
22
23
23 __SCHAIN_ADMINISTRATOR_EMAIL = "CONTACT"
24 __SCHAIN_ADMINISTRATOR_EMAIL = "CONTACT"
24 __SCHAIN_EMAIL_SERVER = "MAILSERVER"
25 __SCHAIN_EMAIL_SERVER = "MAILSERVER"
25 __SCHAIN_SENDER_EMAIL = "MAILSERVER_ACCOUNT"
26 __SCHAIN_SENDER_EMAIL = "MAILSERVER_ACCOUNT"
27 __SCHAIN_SENDER_PASS = "MAILSERVER_PASSWORD"
26
28
27 def __init__(self, initFile = None):
29 def __init__(self, initFile = None):
28
30
29 # Set configuration file
31 # Set configuration file
30 if (initFile == None):
32 if (initFile == None):
31 self.__confFilePath = "/etc/schain.conf"
33 self.__confFilePath = "/etc/schain.conf"
32 else:
34 else:
33 self.__confFilePath = initFile
35 self.__confFilePath = initFile
34
36
35 # open configuration file
37 # open configuration file
36 try:
38 try:
37 self.__confFile = open(self.__confFilePath, "r")
39 self.__confFile = open(self.__confFilePath, "r")
38 except IOError:
40 except IOError:
39 # can't read from file - use all hard-coded values
41 # can't read from file - use all hard-coded values
40 self.__initFromHardCode()
42 self.__initFromHardCode()
41 return
43 return
42
44
43 # create Parser using standard module ConfigParser
45 # create Parser using standard module ConfigParser
44 self.__parser = ConfigParser.ConfigParser()
46 self.__parser = ConfigParser.ConfigParser()
45
47
46 # read conf file into a StringIO with "[madrigal]\n" section heading prepended
48 # read conf file into a StringIO with "[madrigal]\n" section heading prepended
47 strConfFile = StringIO.StringIO("[schain]\n" + self.__confFile.read())
49 strConfFile = StringIO.StringIO("[schain]\n" + self.__confFile.read())
48
50
49 # parse StringIO configuration file
51 # parse StringIO configuration file
50 self.__parser.readfp(strConfFile)
52 self.__parser.readfp(strConfFile)
51
53
52 # read information from configuration file
54 # read information from configuration file
53 self.__readConfFile()
55 self.__readConfFile()
54
56
55 # close conf file
57 # close conf file
56 self.__confFile.close()
58 self.__confFile.close()
57
59
58
60
59 def __initFromHardCode(self):
61 def __initFromHardCode(self):
60
62
61 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
63 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
64 self.__sender_pass = self.__DEFAULT_SENDER_PASS
62 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
65 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
63 self.__email_server = self.__DEFAULT_EMAIL_SERVER
66 self.__email_server = self.__DEFAULT_EMAIL_SERVER
64
67
65 def __readConfFile(self):
68 def __readConfFile(self):
66 """__readConfFile is a private helper function that reads information from the parsed config file.
69 """__readConfFile is a private helper function that reads information from the parsed config file.
67
70
68 Inputs: None
71 Inputs: None
69
72
70 Returns: Void.
73 Returns: Void.
71
74
72 Affects: Initializes class member variables that are found in the config file.
75 Affects: Initializes class member variables that are found in the config file.
73
76
74 Exceptions: MadrigalError thrown if any key not found.
77 Exceptions: MadrigalError thrown if any key not found.
75 """
78 """
76
79
77 # get the sender email
80 # get the sender email
78 try:
81 try:
79 self.__sender_email = self.__parser.get("schain", self.__SCHAIN_SENDER_EMAIL)
82 self.__sender_email = self.__parser.get("schain", self.__SCHAIN_SENDER_EMAIL)
80 except:
83 except:
81 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
84 self.__sender_email = self.__DEFAULT_SENDER_EMAIL
82
85
86 # get the sender password
87 try:
88 self.__sender_pass = self.__parser.get("schain", self.__SCHAIN_SENDER_PASS)
89 except:
90 self.__sender_pass = self.__DEFAULT_SENDER_PASS
91
83 # get the administrator email
92 # get the administrator email
84 try:
93 try:
85 self.__admin_email = self.__parser.get("schain", self.__SCHAIN_ADMINISTRATOR_EMAIL)
94 self.__admin_email = self.__parser.get("schain", self.__SCHAIN_ADMINISTRATOR_EMAIL)
86 except:
95 except:
87 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
96 self.__admin_email = self.__DEFAULT_ADMINISTRATOR_EMAIL
88
97
89 # get the server email
98 # get the server email
90 try:
99 try:
91 self.__email_server = self.__parser.get("schain", self.__SCHAIN_EMAIL_SERVER)
100 self.__email_server = self.__parser.get("schain", self.__SCHAIN_EMAIL_SERVER)
92 except:
101 except:
93 self.__email_server = self.__DEFAULT_EMAIL_SERVER
102 self.__email_server = self.__DEFAULT_EMAIL_SERVER
94
103
95 def getEmailServer(self):
104 def getEmailServer(self):
96
105
97 return self.__email_server
106 return self.__email_server
98
107
99 def getSenderEmail(self):
108 def getSenderEmail(self):
100
109
101 return self.__sender_email
110 return self.__sender_email
102
111
112 def getSenderPass(self):
113
114 return self.__sender_pass
115
103 def getAdminEmail(self):
116 def getAdminEmail(self):
104
117
105 return self.__admin_email
118 return self.__admin_email
106
119
107 class SchainNotify:
120 class SchainNotify:
108 """SchainNotify is an object used to send messages to an administrator about a Schain software.
121 """SchainNotify is an object used to send messages to an administrator about a Schain software.
109
122
110 This object provides functions needed to send messages to an administrator about a Schain , for now
123 This object provides functions needed to send messages to an administrator about a Schain , for now
111 only sendAlert, which sends an email to the site administrator found is ADMIN_EMAIL
124 only sendAlert, which sends an email to the site administrator found is ADMIN_EMAIL
112
125
113 Usage example:
126 Usage example:
114
127
115 import schainpy.admin
128 import schainpy.admin
116
129
117 try:
130 try:
118
131
119 adminObj = schainpy.admin.SchainNotify()
132 adminObj = schainpy.admin.SchainNotify()
120 adminObj.sendAlert('This is important!', 'Important Message')
133 adminObj.sendAlert('This is important!', 'Important Message')
121
134
122 except schainpy.admin.SchainError, e:
135 except schainpy.admin.SchainError, e:
123
136
124 print e.getExceptionStr()
137 print e.getExceptionStr()
125
138
126
139
127 Non-standard Python modules used:
140 Non-standard Python modules used:
128 None
141 None
129
142
130 Exceptions thrown: None - Note that SchainNotify tries every trick it knows to avoid
143 Exceptions thrown: None - Note that SchainNotify tries every trick it knows to avoid
131 throwing exceptions, since this is the class that will generally be called when there is a problem.
144 throwing exceptions, since this is the class that will generally be called when there is a problem.
132
145
133 Change history:
146 Change history:
134
147
135 Written by "Miguel Urco":mailto:miguel.urco@jro.igp.gob.pe Dec. 1, 2015
148 Written by "Miguel Urco":mailto:miguel.urco@jro.igp.gob.pe Dec. 1, 2015
136 """
149 """
137
150
138 #constants
151 #constants
139
152
140 def __init__(self):
153 def __init__(self):
141 """__init__ initializes SchainNotify by getting some basic information from SchainDB and SchainSite.
154 """__init__ initializes SchainNotify by getting some basic information from SchainDB and SchainSite.
142
155
143 Note that SchainNotify tries every trick it knows to avoid throwing exceptions, since
156 Note that SchainNotify tries every trick it knows to avoid throwing exceptions, since
144 this is the class that will generally be called when there is a problem.
157 this is the class that will generally be called when there is a problem.
145
158
146 Inputs: Existing SchainDB object, by default = None.
159 Inputs: Existing SchainDB object, by default = None.
147
160
148 Returns: void
161 Returns: void
149
162
150 Affects: Initializes self.__binDir.
163 Affects: Initializes self.__binDir.
151
164
152 Exceptions: None.
165 Exceptions: None.
153 """
166 """
154
167
155 # note that the main configuration file is unavailable
168 # note that the main configuration file is unavailable
156 # the best that can be done is send an email to root using localhost mailserver
169 # the best that can be done is send an email to root using localhost mailserver
157 confObj = SchainConfigure()
170 confObj = SchainConfigure()
158
171
159 self.__emailFromAddress = confObj.getSenderEmail()
172 self.__emailFromAddress = confObj.getSenderEmail()
173 self.__emailPass = confObj.getSenderPass()
160 self.__emailToAddress = confObj.getAdminEmail()
174 self.__emailToAddress = confObj.getAdminEmail()
161 self.__emailServer = confObj.getEmailServer()
175 self.__emailServer = confObj.getEmailServer()
162
176
163 def sendEmail(self, email_from, email_to, subject='Error running ...', message="", subtitle="", filename="", html_format=True):
177 def sendEmail(self, email_from, email_to, subject='Error running ...', message="", subtitle="", filename="", html_format=True):
164
178
165 msg = MIMEMultipart()
179 msg = MIMEMultipart()
166 msg['Subject'] = subject
180 msg['Subject'] = subject
167 msg['From'] = "(Python SChain API): " + email_from
181 msg['From'] = "(Python SChain API): " + email_from
168 msg['Reply-to'] = email_from
182 msg['Reply-to'] = email_from
169 msg['To'] = email_to
183 msg['To'] = email_to
170
184
171 # That is what u see if dont have an email reader:
185 # That is what u see if dont have an email reader:
172 msg.preamble = 'SChainPy'
186 msg.preamble = 'SChainPy'
173
187
174 if html_format:
188 if html_format:
175 message = "<h1> %s </h1>" %subject + "<h3>" + subtitle.replace("\n", "</h3><h3>\n") + "</h3>" + message.replace("\n", "<br>\n")
189 message = "<h1> %s </h1>" %subject + "<h3>" + subtitle.replace("\n", "</h3><h3>\n") + "</h3>" + message.replace("\n", "<br>\n")
176 message = "<html>\n" + message + '</html>'
190 message = "<html>\n" + message + '</html>'
177
191
178 # This is the textual part:
192 # This is the textual part:
179 part = MIMEText(message, "html")
193 part = MIMEText(message, "html")
180 else:
194 else:
181 message = subject + "\n" + subtitle + "\n" + message
195 message = subject + "\n" + subtitle + "\n" + message
182 part = MIMEText(message)
196 part = MIMEText(message)
183
197
184 msg.attach(part)
198 msg.attach(part)
185
199
186 if os.path.isfile(filename):
200 if os.path.isfile(filename):
187 # This is the binary part(The Attachment):
201 # This is the binary part(The Attachment):
188 part = MIMEApplication(open(filename,"rb").read())
202 part = MIMEApplication(open(filename,"rb").read())
189 part.add_header('Content-Disposition',
203 part.add_header('Content-Disposition',
190 'attachment',
204 'attachment',
191 filename=os.path.basename(filename))
205 filename=os.path.basename(filename))
192 msg.attach(part)
206 msg.attach(part)
193
207
194 # Create an instance in SMTP server
208 # Create an instance in SMTP server
195 smtp = smtplib.SMTP(self.__emailServer)
209 try:
210 smtp = smtplib.SMTP(self.__emailServer)
211 except:
212 print "***** Could not connect to server %s *****" %self.__emailServer
213 return 0
214
196 # Start the server:
215 # Start the server:
197 # smtp.ehlo()
216 # smtp.ehlo()
198 # smtp.login(email_from, email_from_pass)
217 if self.__emailPass:
218 smtp.login(self.__emailFromAddress, self.__emailPass)
199
219
200 # Send the email
220 # Send the email
201 smtp.sendmail(msg['From'], msg['To'], msg.as_string())
221 smtp.sendmail(msg['From'], msg['To'], msg.as_string())
202 smtp.quit()
222 smtp.quit()
223 smtp.close()
203
224
225 return 1
204
226
205 def sendAlert(self, message, subject = "", subtitle="", filename=""):
227 def sendAlert(self, message, subject = "", subtitle="", filename=""):
206 """sendAlert sends an email with the given message and optional title.
228 """sendAlert sends an email with the given message and optional title.
207
229
208 Inputs: message (string), and optional title (string)
230 Inputs: message (string), and optional title (string)
209
231
210 Returns: void
232 Returns: void
211
233
212 Affects: none
234 Affects: none
213
235
214 Exceptions: None.
236 Exceptions: None.
215 """
237 """
216 print "***** Sending alert to %s *****" %self.__emailToAddress
238 print "***** Sending alert to %s *****" %self.__emailToAddress
217 # set up message
239 # set up message
218
240
219 self.sendEmail(email_from=self.__emailFromAddress,
241 sent=self.sendEmail(email_from=self.__emailFromAddress,
220 email_to=self.__emailToAddress,
242 email_to=self.__emailToAddress,
221 subject=subject,
243 subject=subject,
222 message=message,
244 message=message,
223 subtitle=subtitle,
245 subtitle=subtitle,
224 filename=filename)
246 filename=filename)
225
247
226 print "***** Your system administrator has been notified *****"
248 if sent:
249 print "***** Your system administrator has been notified *****"
227
250
228
251
229 def notify(self, email, message, subject = "", subtitle="", filename=""):
252 def notify(self, email, message, subject = "", subtitle="", filename=""):
230 """notify sends an email with the given message and title to email.
253 """notify sends an email with the given message and title to email.
231
254
232 Inputs: email (string), message (string), and subject (string)
255 Inputs: email (string), message (string), and subject (string)
233
256
234 Returns: void
257 Returns: void
235
258
236 Affects: none
259 Affects: none
237
260
238 Exceptions: None.
261 Exceptions: None.
239 """
262 """
240
263
241 print "Notifying to %s ..." %email
264 print "Notifying to %s ..." %email
242
265
243 self.sendEmail(email_from=self.__emailFromAddress,
266 self.sendEmail(email_from=self.__emailFromAddress,
244 email_to=email,
267 email_to=email,
245 subject=subject,
268 subject=subject,
246 message=message,
269 message=message,
247 subtitle=subtitle,
270 subtitle=subtitle,
248 filename=filename)
271 filename=filename)
249
272
250 print "***** Your system administrator has been notified *****"
273 print "***** Your system administrator has been notified *****"
251
274
252 class SchainError:
275 class SchainError:
253 """SchainError is an exception class that is thrown for all known errors in using Schain Py lib.
276 """SchainError is an exception class that is thrown for all known errors in using Schain Py lib.
254
277
255 Usage example:
278 Usage example:
256
279
257 import sys, traceback
280 import sys, traceback
258 import schainpy.admin
281 import schainpy.admin
259
282
260 try:
283 try:
261
284
262 test = open('ImportantFile.txt', 'r')
285 test = open('ImportantFile.txt', 'r')
263
286
264 except:
287 except:
265
288
266 raise schainpy.admin.SchainError('ImportantFile.txt not opened!',
289 raise schainpy.admin.SchainError('ImportantFile.txt not opened!',
267 traceback.format_exception(sys.exc_info()[0],
290 traceback.format_exception(sys.exc_info()[0],
268 sys.exc_info()[1],
291 sys.exc_info()[1],
269 sys.exc_info()[2]))
292 sys.exc_info()[2]))
270 """
293 """
271
294
272
295
273 def __init__(self, strInterpretation, exceptionList):
296 def __init__(self, strInterpretation, exceptionList):
274 """ __init__ gathers the interpretation string along with all information from sys.exc_info().
297 """ __init__ gathers the interpretation string along with all information from sys.exc_info().
275
298
276 Inputs: strIntepretation - A string representing the programmer's interpretation of
299 Inputs: strIntepretation - A string representing the programmer's interpretation of
277 why the exception occurred
300 why the exception occurred
278
301
279 exceptionList - a list of strings completely describing the exception.
302 exceptionList - a list of strings completely describing the exception.
280 Generated by traceback.format_exception(sys.exc_info()[0],
303 Generated by traceback.format_exception(sys.exc_info()[0],
281 sys.exc_info()[1],
304 sys.exc_info()[1],
282 sys.exc_info()[2])
305 sys.exc_info()[2])
283
306
284 Returns: Void.
307 Returns: Void.
285
308
286 Affects: Initializes class member variables _strInterp, _strExcList.
309 Affects: Initializes class member variables _strInterp, _strExcList.
287
310
288 Exceptions: None.
311 Exceptions: None.
289 """
312 """
290
313
291 self._strInterp = strInterpretation
314 self._strInterp = strInterpretation
292 self._strExcList = exceptionList
315 self._strExcList = exceptionList
293
316
294
317
295 def getExceptionStr(self):
318 def getExceptionStr(self):
296 """ getExceptionStr returns a formatted string ready for printing completely describing the exception.
319 """ getExceptionStr returns a formatted string ready for printing completely describing the exception.
297
320
298 Inputs: None
321 Inputs: None
299
322
300 Returns: A formatted string ready for printing completely describing the exception.
323 Returns: A formatted string ready for printing completely describing the exception.
301
324
302 Affects: None
325 Affects: None
303
326
304 Exceptions: None.
327 Exceptions: None.
305 """
328 """
306 excStr = 'The following Schain Python exception has occurred:\n'
329 excStr = 'The following Schain Python exception has occurred:\n'
307 excStr = excStr + self._strInterp + '\n\n'
330 excStr = excStr + self._strInterp + '\n\n'
308
331
309 if self._strExcList != None:
332 if self._strExcList != None:
310 for item in self._strExcList:
333 for item in self._strExcList:
311 excStr = excStr + str(item) + '\n'
334 excStr = excStr + str(item) + '\n'
312
335
313 return excStr
336 return excStr
314
337
315 def __str__(self):
338 def __str__(self):
316 return(self.getExceptionStr())
339 return(self.getExceptionStr())
317
340
318
341
319 def getExceptionHtml(self):
342 def getExceptionHtml(self):
320 """ getExceptionHtml returns an Html formatted string completely describing the exception.
343 """ getExceptionHtml returns an Html formatted string completely describing the exception.
321
344
322 Inputs: None
345 Inputs: None
323
346
324 Returns: A formatted string ready for printing completely describing the exception.
347 Returns: A formatted string ready for printing completely describing the exception.
325
348
326 Affects: None
349 Affects: None
327
350
328 Exceptions: None.
351 Exceptions: None.
329 """
352 """
330
353
331 excStr = '<BR>The following Schain Python exception has occurred:\n<BR>'
354 excStr = '<BR>The following Schain Python exception has occurred:\n<BR>'
332 excStr = excStr + self._strInterp + '\n<BR>\n'
355 excStr = excStr + self._strInterp + '\n<BR>\n'
333
356
334 if self._strExcList != None:
357 if self._strExcList != None:
335 for item in self._strExcList:
358 for item in self._strExcList:
336 excStr = excStr + str(item) + '\n<BR>'
359 excStr = excStr + str(item) + '\n<BR>'
337
360
338 return excStr
361 return excStr
339
362
340 if __name__ == '__main__':
363 if __name__ == '__main__':
341
364
342 test = SchainNotify()
365 test = SchainNotify()
343
366
344 test.sendAlert('This is a message from the python module SchainNotify', 'Test from SchainNotify')
367 test.sendAlert('This is a message from the python module SchainNotify', 'Test from SchainNotify')
345
368
346 print 'Hopefully message sent - check.'
369 print 'Hopefully message sent - check.'
@@ -1,5 +1,8
1 #Copy this file to /etc/schain.conf
1 #Copy this file to /etc/schain.conf
2
2 [schain]
3 [schain]
4
3 CONTACT = miguel.urco@jro.igp.gob.pe
5 CONTACT = miguel.urco@jro.igp.gob.pe
4 MAILSERVER = jro-zimbra.igp.gob.pe
6 MAILSERVER = jro-zimbra.igp.gob.pe
5 MALSERVER_ACCOUNT = notifier-schain@jro.igp.gob.pe No newline at end of file
7 MALSERVER_ACCOUNT = notifier-schain@jro.igp.gob.pe
8 MAILSERVER_PASSWORD = No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now