summaryrefslogtreecommitdiffstats
path: root/mgaonline.pm
blob: f4629a3a5270a75d438a6e67d54c7067a50c823e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
################################################################################
# Mandriva Online functions                                                    # 
#                                                                              #
# Copyright (C) 2004-2005 Mandrakesoft                                         #
#               2005-2006 Mandriva                                             #
#                                                                              #
# Daouda Lo                                                                    #
# Thierry Vignaud <tvignaud at mandriva dot com>                               #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   #
################################################################################

package mgaonline;

use strict;

use lib qw(/usr/lib/libDrakX);
use common;
use ugtk2;

use LWP::UserAgent;
use URI::Escape;
use XML::Simple;
use HTTP::Request::Common;
use HTTP::Request;

our @ISA = qw(Exporter);
our @EXPORT = qw(find_current_distro
                 fork_exec
                 get_banner
                 get_distro_list
                 get_from
                 get_product_id
                 get_release
                 get_stale_upgrade_filename
                 get_urpmi_options
                 is_it_2008_0
                 is_enterprise_media_supported
                 is_extmaint_supported
                 is_restricted_media_supported
                 read_sys_config
                 translate_product
                 xml2perl
                 %config
                 $config_file
                 $product_id
                 $root);
our @EXPORT_OK = qw(
    get_product_info
    get_my_mdv_profile
    add_medium_powerpack
    add_medium_enterprise
    add_medium_extended
);

our (%config, $product_id, $root);
our $version = 1;

use log;

our $config_file = '/etc/sysconfig/mgaapplet';
my $release_file = find { -f $_ } map { "/etc/${_}release" } '', qw(mageia- mandriva- mandrakelinux- mandrake- redhat-);

sub read_sys_config() {
    %config = getVarsFromSh($config_file);
}

sub get_stale_upgrade_filename() {
    '/var/lib/urpmi/stale_upgrade_in_progress';
}

sub get_product_id() {
    $product_id = common::parse_LDAP_namespace_structure(cat_("$root/etc/product.id"));
}

sub get_release() {
    my ($r) = cat_($release_file) =~ /release\s+(\S+)/;
    ($r);
}

sub is_it_2008_0() {
  $product_id->{version} eq '2008.0';
}

sub is_extmaint_supported() {
    $product_id->{support} eq 'extended';
}

sub is_enterprise_media_supported() {
    return if is_it_2008_0();
    to_bool($product_id->{type} eq 'Enterprise' && $product_id->{product} eq 'Server');
}

sub is_restricted_media_supported() {
    return if is_it_2008_0();
    to_bool($product_id->{product} =~ /powerpack/i);
}

sub find_current_distro {
    find { $_->{version} eq $product_id->{version} } @_;
}

sub get_distro_list_() {
    #- contact the following URL to retrieve the list of released distributions.
    my $type = lc($product_id->{type}); $type =~ s/\s//g;
    my $extra_path = $::testing || uc($config{TEST_DISTRO_UPGRADE}) eq 'YES' ? 'testing-' : '';
    my $list = 
      join('&',
           "https://releases.mageia.org/api/a/$extra_path$product_id->{arch}?product=$product_id->{product}",
          "version=$product_id->{version}",
           "mgaonline_version=$mgaonline::version",
       );
    log::explanations("trying distributions list from $list");

    eval {
        my $urpm = Rpmdrake::open_db::fast_open_urpmi_db();

        # prevent SIGCHILD handler's waitpid to interfere with urpmi waiting
        # for curl exit code, which broke downloads:
        local $SIG{CHLD} = 'DEFAULT';

        # old API:
        if (member($product_id->{version}, qw(2007.1 2008.0 2008.1))) {
            require mgaapplet_urpm;
            mgaapplet_urpm::ensure_valid_cachedir($urpm);
            mgaapplet_urpm::get_content($urpm, $list);
        } else {
            urpm::ensure_valid_cachedir($urpm);
            urpm::download::get_content($urpm, $list);
        }
    };
}

