add some more discontinued build trees
[build-farm.git] / admin.pl
1 #!/usr/bin/perl
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 use FindBin qw($RealBin);
21
22 use hostdb;
23 use Mail::Send;
24 use warnings;
25 use strict;
26 use Data::Dumper;
27 use util;
28
29 my $hostname;
30
31 my $db = new hostdb("$RealBin/hostdb.sqlite") or die("Unable to connect to host database: $!");
32 my $dry_run = 0;
33
34 print "Samba Build farm management tool\n";
35 print "================================\n";
36
37 my $op;
38
39 if ($#ARGV > -1) {
40         $op = shift(@ARGV);
41 } else {
42         print "Initialize host database:       init\n";
43         print "Add Machine to build farm:      add\n";
44         print "Remove Machine from build farm: remove\n";
45         print "Modify build farm account:      modify\n";
46         print "Print build farm host info:     info\n";
47         print "Print build farm host list:     list\n";
48         print "Select Operation: [add] ";
49
50         $op = lc(<STDIN>);
51         chomp($op);
52
53         if ($op eq "") {
54                 $op = "add";
55         }
56 }
57
58 if ($op eq "remove") {
59         print "Please enter hostname to delete: ";
60         $hostname = <>;
61         chomp($hostname);
62         $db->deletehost($hostname) or die("Unable to delete host $hostname");
63 } elsif ($op eq "modify") {
64         print "Please enter hostname to modify: ";
65         $hostname = <>;
66         chomp($hostname);
67         my $host = $db->host($hostname);
68         print "Owner: $host->{owner} <$host->{owner_email}>\n";
69         print "Platform: $host->{platform}\n";
70         print "\n";
71         print "Modify owner or platform: [platform] ";
72         my $mod_op = <>;
73         chomp($mod_op);
74         if ($mod_op eq "") {
75                 $mod_op = "platform";
76         }
77         if ($mod_op eq "platform") {
78                 print "Enter new platform: ";
79                 my $platform = <>;
80                 chomp($platform);
81                 $db->update_platform($hostname, $platform) or die "Unable to update platform";
82         } elsif ($mod_op eq "owner") {
83                 print "Enter new owner's name: ";
84                 my $owner = <>;
85                 chomp($owner);
86                 print "Enter new owner's e-mail address: ";
87                 my $owner_email = <>;
88                 chomp($owner_email);
89                 $db->update_owner($hostname, $owner, $owner_email) or die "Unable to update owner";
90                 
91         }       
92 } elsif ($op eq "add") {
93         print "Machine hostname: ";
94         my $hostname = <>;
95         chomp($hostname);
96         print "Machine platform (eg Fedora 9 x86_64): ";
97         my $platform = <>;
98         chomp($platform);
99         print "Machine Owner Name: ";
100         my $owner = <>;
101         chomp($owner);
102         print "Machine Owner E-mail: ";
103         my $owner_email = <>;
104         chomp($owner_email);
105         print "Enter password: [generate random] ";
106         my $password = <>;
107         chomp($password);
108         if ($password eq "") {
109                 $password = `pwgen 16 1`;
110                 chomp($password);
111                 print "Password will be: $password\n";
112         }
113         print "Enter permission e-mail, finish with a .\n";
114         my $permission;
115         while (<>) {
116                 last if $_ eq ".\n";
117                 $permission = $_;
118         }
119         
120         $db->createhost($hostname, $platform, $owner, $owner_email, $password, $permission) or die("Unable to create host $hostname");
121
122         my ($fh, $msg);
123         
124         # send the password in an e-mail to that address
125         my $subject = "Your new build farm host $hostname";
126         if ($dry_run) {
127                 print "To: $owner <$owner_email>\n";
128                 print "Subject: $subject\n";
129                 open(MAIL,"|cat");
130         } else {
131                 $msg = new Mail::Send(Subject=>$subject, To=>"\"$owner\" \<$owner_email\>", Bcc=>"build\@samba.org");
132                 $msg->set("From", "\"Samba Build Farm\" \<build\@samba.org\>");
133                 $fh = $msg->open; 
134         }
135
136         my $body = << "__EOF__";        
137 Welcome to the Samba.org build farm.  
138
139 Your host $hostname has been added to the Samba Build farm.  
140
141 We have recorded that it is running $platform.  
142
143 If you have not already done so, please read:
144 http://build.samba.org/instructions.html
145
146 The password for your rsync .password file is $password
147
148 An e-mail asking you to subscribe to the build-farmers mailing
149 list will arrive shortly.  Please ensure you maintain your 
150 subscription to this list while you have hosts in the build farm.
151
152 Thank you for your contribution to ensuring portability and quality
153 of Samba.org projects.
154
155
156 __EOF__
157         if ($dry_run) {
158                 print MAIL $body;
159
160                 close(MAIL);
161         } else {
162                 print $fh "$body";
163                 $fh->close;
164
165                 $msg = new Mail::Send(Subject=>'Subscribe to build-farmers mailing list', To=>'build-farmers-join@lists.samba.org');
166                 $msg->set("From", "\"$owner\" \<$owner_email\>");
167                 $fh = $msg->open; 
168                 print $fh "Please subscribe $owner to the build-farmers mailing list\n\n";
169                 print $fh "Thanks, your friendly Samba build farm administrator <build\@samba.org>";
170                 $fh->close;
171         }
172         
173
174         
175 } elsif ($op eq "init") {
176         $db->provision();
177         print "Host database initialized successfully.\n";
178 } elsif ($op eq "info") {
179         print "Hostname: ";
180         $hostname = <>;
181         chomp($hostname);
182         my $host = $db->host($hostname);
183         die ("No such host $host") unless ($host);
184         print "Host: $host->{name}";
185         if ($host->{fqdn}) { print " ($host->{fqdn})"; }
186         print "\n";
187         print "Platform: $host->{platform}\n";
188         print "Owner: $host->{owner} <$host->{owner_email}>\n";
189         
190         # Don't run the update of the text files
191         exit(0);
192 } elsif ($op eq "list") {
193     my $hosts = $db->host_ages();
194     foreach (@$hosts) {
195         my $h = $_;
196         my $age = util::dhm_time(time() - $h->{last_update});
197         printf "%-12s $h->{host}\n", $age;
198     }
199 #    print Dumper($hosts);
200 } else {
201         die("Unknown command $op");
202 }
203
204 open(RSYNC_SECRETS, ">$RealBin/../rsyncd.secrets.new") or die("Unable to open rsyncd.secrets file: $!");
205 print RSYNC_SECRETS $db->create_rsync_secrets();
206 close(RSYNC_SECRETS);
207
208 rename("$RealBin/../rsyncd.secrets.new", "../rsyncd.secrets");
209
210 open(HOSTS, ">$RealBin/web/hosts.list.new") or die("Unable to open hosts file: $!");
211 print HOSTS $db->create_hosts_list();
212 close(HOSTS);
213
214 rename("$RealBin/web/hosts.list.new", "$RealBin/web/hosts.list");
215
216 1;
217