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