sub get_distro_list() {
    return if $product_id->{product} =~ /Flash/;

    my @lines = get_distro_list_();

    if (my $err = $@) {
        log::explanations("failed to download distribution list:\n$err");
        return; # not a fatal error
    }
    
    if (!@lines) {
        log::explanations("empty distribution list");
        return;
    }

    map { common::parse_LDAP_namespace_structure(chomp_($_)) } grep { /^[^#]/ } @lines;
}


sub clean_confdir() {
    my $confdir = '/root/.MgaOnline';
    system "/bin/rm", "-f", "$confdir/*log.bz2", "$confdir/*log.bz2.uue", "$confdir/*.dif $confdir/rpm_qa_installed_before", "$confdir/rpm_qa_installed_after";
}


sub fork_exec {
    run_program::raw({ detach => 1 }, @_);
}

sub translate_product {
    my ($product) = @_;
    my %strings = (
        flash => N("Mageia Flash"),
        free => N("Mageia Free"),
        mini => N("Mageia Mini"),
        one => N("Mageia One"),
        powerPack => N("Mageia PowerPack"),
        server => N("Mageia Enterprise Server"),
    );
    $product ||= lc $product_id->{product};
    $strings{$product} || $product;
}

# TODO Move all product.id handling to a separate module
# (MDV::ProductId?)
sub get_product_info {
    my ($product) = @_;
    my %info = (
        powerpack => {
            name => N("Mageia PowerPack"),
            description => N("The Mageia Linux distribution with even more softwares and official support."),
        },
        free => {
            name => N("Mageia Free"),
            description => N("The 100%% Open Source distribution freely available."),
        },
    );
    $product ||= lc $product_id->{product};
    return $info{$product};
}

sub get_banner_icon() {
    find { -e $_ } 
      qw(/usr/share/mcc/themes/default/rpmdrake-mga.png /usr/share/icons/large/mgaonline.png);
}

sub get_banner {
    my ($o_title) = @_;
    Gtk2::Banner->new(get_banner_icon(), $o_title || N("Distribution Upgrade"));
}

sub get_urpmi_options() {
    ({ sensitive_arguments => 1 }, 'urpmi.addmedia', if_(!is_it_2008_0(), '--xml-info', 'always'));
}

sub add_medium_enterprise {
    my ($email, $password, $version, $arch) = @_;
    my $uri = sprintf('https://%s:%s@download.FIXME.com/%s/rpms/%s/',
                      uri_escape($email),
                      uri_escape($password),
                      $version,
                      $arch);
    my @options = get_urpmi_options();
    run_program::raw(@options, '--update', '--distrib', $uri);
}

sub add_medium_powerpack {
    my ($email, $password, $version, $arch) = @_;
    my $uri = sprintf('https://%s:%s@dl.FIXME.com/rpm/comm/%s/',
                      uri_escape($email),
                      uri_escape($password),
                      $version);
    my @options = get_urpmi_options();

    # add release and updates media...
    run_program::raw(@options,
                     "Restricted $arch " . int(rand(100000)),
                     "$uri$arch") 
        or return 0;
    run_program::raw(@options,
                     '--update',
                     "Restricted Updates $arch " . int(rand(100000)),
                     "${uri}updates/$arch");
}

sub add_medium_extended {
    my ($email, $password, $version, $arch) = @_;
    my $uri = sprintf("https://%s:%s\@dl.FIXME.com/extended/%s/%s",
                      uri_escape($email),
                      uri_escape($password),
                      $version,
                      $arch);
    my @options = (get_urpmi_options(), '--update');
    run_program::raw(@options, 
                     "Extended Maintenance $arch " . int(rand(100000)), 
                     ${uri});
}

sub is_running {
    my ($name) = @_;
    my $found;
    foreach (`ps -o '%P %p %c' -u $ENV{USER}`) {
        my ($ppid, $pid, $n) = /^\s*(\d+)\s+(\d+)\s+(.*)/;
        if ($ppid != 1 && $pid != $$ && $n eq $name) {
            $found = $pid;
            last;
        }
    }
    $found;
}


sub get_from {
    my ($link, $header) = @_;
    
    my $ua = LWP::UserAgent->new;
    $ua->agent(sprintf('mgaapplet (mgaonline-%s; distribution: %s)',
                       $mgaonline::version, $version));
    $ua->env_proxy;

    my $response = $ua->post($link, $header);
    $response;
}

sub get_my_mdv_profile {
    my ($email, $password) = @_;
    xml2perl(get_from('https://my.FIXME.com/rest/authenticate',
                      [ 'username', $email, 'password', $password, 
                        'return', 'userdata' ]));
}

# callers need to require XML::Simple
sub xml2perl {
    my ($res) = @_;
    my $ref = eval { XML::Simple->new->XMLin($res->{_content}) };
    if (my $err = $@) {
        warn ">> XML error: $err\n";
        $ref = {
            code => 1,
            message => $err,
        };
    }
    $ref;
}


1;