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