get_lcov_cached_status has been removed build the cachefile name for lcov in the...
[build-farm.git] / hostdb.pm
index c7a2ccd8477e243190013c3e54999d873dea75b8..372483db942dcb62b72829eef1c98436f37f9399 100644 (file)
--- a/hostdb.pm
+++ b/hostdb.pm
@@ -28,7 +28,8 @@ sub new($)
  {
     my ($class, $filename) = @_;
     
-    my $dbh = DBI->connect("dbi:SQLite:$filename") or return undef;
+    my $dbh = DBI->connect("dbi:SQLite:$filename", "", "", {RaiseError => 1, PrintError => 0,
+                        ShowErrorStatement => 1, AutoCommit => 0}) or return undef;
     
     my $self = { filename => $filename, dbh => $dbh };
     
@@ -38,27 +39,60 @@ sub new($)
 sub provision($)
 {
        my ($self) = @_;
-       
-       $self->{dbh}->do("CREATE TABLE host ( name text, owner text, owner_email text, password text, ssh_access int, fqdn text, platform text, permission text );");
-       
-       $self->{dbh}->do("CREATE UNIQUE INDEX unique_hostname ON host (name);");
+       eval {
+           $self->{dbh}->do("CREATE TABLE host ( name text, owner text, owner_email text, password text, ssh_access int, fqdn text, platform text, permission text, last_dead_mail int, join_time int );");
+           
+           $self->{dbh}->do("CREATE UNIQUE INDEX unique_hostname ON host (name);");
+           
+           $self->{dbh}->do("CREATE TABLE build ( id integer primary key autoincrement, tree text, revision text, host text, compiler text, checksum text, age int, status text, commit_revision text);");
+           $self->{dbh}->do("CREATE UNIQUE INDEX unique_checksum ON build (checksum);");
+           
+           $self->{dbh}->do("CREATE TABLE test_run ( build int, test text, result text, output text);");
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
+       return 1;
 }
 
 sub createhost($$$$$$)
 {
        my ($self, $name, $platform, $owner, $owner_email, $password, $permission) = @_;
-       my $sth = $self->{dbh}->prepare("INSERT INTO host (name, platform, owner, owner_email, password, permission) VALUES (?,?,?,?,?,?)");
-       
-       $sth->execute($name, $platform, $owner, $owner_email, $password, $permission);
+       my $sth = $self->{dbh}->prepare("INSERT INTO host (name, platform, owner, owner_email, password, permission, join_time) VALUES (?,?,?,?,?,?,?)");
+       
+       eval {
+           $sth->execute($name, $platform, $owner, $owner_email, $password, $permission, time());
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
+       return 1;
 }
 
 sub deletehost($$)
 {
        my ($self, $name) = @_;
-       
+       my $ret;
        my $sth = $self->{dbh}->prepare("DELETE FROM host WHERE name = ?");
        
-       my $ret = $sth->execute($name);
+       eval {
+           $ret = $sth->execute($name);
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
        
        return ($ret == 1);
 }
@@ -67,7 +101,39 @@ sub hosts($)
 {
        my ($self) = @_;
        
-       return $self->{dbh}->selectall_arrayref("SELECT * FROM host", { Slice => {} });
+       return $self->{dbh}->selectall_arrayref("SELECT * FROM host ORDER BY name", { Slice => {} });
+}
+
+sub dead_hosts($$)
+{
+        my ($self, $age) = @_;
+       my $dead_age = time() - $age;
+       return $self->{dbh}->selectall_arrayref("SELECT host.name AS host, host.owner AS owner, host.owner_email AS owner_email, MAX(age) AS last_update FROM host LEFT JOIN build ON ( host.name == build.host) WHERE ifnull(last_dead_mail, 0) < $dead_age AND ifnull(join_time, 0) < $dead_age GROUP BY host.name having ifnull(MAX(age),0) < $dead_age", { Slice => {} });
+}
+
+sub host_ages($)
+{
+        my ($self) = @_;
+       return $self->{dbh}->selectall_arrayref("SELECT host.name AS host, host.owner AS owner, host.owner_email AS owner_email, MAX(age) AS last_update FROM host LEFT JOIN build ON ( host.name == build.host) GROUP BY host.name ORDER BY age", { Slice => {} });
+}
+
+sub sent_dead_mail($$) 
+{
+        my ($self, $host) = @_;
+       my $changed;
+       eval {
+           $changed = $self->{dbh}->do("UPDATE host SET last_dead_mail = ? WHERE name = ?", undef, 
+               (time(), $host));
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
+       
+       return ($changed == 1);
 }
 
 sub host($$)
@@ -86,9 +152,19 @@ sub host($$)
 sub update_platform($$$)
 {
        my ($self, $name, $new_platform) = @_;
-       
-       my $changed = $self->{dbh}->do("UPDATE host SET platform = ? WHERE name = ?", undef, 
+       my $changed;
+
+       eval {
+           $changed = $self->{dbh}->do("UPDATE host SET platform = ? WHERE name = ?", undef, 
                ($new_platform, $name));
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
        
        return ($changed == 1);
 }
@@ -96,9 +172,18 @@ sub update_platform($$$)
 sub update_owner($$$$)
 {
        my ($self, $name, $new_owner, $new_owner_email) = @_;
-       
-       my $changed = $self->{dbh}->do("UPDATE host SET owner = ?, owner_email = ? WHERE name = ?", 
+       my $changed;
+
+       eval {
+           $changed = $self->{dbh}->do("UPDATE host SET owner = ?, owner_email = ? WHERE name = ?", 
                                       undef, ($new_owner, $new_owner_email, $name));
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           return 0;
+       }
        
        return ($changed == 1);
 }
@@ -116,8 +201,17 @@ sub create_rsync_secrets($)
        $res .= "# automatically generated by textfiles.pl. DO NOT EDIT!\n\n";
        
        foreach (@$hosts) {
-               $res .= "# $_->{name}, owner: $_->{owner} <$_->{owner_email}>\n";
-               $res .= "$_->{name}:$_->{password}\n\n";
+               $res .= "# $_->{name}";
+               if ($_->{owner}) {
+                       $res .= ", owner: $_->{owner} <$_->{owner_email}>\n";
+               } else {
+                       $res .= ", owner unknown\n";
+               }
+               if ($_->{password}) {
+                       $res .= "$_->{name}:$_->{password}\n\n";
+               } else {
+                       $res .= "# $->{name} password is unknown\n\n";
+               }
        }
        
        return $res;
@@ -139,4 +233,4 @@ sub create_hosts_list($)
        return $res;
 }
 
-1;
\ No newline at end of file
+1;