]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/pakfire/pakfire
pakfire: Optimize upgradecore function
[people/pmueller/ipfire-2.x.git] / src / pakfire / pakfire
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2021 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 ### Clean up our environment
23 #
24 delete @ENV{qw(IFS CDPATH ENV BASH_ENV PATH)};
25 $< = $>;
26
27 # Store keys here
28 $ENV{"GNUPGHOME"} = "/opt/pakfire/etc/.gnupg";
29
30 require "/opt/pakfire/lib/functions.pl";
31
32 my $interactive = 1;
33 my $force = "noforce";
34 my $locked;
35
36 &Pakfire::logger("PAKFIRE INFO: IPFire Pakfire $Conf::version started!");
37
38 ### Check if we are running as root
39 #
40 my $user = qx(whoami);
41 chomp($user);
42 unless ( "$user" eq "root" ) {
43 &Pakfire::message("PAKFIRE ERROR: You must run pakfire as user root!");
44 exit 1;
45 }
46
47 unless ( -e "/var/ipfire/red/active" ) {
48 &Pakfire::message("PAKFIRE ERROR: You need to be online to run pakfire!");
49 exit 2;
50 }
51
52 # Check if a lockfile already exists.
53 if (-e "$Pakfire::lockfile") {
54 &Pakfire::message("PAKFIRE ERROR: Another instance of pakfire is already running!");
55 exit 1;
56 }
57
58 # Write lockfile.
59 open(LOCK, ">$Pakfire::lockfile");
60
61 # Pakfire has locked in this session set locket to "1".
62 $locked = "1";
63
64 # Close filehandle.
65 close(LOCK);
66
67 ### Check if we are started by another name
68 #
69 if ( $0 =~ /pakfire-update$/ ) {
70 &Pakfire::message("CRON INFO: Running an update");
71 my $random = int(rand(60));
72 &Pakfire::logger("CRON INFO: Waiting for $random seconds.");
73 sleep($random);
74 $ARGV[0] = "update";
75 $interactive = 0;
76 } elsif ( $0 =~ /pakfire-upgrade$/ ) {
77 &Pakfire::message("CRON INFO: Running an upgrade");
78 my $random = int(rand(3600));
79 &Pakfire::logger("CRON INFO: Waiting for $random seconds.");
80 sleep($random);
81 $ARGV[0] = "upgrade";
82 $interactive = 0;
83 }
84
85 unless (@ARGV) {
86 &Pakfire::usage;
87 }
88
89 foreach (@ARGV) {
90 if ("$_" =~ "^-") {
91 # Turn off interactive mode
92 $interactive = 0 if ("$_" eq "--non-interactive");
93 $interactive = 0 if ("$_" eq "-y");
94
95 # Turn off shell colors - Bad for displaying in webinterface
96 $Pakfire::enable_colors = 0 if ("$_" eq "--no-colors");
97
98 # Turn on force mode
99 $force = "force" if ("$_" eq "-f" );
100 $force = "force" if ("$_" eq "--force" );
101 }
102 }
103
104 if ("$ARGV[0]" eq "install") {
105 shift;
106
107 ### Make sure that the list is not outdated.
108 &Pakfire::dbgetlist("noforce");
109
110 my %paklist = &Pakfire::dblist("all");
111
112 my $dep;
113 my @deps;
114 my $pak;
115 my @paks;
116 my @temp;
117 my $return;
118 my @all;
119 foreach $pak (@ARGV) {
120 unless ("$pak" =~ "^-") {
121 if (defined $paklist{$pak}) {
122 if ("$paklist{$pak}{'Installed'}" eq "yes") {
123 &Pakfire::message("PAKFIRE INFO: $pak is already installed");
124 next;
125 }
126 push(@paks,$pak);
127 push(@all,$pak);
128 @temp = &Pakfire::resolvedeps("$pak");
129 foreach $dep (@temp) {
130 push(@deps,$dep) if $dep;
131 push(@all,$dep) if $dep;
132 }
133 } else {
134 &Pakfire::message("");
135 &Pakfire::message("PAKFIRE WARN: The pak \"$pak\" is not known. Please try running \"pakfire update\".");
136 }
137 }
138 }
139
140 unless (@paks) {
141 &Pakfire::message("PAKFIRE ERROR: No packages to install. Exiting...");
142 exit 1;
143 }
144
145 if (@deps) {
146 my %sort = map{ $_, 1 } @deps;
147 @deps = keys %sort;
148 &Pakfire::message("");
149 &Pakfire::message("PAKFIRE INFO: Packages to install for dependencies:");
150 }
151 foreach $dep (@deps) {
152 my $size = &Pakfire::getsize("$dep");
153 $size = &Pakfire::beautifysize($size);
154 &Pakfire::message("PAKFIRE INFO: $dep \t - $size");
155 }
156
157 &Pakfire::message("");
158 &Pakfire::message("");
159 &Pakfire::message("PAKFIRE INFO: Packages to install:");
160 foreach $pak (@paks) {
161 my $size = &Pakfire::getsize("$pak");
162 $size = &Pakfire::beautifysize($size);
163 &Pakfire::message("PAKFIRE INFO: $pak \t - $size");
164 }
165
166 my $totalsize;
167 foreach $pak (@all) {
168 $totalsize = ($totalsize + &Pakfire::getsize("$pak"));
169 }
170 $totalsize = &Pakfire::beautifysize($totalsize);
171 &Pakfire::message("");
172 &Pakfire::message("PAKFIRE INFO: Total size: \t ~ $totalsize");
173 &Pakfire::message("");
174
175 if ($interactive) {
176 &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
177 my $ret = <STDIN>;
178 chomp($ret);
179 &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
180 if ( $ret ne "y" ) {
181 &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
182 exit 1;
183 }
184 } else {
185 &Pakfire::logger("PAKFIRE INFO: Interaction skipped.");
186 }
187
188 # my %sort = map{ $_, 1 } @all;
189 # @all = sort keys %sort;
190
191 ### Download first
192 foreach $pak (@all) {
193 &Pakfire::getpak("$pak", "");
194 }
195
196 &Pakfire::message("");
197
198 foreach $pak (@deps) {
199 &Pakfire::setuppak("$pak") if ($pak ne "");
200 }
201
202
203 foreach $pak (@paks) {
204 &Pakfire::setuppak("$pak") if ($pak ne "");
205 }
206
207
208 } elsif ("$ARGV[0]" eq "remove") {
209 shift;
210
211 my @paks;
212 my $pak;
213 foreach $pak (@ARGV) {
214 unless ("$pak" =~ "^-") {
215 $return = &Pakfire::isinstalled($pak);
216 if ($return ne 0) {
217 &Pakfire::message("PAKFIRE WARN: $pak is not installed");
218 next;
219 }
220 push(@paks, $pak);
221 }
222 }
223
224 unless (@paks) {
225 &Pakfire::message("PAKFIRE ERROR: No packages to remove. Exiting...");
226 exit 1;
227 }
228
229 &Pakfire::message("");
230 &Pakfire::message("");
231 &Pakfire::message("PAKFIRE INFO: Packages to remove:");
232 foreach $pak (sort @paks) {
233 my $size = &Pakfire::getsize("$pak");
234 $size = &Pakfire::beautifysize($size);
235 &Pakfire::message("PAKFIRE INFO: $pak \t - $size");
236 }
237
238 if ($interactive) {
239 &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
240 my $ret = <STDIN>;
241 chomp($ret);
242 &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
243 if ( $ret ne "y" ) {
244 &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
245 exit 1;
246 }
247 }
248
249 foreach $pak (@paks) {
250 &Pakfire::removepak("$pak");
251 }
252
253
254 } elsif ("$ARGV[0]" eq "update") {
255 &Pakfire::makeuuid();
256 &Pakfire::getmirrors("$force");
257 &Pakfire::dbgetlist("$force");
258 &Pakfire::getcoredb("$force");
259
260 } elsif ("$ARGV[0]" eq "upgrade") {
261 my $use_color = "";
262 my $reset_color = "";
263
264 if ("$Pakfire::enable_colors" eq "1") {
265 $reset_color = "$Pakfire::color{'normal'}";
266 $use_color = "$Pakfire::color{'lightpurple'}";
267 }
268
269 &Pakfire::message("CORE INFO: Checking for Core-Updates...");
270
271 ### Make sure that the core db is not outdated.
272 &Pakfire::getcoredb("noforce");
273 my %coredb = &Pakfire::coredbinfo();
274
275 if (defined $coredb{'AvailableRelease'}) {
276 &Pakfire::upgradecore();
277 } else {
278 &Pakfire::message("CORE INFO: No new Core-Updates available. You are on release ".$coredb{'Release'});
279 }
280
281 &Pakfire::message("PAKFIRE INFO: Checking for package updates...");
282 ### Make sure that the package list is not outdated.
283 &Pakfire::dbgetlist("noforce");
284
285 my @deps = ();
286 if (my %upgradepaks = &Pakfire::dblist("upgrade")) {
287 # Resolve the dependencies of the to be upgraded packages
288 @deps = &Pakfire::resolvedeps_recursive(keys %upgradepaks);
289
290 foreach $pak (sort keys %upgradepaks) {
291 print "${use_color}Update: $pak\nVersion: $upgradepaks{$pak}{'ProgVersion'} -> $upgradepaks{$pak}{'AvailableProgVersion'}\n";
292 print "Release: $upgradepaks{$pak}{'Release'} -> $upgradepaks{$pak}{'AvailableRelease'}${reset_color}\n";
293 }
294 &Pakfire::message("");
295 &Pakfire::message("PAKFIRE UPGR: We are going to install all packages listed above.");
296 if ($interactive) {
297 &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
298 my $ret = <STDIN>;
299 chomp($ret);
300 &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
301 if ( $ret ne "y" ) {
302 &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
303 exit 1;
304 }
305 }
306
307 # Download packages
308 foreach $pak (sort keys %upgradepaks) {
309 &Pakfire::getpak("$pak", "");
310 }
311
312 # Download dependencies
313 foreach $pak (@deps) {
314 &Pakfire::getpak("$pak", "");
315 }
316
317 # Install dependencies first
318 foreach $pak (@deps) {
319 &Pakfire::setuppak("$pak");
320 }
321
322 # Install all upgrades
323 foreach $pak (sort keys %upgradepaks) {
324 &Pakfire::upgradepak("$pak");
325 }
326 } else {
327 &Pakfire::message("PAKFIRE WARN: No new package upgrades available.");
328 }
329
330 } elsif ("$ARGV[0]" eq "list") {
331 my $count;
332 my $use_color = "";
333 my $reset_color = "";
334 my $filter = "all";
335
336 if ("$ARGV[1]" =~ /installed|notinstalled/) {
337 $filter = "$ARGV[1]";
338 } else {
339 &Pakfire::message("PAKFIRE WARN: Not a known option $ARGV[1]") if ($ARGV[1]);
340 }
341
342 my $pak;
343 my %paklist = &Pakfire::dblist($filter);
344
345 if ("$Pakfire::enable_colors" eq "1") {
346 $reset_color = "$Pakfire::color{'normal'}";
347 $use_color = "$Pakfire::color{'lightgreen'}";
348 }
349
350 foreach $pak (sort keys %paklist) {
351 if ("$Pakfire::enable_colors" eq "1") {
352 if ("$paklist{$pak}{'Installed'}" eq "yes") {
353 if (defined $paklist{$pak}{'AvailableProgVersion'}) {
354 $use_color = "$Pakfire::color{'lightgreen'}";
355 } else {
356 $use_color = "$Pakfire::color{'green'}";
357 }
358 } else {
359 $use_color = "$Pakfire::color{'red'}";
360 }
361 }
362
363 print "${use_color}Name: $pak\nProgVersion: $paklist{$pak}{'ProgVersion'}\n";
364 print "Release: $paklist{$pak}{'Release'}\nInstalled: $paklist{$pak}{'Installed'}\n";
365 if (defined $paklist{$pak}{'AvailableProgVersion'}) {
366 print "Update available:\n Version: $paklist{$pak}{'ProgVersion'} -> $paklist{$pak}{'AvailableProgVersion'}\n Release: $paklist{$pak}{'Release'} -> $paklist{$pak}{'AvailableRelease'}\n";
367 }
368 print "${reset_color}\n";
369
370 }
371
372 $count = keys %paklist;
373 if ($count > 0) {
374 print "$count packages total.\n";
375 } else {
376 &Pakfire::message("PAKFIRE WARN: No packages where found using filter $filter.");
377 }
378 } elsif ("$ARGV[0]" eq "resolvedeps") {
379 foreach (@ARGV) {
380 next if ("$_" eq "resolvedeps");
381 next if ("$_" =~ "^-");
382 &Pakfire::resolvedeps("$_");
383 }
384 } elsif ("$ARGV[0]" eq "enable") {
385 if ("$ARGV[1]" eq "updates") {
386 system("ln -s ../../opt/pakfire/pakfire /etc/fcron.daily/pakfire-update");
387 } elsif ("$ARGV[1]" eq "upgrades") {
388 system("ln -s ../../opt/pakfire/pakfire /etc/fcron.daily/pakfire-upgrade");
389 }
390 } elsif ("$ARGV[0]" eq "disable") {
391 if ("$ARGV[1]" eq "updates") {
392 system("rm -f /etc/fcron.daily/pakfire-update");
393 } elsif ("$ARGV[1]" eq "upgrades") {
394 system("rm -f /etc/fcron.daily/pakfire-upgrade");
395 }
396 } elsif ("$ARGV[0]" eq "status") {
397 &Pakfire::status;
398 } else {
399 &Pakfire::usage;
400 }
401
402 &Pakfire::logger("PAKFIRE INFO: Pakfire has finished. Closing.");
403
404 END {
405 # Check if pakfire has been locked in this session.
406 if ($locked) {
407 # Remove lockfile.
408 unlink($Pakfire::lockfile);
409 }
410 }
411
412 exit 0;