]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/pakfire/pakfire
pakfire: Replace duplicate code with dblist functioncall
[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::upgradecore();
270
271 my @deps = ();
272 if (my %upgradepaks = &Pakfire::dblist("upgrade")) {
273 # Resolve the dependencies of the to be upgraded packages
274 @deps = &Pakfire::resolvedeps_recursive(keys %upgradepaks);
275
276 foreach $pak (sort keys %upgradepaks) {
277 print "${use_color}Update: $pak\nVersion: $upgradepaks{$pak}{'ProgVersion'} -> $upgradepaks{$pak}{'AvailableProgVersion'}\n";
278 print "Release: $upgradepaks{$pak}{'Release'} -> $upgradepaks{$pak}{'AvailableRelease'}${reset_color}\n";
279 }
280 &Pakfire::message("");
281 &Pakfire::message("PAKFIRE UPGR: We are going to install all packages listed above.");
282 if ($interactive) {
283 &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
284 my $ret = <STDIN>;
285 chomp($ret);
286 &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
287 if ( $ret ne "y" ) {
288 &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
289 exit 1;
290 }
291 }
292
293 # Download packages
294 foreach $pak (sort keys %upgradepaks) {
295 &Pakfire::getpak("$pak", "");
296 }
297
298 # Download dependencies
299 foreach $pak (@deps) {
300 &Pakfire::getpak("$pak", "");
301 }
302
303 # Install dependencies first
304 foreach $pak (@deps) {
305 &Pakfire::setuppak("$pak");
306 }
307
308 # Install all upgrades
309 foreach $pak (sort keys %upgradepaks) {
310 &Pakfire::upgradepak("$pak");
311 }
312 } else {
313 &Pakfire::message("PAKFIRE WARN: No new package upgrades available.");
314 }
315
316 } elsif ("$ARGV[0]" eq "list") {
317 my $count;
318 my $use_color = "";
319 my $reset_color = "";
320 my $filter = "all";
321
322 if ("$ARGV[1]" =~ /installed|notinstalled/) {
323 $filter = "$ARGV[1]";
324 } else {
325 &Pakfire::message("PAKFIRE WARN: Not a known option $ARGV[1]") if ($ARGV[1]);
326 }
327
328 my $pak;
329 my %paklist = &Pakfire::dblist($filter);
330
331 if ("$Pakfire::enable_colors" eq "1") {
332 $reset_color = "$Pakfire::color{'normal'}";
333 $use_color = "$Pakfire::color{'lightgreen'}";
334 }
335
336 foreach $pak (sort keys %paklist) {
337 if ("$Pakfire::enable_colors" eq "1") {
338 if ("$paklist{$pak}{'Installed'}" eq "yes") {
339 if (defined $paklist{$pak}{'AvailableProgVersion'}) {
340 $use_color = "$Pakfire::color{'lightgreen'}";
341 } else {
342 $use_color = "$Pakfire::color{'green'}";
343 }
344 } else {
345 $use_color = "$Pakfire::color{'red'}";
346 }
347 }
348
349 print "${use_color}Name: $pak\nProgVersion: $paklist{$pak}{'ProgVersion'}\n";
350 print "Release: $paklist{$pak}{'Release'}\nInstalled: $paklist{$pak}{'Installed'}\n";
351 if (defined $paklist{$pak}{'AvailableProgVersion'}) {
352 print "Update available:\n Version: $paklist{$pak}{'ProgVersion'} -> $paklist{$pak}{'AvailableProgVersion'}\n Release: $paklist{$pak}{'Release'} -> $paklist{$pak}{'AvailableRelease'}\n";
353 }
354 print "${reset_color}\n";
355
356 }
357
358 $count = keys %paklist;
359 if ($count > 0) {
360 print "$count packages total.\n";
361 } else {
362 &Pakfire::message("PAKFIRE WARN: No packages where found using filter $filter.");
363 }
364 } elsif ("$ARGV[0]" eq "resolvedeps") {
365 foreach (@ARGV) {
366 next if ("$_" eq "resolvedeps");
367 next if ("$_" =~ "^-");
368 &Pakfire::resolvedeps("$_");
369 }
370 } elsif ("$ARGV[0]" eq "enable") {
371 if ("$ARGV[1]" eq "updates") {
372 system("ln -s ../../opt/pakfire/pakfire /etc/fcron.daily/pakfire-update");
373 } elsif ("$ARGV[1]" eq "upgrades") {
374 system("ln -s ../../opt/pakfire/pakfire /etc/fcron.daily/pakfire-upgrade");
375 }
376 } elsif ("$ARGV[0]" eq "disable") {
377 if ("$ARGV[1]" eq "updates") {
378 system("rm -f /etc/fcron.daily/pakfire-update");
379 } elsif ("$ARGV[1]" eq "upgrades") {
380 system("rm -f /etc/fcron.daily/pakfire-upgrade");
381 }
382 } elsif ("$ARGV[0]" eq "status") {
383 &Pakfire::status;
384 } else {
385 &Pakfire::usage;
386 }
387
388 &Pakfire::logger("PAKFIRE INFO: Pakfire has finished. Closing.");
389
390 END {
391 # Check if pakfire has been locked in this session.
392 if ($locked) {
393 # Remove lockfile.
394 unlink($Pakfire::lockfile);
395 }
396 }
397
398 exit 0;