]> git.proxmox.com Git - aab.git/blame - aab
aab: add use local lib
[aab.git] / aab
CommitLineData
7b25f331
WB
1#!/usr/bin/perl
2
3use strict;
4use warnings;
9cf81063
TL
5
6use lib '.';
7
7b25f331
WB
8use Getopt::Long;
9
10use PVE::AAB;
11
12$ENV{'LC_ALL'} = 'C';
13
1e25020c
TL
14my $cmds = {
15 basedir => 'Print rootfs path of CT appliance currently build',
16 bootstrap => 'Bootstrap the base system',
17 clean => '',
18 'dist-clean' => '',
19 enter => 'Enter container with shell',
20 exec => 'Execute command in container context',
21 finalize => 'Build final appliance archive',
22 help => 'This message',
23 init => 'Initial checks and pacman config generation',
24 install => 'Install specified package(s) and its dependencies',
25 keyring => 'Populate pacman keyring in CT',
26 list => 'List installed packages',
27 packagefile => 'Get name of resulting appliance archive',
28 veid => 'Print current VE-ID',
29};
30
7b25f331
WB
31sub print_usage {
32 my ($msg) = @_;
33
34 if ($msg) {
35 print STDERR "ERROR: $msg\n";
36 }
1e25020c
TL
37 print STDERR "$0 <command> [parameters]\n\n";
38
39 for my $cmd (sort keys %$cmds) {
40 if (my $desc = $cmds->{$cmd}) {
41 my $tabs = length($cmd) > 7 ? "\t" : "\t\t";
42 print STDERR "\t$cmd$tabs-- $desc\n";
43 } else {
44 print STDERR "\t$cmd\n";
45 }
46 }
7b25f331
WB
47}
48
49$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub {
50 die "interrupted by signal\n";
51};
52
53my $aab = PVE::AAB->new();
54
55$aab->writelog("aab: " . join(' ', @ARGV) . "\n");
56
57my $cmd = shift @ARGV;
58if (!$cmd) {
59 print_usage('missing command');
60 exit -1;
61}
62
63eval {
1e25020c
TL
64 if ($cmd eq 'help') {
65 print_usage();
66 } elsif ($cmd eq 'init') {
7b25f331
WB
67
68 die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0;
69 $aab->initialize();
70
71 } elsif ($cmd eq 'bootstrap') {
72
73 my ($datadir, $keep);
74 if (!GetOptions('datadir=s' => \$datadir,
75 'keep' => \$keep)) {
76 print_usage();
77 exit -1;
78 }
79
80 my $here = "$aab->{working_dir}/scripts/init.bash";
81 if (!$datadir && -f $here) {
82 print "Using current working directory as data directory\n";
83 $datadir = $aab->{working_dir};
84 }
85
86 PVE::AAB::setup_defaults($datadir) if $datadir;
87 $aab->ve_init() if !$keep;
88 $aab->bootstrap();
89
766f0fa3
WB
90 } elsif ($cmd eq 'keyring') {
91 # for debugging:
92
93 die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0;
94 $aab->populate_keyring();
95
7b25f331
WB
96 } elsif ($cmd eq 'basedir') {
97
98 die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0;
99 print $aab->{rootfs} . "\n";
100
101 } elsif ($cmd eq 'veid') {
102
103 die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0;
104 print $aab->{veid} . "\n";
105
106 } elsif ($cmd eq 'packagefile') {
107
108 die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0;
109
110 print "$aab->{targetname}.tar.gz\n";
111
112 } elsif ($cmd eq 'finalize') {
113
114 $aab->finalize();
115
116 } elsif ($cmd eq 'install') {
117
118 my $required;
119 foreach my $arg (@ARGV) {
120 if ($arg =~ m/\.pkglist$/) {
121 open my $fh, $arg or
122 die "cant open package list '$arg' - $!";
123 while (defined (my $line = <$fh>)) {
124 chomp $line;
125 next if $line =~ m/^\s*$/;
126 next if $line =~ m/\#/;
127 if ($line =~ m/^\s*(\S+)\s*$/) {
128 push @$required, $1;
129 } else {
130 die "invalid package name in '$arg' - $line\n";
131 }
132 }
133 close ($fh);
134 } else {
135 push @$required, $arg;
136 }
137 }
138
139 $aab->install ($required);
140
141 } elsif ($cmd eq 'exec') {
142
143 $aab->ve_exec (@ARGV);
144
145 } elsif ($cmd eq 'enter') {
146
147 $aab->enter();
148
149 } elsif ($cmd eq 'clean') {
150
151 $aab->clean();
152
153 } elsif ($cmd eq 'dist-clean') {
154
155 $aab->clean(1);
156
157 } elsif ($cmd eq 'list') {
158
159 my $verbose;
160
161 if (!GetOptions ('verbose' =>\$verbose)) {
162 print_usage ();
163 exit (-1);
164 }
165
166 die "command '$cmd' expects no arguments.\n" if scalar (@ARGV) != 0;
167
168 my $query = '-Q';
169 $query .= 'q' if !$verbose;
170 print $aab->run_command(['chroot', $aab->{rootfs}, 'pacman', $query], undef, 1);
171
172 } else {
173
174 print_usage("invalid command '$cmd'");
175 exit (-1);
176
177 }
178};
179
180if (my $err = $@) {
181 $aab->logmsg($err);
182 die $err;
183}
184
185exit 0;