From: rettenbe Date: Thu, 28 Feb 2008 13:43:46 +0000 (+0000) Subject: add db packages_list X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=9e769505a46929d88407269cc9b8ec759d58827d;p=gosa.git add db packages_list git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@9188 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index 90579fd08..6ba6160af 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -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 = ){ + # 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 = ){ + # 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, } ); diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index 8bd357fb6..b2b628fd5 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -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; diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index ec8f6d0eb..a230a309e 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -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= "error"; - - # 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= "
answer
$server_addressGOSA$count
"; - 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 = "
answer
$server_addressGOSA$res
"; - 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= "1"; - - 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 = "
answer
$server_addressGOSA0
"; - } - 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= "
answer
$server_addressGOSA1
"; - - 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 = "1there is no timestamp update allowed while status is 'processing'"; - } - - 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 = "0"; - } - - my $out_msg = sprintf("
answer
%sGOSA%s
", $server_address, $out_xml); - my @out_msg_l = ( $out_msg ); - return @out_msg_l; -} - - 1; diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index 6f045cd67..244686b60 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -34,13 +34,17 @@ if($sock->connected()){ #$data = "
gosa_update_status_jobdb_entry
GOSAGOSA waiting processing update
"; # jobdb query -#$data = "
gosa_query_jobdb
andgt0le5
"; -#$data= "
gosa_query_jobdb
ping03
"; -#$data= "
gosa_query_jobdb
GOSA GOSAtrigger_action_reinstall025timestamp DESC
"; +#$data = "
gosa_query_jobdb
GOSA GOSA". +# "andgt0le5
"; +#$data= "
gosa_query_jobdb
GOSA GOSA". +# "ping03
"; +#$data= "
gosa_query_jobdb
GOSA GOSA". +# "trigger_action_reinstall". +# "025timestamp DESC
"; #$data= "
gosa_query_jobdb
GOSA GOSA
"; # jobdb count -#$data = "
gosa_count_jobdb
"; +#$data = "
gosa_count_packages_list
GOSA GOSA
"; # jobdb clear #$data = "
gosa_clear_jobdb
"; @@ -54,8 +58,9 @@ if($sock->connected()){ #$data = "
gosa_new_key_for_client
00:01:6c:9d:b9:fa 10.89.1.31:20081
"; #$data = "
gosa_trigger_action_wake
00:01:6c:9d:b9:fa 10.89.1.31:20081
"; #$data = "
gosa_trigger_action_faireboot
00:01:6c:9d:b9:fa GOSA
"; -#$data = "
job_trigger_action_reinstall
GOSA 00:01:6c:9d:b9:fa 00:01:6c:9d:b9:fa 20130101000000
"; -#$data = "
gosa_ping
00:01:6c:9d:b9:fa GOSA
"; +#$data = "
job_trigger_action_reinstall
GOSA 00:01:6c:9d:b9:fa ". +# "00:01:6c:9d:b9:fa 20130101000000
"; +$data = "
gosa_ping
00:01:6c:9d:b9:fa GOSA
"; # to test @@ -80,7 +85,7 @@ if($sock->connected()){ #$data = "
gosa_get_client_for_login_usr
GOSA GOSA harald
"; # recreate_fai_server_db -$data = "
gosa_recreate_fai_server_db
GOSA GOSA
"; +#$data = "
gosa_recreate_fai_server_db
GOSA GOSA
"; $sock->write($data); $answer = "nothing";