summaryrefslogtreecommitdiffstats
path: root/mgaapplet-upgrade-helper
blob: 90a59218676216b445d21d5c2d0655035825a381 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/perl
################################################################################
# Mandriva Online Distribution Upgrade Helper                                  # 
#                                                                              #
# Copyright (C) 2008-2010 Mandriva                                             #
#                                                                              #
# 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.   #
################################################################################

use strict;
use POSIX ":sys_wait_h";
use lib qw(/usr/lib/libDrakX);
use standalone; # for explanations
use common;
BEGIN { require_root_capability() }
use run_program;
use feature 'state';

BEGIN { unshift @::textdomains, 'mgaonline' }

use mygtk3 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version
use ugtk3 qw(:all);
use interactive;
use do_pkgs;
use lib qw(/usr/lib/libDrakX/drakfirsttime);
use mgaonline;
use Rpmdrake::open_db;
use lang;
use mgaapplet_gui qw(run_ask_credentials_dialog);

ugtk3::add_icon_path("/usr/share/mgaonline/pixmaps/");
my ($log_file);

my ($new_distro_version, $download_dir);
foreach my $opt (@ARGV) {
    if ($opt =~ /--(rpm-root|urpmi-root)=(.*)/) {
        $::rpmdrake_options{$1}[0] = $2;
    } elsif ($opt =~ /--new_distro_version=(.*)/) {
        $new_distro_version = $1;
    } elsif ($opt =~ /--download-all=(.*)/) {
        $download_dir = $1;
    }
}

die("Usage: mgaapplet-upgrade-helper --new_distro_version=n [--rpm-root-dir] [--urpmi-root=dir] [--download-all=dir]\n") if !$new_distro_version;

$root = Rpmdrake::open_db::fast_open_urpmi_db()->{root};

read_sys_config();
my $product_id = get_product_id();

check_preparation();

if (!$ENV{URPMI_IGNORESIZE}) {
    check_available_free_space('/usr', 800) &&
      check_available_free_space('/var', 800)
	or exit(1);
}

run_program::raw({ detach => 1 }, 'ionice', '-p', $$, '-n7');

upgrade();

my ($refork_gurpmi);

sub check_available_free_space {
    my ($dir, $wanted_MB) = @_;

    my (undef, $free_KB) = MDK::Common::System::df($dir);

    if ($free_KB / 1024 < $wanted_MB) {
	my $msg = ugtk3::escape_text_for_TextView_markup_format(
	    N("Your system does not have enough space left in %s for upgrade (%dMB < %dMB)",
	      $dir,
	      $free_KB / 1024, $wanted_MB));
	ugtk3::ask_warn(N("Error"), $msg);
	0;
    } else {
	1;
    }
}

sub run_gurpmi() {
    # Windows are created before upgrading packages, to guarantee that
    # any possible related GTK upgrade won't affect us...
    my $succeeded_win = create_upgrade_succeeded_window();
    my $failed_win = create_upgrade_failed_window();

    while ($refork_gurpmi) {
        
        my $ok = fork_gurpmi();
        if ($refork_gurpmi && !$ok) {
            $refork_gurpmi--;
        } else {
            undef $refork_gurpmi;
        }

        # update media in case mirrors got updated if needed:
        update_media() if $refork_gurpmi;

        if (!$refork_gurpmi) {
            if (!$ok) {
                $failed_win->main && upgrade();
            } else {
                rm_rf(get_stale_upgrade_filename());
                $succeeded_win->main and !$::testing and any::reboot();
                my $mgaapp_pid = common::is_running('mgaapplet');
                defined($mgaapp_pid) && kill('HUP', $mgaapp_pid);
            }
        }
    }
}

my $width = 500;
my @common = (
    # explicitely wrap (for 2008.1):
    line_wrap => 1,
    # workaround infamous 6 years old gnome bug #101968:
    width => $width - 50,
);

