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