[email protected] wrote: > From: Jeff Schroeder <[email protected]> > > With multiple admins doing changes and building servers or vms in many places > it is nice to see whats going on. This patch adds the ability for cobbler to > email out reports when new hosts are built. The original idea and smtplib code > is from Christopher Johnston. The API code and format string hacks are all me. > > Changelog since v1: > - Iterate over all availabe interfaces and print their information > - Don't use private methods to get network information > - Actually make sure this all works against the devel branch > - Remove debug prints per mpdehann > > An example report (made up) will look like this: > Profile: Fedora-10-x86_64 > > eth2 Hostname: ns1.tra03.int > eth2 IP Address: 10.123.234.56 > eth2 Mac Address: 0A:31:CA:4D:21:7E > eth2 Static: True > > eth3 Hostname: ns1-mgmt.tra03.int > eth3 IP Address: 192.168.234.56 > eth3 Mac Address: 0C:21:5C:4A:31:F2 > eth3 Static: True > > Kernel Options: ksdevice=bootif console=ttyS0,115200 selinux=0 > Templates: /etc/ntp.conf, /etc/snmp/snmpd.conf > > Signed-off-By: Jeff Schroeder <[email protected]> > --- > cobbler.spec | 6 ++- > cobbler/settings.py | 5 ++ > installer_templates/settings.template | 6 ++ > setup.py | 2 +- > triggers/build_report.trigger | 118 > +++++++++++++++++++++++++++++++++ > 5 files changed, 135 insertions(+), 2 deletions(-) > create mode 100755 triggers/build_report.trigger > > diff --git a/cobbler.spec b/cobbler.spec > index 0f5ac05..5c0ce25 100644 > --- a/cobbler.spec > +++ b/cobbler.spec > @@ -3,7 +3,7 @@ Summary: Boot server configurator > Name: cobbler > AutoReq: no > Version: 1.5.0 > -Release: 1%{?dist} > +Release: 2%{?dist} > Source0: %{name}-%{version}.tar.gz > License: GPLv2+ > Group: Applications/System > @@ -252,6 +252,7 @@ test "x$RPM_BUILD_ROOT" != "x" && rm -rf $RPM_BUILD_ROOT > %config(noreplace) /var/lib/cobbler/triggers/install/pre/status_pre.trigger > %config(noreplace) > /var/lib/cobbler/triggers/install/pre/clear_anamon_logs.trigger > %config(noreplace) /var/lib/cobbler/triggers/install/post/status_post.trigger > +%config(noreplace) > /var/lib/cobbler/triggers/install/post/build_report.trigger > > %defattr(664,root,root) > %config(noreplace) /etc/cobbler/settings > @@ -288,6 +289,9 @@ test "x$RPM_BUILD_ROOT" != "x" && rm -rf $RPM_BUILD_ROOT > > %changelog > > +* Fri Feb 6 2009 Jeff Schroeder <[email protected]> - 1.5.0-2 > +- Adding build_report.trigger > + > * Mon Dec 22 2008 Michael DeHaan <[email protected]> - 1.5.0-1 > - Development release start. > > diff --git a/cobbler/settings.py b/cobbler/settings.py > index e87536c..e3377e7 100644 > --- a/cobbler/settings.py > +++ b/cobbler/settings.py > @@ -105,6 +105,11 @@ DEFAULTS = { > "yumdownloader_flags" : "--resolve", > "yumreposync_flags" : "-l", > "anamon_enabled" : 0, > + "build_reporting" : 0, > + "build_reporting_to_address" : "", > + "build_reporting_sender" : "", > + "build_reporting_subject" : "", > + "build_reporting_smtp_server" : "localhost", > } > > > diff --git a/installer_templates/settings.template > b/installer_templates/settings.template > index 1733b49..3c9f91e 100644 > --- a/installer_templates/settings.template > +++ b/installer_templates/settings.template > @@ -25,6 +25,12 @@ allow_duplicate_macs: 0 > # the path to BIND's executable for this distribution. > bind_bin: /usr/sbin/named > > +# Email out a host report when cobbler finishes building it. > +# The sender is optional and this is disabled by default. > +build_reporting: 0 > +build_reporting_sender: "" > +#build_reporting_email: ['[email protected]', > '[email protected]'] > + > # Cheetah-language kickstart templates can import Python modules. > # while this is a useful feature, it is not safe to allow them to > # import anything they want. This whitelists which modules can be > diff --git a/setup.py b/setup.py > index b31e5fc..e068c08 100644 > --- a/setup.py > +++ b/setup.py > @@ -339,7 +339,7 @@ if __name__ == "__main__": > ("%s/delete/repo/post" % trigpath, []), > ("%s/delete/repo/post" % trigpath, []), > ("%s/install/pre" % trigpath, [ > "triggers/status_pre.trigger", "triggers/clear_anamon_logs.trigger"]), > - ("%s/install/post" % trigpath, [ > "triggers/status_post.trigger"]), > + ("%s/install/post" % trigpath, [ > "triggers/status_post.trigger", "triggers/build_report.trigger"]), > ("%s/sync/pre" % trigpath, []), > ("%s/sync/post" % trigpath, [ > "triggers/restart-services.trigger" ]) > ], > diff --git a/triggers/build_report.trigger b/triggers/build_report.trigger > new file mode 100755 > index 0000000..3bc36be > --- /dev/null > +++ b/triggers/build_report.trigger > @@ -0,0 +1,118 @@ > +#!/usr/bin/python > + > + ############################################################### > + # Written 2/3/09 and released under the GNU/GPLv2 ## > + # (c) Jeff Schroeder <[email protected]> # # > +############################################################### # > +# # # > +# build_report.trigger - Post install trigger for cobbler to # # > +# send out a pretty email report that # # > +# contains system information. # # > +# # # > +################################################################ > + > +import smtplib > +from sys import (argv, exit) > +from socket import getfqdn > +from cobbler import api > +cobbler_api = api.BootAPI() > +from utils import uniquify > +settings = cobbler_api.settings() > + > +name = argv[2] # name of system or profile > +profile = cobbler_api.find_system(name).profile > + > +# Make this trigger a no-op if host_build_reporting is disabled or if > +# it is enabled with no email address configured in the settings file > +if not settings.build_reporting: > + exit(0) > +else: > + if not settings.build_reporting_email: > + exit(0) > + else: > + TO = settings.build_reporting_email > + > +# Add the ability to specify an MTA for servers that don't run their own > +if settings.build_reporting_smtp_server: > + SERVER = settings.build_reporting_smtp_server > +else: > + SERVER = "localhost" > + > +# Use a custom from address or fall back to a reasonable default > +if settings.build_reporting_sender: > + FROM = settings.build_reporting_sender > +else: > + FROM = "cobbler@" + getfqdn() > + > +if not settings.build_reporting_subject: > + SUBJECT = 'Cobbler Host Report for [' + name + ']' > + > +# Show the template files used and unique the list > +templates = uniquify(cobbler_api.find_system(name).template_files.values() + > + > cobbler_api.find_profile(profile).template_files.values() > + ) > +templates_str = ", ".join(templates) > + > +# Kernel information > +kernel_options_str = "" > +system_kopts = cobbler_api.find_system(name).settings.kernel_options > +kernel_options = cobbler_api.find_profile(profile).settings.kernel_options > + > +# System specific kernel options take precedence over profile level ones > +kernel_options.update(system_kopts) > + > +if kernel_options: > + for opt in kernel_options: > + # You want to know if selinux=0 but don't care as much if lang = ' ' > + if (kernel_options[opt] or opt == "selinux") and kernel_options[opt] > != ' ': > + kernel_options_str += "%s=%s " % (opt, kernel_options[opt]) > + > +# Network information for all interfaces > +network_str = "" > +interfaces = cobbler_api.find_system(name).interfaces.keys() > + > +# People expect eth0 to come before eth1 in a report > +interfaces.sort() > + > +for int in interfaces: > + network_info = cobbler_api.find_system(name).interfaces[int] > + hostname = network_info['dns_name'] > + ip = network_info['ip_address'] > + mac = network_info['mac_address'] > + static = network_info['static'] > + network_str += """ > +%20s:\t%s > +%20s:\t%s > +%20s:\t%s > +%20s:\t%s > +""" % (int + " Hostname", hostname, > + int + " IP Address", ip, > + int + " Mac Address", mac, > + int + " Static", static > + ) > + > +TEXT = """ > +%20s:\t%s > +%20s:\t%s > +%s > +%20s:\t%s > +%20s:\t%s > +""" % ("Name", name, > + "Profile", profile, > + network_str, > + "Kernel Options", kernel_options_str, > + "Templates", templates_str) > + > +# Prepare actual message > +message = """\ > +From: %s > +To: %s > +Subject: %s > + > +%s > +""" % (FROM, ", ".join(TO), SUBJECT, TEXT) > + > +# Send the mail > +server = smtplib.SMTP(SERVER) > +server.sendmail(FROM, TO, message) > +server.quit() >
Can we make this use a Cheetah template? If so, users could customize what they wanted to have in the report. (See templar.py, and various code that references it). --Michael _______________________________________________ cobbler mailing list [email protected] https://fedorahosted.org/mailman/listinfo/cobbler
