Force to have a ldb backend for make test
[build-farm.git] / admin.py
1 #!/usr/bin/python
2 # Samba.org buildfarm
3 # Copyright (C) 2008 Andrew Bartlett <abartlet@samba.org>
4 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 from buildfarm import (
21     hostdb,
22     open_hostdb,
23     )
24 import commands
25 import os
26 import smtplib
27 import sys
28 import time
29 from email.MIMEText import MIMEText
30
31 db = open_hostdb()
32
33 dry_run = False
34
35 print "Samba Build farm management tool"
36 print "================================"
37
38 if len(sys.argv) > 1:
39     op = sys.argv[1]
40 else:
41     print "Add Machine to build farm:      add"
42     print "Remove Machine from build farm: remove"
43     print "Modify build farm account:      modify"
44     print "Print build farm host info:     info"
45     print "Print build farm host list:     list"
46
47     op = raw_input("Select Operation: [add] ").lower()
48
49     if op == "":
50         op = "add"
51
52 if op == "remove":
53     hostname = raw_input("Please enter hostname to delete: ")
54     try:
55         db.deletehost(hostname)
56     except hostdb.NoSuchHost, e:
57         print "No such host '%s'" % e.name
58         sys.exit(1)
59 elif op == "modify":
60     hostname = raw_input("Please enter hostname to modify: ")
61     host = db.host(hostname)
62     print "Owner: %s <%s>" % host.owner
63     print "Platform: %s" % host.platform
64     print ""
65     mod_op = raw_input("Modify owner or platform: [platform] ")
66     if mod_op == "":
67         mod_op = "platform"
68     if mod_op == "platform":
69         platform = raw_input("Enter new platform: ")
70         try:
71             db.update_platform(hostname, platform)
72         except hostdb.NoSuchHost, e:
73             print "No such host: %s" % e.name
74             sys.exit(1)
75     elif mod_op == "owner":
76         owner = raw_input("Enter new owner's name: ")
77         owner_email = raw_input("Enter new owner's e-mail address: ")
78         try:
79             db.update_owner(hostname, owner, owner_email)
80         except hostdb.NoSuchHost, e:
81             print "No such host: %s" % e.name
82             sys.exit(1)
83     else:
84         print "Unknown subcommand %s" % mod_op
85         sys.exit(1)
86 elif op == "add":
87     hostname = raw_input("Machine hostname: ")
88     platform = raw_input("Machine platform (eg Fedora 9 x86_64): ")
89     owner = raw_input("Machine Owner Name: ")
90     owner_email = raw_input("Machine Owner E-mail: ")
91     password = raw_input("Enter password: [generate random] ")
92     if password == "":
93         password = commands.getoutput("pwgen 16 1").strip()
94         print "Password will be: %s" % password
95     permission = []
96     print "Enter permission e-mail, finish with a ."
97     line = raw_input("")
98     while line != ".":
99         permission += line
100         line = raw_input("")
101
102     try:
103         db.createhost(hostname, platform, owner, owner_email, password, "".join(permission))
104     except hostdb.HostAlreadyExists, e:
105         print "A host with the name %s already exists." % e.name
106         sys.exit(1)
107
108     body = """
109 Welcome to the Samba.org build farm.  
110
111 Your host %(hostname)s has been added to the Samba Build farm.  
112
113 We have recorded that it is running %(platform)s.  
114
115 If you have not already done so, please read:
116 http://build.samba.org/instructions.html
117
118 The password for your rsync .password file is %(password)s
119
120 An e-mail asking you to subscribe to the build-farmers mailing
121 list will arrive shortly.  Please ensure you maintain your 
122 subscription to this list while you have hosts in the build farm.
123
124 Thank you for your contribution to ensuring portability and quality
125 of Samba.org projects.
126
127
128 """ % { "hostname": hostname, "platform": platform, "password": password }
129
130     msg_notification = MIMEText(body)
131
132     # send the password in an e-mail to that address
133     msg_notification["Subject"] = "Your new build farm host %s" % hostname
134     msg_notification["To"] = "\"%s\" <%s>" % (owner, owner_email)
135     msg_notification["Bcc"] = "build@samba.org"
136     msg_notification["From"] = "\"Samba Build Farm\" <build@samba.org>"
137
138     msg_subscribe = MIMEText("""Please subscribe %s to the build-farmers mailing list
139
140 Thanks, your friendly Samba build farm administrator <build@samba.org>""" % owner)
141     msg_subscribe["From"] = "\"%s\" <%s>" % (owner, owner_email)
142     msg_subscribe["Subject"] = 'Subscribe to build-farmers mailing list'
143     msg_subscribe["To"] = 'build-farmers-join@lists.samba.org'
144
145     if dry_run:
146         print msg_notification
147     else:
148         s = smtplib.SMTP()
149         s.connect()
150         for msg in (msg_notification, msg_subscribe):
151             recipients = [msg["To"]]
152             if msg["Bcc"]:
153                 recipients.append(msg["Bcc"])
154             s.sendmail(msg["From"], recipients, msg.as_string())
155         s.quit()
156
157 elif op == "info":
158     hostname = raw_input("Hostname: ")
159     host = db.host(hostname)
160     if host.fqdn:
161         opt_fqdn = " (%s)" % host.fqdn
162     else:
163         opt_fqdn = ""
164     print "Host: %s%s" % (host.name, opt_fqdn)
165     print "Platform: %s" % host.platform
166     print "Owner: %s <%s>" % host.owner
167
168     # Don't run the update of the text files
169     sys.exit(0)
170 elif op == "list":
171     for host in db.host_ages():
172         if host.last_update:
173             age = time.time() - host.last_update
174         else:
175             age = ""
176         print "%-12s %s" % (age, host.name)
177 else:
178     print "Unknown command %s" % op
179     sys.exit(1)
180
181 temp_rsyncd_secrets = os.path.join(os.path.dirname(__file__), "../rsyncd.secrets.new")
182 f = open(temp_rsyncd_secrets, "w")
183 f.writelines(db.create_rsync_secrets())
184 f.close()
185
186 os.rename(temp_rsyncd_secrets, "../rsyncd.secrets")
187
188 temp_hosts_list_file = os.path.join(os.path.dirname(__file__), "web", "hosts.list.new")
189 f = open(temp_hosts_list_file, "w")
190 f.writelines(db.create_hosts_list())
191 f.close()
192
193 os.rename(temp_hosts_list_file, os.path.join(os.path.dirname(__file__), "web/hosts.list"))