File: install_packages

package info (click to toggle)
fai 2.8.4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,284 kB
  • ctags: 176
  • sloc: sh: 3,362; perl: 1,591; makefile: 142
file content (418 lines) | stat: -rwxr-xr-x 12,473 bytes parent folder | download
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/perl

# $Id: install_packages 3033 2005-11-10 23:28:59Z lange $
#*********************************************************************
#
# install_packages -- read package config and install packages via apt-get
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2000-2005, Thomas Lange, lange@informatik.uni-koeln.de
#
#*********************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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; see the file COPYING. If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
#*********************************************************************

my $version = "Version 2.15, 11-nov-2005";
$0=~ s#.+/##; # remove path from program name

# import variables: $verbose, $MAXPACKAGES, $classes, $FAI, $FAI_ROOT

use strict;
use Getopt::Std;
use AptPkg::Config '$_config';
use AptPkg::System '$_system';
use AptPkg::Cache;

# global variables
our ($opt_d,$opt_l,$opt_L,$opt_v,$opt_h,$opt_t,$opt_m);
my $listonly; # flag, that indicates that only a list of packages will be printed
our @commands;
our %command;
our $atype;    # type of action (for apt-get, aptitude,..) that will be performed
my $test;
my $verbose;
my $FAI_ROOT;
my @classes;
my $classpath;
my $rootcmd;
my @preloadlist;
my @preloadrmlist;
my %list;
my %classisdef;
my $maxpl;  # maximum length of package list
my $cache;  # instance of AptPkg::Cache
my @unknown; # unknown packages
my @known;   # list of all known packages

# PRELOAD feature from Thomas Gebhardt  <gebhardt@hrz.uni-marburg.de>

$| = 1;
# order of commands to execute

my $aptopt='-y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"';

@commands = qw/hold taskrm taskinst clean aptitude-r aptitude install remove dselect-upgrade/;
%command = (
	    install => "apt-get $aptopt --force-yes --fix-missing install",
	   ninstall => "apt-get $aptopt --force-yes --fix-missing -s install",
	    remove  => "apt-get $aptopt --purge remove",
  "dselect-upgrade" => "apt-get $aptopt dselect-upgrade",
         "taskinst" => "tasksel -n install",
           "taskrm" => "tasksel -n remove",
             "hold" => "dpkg --set-selections",
            "clean" => "apt-get clean",
         "aptitude" => "aptitude $aptopt install",
       "aptitude-r" => "aptitude -r $aptopt install",
          "pending" => "dpkg --configure --pending",
            "dpkgc" => "dpkg -C",
);

$test = 0;
getopts('dthvlLm:');

$listonly = $opt_l || $opt_L;
$opt_h && usage();
$opt_t and $test = 1;
$verbose=$ENV{verbose} || $opt_v;
$opt_d && setdownloadonly();
$maxpl=$ENV{MAXPACKAGES} || $opt_m ;
$maxpl && $verbose && warn "Maxmimum number of packages installed at a time set to $maxpl\n";
$maxpl or $maxpl = 99 ; # set default value

my $qopt="-qq" unless $verbose;
my $devnull=">/dev/null" unless $verbose;

