1
# ----------------------------------------------------------------------
2
# Copyright (c) 2008 Dominic Reynolds
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of version 2 of the GNU General Public
6
# License as published by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
14
# ----------------------------------------------------------------------
16
package Immunix::Repository;
22
use Cwd qw(cwd realpath);
25
use File::Temp qw/ tempfile tempdir /;
34
our @ISA = qw(Exporter);
43
fetch_profiles_by_user
44
fetch_profiles_by_name
45
fetch_profiles_by_name_and_user
57
sub get_repo_client ($) {
59
unless ( $clients{$repo_url} ) {
60
$clients{$repo_url} = new RPC::XML::Client $repo_url;
62
return $clients{$repo_url};
65
sub did_result_succeed {
68
my $ref = ref $result;
69
return ($ref && $ref ne "RPC::XML::fault") ? 1 : 0;
72
sub get_result_error {
76
if (ref $result eq "RPC::XML::fault") {
77
$result = $result->string;
85
sub user_login ($$$) {
86
my ($repo_url,$user,$pass) = @_;
88
my $repo_client = get_repo_client( $repo_url );
90
my $res = $repo_client->send_request('LoginConfirm', $user, $pass);
91
if (did_result_succeed($res)) {
96
$detail = get_result_error($res);
99
return $status,$detail;
103
sub user_register ($$$$) {
104
my ($repo_url,$user,$pass,$email) = @_;
105
my $repo_client = get_repo_client( $repo_url );
106
my ($status,$detail);
107
if ( $repo_client ) {
108
my $res = $repo_client->send_request('Signup', $user, $pass, $email);
109
if (did_result_succeed($res)) {
114
$detail = get_result_error($res);
117
return $status,$detail;
120
sub upload_profile ($$$$$$$) {
121
my ($repo_url,$user,$pass,$distro,$pname,$profile,$changelog) = @_;
122
my ($status,$detail);
123
my $repo_client = get_repo_client( $repo_url );
124
my $res = $repo_client->send_request( 'Create', $user, $pass, $distro,
125
$pname, $profile, $changelog);
126
if (did_result_succeed($res)) {
127
$detail = $res->value;
130
$detail = get_result_error($res);
133
return $status,$detail;
136
sub fetch_profile_by_id ($$) {
137
my ($repo_url,$id) = @_;
138
my $repo_client = get_repo_client( $repo_url );
140
my ($status,$detail);
141
my $res = $repo_client->send_request('Show', $id);
142
if (did_result_succeed($res)) {
144
$detail = $res->value();
147
$detail = get_result_error($res);
150
return $status, $detail;
154
sub fetch_profiles ($$$$) {
155
my ($repo_url,$distro,$username,$fqdn) = @_;
157
my ($status,$detail);
158
my $repo_client = get_repo_client( $repo_url );
160
$repo_client->send_request('FindProfiles', $distro, $fqdn, $username);
161
if (did_result_succeed($res)) {
163
for my $p ( @$res ) {
164
my $p_repo = $p->{profile}->value();
165
$p_repo =~ s/flags=\(complain\)// if ( $p_repo ); #strip complain flag
166
$p->{profile} = $p_repo;
167
$p->{user_id} = $p->{user_id}->value();
168
$p->{id} = $p->{id}->value();
169
$p->{name} = $p->{name}->value();
170
$p->{created_at} = $p->{created_at}->value();
171
$p->{downloaded_count} = $p->{downloaded_count}->value();
176
$detail = get_result_error($res);
178
return $status,$detail;
181
sub fetch_profiles_by_user ($$$) {
182
my ($repo_url,$distro,$username) = @_;
184
my ($status,$detail) = fetch_profiles( $repo_url, $distro, $username, "" );
186
for my $p ( @$detail ) {
187
my $p_repo = $p->{profile};
189
$p->{username} = $username;
190
$p_hash->{$p->{name}} = $p;
194
return ($status,$detail);
196
return($status,$p_hash);
200
sub fetch_profiles_by_name_and_user ($$$$) {
201
my ($repo_url,$distro,$fqdbin, $username) = @_;
203
my ($status,$detail) = fetch_profiles( $repo_url, $distro, $username, $fqdbin );
205
for my $p ( @$detail ) {
206
my $p_repo = $p->{profile}?$p->{profile}:"";
207
$p_hash->{$p->{name}} = $p if ($p_repo ne "");
210
return ($status,$detail);
212
return($status,$p_hash);
216
sub fetch_profiles_by_name ($$$) {
217
my ($repo_url,$distro,$fqdbin) = @_;
218
my ($status,$detail,$data);
220
($status,$data) = fetch_profiles( $repo_url, $distro, "", $fqdbin);
224
push @uids, $p->{user_id};
226
my ($status_unames,$unames) = fetch_usernames_from_uids($repo_url, @uids);
227
if ( $status_unames ) {
229
if ( $unames->{$p->{user_id}} ) {
230
$p->{username} = $unames->{$p->{user_id}};
232
$p->{username} = "unkown-" . $p->{user_id};
237
print STDOUT "ERROR UID\n";
240
$p->{profile_type} = "REPOSITORY";
241
$detail->{$p->{username}} = $p;
246
return $status,$detail;
250
sub fetch_newer_profile ($$$$$) {
251
my ($repo_url,$distro,$user,$id,$profile) = @_;
252
my $repo_client = get_repo_client( $repo_url );
254
my ($status,$detail);
258
$repo_client->send_request('FindProfiles', $distro, $profile, $user);
259
if (did_result_succeed($res)) {
261
my @profile_list = @{$res->value};
265
if ($profile_list[0]->{id} > $id) {
266
$p = $profile_list[0];
272
$detail = get_result_error($res);
275
return $status,$detail;
278
sub fetch_usernames_from_uids ($) {
279
my ($repo_url,@searchuids) = @_;
280
my ($status,$result) = (1,{});
283
for my $uid ( @searchuids ) {
284
if ( $uid2login{$uid} ) {
285
$result->{$uid} = $uid2login{$uid};
291
my $repo_client = get_repo_client( $repo_url );
292
#RPC::XML will serialize the array into XML with the is_utf8 flag set
293
#which causes, HTTP:Message to fail. Looping on the array elements
294
#stops this from happening, and since these are all numbers it
295
#will not cause problems.
296
for my $foo (@uids) {
297
Encode::_utf8_off($foo);
299
my $res = $repo_client->send_request('LoginNamesFromUserIds', [@uids]);
300
if (did_result_succeed($res)) {
301
my @usernames = @{ $res->value };
302
for my $uid (@uids) {
303
my $username = shift @usernames;
304
$uid2login{$uid} = $username;
305
$result->{$uid} = $uid2login{$uid};
309
$result = get_result_error($res);
312
return $status,$result;
315
sub get_repo_config {
316
unless ( $repo_cfg ) {
317
$repo_cfg = Immunix::Config::read_config("repository.conf");
320
$aa_cfg = Immunix::Config::read_config("logprof.conf");
323
"url" => $aa_cfg->{repository}{url},
324
"distro" => $aa_cfg->{repository}{distro},
325
"enabled" => $repo_cfg->{repository}{enabled},
326
"upload" => $repo_cfg->{repository}{upload},
327
"user" => $repo_cfg->{repository}{user},
328
"password" => $repo_cfg->{repository}{pass},
329
"email" => $repo_cfg->{repository}{email}
333
sub set_repo_config ($) {
335
my ($url,$distro,$enabled,$upload,$user,$pass);
336
unless ( $repo_cfg ) {
337
$repo_cfg = Immunix::Config::read_config("repository.conf");
340
$aa_cfg = Immunix::Config::read_config("logprof.conf");
342
$repo_cfg->{repository}{enabled} = $cfg->{enabled} if ( $cfg->{enabled} );
343
$repo_cfg->{repository}{upload} = $cfg->{upload} if ( $cfg->{upload} );
344
$repo_cfg->{repository}{user} = $cfg->{user} if ( $cfg->{user} );
345
$repo_cfg->{repository}{pass} = $cfg->{password}if ( $cfg->{password} );
346
$repo_cfg->{repository}{email} = $cfg->{email} if ( $cfg->{email} );
347
$aa_cfg->{repository}{distro} = $cfg->{distro} if ( $cfg->{distro} );
348
$aa_cfg->{repository}{url} = $cfg->{url} if ( $cfg->{url} );
349
write_config("repository.conf", $repo_cfg);
350
write_config("logprof.conf", $aa_cfg);