import cgi
import textwrap
import copy
+import socket
+import pwd
from email.message import EmailMessage
LOG = logging.getLogger(__name__)
DEFAULT_FROM_EMAIL = 'puppetmaster@pixelpark.com'
-DEFAULT_FROM_SENDER = 'Puppetmaster <{}>'.format(DEFAULT_FROM_EMAIL)
+DEFAULT_FROM_SENDER = 'Puppetmaster at {h} <{a}>'.format(
+ h=socket.gethostname(), a=DEFAULT_FROM_EMAIL)
DEFAULT_TO_EMAIL = 'puppet@pixelpark.com'
DEFAULT_TO_SENDER = 'Puppet <{}>'.format(DEFAULT_TO_EMAIL)
default_output_type = 'txt'
default_mime_type = valid_output_types[default_output_type]
+ re_selbstlaut = re.compile(r'^[ieaouy]', re.IGNORECASE)
+
open_args = {}
if six.PY3:
open_args = {
self._html_title = None
self._no_error_mail = False
self._cachefile = self.default_cachefile_stem
+ self._mail_subject = None
+ self._mail_headline = None
if not hasattr(self, '_output_type'):
self._output_type = self.default_output_type
self.mail_cc_addresses = [self.default_email,]
self.sender_address = DEFAULT_FROM_SENDER
+ try:
+ pwd_info = pwd.getpwuid(os.geteuid())
+ self.sender_address = "{u} at {h} <{a}>".format(
+ u=pwd_info.pw_name, h=socket.gethostname(), a=DEFAULT_FROM_EMAIL)
+ except Exception as e:
+ sys.stderr.write("\n{cn}: {e}\n\n".format(cn=e.__class__.__name__, e=e))
+
self._log_directory = os.sep + os.path.join('var', 'log', 'webhooks')
self.do_arg_parser()
return self._html_title
return self.appname
+ # -----------------------------------------------------------
+ @property
+ def mail_subject(self):
+ """The subject to use for sending the mail."""
+ return self._mail_subject
+
+ @mail_subject.setter
+ def mail_subject(self, value):
+ if not to_bool(value):
+ self._mail_subject = None
+ return
+
+ val = str(value).strip()
+ if val == '':
+ self._mail_subject = None
+ return
+
+ self._mail_subject = val
+
+ # -----------------------------------------------------------
+ @property
+ def mail_headline(self):
+ """The subject to use for sending the mail."""
+ return self._mail_headline
+
+ @mail_headline.setter
+ def mail_headline(self, value):
+ if not to_bool(value):
+ self._mail_headline = None
+ return
+
+ val = str(value).strip()
+ if val == '':
+ self._mail_headline = None
+ return
+
+ self._mail_headline = val
+
# -----------------------------------------------------------
@property
def log_directory(self):
res['no_error_mail'] = self.no_error_mail
res['open_args'] = self.open_args
res['main_branches'] = copy.copy(self.main_branches)
+ res['mail_subject'] = self.mail_subject
+ res['mail_headline'] = self.mail_headline
return res
arg_parser.print_usage(sys.stdout)
sys.exit(0)
+ if self.cmdline_args.no_error_mail:
+ self.no_error_mail = True
+
if self.cmdline_args.data_dir:
path = self.cmdline_args.data_dir
if not os.path.isabs(path):
if 'default_email' in config and config['default_email']:
self.default_email = config['default_email']
+ if 'sender_address' in config and config['sender_address']:
+ self.sender_address = config['sender_address']
+
if 'smtp_server' in config and config['smtp_server'].strip():
self.smtp_server = config['smtp_server'].strip()
if self.pre_run():
self.run()
except BaseHookError as e:
- msg = "Got a {n} performing {a}: {e}".format(
- n=e.__class__.__name__, a=self.appname, e=e)
+ cn = e.__class__.__name__
+ n = ''
+ if self.re_selbstlaut.match(cn):
+ n = 'n'
+ msg = "Got a{n} {cn} performing {a}: {e}".format(n=n, cn=cn, a=self.appname, e=e)
self.error_data.append(msg)
LOG.error(msg)
except Exception as e:
- msg = "Got a {n} performing {a}: {e}".format(
- n=e.__class__.__name__, a=self.appname, e=e)
+ cn = e.__class__.__name__
+ n = ''
+ if self.re_selbstlaut.match(cn):
+ n = 'n'
+ msg = "Got a{n} {cn} performing {a}: {e}".format(n=n, cn=cn, a=self.appname, e=e)
msg += "\n\nTraceback:\n{}".format(traceback.format_exc())
self.error_data.append(msg)
LOG.error(msg)
except Exception as e:
- msg = "Got a {n} reading input data as JSON: {e}".format(n=e.__class__.__name__, e=e)
+ cn = e.__class__.__name__
+ n = ''
+ if self.re_selbstlaut.match(cn):
+ n = 'n'
+ msg = "Got a{n} {cn} reading input data as JSON: {e}".format(n=n, cn=cn, e=e)
msg += "\nInput data: {!r}".format(self.data)
LOG.error(msg)
self.error_data.append(msg)
if len(self.error_data) > 1:
s = 's'
+
if project:
body = 'Error{s} while processing {p!r} project:\n\n'.format(
s=s, p=project)
s=s, a=self.appname)
subject = 'Puppetmaster error{s} processing {a!r}'.format(
s=s, a=self.appname)
+ if self.mail_subject:
+ subject = self.mail_subject
+ if self.mail_headline:
+ body = self.mail_headline + '\n\n'
body += '\n'.join(self.error_data)
body += '\n\nCheers\nPuppetmaster'
msg.set_content(body)
import errno
import sys
import re
+import time
from operator import itemgetter
def post_init(self):
self.read_stdin = False
- self.no_error_mail = True
+ #self.no_error_mail = True
self.initialized = True
# -------------------------------------------------------------------------
def generate_version_msgs(self, version_infos):
+ self.mail_subject = (
+ "Check for newer versions of Puppet modules in environment {!r}").format(
+ self.environment)
+
+ self.mail_headline = (
+ "Results of checking for newer versions of Puppet modules "
+ "in environment {!r}:").format(self.environment)
+
+ dt = datetime.datetime.fromtimestamp(time.time(), tz=self.tz)
+ dt_str = dt.strftime('%Y-%m-%d %H:%M:%S %Z')
+
if not version_infos:
msg = (
"Didn't found any modules in environment {!r} with a\n"
"newer version on Puppet Forge.\n\n:-D"
).format(self.environment)
self.error_data.append(msg)
+ self.error_data.append("\nChecked at: {}".format(dt_str))
return
s = ''
forge_version=str(version_info['forge_version'])
))
+ self.error_data.append("\nChecked at: {}".format(dt_str))
+
# =============================================================================
if __name__ == "__main__":