$FAI_ROOT = $ENV{FAI_ROOT};
$classpath = "$ENV{FAI}/package_config";
$rootcmd = ($FAI_ROOT eq "/" ) ? '' : "chroot $FAI_ROOT";
@classes = grep { !/^#|^\s*$/ } split(/[\s\n]+/,$ENV{classes});
foreach (@classes) { $classisdef{$_}=1;}

$_config->init;                 # initialize AptPkg
$_config->set("Dir",$FAI_ROOT); # simulate "chroot"
$_config->{quiet}=2;            # don't show cache initialization messages
$_system = $_config->system;
$cache = new AptPkg::Cache;

# read all package config files
foreach (@classes) {
  my $filename = "$classpath/$_";
  &readconfig($filename) if -f $filename;
}

# get files which must exist before installing packages
foreach my $entry (@preloadlist,@preloadrmlist) {
  my ($url, $directory) = @$entry;
  if ($url =~ m!^file:/(.+)!) {
    my $file = $1;
    execute("cp $FAI_ROOT/$file $FAI_ROOT/$directory") unless $listonly;
  } else {
    execute("wget -nv -P$FAI_ROOT/$directory $url") unless $listonly;
  }
}

# call apt-get or tasksel for each type of command whith the list of packages
foreach $atype (@commands) {
  if ($atype eq "clean") {
    execute("$rootcmd $command{clean}") unless $listonly;
    next;
  }

  # skip if empty list
  next unless defined $list{$atype};

  if ($atype eq "dselect-upgrade") {
    dselectupgrade($atype);
    next;
  }

  my $packlist = join(' ',@{$list{$atype}});

  if ($atype eq "hold") {
    my $hold = join " hold\n", @{$list{hold}};
    $hold .= " hold\n";
    execute("echo \"$hold\" | $rootcmd $command{hold}");
    next;
  }

  if ($atype eq "install" || $atype eq "aptitude" || $opt_l || $opt_L) {

    mkpackagelist(@{$list{$atype}}); # create lists of known and unknown packages
    if ($opt_l) {
      # only print the package list
      print join ' ',@known,"\n";
      exit 0;
    }
    if ($opt_L) {
      # only print the package list
      execute("$rootcmd $command{ninstall} @known | egrep ^Inst");
      exit 0;
    }
    # pass only maxpl packages to apt-get
    while (@known) {
      my $shortlist = join(' ', splice @known,0,$maxpl);
      # print "SL $shortlist\n";
      execute("$rootcmd $command{$atype} $shortlist") if $shortlist;
      execute("$rootcmd $command{clean}"); # XXX do not execute always
    }
    next;
  }

  if ($atype eq "taskinst" || $atype eq "taskrm") {
    foreach my $tsk (@{$list{$atype}}) {
      execute("$rootcmd $command{$atype} $tsk");
    }
    next;
  }

  # other types
  execute("$rootcmd $command{$atype} $packlist") if $packlist;
}

# remove preloaded files
foreach my $entry (@preloadrmlist) {
  my ($url, $directory) = @$entry;
  $url =~ m#/([^/]+$)#;
  my $file =  "$directory/$1";
  print "rm $file\n" if $verbose;
  unlink $file || warn "Can't remove $file\n";
}


# in case of unconfigured packages because of apt errors
# retry configuration
execute("$rootcmd $command{pending}");
# check if all went right
execute("$rootcmd $command{dpkgc}");
# clean apt cache
execute("$rootcmd $command{clean}");
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub readconfig {

  my $filename = shift;
  my ($package,$type,$cllist,@oclasses,$doit);

  open (FILE,"$filename") || warn "ERROR $0: Can't read config file: $filename\n";
  warn "$0: read config file $filename\n" if $verbose;

  while (<FILE>) {
    next if /^#/;    # skip comments
    s/#.*$//;        # delete comments
    next if /^\s*$/; # skip empty lines
    chomp;
    /^PRELOAD\s+(\S+)\s+(\S+)/   and push(@preloadlist,   [$1,$2]),next;
    /^PRELOADRM\s+(\S+)\s+(\S+)/ and push(@preloadrmlist, [$1,$2]),next;

    if (/^PACKAGES\s+(\S+)\s*/) {
      ($type,$cllist) = ($1,$');
      # by default no classes are listed after this command so doit
      $doit = 1;
      if ($cllist) {
	# no classes specified after PACKAGES command
      # so add all packages listed
	# use packages on for a list of classes
	$doit = 0; # assume no class is defined
	@oclasses = split(/\s+/,$cllist);
	# if a listed class is defined, add the packaes, otherwise skip these packages
	foreach (@oclasses) { exists $classisdef{$_} and $doit = 1;}
      }
      next;
    }

    warn "PACKAGES .. line missing in $filename\n",next unless $type;
    push @{$list{$type}}, split if $doit;
  }
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub execute {

  # execute a command or only print it
  my @cmds = @_;

  # TODO: split cmds into array except when a pipe is found

  my $command = join (' ',@cmds);
  my $error;

  $test and $verbose = 1;
  $verbose and warn "$0: executing $command\n";
  $test and return;

  # @cmds should me more efficient
  $error = system @cmds;
  warn "ERROR: $error $?\n" if $error;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub dselectupgrade {

  my $type = shift;
  my ($package,$action,$list);
  my $tempfile = "$FAI_ROOT/tmp/dpkg-selections.tmp"; # TODO: use better uniq filename
  while (@{$list{$type}}) {
    $package = shift @{$list{$type}};
    $action  = shift @{$list{$type}};
    $list .= "$package $action\n";
  }

  open TMP,"> $tempfile" || die " Can't write to $tempfile";
  print TMP $list;
  close TMP;

  execute("$rootcmd dpkg --set-selections < $tempfile");
  execute("$rootcmd $command{$type}");
  unlink $tempfile;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub installable {
  # ckeck, wether package can be installed.
  # return false for packages which only exist in 
  # dpkg status files

  my $package  = shift;
  # print "Checking wether $package is installable...\n";

  # version list of package
  my $vlist = $cache->{$package}{VersionList};

  # for each version
  foreach my $version (@$vlist) {
    # list of package files
    my $flist = $version->{FileList};
    foreach my $fitem (@$flist) {
      # if the file is a package index (not a dpkg status file)
      # the package is installable 
      return 1 if ($fitem->{File}{IndexType} eq "Debian Package Index");
    }
  }

  # no package index file found for the current package
  return 0;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub mkpackagelist {

  my @complete = @_; # copy original list
  my @orig = ();
  @unknown = @known = ();
  my %complete = ();

  # create two list of packages
  # @unknown contains the unknown packages
  # @known contains the known packages

  # ignore packages ending with - when using -d
  @complete = grep {!/-$/} @complete if $opt_d; 

  foreach (reverse @complete) {
    my $pack;
    if ( /^(.*)[_=+-]$/ and $1 and $cache->exists($1)) {
      $pack = $1;
    } else {
      $pack = $_;
    }
    if (! defined ($complete{$pack})) {
      $complete{$pack} = 1;
      unshift @orig, $_;
    }
  }

  # currently we disable the complete checking of package names if aptitude
  # is used. This is because we can't check task names, which are supported by aptitude
  if ($atype eq "aptitude") {
    @known=@orig;
    return;
  }

  foreach my $pack (@orig) {

    if ($pack =~ /[$\`]/) {
      # evaluate via shell if backtick or $ is found
      my $packeval=`echo -n $pack`;
      print " Selecting $packeval for $pack";
      $pack=$packeval;
    }

#     if ($atype eq "aptitude" && $pack =~ /^~/) {
#       # add to known if is it an aptitude search pattern
#       push @known, $pack;
#       next;
#     }
# TODO: aptitude also can install tasks. How do we check them? Currently they will push to @unknown

    if ($cache->exists($pack)) { # simple package name
      # check for packages with no installation candidates
      # explicitely don't check if the name is provided by some other package:
      # apt-get install <virtual> fails with newer apt

      if (installable($pack)) {
	push @known, $pack;
      } else {
	push @unknown, $pack;
      }

    }

    # simulate APTs and aptitudes understanding of "special" package names
    # suffixed by -, +
    elsif ( $pack =~ /^(.*)[_=+-]$/ and $cache->exists($1)) {

      if (defined ($cache->{$1}{VersionList})) {
	push @known, $pack;
      } else {
	push @unknown, $pack;
      }

    } else {
      push @unknown, $pack;
    }
  }
  warn "WARNING: These unknown packages are removed from the installation list: " . join(' ',sort @unknown) . "\n" if @unknown;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub usage {

  print << "EOF";
install_packages $version

 Please read the manual pages install_packages(8).
EOF
  exit 0;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub setdownloadonly {

# Definitions for install_packages(8) for download only mode
# Used by fai-mirror(1)

# we are using a variable from %ENV
# for debugging remove >/dev/null and -q

undef @commands;
undef %command;

$maxpl=9999;
@commands = qw/taskinst aptitude install/;
%command = (
	    install => "apt-get $qopt -d $ENV{aptoptions} -y --force-yes --fix-missing install",
         "taskinst" => "aptitude -d $ENV{aptoptions} -y install $devnull",
         "aptitude" => "aptitude -d $ENV{aptoptions} -y install $devnull",
            "clean" => 'true',
          "pending" => 'true',
            "dpkgc" => 'true',
);

}
# - - - - - - - - - - - - - - - - - - - - - - - - - - -