sub create_upgrade_failed_window() {
    local $mygtk3::left_padding = 0;
    my $w = ugtk3->new(N("Error"));
    gtkadd($w->{window},
           gtknew('VBox', children_tight => [
               get_banner(),
               gtknew('Label_Left', text => N("Installation failed"), @common),
               gtknew('Label_Left', text => N("Installation logs can be found in '%s'", $log_file), @common),
               create_okcancel($w, N("Retry"), N("Cancel")),
           ]),
       );
    $w->{ok}->grab_focus;
    return $w;
}

sub create_upgrade_succeeded_window() {
    local $mygtk3::left_padding = 0;
    my $w = ugtk3->new(N("Congratulations"));
    gtkadd($w->{window},
           gtknew('VBox', children_tight => [
               get_banner(),
               gtknew('Label_Left', text => N("Upgrade to Mageia %s release was successfull.", $new_distro_version),
                      @common),
               gtknew('Label_Left', text => N("You must restart your system."), @common),
               create_okcancel($w, N("Reboot"), N("Cancel")),
           ]),
       );
    $w->{ok}->grab_focus;
    return $w;
}

sub check_preparation() {
    # Find the matching new_distro_version
    my @distros = get_distro_list();
    if (!@distros) {
        ugtk3::ask_warn(N("Error"), N("Unable to download distro list"));
        die("unable to retrieve distro list\n");
    }

    my $new_distro = find { $_->{version} eq $new_distro_version } @distros;
    if (!$new_distro) {
        ugtk3::ask_warn(N("Error"), N("Distribution version %s was not found in the update list", $new_distro_version));
        die("could not find version '$new_distro_version' in the distro update list\n");
    }

    if ($new_distro->{needs_preparation}) {
        my $statefile = "$root/var/lib/mageia-prepare-upgrade/state";
        my $prepared = -f $statefile && cat_($statefile) =~ /ready/;

        if (!$prepared) {
            ugtk3::ask_yesorno(N("Preparation Required"), N("In order to upgrade, your current installation needs to be prepared.\n\nDo you wish to do this preparation now?")) or exit(0);

            my $in = interactive->vnew;
            my $do_pkgs = do_pkgs::do_pkgs($in);
            $do_pkgs->ensure_is_installed("mageia-prepare-upgrade") or exit(0);

            # Check to see if installation alone is enough to ensure things are in the right state?
            $prepared = -f $statefile && cat_($statefile) =~ /ready/;
            if (!$prepared) {
                my $infofile = "$root/usr/share/doc/mageia-prepare-upgrade/README.prepare";
                my $info = -f $infofile && ugtk3::escape_text_for_TextView_markup_format(join('', cat_($infofile)));
                $info ||= N("Further action is required before you can continue.\n\nPlease see %s for more information.", $new_distro->{url});
                ugtk3::ask_warn(N("Next Steps"), $info);
                exit(0) if !$::testing;
                log::l("I would validate /var/lib/mageia-prepare-upgrade/state == 'ready'");
            }
        }
    }
}

