fix broken parameter
[build-farm.git] / mail-dead-hosts.py
index 725baa3c68e9b9ec6e3afdc4f13b1a8e5fc69af2..22f7bcac321100076b6ebc959228ced543dff8a5 100755 (executable)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-from buildfarm import (
-    hostdb,
-    open_hostdb,
-    )
+from buildfarm.sqldb import StormCachingBuildFarm
+from buildfarm.web import host_uri
+import optparse
 import smtplib
 from email.MIMEText import MIMEText
 import time
 
-db = open_hostdb()
-dry_run = False
+parser = optparse.OptionParser()
+parser.add_option("--dry-run", help="Don't actually send any emails.", action="store_true")
+(opts, args) = parser.parse_args()
 
-hosts = db.dead_hosts(7 * 86400)
-for host in hosts:
-    db.sent_dead_mail(host.name)
+buildfarm = StormCachingBuildFarm(timeout=40.0)
+
+smtp = smtplib.SMTP()
+smtp.connect()
 
+hosts = buildfarm.hostdb.dead_hosts(7 * 86400)
+for host in hosts:
     if host.last_update:
-        last_update = time.strftime ("%a %b %e %H:%M:%S %Y", time.gmtime(host.last_update))
+        last_update = time.strftime("%a %b %e %H:%M:%S %Y", time.gmtime(host.last_update))
     else:
         last_update = "a long time"
 
     body = """
 Your host %s has been part of the Samba Build farm, hosted
-at http://build.samba.org.
+at http://build.samba.org/.
 
 Sadly however we have not heard from it since %s.
 
@@ -51,13 +54,12 @@ If you no longer wish your host to participate in the Samba Build
 Farm, then please let us know so we can remove its records.
 
 You can see the summary for your host at:
-http://build.samba.org/?function=View+Host;host=%s
 
 Thanks,
 
 The Build Farm administration team.
 
-""" % (host.name, last_update, host.name)
+""" % (host.name, last_update, host_uri("http://build.samba.org/build.cgi", host.name))
 
     msg = MIMEText(body)
 
@@ -65,11 +67,12 @@ The Build Farm administration team.
     msg["Subject"] ="Your build farm host %s appears dead" % host.name
     msg["From"] = "\"Samba Build Farm\" <build@samba.org>"
     msg["To"] = "\"%s\" <%s>" % host.owner
+    msg["Bcc"] = "\"Samba Build Farm\" <build@samba.org>"
 
-    if dry_run:
+    if opts.dry_run:
         print msg.as_string()
     else:
-        s = smtplib.Connection()
-        s.connect()
-        s.send(msg["From"], [msg["To"]], msg.as_string())
-        s.quit()
+        smtp.sendmail(msg["From"], [msg["To"], msg["Bcc"]], msg.as_string())
+        host.dead_mail_sent()
+buildfarm.commit()
+smtp.quit()