two more hosts missing ACL support
[build-farm.git] / admin.py
index 79c6ce70d87cb98fe9e938c503dbe4c11a7805ff..b78fb32822121f2a7f1ece486ba966009554db23 100755 (executable)
--- a/admin.py
+++ b/admin.py
@@ -18,8 +18,8 @@
 #
 
 from buildfarm import (
-    hostdb,
     BuildFarm,
+    hostdb,
     )
 import commands
 import os
@@ -30,32 +30,25 @@ from email.MIMEText import MIMEText
 
 buildfarm = BuildFarm()
 
-db = buildfarm.hostdb
-
 def update_rsyncd_secrets():
     temp_rsyncd_secrets = os.path.join(os.path.dirname(__file__), "../rsyncd.secrets.new")
     f = open(temp_rsyncd_secrets, "w")
-    f.writelines(db.create_rsync_secrets())
+    f.writelines(buildfarm.hostdb.create_rsync_secrets())
     f.close()
 
     os.rename(temp_rsyncd_secrets, "../rsyncd.secrets")
 
-def update_hosts_list():
-    temp_hosts_list_file = os.path.join(os.path.dirname(__file__), "web", "hosts.list.new")
-    f = open(temp_hosts_list_file, "w")
-    f.writelines(db.create_hosts_list())
-    f.close()
-
-    os.rename(temp_hosts_list_file, os.path.join(os.path.dirname(__file__), "web/hosts.list"))
-
 dry_run = False
 
 print "Samba Build farm management tool"
 print "================================"
 
-if len(sys.argv) > 1:
-    op = sys.argv[1]
-else:
+args = sys.argv[1:]
+
+try:
+    op = args.pop(0)
+except IndexError:
+    print "Initialize the buildfarm:       init"
     print "Add Machine to build farm:      add"
     print "Remove Machine from build farm: remove"
     print "Modify build farm account:      modify"
@@ -67,21 +60,24 @@ else:
     if op == "":
         op = "add"
 
-if op == "remove":
-    hostname = raw_input("Please enter hostname to delete: ")
-    try:
-        db.deletehost(hostname.decode("utf-8"))
-    except hostdb.NoSuchHost, e:
-        print "No such host '%s'" % e.name
-        sys.exit(1)
-    else:
-        db.commit()
-        update_rsyncd_secrets()
-        update_hosts_list()
+if op == "init":
+    buildfarm.commit()
+elif op == "remove":
+    if not args:
+        args = [raw_input("Please enter hostname to delete: ")]
+    for hostname in args:
+        try:
+            buildfarm.hostdb.deletehost(hostname)
+        except hostdb.NoSuchHost, e:
+            print "No such host '%s'" % e.name
+            sys.exit(1)
+        else:
+            buildfarm.hostdb.commit()
+            update_rsyncd_secrets()
 elif op == "modify":
     hostname = raw_input("Please enter hostname to modify: ")
     try:
-        host = db.host(hostname.decode("utf-8"))
+        host = buildfarm.hostdb[hostname]
     except hostdb.NoSuchHost, e:
         print "No such host '%s'" % e.name
         sys.exit(1)
@@ -93,22 +89,21 @@ elif op == "modify":
         mod_op = "platform"
     if mod_op == "platform":
         platform = raw_input("Enter new platform: ")
-        host.update_platform(platform.decode("utf-8"))
-        db.commit()
+        host.update_platform(platform.decode('utf-8'))
+        buildfarm.commit()
     elif mod_op == "owner":
         owner = raw_input("Enter new owner's name: ")
         owner_email = raw_input("Enter new owner's e-mail address: ")
-        host.update_owner(owner.decode("utf-8"), owner_email.decode("utf-8"))
-        db.commit()
+        host.update_owner(owner, owner_email.decode("utf-8"))
+        buildfarm.commit()
     else:
         print "Unknown subcommand %s" % mod_op
         sys.exit(1)
     update_rsyncd_secrets()
-    update_hosts_list()
 elif op == "add":
     hostname = raw_input("Machine hostname: ")
     try:
-        db.host(hostname.decode("utf-8"))
+        buildfarm.hostdb[hostname]
     except hostdb.NoSuchHost, e:
         pass
     else:
@@ -129,7 +124,7 @@ elif op == "add":
         line = raw_input("")
 
     try:
-        db.createhost(hostname.decode("utf-8"), platform.decode("utf-8"),
+        buildfarm.hostdb.createhost(hostname, platform.decode("utf-8"),
             owner.decode("utf-8"), owner_email.decode("utf-8"),
             password.decode("utf-8"),
             "".join(permission).decode("utf-8", "replace"))
@@ -137,7 +132,7 @@ elif op == "add":
         print "A host with the name %s already exists." % e.name
         sys.exit(1)
     else:
-        db.commit()
+        buildfarm.commit()
 
     body = """
 Welcome to the Samba.org build farm.  
@@ -188,23 +183,24 @@ Thanks, your friendly Samba build farm administrator <build@samba.org>""" % owne
             s.sendmail(msg["From"], recipients, msg.as_string())
         s.quit()
         update_rsyncd_secrets()
-        update_hosts_list()
 elif op == "info":
-    hostname = raw_input("Hostname: ")
-    try:
-        host = db.host(hostname.decode("utf-8"))
-    except hostdb.NoSuchHost, e:
-        print "No such host '%s'" % e.name
-        sys.exit(1)
-    if host.fqdn:
-        opt_fqdn = " (%s)" % host.fqdn
-    else:
-        opt_fqdn = ""
-    print "Host: %s%s" % (host.name, opt_fqdn)
-    print "Platform: %s" % host.platform
-    print "Owner: %s <%s>" % host.owner
+    if not args:
+        args = [raw_input("Hostname: ")]
+    for hostname in args:
+        try:
+            host = buildfarm.hostdb[hostname]
+        except hostdb.NoSuchHost, e:
+            print "No such host '%s'" % e.name
+            sys.exit(1)
+        if host.fqdn:
+            opt_fqdn = " (%s)" % host.fqdn
+        else:
+            opt_fqdn = ""
+        print "Host: %s%s" % (host.name, opt_fqdn)
+        print "Platform: %s" % host.platform
+        print "Owner: %s <%s>" % host.owner
 elif op == "list":
-    for host in db.host_ages():
+    for host in buildfarm.hostdb.host_ages():
         if host.last_update:
             age = time.time() - host.last_update
         else: