#!/usr/bin/perl use strict; use warnings; use lib '.'; use Getopt::Long; use PVE::AAB; $ENV{'LC_ALL'} = 'C'; my $cmds = { basedir => 'Print rootfs path of CT appliance currently build', bootstrap => 'Bootstrap the base system', clean => '', 'dist-clean' => '', enter => 'Enter container with shell', exec => 'Execute command in container context', finalize => 'Build final appliance archive', help => 'This message', init => 'Initial checks and pacman config generation', install => 'Install specified package(s) and its dependencies', keyring => 'Populate pacman keyring in CT', list => 'List installed packages', packagefile => 'Get name of resulting appliance archive', veid => 'Print current VE-ID', }; sub print_usage { my ($msg) = @_; if ($msg) { print STDERR "ERROR: $msg\n"; } print STDERR "$0 [parameters]\n\n"; for my $cmd (sort keys %$cmds) { if (my $desc = $cmds->{$cmd}) { my $tabs = length($cmd) > 7 ? "\t" : "\t\t"; print STDERR "\t$cmd$tabs-- $desc\n"; } else { print STDERR "\t$cmd\n"; } } } $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; my $aab = PVE::AAB->new(); $aab->writelog("aab: " . join(' ', @ARGV) . "\n"); my $cmd = shift @ARGV; if (!$cmd) { print_usage('missing command'); exit -1; } eval { if ($cmd eq 'help') { print_usage(); } elsif ($cmd eq 'init') { die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0; $aab->initialize(); } elsif ($cmd eq 'bootstrap') { my ($datadir, $keep); if (!GetOptions('datadir=s' => \$datadir, 'keep' => \$keep)) { print_usage(); exit -1; } my $here = "$aab->{working_dir}/scripts/init.bash"; if (!$datadir && -f $here) { print "Using current working directory as data directory\n"; $datadir = $aab->{working_dir}; } PVE::AAB::setup_defaults($datadir) if $datadir; $aab->ve_init() if !$keep; $aab->bootstrap(); } elsif ($cmd eq 'keyring') { # for debugging: die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0; $aab->populate_keyring(); } elsif ($cmd eq 'basedir') { die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0; print $aab->{rootfs} . "\n"; } elsif ($cmd eq 'veid') { die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0; print $aab->{veid} . "\n"; } elsif ($cmd eq 'packagefile') { die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0; print "$aab->{targetname}.tar.gz\n"; } elsif ($cmd eq 'finalize') { $aab->finalize(@ARGV); } elsif ($cmd eq 'install') { my $required; foreach my $arg (@ARGV) { if ($arg =~ m/\.pkglist$/) { open my $fh, $arg or die "cant open package list '$arg' - $!"; while (defined (my $line = <$fh>)) { chomp $line; next if $line =~ m/^\s*$/; next if $line =~ m/\#/; if ($line =~ m/^\s*(\S+)\s*$/) { push @$required, $1; } else { die "invalid package name in '$arg' - $line\n"; } } close ($fh); } else { push @$required, $arg; } } $aab->install ($required); } elsif ($cmd eq 'exec') { $aab->ve_exec (@ARGV); } elsif ($cmd eq 'enter') { $aab->enter(); } elsif ($cmd eq 'clean') { $aab->clean(); } elsif ($cmd eq 'dist-clean') { $aab->clean(1); } elsif ($cmd eq 'list') { my $verbose; if (!GetOptions ('verbose' =>\$verbose)) { print_usage (); exit (-1); } die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0; my $query = '-Q'; $query .= 'q' if !$verbose; print $aab->run_command(['chroot', $aab->{rootfs}, 'pacman', $query], undef, 1); } else { print_usage("invalid command '$cmd'"); exit (-1); } }; if (my $err = $@) { $aab->logmsg($err); die $err; } exit 0;