Advertisement
Guest User

debian-spacewalk-securityrepo

a guest
Feb 15th, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Debian Repository Sync
  4. #
  5. # Downloads Debian packages from mirror and pushes them into Spacewalk
  6. # using rhnpush. This is a workaround until spacewalk-repo-sync natively
  7. # supports Debian repositories
  8. #
  9. # Author: Steve Meier
  10. # Version: 20130204
  11. #
  12. # Changelog:
  13. # 20130204 - Initial release
  14. #
  15. # Here are some sample URLs:
  16. #
  17. # squeeze (Debian 6) Base for i386
  18. # -> http://ftp.debian.org/debian/dists/squeeze/main/binary-i386/
  19. #
  20. # squeeze (Debian 6) Updates for i386
  21. # -> http://ftp.debian.org/debian/dists/squeeze-updates/main/binary-i386/
  22. #
  23. # Replace for i386 with amd64 for 64-bit (x86_64 in CentOS/RHEL)
  24. # Besides main/ there are also contrib/ and non-free/ which you might
  25. # want to add as sub-channels to main/
  26.  
  27. use strict;
  28. use warnings;
  29. use Compress::Zlib;
  30. use File::Basename;
  31. use Frontier::Client;
  32. use Getopt::Long;
  33. use WWW::Mechanize;
  34.  
  35. # No buffering
  36. $| = 1;
  37.  
  38. my $debug = 0;
  39. my ($getopt, $url, $channel, $username, $password);
  40. my $mech;
  41. my ($packages, $package);
  42. my ($pkgname, $fileurl, $md5, $sha1, $sha256);
  43. my ($client, $session, $allpkg);
  44. my (%inrepo, %inchannel);
  45. my ($synced, $tosync);
  46. my %download;
  47.  
  48. $getopt = GetOptions( 'url=s' => \$url,
  49. 'channel=s' => \$channel,
  50. 'username=s' => \$username,
  51. 'password=s' => \$password
  52. );
  53.  
  54. #if ($url =~ /(.*debian\/)/) {
  55. # $debianroot = $1;
  56. # &info("Repo URL: $url\n");
  57. # &info("Debian root is $debianroot\n");
  58. #} else {
  59. # &error("ERROR: Could not find Debian root directory\n");
  60. # exit(1);
  61. #}
  62.  
  63. # Connect to API
  64. $client = new Frontier::Client(url => "http://localhost/rpc/api") ||
  65. die "ERROR: Could not connect to API";
  66.  
  67. # Authenticate to API
  68. $session = $client->call('auth.login', "$username", "$password");
  69. if ($session =~ /^\w+$/) {
  70. &debug("API Authentication successful\n");
  71. } else {
  72. &error("API Authentication FAILED!\n");
  73. exit 3;
  74. }
  75.  
  76. # Index channel on server
  77. $allpkg = $client->call('channel.software.list_all_packages', $session, $channel);
  78. foreach my $pkg (@$allpkg) {
  79. &debug("Found $pkg->{'name'} with checksum $pkg->{'checksum'}\n");
  80. $inchannel{$pkg->{'checksum'}} = 1;
  81. }
  82.  
  83. # Logout from API
  84. $client->call('auth.logout', $session);
  85.  
  86. # Download Packages.gz (why does this fail on some mirrors? HTTP deflate maybe?)
  87. $mech = WWW::Mechanize->new;
  88. print "INFO: Fetching Packages.gz... ";
  89. $mech->get("$url/Packages.gz");
  90. print "done\n";
  91.  
  92. if (not($mech->success)) {
  93. print "ERROR: Could not retrieve Packages.gz\n";
  94. exit(1);
  95. }
  96.  
  97. # Uncompress Packages.gz in memory
  98. $packages = Compress::Zlib::memGunzip($mech->content())
  99. or die "ERROR: Failed to uncompress Packages.gz\n";
  100.  
  101. # Parse uncompressed Packages.gz
  102. $tosync = 0;
  103. $synced = 0;
  104. foreach $package (split(/\n\n/, $packages)) {
  105. foreach $_ (split(/\n/, $package)) {
  106. # if (/^Package: (.*)$/) { $pkgname = $1; };
  107. if (/^Filename: (.*)$/) { $fileurl = $1; };
  108. if (/^MD5sum: (.*)$/) { $md5 = $1; };
  109. if (/^SHA1: (.*)$/) { $sha1 = $1; };
  110. if (/^SHA256: (.*)$/) { $sha256 = $1; };
  111. }
  112. $inrepo{basename($fileurl)} = $fileurl;
  113. &debug("Package ".basename($fileurl)." at $fileurl\n");
  114.  
  115. if ( (not(defined($inchannel{$md5}))) &&
  116. (not(defined($inchannel{$sha1}))) &&
  117. (not(defined($inchannel{$sha256}))) ) {
  118. $download{basename($fileurl)} = $fileurl;
  119. $tosync++;
  120. &debug(basename($fileurl)." needs to be synced\n");
  121. } else {
  122. $synced++;
  123. }
  124. }
  125. &info("Packages in repo:\t\t".scalar(keys %inrepo)."\n");
  126. &info("Packages already synced:\t$synced\n");
  127. &info("Packages to sync:\t\t$tosync\n");
  128.  
  129. # Download missing packages
  130. $synced = 0;
  131. foreach $_ (keys %download) {
  132. $synced++;
  133. &info("$synced/$tosync : $_\n");
  134.  
  135. $mech->get("/$download{$_}", ':content_file' => "/tmp/$_");
  136. if ($mech->success) {
  137. system("rhnpush -c $channel -u $username -p $password /tmp/$_");
  138. }
  139. unlink("/tmp/$_");
  140. }
  141.  
  142. &info("Sync complete.\n");
  143. exit;
  144.  
  145. # SUBS
  146. sub debug() {
  147. if ($debug) { print "DEBUG: @_"; }
  148. }
  149.  
  150. sub info() {
  151. print "INFO: @_";
  152. }
  153.  
  154. sub warning() {
  155. print "WARNING: @_";
  156. }
  157.  
  158. sub error() {
  159. print "ERROR: @_";
  160. }
  161.  
  162. sub notice() {
  163. print "NOTICE: @_";
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement