Code

add db packages_list
authorrettenbe <rettenbe@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 28 Feb 2008 13:43:46 +0000 (13:43 +0000)
committerrettenbe <rettenbe@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 28 Feb 2008 13:43:46 +0000 (13:43 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@9188 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-si/gosa-si-server
gosa-si/modules/DBsqlite.pm
gosa-si/modules/GosaPackages.pm
gosa-si/tests/client.php

index 90579fd082d47459e5da18b09a5b17084e93d0ef..6ba6160af2e87cbb218a7fba6611fe06c5b699d2 100755 (executable)
@@ -41,6 +41,7 @@ use Sys::Syslog qw( :DEFAULT setlogsock);
 use Cwd;
 use File::Spec;
 use File::Basename;
+use File::Path;
 use GOSA::DBsqlite;
 use GOSA::GosaSupportDaemon;
 use POE qw(Component::Server::TCP);
@@ -59,6 +60,7 @@ my ($pid_file, $procid, $pid, $log_file);
 my ($arp_activ, $arp_fifo);
 my ($xml);
 my ($ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
+my $sources_list;
 
 # variables declared in config file are always set to 'our'
 our (%cfg_defaults, $log_file, $pid_file, 
@@ -125,6 +127,14 @@ our $fai_server_tn = "fai_server";
 my $fai_server_file_name;
 our @fai_server_col_names = ('timestamp', 'server', 'release', 'tag'); 
 
+# holds all packages available from different repositories
+our $packages_list_db;
+our $packages_list_tn = "packages_list";
+my $packages_list_file_name;
+our @packages_list_col_names = ('distribution', 'package', 'version', 'section', 'description', 'template', 'timestamp');
+my $outdir = "/tmp/packages_list_db";
+my $arch = "i386"; 
+
 %cfg_defaults = (
 "general" => {
     "log-file" => [\$log_file, "/var/run/".$prg.".log"],
@@ -134,12 +144,13 @@ our @fai_server_col_names = ('timestamp', 'server', 'release', 'tag');
     "activ" => [\$bus_activ, "true"],
     },
 "server" => {
-#    "ip" => [\$server_ip, "0.0.0.0"],  
     "port" => [\$server_port, "20081"],
     "known-clients" => [\$known_clients_file_name, '/var/lib/gosa-si/clients.db' ],
     "known-servers" => [\$known_server_file_name, '/var/lib/gosa-si/servers.db'],
     "login-users" => [\$login_users_file_name, '/var/lib/gosa-si/users.db'],
     "fai-server" => [\$fai_server_file_name, '/var/lib/gosa-si/fai.db'],
+    "packages-list" => [\$packages_list_file_name, '/var/lib/gosa-si/packages.db'],
+    "source-list" => [\$sources_list, '/etc/apt/sources.list'],
     "ldap-uri" => [\$ldap_uri, ""],
     "ldap-base" => [\$ldap_base, ""],
     "ldap-admin-dn" => [\$ldap_admin_dn, ""],
@@ -882,6 +893,8 @@ sub send_msg_to_target {
 sub _start {
     my ($kernel) = $_[KERNEL];
     &trigger_db_loop($kernel);
+    $kernel->yield('create_fai_server_db', $fai_server_tn );
+    $kernel->yield('create_packages_list_db', $sources_list );
 }
 
 
@@ -1155,7 +1168,7 @@ sub refresh_ldap_handle {
 
 
 sub create_fai_server_db {
-    my ($table_name) = @_;
+    my ($table_name) = @_[ARG0];
     
 #####################################################################
 #
@@ -1166,14 +1179,153 @@ sub create_fai_server_db {
     $fai_server_db->add_dbentry( { 
             table => $table_name,
             primkey => [],
-            server => "dummyserver",
-            release => "kleinkind",
-            tag => "imwachstum",
+            server => "dummyserver",        # neuer fai-server 
+            release => "kleinkind",         # neuer release
+            tag => "imwachstum",            # neues tag
             } );
     return;
 }
 
 
+sub create_packages_list_db {
+    my ($sources_file) = @_[ARG0] ;
+    my $line;
+
+    open(CONFIG, "<$sources_file") or do {
+        daemon_log( "ERROR: Failed to open '$sources_file', creating packages.db stopped", 1);
+        return;
+    };
+    
+    # Read lines
+    while ($line = <CONFIG>){
+        # Unify
+        chop($line);
+        $line =~ s/^\s+//;
+        $line =~ s/^\s+/ /;
+
+        # Strip comments
+        $line =~ s/#.*$//g;
+
+        # Skip empty lines
+        if ($line =~ /^\s*$/){
+            next;
+        }
+
+        # Interpret deb line
+        if ($line =~ /^deb [^\s]+\s[^\s]+\s[^\s]+/){
+            my( $baseurl, $dist, $sections ) = ($line =~ /^deb\s([^\s]+)\s+([^\s]+)\s+(.*)$/);
+            my $section;
+            foreach $section (split(' ', $sections)){
+                &parse_package_info( $baseurl, $dist, $section );
+            }
+        }
+    }
+
+    close (CONFIG);
+
+
+    return;
+}
+sub parse_package_info {
+  my ($baseurl, $dist, $section)= @_;
+  my ($package);
+
+  my ($path) = ($baseurl =~ m%://[^/]*(.*)$%);
+
+  foreach $package ("Packages.gz"){
+    print "getting $baseurl , $dist , $section... \n";
+    get_package( "$baseurl/dists/$dist/$section/binary-$arch/$package", "$outdir/$dist/$section" );
+    print "done\n";
+    print "parsing packages... \n"; 
+    parse_package( "$outdir/$dist/$section", $dist, $path );
+    print "done\n";
+    last;
+  }
+}
+sub get_package {
+  my ($url, $dest)= @_;
+
+  my $tpath = dirname($dest);
+  -d "$tpath" || mkpath "$tpath";
+
+  # This is ugly, but I've no time to take a look at "how it works in perl"
+  if( system("wget '$url' -O '$dest' 2>/dev/null") ) {
+    system("gzip -cd '$dest' > '$dest.in'");
+    system("rm -f '$dest'");
+  }
+
+  return 0;
+}
+sub parse_package {
+    my ($path, $dist, $srv_path )= @_;
+    my ($package, $version, $section, $description);
+    my @sql_list;
+
+    open(PACKAGES, "<$path.in") or return;
+
+    # Read lines
+    while (my $line = <PACKAGES>){
+        # Unify
+        chop($line);
+
+        # Use empty lines as a trigger
+        if ($line =~ /^\s*$/){
+            my $sql = "INSERT INTO packages_list VALUES ('$dist', '$package', '$version', '$section', 'xxx', 'none', '0')";
+            push(@sql_list, $sql);
+            $package = "none";
+            $version = "none";
+            $section = "none";
+            $description = "none"; 
+            next;
+        }
+
+        # Trigger for package name
+        if ($line =~ /^Package:\s/){
+            ($package)= ($line =~ /^Package: (.*)$/);
+            next;
+        }
+
+        # Trigger for version
+        if ($line =~ /^Version:\s/){
+            ($version)= ($line =~ /^Version: (.*)$/);
+            next;
+        }
+
+        # Trigger for description
+        if ($line =~ /^Description:\s/){
+            ($description)= ($line =~ /^Description: (.*)$/);
+            next;
+        }
+
+        # Trigger for section
+        if ($line =~ /^Section:\s/){
+            ($section)= ($line =~ /^Section: (.*)$/);
+            next;
+        }
+
+    }
+
+    close( PACKAGES );
+    unlink( "$path.in" );
+    
+    $packages_list_db->exec_statementlist(\@sql_list);
+}
+
+#
+#sub store_fileinfo {
+#  my( $package, $file, $dist, $path, $vers ) = @_;
+#
+#  my %fileinfo = (
+#    'package' => $package,
+#    'dist' => $dist,
+#    'version' => $vers
+#  );
+#
+#  $repo_files{ "${srvdir}${path}/$file" } = \%fileinfo;
+#}
+
+
+
 #==== MAIN = main ==============================================================
 #  parse commandline options
 Getopt::Long::Configure( "bundling" );
@@ -1246,11 +1398,14 @@ $known_server_db->create_table($known_server_tn, \@known_server_col_names);
 $login_users_db = GOSA::DBsqlite->new($login_users_file_name);
 $login_users_db->create_table($login_users_tn, \@login_users_col_names);
 
-
 # connect to fai_server_db
 $fai_server_db = GOSA::DBsqlite->new($fai_server_file_name);
 $fai_server_db->create_table($fai_server_tn, \@fai_server_col_names);
-&create_fai_server_db($fai_server_tn);
+
+# connect to packages_list_db
+unlink($packages_list_file_name);
+$packages_list_db = GOSA::DBsqlite->new($packages_list_file_name);
+$packages_list_db->create_table($packages_list_tn, \@packages_list_col_names);
 
 # create xml object used for en/decrypting
 $xml = new XML::Simple();
@@ -1267,6 +1422,8 @@ POE::Session->create(
        inline_states => {
                _start => \&_start,
                watch_for_new_jobs => \&watch_for_new_jobs,
+        create_packages_list_db => \&create_packages_list_db,
+        create_fai_server_db => \&create_fai_server_db,
        }
 );
 
index 8bd357fb69b2acf34ee0491855685907b601228e..b2b628fd56f9183c6217aa6f3e420cf0213f7f78 100644 (file)
@@ -52,7 +52,7 @@ sub create_lock : locked {
     my $lock = $self->{db_lock};
     while( -f $lock ) {
                #&main::daemon_log("(".((defined $funcname)?$funcname:"").") Lock (PID ".$$.") $lock found",8);
-        sleep 1;
+        usleep 100;
     }
 
     open($self->{db_lock_handle},'>',$self->{db_lock});
@@ -268,6 +268,20 @@ sub exec_statement {
 }
 
 
+sub exec_statementlist {
+    my $self = shift;
+    my $sql_list = shift;
+    my @db_answer;
+
+    &create_lock($self,'exec_statement');
+    foreach my $sql (@$sql_list) {
+        @db_answer = @{$self->{dbh}->selectall_arrayref($sql)};
+    }
+    &remove_lock($self, 'exec_statement');
+
+    return \@db_answer;
+}
+
 sub count_dbentries {
     my ($self, $table)= @_;
     my $error= 0;
index ec8f6d0eb5bf285a0cb25836abf4a7a22d6d57ef..a230a309e4205a1dceae887f78898d28f99d268a 100644 (file)
@@ -308,26 +308,11 @@ sub process_gosa_msg {
     my $header = @{$msg_hash->{'header'}}[0];
     $header =~ s/gosa_//;
 
-    # decide wether msg is a core function or a event handler
-    if ( $header eq 'query_jobdb') {
-        @out_msg_l = &query_jobdb
-    } elsif ($header eq 'delete_jobdb_entry') {
-        @out_msg_l = &delete_jobdb_entry
-    } elsif ($header eq 'clear_jobdb') {
-        @out_msg_l = &clear_jobdb
-    } elsif ($header eq 'update_status_jobdb_entry' ) {
-        @out_msg_l = &update_status_jobdb_entry
-    } elsif ($header eq 'count_jobdb' ) {
-        @out_msg_l = &count_jobdb
-    } else {
-        # msg could not be assigned to core function
-        # maybe it is an eventa
-        if( exists $event_hash->{$header} ) {
-            # a event exists with the header as name
-            &main::daemon_log("found event '$header' at event-module '".$event_hash->{$header}."'", 5);
-            no strict 'refs';
-            @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
-         }
+    if( exists $event_hash->{$header} ) {
+        # a event exists with the header as name
+        &main::daemon_log("found event '$header' at event-module '".$event_hash->{$header}."'", 5);
+        no strict 'refs';
+        @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
     }
 
     # if delivery not possible raise error and return 
@@ -400,142 +385,6 @@ sub process_job_msg {
     return @out_msg_l;
 }
 
-
-
-## CORE FUNCTIONS ############################################################
-
-sub query_jobdb {
-    my ($msg) = @_;
-    my $msg_hash = &transform_msg2hash($msg);
-    my $target = @{$msg_hash->{'target'}}[0];
-    my $source = @{$msg_hash->{'source'}}[0];
-
-    # prepare query sql statement
-    my $select= &get_select_statement($msg, $msg_hash);
-    my $table= $main::job_queue_tn;
-    my $where= &get_where_statement($msg, $msg_hash);
-    my $limit= &get_limit_statement($msg, $msg_hash);
-    my $orderby= &get_orderby_statement($msg, $msg_hash);
-    my $sql_statement= "SELECT $select FROM $table $where $orderby $limit";
-
-    # execute db query   
-    my $res_hash = $main::job_db->select_dbentry($sql_statement);
-    my $out_xml = &db_res2si_msg($res_hash, "query_jobdb", $target, $source);
-
-    my @out_msg_l = ( $out_xml );
-    return @out_msg_l;
-}
-
-
-sub count_jobdb {
-    my ($msg)= @_;
-    my $out_xml= "<xml><count>error</count></xml>";
-
-    # prepare query sql statement
-    my $table= $main::job_queue_tn;
-    my $sql_statement= "SELECT * FROM $table ";
-    
-    # execute db query
-    my $res_hash = $main::job_db->select_dbentry($sql_statement);
-
-    my $count = keys(%{$res_hash});
-    $out_xml= "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><count>$count</count></xml>";
-    my @out_msg_l = ( $out_xml );
-    return @out_msg_l;
-}
-
-
-sub delete_jobdb_entry {
-    my ($msg) = @_ ;
-    my $msg_hash = &transform_msg2hash($msg);
-    
-    # prepare query sql statement
-    my $table= $main::job_queue_tn;
-    my $where= &get_where_statement($msg, $msg_hash);
-    my $sql_statement = "DELETE FROM $table $where";
-    
-    # execute db query
-    my $db_res = $main::job_db->del_dbentry($sql_statement);
-
-    my $res;
-    if( $db_res > 0 ) { 
-        $res = 0 ;
-    } else {
-        $res = 1;
-    }
-
-    # prepare xml answer
-    my $out_xml = "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
-    my @out_msg_l = ( $out_xml );
-    return @out_msg_l;
-
-}
-
-
-sub clear_jobdb {
-    my ($msg) = @_ ;
-    my $msg_hash = &transform_msg2hash($msg);
-    my $error= 0;
-    my $out_xml= "<xml><answer1>1</answer1></xml>";
-    my $table= $main::job_queue_tn;
-    
-    my $sql_statement = "DELETE FROM $table";
-    my $db_res = $main::job_db->del_dbentry($sql_statement);
-    if( not $db_res > 0 ) { $error++; };
-    
-    if( $error == 0 ) {
-        $out_xml = "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><answer1>0</answer1></xml>";
-    }
-    my @out_msg_l = ( $out_xml );
-    return @out_msg_l;
-}
-
-
-sub update_status_jobdb_entry {
-    my ($msg) = @_ ;
-    my $msg_hash = &transform_msg2hash($msg);
-    my $error= 0;
-    my $out_xml= "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><answer1>1</answer1></xml>";
-
-    my @len_hash = keys %{$msg_hash};
-    if( 0 == @len_hash) {  $error++; };
-    
-    # prepare query sql statement
-    if( $error == 0) {
-        my $table= $main::job_queue_tn;
-        my $where= &get_where_statement($msg, $msg_hash);
-        my $update= &get_update_statement($msg, $msg_hash);
-
-        # conditions
-        # no timestamp update if status eq waiting
-        my $res_hash = $main::job_db->select_dbentry("SELECT * FROM $table $where AND status='processing' ");
-        if( (0 != keys(%$res_hash)) && ($update =~ /timestamp/i) ) {
-            $error ++;
-            $out_xml = "<answer1>1</answer1><error_string>there is no timestamp update allowed while status is 'processing'</error_string>";
-        }
-
-        if( $error == 0 ) {
-            my $sql_statement = "UPDATE $table $update $where";
-            # execute db query
-            my $db_res = $main::job_db->update_dbentry($sql_statement);
-
-            # check success of db update
-            if( not $db_res > 0 ) { $error++; };
-
-        }
-    }
-
-    if( $error == 0) {
-        $out_xml = "<answer1>0</answer1>";
-    }
-    
-    my $out_msg = sprintf("<xml><header>answer</header><source>%s</source><target>GOSA</target>%s</xml>", $server_address, $out_xml);
-    my @out_msg_l = ( $out_msg );
-    return @out_msg_l;
-}
-
-
 1;
 
 
index 6f045cd67fb10c20ed10b578bd126842501006cf..244686b6055f6e39244d8b11164c2af5e0c5bd40 100755 (executable)
@@ -34,13 +34,17 @@ if($sock->connected()){
 #$data = "<xml> <header>gosa_update_status_jobdb_entry</header> <source>GOSA</source><target>GOSA</target><where><clause><phrase> <status>waiting</status></phrase></clause> </where> <update><status>processing</status> <result>update</result></update></xml>";
 
 # jobdb query
-#$data = "<xml><header>gosa_query_jobdb</header><where><clause><connector>and</connector><phrase><operator>gt</operator><ROWID>0</ROWID></phrase><phrase><operator>le</operator><ROWID>5</ROWID></phrase></clause></where></xml>";
-#$data= "<xml><header>gosa_query_jobdb</header><where><clause><phrase><headertag>ping</headertag></phrase></clause></where><limit><from>0</from><to>3</to></limit></xml>";
-#$data= "<xml><header>gosa_query_jobdb</header><source>GOSA</source> <target>GOSA</target><where><clause><phrase><HEADERTAG>trigger_action_reinstall</HEADERTAG></phrase></clause></where><limit><from>0</from><to>25</to></limit><orderby>timestamp DESC</orderby></xml>";
+#$data = "<xml><header>gosa_query_jobdb</header><source>GOSA</source> <target>GOSA</target>".
+#    "<where><clause><connector>and</connector><phrase><operator>gt</operator><ROWID>0</ROWID></phrase><phrase><operator>le</operator><ROWID>5</ROWID></phrase></clause></where></xml>";
+#$data= "<xml><header>gosa_query_jobdb</header><source>GOSA</source> <target>GOSA</target>".
+#    "<where><clause><phrase><headertag>ping</headertag></phrase></clause></where><limit><from>0</from><to>3</to></limit></xml>";
+#$data= "<xml><header>gosa_query_jobdb</header><source>GOSA</source> <target>GOSA</target>".
+#    "<where><clause><phrase><HEADERTAG>trigger_action_reinstall</HEADERTAG></phrase></clause></where>".
+#    "<limit><from>0</from><to>25</to></limit><orderby>timestamp DESC</orderby></xml>";
 #$data= "<xml><header>gosa_query_jobdb</header><source>GOSA</source> <target>GOSA</target></xml>";
 
 # jobdb count
-#$data = "<xml> <header>gosa_count_jobdb</header></xml>";
+#$data = "<xml> <header>gosa_count_packages_list</header><source>GOSA</source> <target>GOSA</target></xml>";
 
 # jobdb clear
 #$data = "<xml> <header>gosa_clear_jobdb</header> </xml>";
@@ -54,8 +58,9 @@ if($sock->connected()){
 #$data = "<xml> <header>gosa_new_key_for_client</header> <target>00:01:6c:9d:b9:fa</target> <source>10.89.1.31:20081</source> </xml>";
 #$data = "<xml> <header>gosa_trigger_action_wake</header> <target>00:01:6c:9d:b9:fa</target> <source>10.89.1.31:20081</source> </xml>";
 #$data = "<xml> <header>gosa_trigger_action_faireboot</header> <target>00:01:6c:9d:b9:fa</target> <source>GOSA</source> </xml>";
-#$data = "<xml> <header>job_trigger_action_reinstall</header> <source>GOSA</source> <target>00:01:6c:9d:b9:fa</target> <macaddress>00:01:6c:9d:b9:fa</macaddress> <timestamp>20130101000000</timestamp> </xml>";
-#$data = "<xml> <header>gosa_ping</header> <target>00:01:6c:9d:b9:fa</target> <source>GOSA</source> </xml>";
+#$data = "<xml> <header>job_trigger_action_reinstall</header> <source>GOSA</source> <target>00:01:6c:9d:b9:fa</target> ".
+#    "<macaddress>00:01:6c:9d:b9:fa</macaddress> <timestamp>20130101000000</timestamp> </xml>";
+$data = "<xml> <header>gosa_ping</header> <target>00:01:6c:9d:b9:fa</target> <source>GOSA</source> </xml>";
 
 
 # to test
@@ -80,7 +85,7 @@ if($sock->connected()){
 #$data = "<xml> <header>gosa_get_client_for_login_usr</header> <target>GOSA</target> <source>GOSA</source> <usr>harald</usr></xml>";
 
 # recreate_fai_server_db
-$data = "<xml> <header>gosa_recreate_fai_server_db</header> <target>GOSA</target> <source>GOSA</source></xml>"; 
+#$data = "<xml> <header>gosa_recreate_fai_server_db</header> <target>GOSA</target> <source>GOSA</source></xml>"; 
 
     $sock->write($data);
     $answer = "nothing";