Add functionality for modifying hosts. Pair programmed with Andrew.
authorjelmer <jelmer@1e5ffdc8-eadd-0310-9daa-9cb4117fe24b>
Mon, 21 Apr 2008 10:39:48 +0000 (10:39 +0000)
committerjelmer <jelmer@1e5ffdc8-eadd-0310-9daa-9cb4117fe24b>
Mon, 21 Apr 2008 10:39:48 +0000 (10:39 +0000)
git-svn-id: file:///home/svn/build-farm/trunk@779 1e5ffdc8-eadd-0310-9daa-9cb4117fe24b

admin.pl
hostdb.pm
tests/hostdb.pl

index fc46686e58a833c9ceb5356e6f708c76a6b23d42..f2e842142b3047b5586c741cdc4c2cf1f3481b0d 100644 (file)
--- a/admin.pl
+++ b/admin.pl
@@ -17,7 +17,6 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-
 use FindBind($RealBin);
 
 use hostdb;
@@ -29,15 +28,20 @@ print "===============================\n";
 
 print "Add Machine to build farm:      add\n";
 print "Remove Machine from build farm: remove\n";
-print "Modify build farm account:       modify\n";
+print "Modify build farm account:      modify\n";
 print "Select Operation: [add]";
 my lc($op) = chomp(<>);
 
-if ($op == "") {
+if ($op eq "") {
        $op = "add";
 }
 
-if ($op == "add") {
+if ($op eq "remove") {
+       print "Please enter hostname to delete: \n";
+       my $hostname = chomp(<>);
+       $ok = $db->deletehost($hostname);
+       assert($ok);
+} elif ($op eq "add") {
        print "Machine hostname: ";
        my $hostname = chomp(<>);
        print "Machine platform (eg Fedora 9 x86_64): "
@@ -52,14 +56,14 @@ if ($op == "add") {
        }
        print "Enter password (press enter for random)";
        my $password = chomp(<>);
-       if ($password == "") {
+       if ($password eq "") {
                $password = chomp(`pwgen 16 1`);
                print "Password will be: $password\n"
        }
        print "Enter permission e-mail, finish with a ."
        my $permission;
        while (<>) {
-               last if $_ = ".\n";
+               last if $_ eq ".\n";
                $permission = $_;
        }
        
@@ -71,7 +75,7 @@ if ($op == "add") {
                print "Subject: $subject\n";
                open(MAIL,"|cat");
        } else {
-               open(MAIL,"|Mail -s \"Your new build farm host $hostname\" \"$owner\" \<$owner_email\>");
+               open(MAIL,"|Mail -s \"Your new build farm host $hostname\" \"$owner <$owner_email\>");
        }
 
        my $body = << "__EOF__";
@@ -91,8 +95,9 @@ and quality of Samba.org projects.
 
 
 __EOF__
-print MAIL $body;
+       print MAIL $body;
 
-close(MAIL);
+       close(MAIL);
+} else {
+       die("Unknown command $op");
 }
-
index f77dc777d8c33ede884b59a132b55afd756714d4..36eeaf0ecb7966a412ef7703b21485db0774e553 100644 (file)
--- a/hostdb.pm
+++ b/hostdb.pm
@@ -64,6 +64,43 @@ sub hosts($)
        return $self->{dbh}->selectall_arrayref("SELECT * FROM host", { Slice => {} });
 }
 
+sub host($$)
+{
+       my ($self, $name) = @_;
+       
+       my $hosts = $self->hosts();
+       
+       foreach (@$hosts) {
+               return $_ if ($_->{name} eq $name);
+       }
+       
+       return undef;
+}
+
+sub update_platform($$$)
+{
+       my ($self, $name, $new_platform) = @_;
+       
+       my $changed = $self->{dbh}->do("UPDATE host SET platform = ? WHERE name = ?", undef, 
+               ($new_platform, $name));
+
+       die("Inconsistent database: More than one entry with name $name") if ($changed > 1);
+       
+       return ($changed == 1);
+}
+
+sub update_owner($$$$)
+{
+       my ($self, $name, $new_owner, $new_owner_email) = @_;
+       
+       my $changed = $self->{dbh}->do("UPDATE host SET owner = ?, owner_email = ? WHERE name = ?", 
+                                      undef, ($new_owner, $new_owner_email, $name));
+                                      
+       die("Inconsistent database: More than one entry with name $name") if ($changed > 1);
+       
+       return ($changed == 1);
+}
+
 # Write out the rsyncd.secrets
 sub create_rsync_secrets($)
 {
@@ -78,7 +115,7 @@ sub create_rsync_secrets($)
        
        foreach (@$hosts) {
                $res .= "# $_->{name}, owner: $_->{owner} <$_->{owner_email}>\n";
-               $res .= "$_->{name} $_->{password}\n\n";
+               $res .= "$_->{name}:$_->{password}\n\n";
        }
        
        return $res;
index 51da216a604cf253d3435e350bcc7d11a2d3915a..db048837deb10c80a529282f352c0a3b9fb07cf4 100755 (executable)
@@ -23,7 +23,7 @@ use FindBin qw($RealBin);
 use lib "$RealBin/..";
 use lib "$RealBin/../web";
 
-use Test::More tests => 9;
+use Test::More tests => 15;
 use strict;
 use hostdb;
 
@@ -36,16 +36,35 @@ ok($db->createhost("gwalcmai", "vax", "jelmer", "jelmer\@example.com", "geheim",
 
 is_deeply([["gwalcmai"]], $db->{dbh}->selectall_arrayref("SELECT name FROM host"));
 
-is_deeply([{ name => "gwalcmai", owner => "jelmer", owner_email => "jelmer\@example.com", 
+my $expected_host = { name => "gwalcmai", owner => "jelmer", owner_email => "jelmer\@example.com", 
              platform => "vax", permission => 'Yo! Please put me on the buildfarm', fqdn => undef,
-             password => "geheim", ssh_access => undef }], $db->hosts());
+             password => "geheim", ssh_access => undef };
+
+is_deeply([$expected_host], $db->hosts());
+
+is_deeply($expected_host, $db->host("gwalcmai"));
+
+is(undef, $db->host("foo"));
 
 is("gwalcmai: vax\n", $db->create_hosts_list());
 
 is("# rsyncd.secrets file\n" . 
    "# automatically generated by textfiles.pl. DO NOT EDIT!\n\n" .
-   "# gwalcmai, jelmer <jelmer\@example.com>\n" . 
-   "gwalcmai: geheim\n\n", $db->create_rsync_secrets());
+   "# gwalcmai, owner: jelmer <jelmer\@example.com>\n" . 
+   "gwalcmai:geheim\n\n", $db->create_rsync_secrets());
+
+ok($db->update_platform("gwalcmai", "s390")); 
+
+$expected_host->{platform} = "s390";
+
+is_deeply($expected_host, $db->host("gwalcmai"));
+
+ok($db->update_owner("gwalcmai", "Andrew", "abartlet\@example.com"));
+
+$expected_host->{owner} = "Andrew";
+$expected_host->{owner_email} = "abartlet\@example.com";
+
+is_deeply($expected_host, $db->host("gwalcmai"));
 
 ok($db->deletehost("gwalcmai"));
 is_deeply([], $db->{dbh}->selectall_arrayref("SELECT name FROM host"));