| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Repositorio::Repository::OpenSuSE; | ||
| 8 | |||
| 9 | use Moose; | ||
| 10 | use Try::Tiny; | ||
| 11 | use File::Basename qw'basename dirname'; | ||
| 12 | use Data::Dumper; | ||
| 13 | use Carp; | ||
| 14 | use Params::Validate qw(:all); | ||
| 15 | use File::Spec; | ||
| 16 | use File::Path 'make_path'; | ||
| 17 | use IO::All; | ||
| 18 | use JSON::XS; | ||
| 19 | |||
| 20 | our $VERSION = '1.2.1'; # VERSION | ||
| 21 | |||
| 22 | extends "Rex::Repositorio::Repository::Yum"; | ||
| 23 | |||
| 24 | sub mirror { | ||
| 25 | my ( $self, %option ) = @_; | ||
| 26 | |||
| 27 | $self->repo->{url} =~ s/\/$//; | ||
| 28 | $self->repo->{local} =~ s/\/$//; | ||
| 29 | my $name = $self->repo->{name}; | ||
| 30 | |||
| 31 | $self->app->print_info("Downloading repository information..."); | ||
| 32 | |||
| 33 | my $content = $self->download( $self->repo->{url} . "/content" ); | ||
| 34 | my $desc_dir = "suse/setup/descr"; | ||
| 35 | my $data_dir = "suse"; | ||
| 36 | |||
| 37 | for my $line ( split( /\n/, $content ) ) { | ||
| 38 | if ( $line =~ m/^DESCRDIR/ ) { | ||
| 39 | my $_t; | ||
| 40 | ( $_t, $desc_dir ) = split( /\s+/, $line ); | ||
| 41 | last; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | $self->app->logger->debug("Found descr dir: $desc_dir"); | ||
| 45 | |||
| 46 | for my $line ( split( /\n/, $content ) ) { | ||
| 47 | if ( $line =~ m/^DATADIR/ ) { | ||
| 48 | my $_t; | ||
| 49 | ( $_t, $data_dir ) = split( /\s+/, $line ); | ||
| 50 | last; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | $self->app->logger->debug("Found data dir: $data_dir"); | ||
| 54 | |||
| 55 | my @all_meta_files = $self->_get_yast_directory(); | ||
| 56 | push @all_meta_files, $self->_get_yast_directory($desc_dir); | ||
| 57 | |||
| 58 | my %content_sha; | ||
| 59 | for my $line ( split /\n/, $content ) { | ||
| 60 | next if ( $line !~ m/^(HASH|KEY|META)/ ); | ||
| 61 | |||
| 62 | my ( $type, $sha, $sum, $file ) = | ||
| 63 | ( $line =~ m/^(HASH|KEY|META)\s*([^\s]+)\s*([^\s]+)\s*(.*)/ ); | ||
| 64 | $content_sha{$file} = $sum; | ||
| 65 | } | ||
| 66 | |||
| 67 | for my $file (@all_meta_files) { | ||
| 68 | if ( $file =~ m/\/$/ ) { | ||
| 69 | |||
| 70 | # directory | ||
| 71 | push @all_meta_files, $self->_get_yast_directory($file); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | @all_meta_files = grep { !m/\/$/ } @all_meta_files; | ||
| 76 | |||
| 77 | push @all_meta_files, "$data_dir/repodata/repomd.xml"; | ||
| 78 | push @all_meta_files, "$data_dir/repodata/repomd.xml.asc"; | ||
| 79 | push @all_meta_files, "$data_dir/repodata/repomd.xml.key"; | ||
| 80 | push @all_meta_files, "$data_dir/repodata/appdata.xml"; | ||
| 81 | push @all_meta_files, "$data_dir/repodata/appdata.xml.gz"; | ||
| 82 | |||
| 83 | $self->app->logger->notice('Downloading metadata...'); | ||
| 84 | my $m_count = 0; | ||
| 85 | my $m_total = scalar(@all_meta_files); | ||
| 86 | |||
| 87 | for my $file (@all_meta_files) { | ||
| 88 | my $path = $file; | ||
| 89 | my $repo_url = $self->repo->{url}; | ||
| 90 | $path =~ s/$repo_url//g; | ||
| 91 | |||
| 92 | $m_count++; | ||
| 93 | $self->app->logger->info("${m_count}/$m_total ${repo_url}"); | ||
| 94 | |||
| 95 | my $local_file = File::Spec->catdir($self->app->get_repo_dir(repo => $self->repo->{name} ), $path); | ||
| 96 | make_path(dirname($local_file)); | ||
| 97 | |||
| 98 | $self->download_package( | ||
| 99 | url => "$repo_url/$file", | ||
| 100 | name => basename($path), | ||
| 101 | dest => $local_file, | ||
| 102 | cb => sub { | ||
| 103 | my ($dest) = @_; | ||
| 104 | if ( exists $content_sha{$file} ) { | ||
| 105 | return $self->_checksum( $dest, "sha256", $content_sha{$file} ); | ||
| 106 | } | ||
| 107 | }, | ||
| 108 | force => $option{update_metadata} | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | my ( $packages_ref, $repomd_ref ); | ||
| 113 | ( $packages_ref, $repomd_ref ) = | ||
| 114 | $self->_get_repomd_xml( $self->repo->{url} . "/$data_dir/" ); | ||
| 115 | my @packages = map { $_->{location} = "/$data_dir/" . $_->{location}; $_; } | ||
| 116 | @{$packages_ref}; | ||
| 117 | |||
| 118 | $self->_download_packages( \%option, @packages ); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _get_yast_directory { | ||
| 122 | my ( $self, $url ) = @_; | ||
| 123 | $url ||= ""; | ||
| 124 | $self->app->print_info("Reading directory contents: /$url"); | ||
| 125 | my $content; | ||
| 126 | try { | ||
| 127 | $content = $self->download( $self->repo->{url} . "/$url/directory.yast" ); | ||
| 128 | } | ||
| 129 | catch { | ||
| 130 | $self->app->logger->debug("No directory.yast found in /$url"); | ||
| 131 | $content = ""; | ||
| 132 | }; | ||
| 133 | |||
| 134 | my @data; | ||
| 135 | for my $line ( split( /\n/, $content ) ) { | ||
| 136 | if ($url) { | ||
| 137 | push @data, "$url/$line"; | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | push @data, $line; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return @data; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; |