sub upgrade() {
    my $xid = gtkroot()->XWINDOW;
    
    run_program::raw({ detach => 1 }, 'xdg-screensaver', 'suspend', $xid);
    my $_w = before_leaving { run_program::raw({ detach => 1 }, 'xdg-screensaver', 'resume', $xid) };
   
    my $file = "$ENV{HOME}/.MgaOnline/urpmi.cfg.backup." . int(rand 100000);
    log::explanations("backuping urpmi configuration in $file");
    cp_af("$root/etc/urpmi/urpmi.cfg", $file);

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

    my $urpmi_lock = eval {
        local $urpm->{fatal} = sub { die @_ };
        urpm::lock::urpmi_db($urpm, 'exclusive');
    };

    if (my $err = $@) {
        log::explanations(sprintf("locking urpmi database failed: %s"), $err);
        ugtk3::ask_warn(N("Error"),
                        N("Installation failed"),
                        '',
                        formatAlaTeX(N("Packages database is locked. Please close other applications
working with packages database (do you have another media
manager on another desktop, or are you currently installing
packages as well?)."))
                    );
        exit(1);
    }

    my ($nonfree, $tainted);

    log::explanations("Checking for enabled Nonfree/Tainted");
    foreach my $media (@{$urpm->{media}}) {
        log::explanations("Checking $media->{name}");
        if (!defined($media->{ignore})) {
            log::explanations("Media $media->{name} is not ignored");
            if ($media->{name} =~ /^Nonfree/) {
                $nonfree = 1;
                log::explanations("Nonfree found to be enabled");
            }
            if ($media->{name} =~ /^Tainted/) {
                $tainted = 1;
                log::explanations("Tainted found to be enabled");
            }
        }
    }

    undef $urpmi_lock;

    touch(get_stale_upgrade_filename());

    log::explanations("removing all existing urpmi media for upgrade");
    run_program::run('urpmi.removemedia', if_($root, '--urpmi-root', $root), '-a');

    #- clear the mirror cache before upgrading:
    eval { rm_rf("$::prefix/var/cache/urpmi/mirrors.cache") };

    $urpm = Rpmdrake::open_db::fast_open_urpmi_db();
    if ($urpm->{global_config}{downloader}) {
        log::explanations("disabling custom downloader in order to use aria2");
        delete $urpm->{global_config}{downloader};
        urpm::media::write_config($urpm);
    }

    log::explanations("adding urpmi media for new distribution");

    $ENV{URPMI_ADDMEDIA_REASON} = join(',', 'reason=upgrade', 'upgrade_by=mgaapplet', "upgrade_from=$product_id->{version}");
    # the following modifies $MIRRORLIST expansion in urpm/mirrors.pm,
    # it must be set for gurpmi.addmedia, but also for gurpmi
    $ENV{URPMI_ADDMEDIA_PRODUCT_VERSION} = $new_distro_version;

    system('gurpmi.addmedia', '--silent-success', if_($root, "--urpmi-root=$root"), '--distrib', '--mirrorlist', '$MIRRORLIST') and do {
        log::explanations("adding media for new distribution failed");
        rm_rf(get_stale_upgrade_filename());
        log::explanations("restoringing urpmi configuration from $file");
        cp_af($file, "$root/etc/urpmi/urpmi.cfg");
        update_media();
        ugtk3::ask_warn(N("Error"),
                        N("Installation failed"),
                        N("Failure when adding medium"),
                    );
        exit(2);
    };

    if ($nonfree || $tainted) {
        if ($nonfree) {
            log::explanations("enabling nonfree as it was there before upgrade");
            enable_media("Nonfree");
        }

        if ($tainted) {
            log::explanations("enabling tainted as it was there before upgrade");
            enable_media("Tainted");
        }

        # Media was added, update it and move along...
        update_media();
    }

    log::explanations("upgrading urpmi and rpmdrake");
    log::explanations("upgrading the whole system");

    # rerun gurpmi in case there was an issue (with eg: big transactions:
    $refork_gurpmi = 4;
    {
        # force PATH:
        local $ENV{TMPDIR} = undef;
	my $log_template = 
	    "gurpmi_upgrade_to_${new_distro_version}_XXXXXXXX";
        $log_file = chomp_(`mktemp -u --tmpdir=$ENV{HOME}/.MgaOnline/ --suffix=.log $log_template`);
        log::explanations("logging gurpmi message in '$log_file'");
    }

    # we cannot use installUpdates() as MandrivaUpdate needs the media
    # flaged as update (or else, we need to add a new option to MandrivaUpdate):
    run_gurpmi();
}

sub fork_gurpmi {
    run_program::raw({ timeout => 'never' },
		     'gurpmi', '>>', $log_file, '2>>', $log_file, '--auto', '--auto-select', '--replacefiles', '--clean',
                      if_($root, "--urpmi-root=$root"),
                      if_($download_dir, "--download-all=$download_dir"));
}

sub update_media() {
    run_program::run('urpmi.update', if_($root, '--urpmi-root', $root), '-a');
}

sub enable_media {
    my ($repos) = @_;
    run_program::run('urpmi.update', if_($root, '--urpmi-root', $root), '--no-ignore', "$repos Release", "$repos Updates");
}