chiark / gitweb /
www-master: add libsoap-lite-perl
[dsa-metapackages.git] / apt-in-chroot
1 #!/bin/bash
2
3 #  apt-in-chroot - runs apt in a specified chroot
4
5 #  Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.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 2, or (at your option)
10 #  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, write to the Free Software Foundation,
19 #  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
20
21
22 VALID_CHROOTS="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /usr/bin/tr ' ' '\n')"
23 VALID_CHROOTS_PARSE="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /bin/sed -e 's/ /|/g')"
24
25 usage() {
26         bn="`basename "$0"`"
27         echo "Usage: $bn <chroot> <apt sub-command> <package>"
28         echo ""
29         echo "  allowed chroots are ($VALID_CHROOTS_PARSE)"
30         echo ""
31         echo "  allowed apt sub-commands are:"
32         echo "          + install"
33         echo "          + remove"
34         echo "          + purge"
35         echo "          + build-dep"
36 }
37
38
39 if [ "$#" -lt "3" ]; then
40         usage >&2
41         exit 1
42 fi
43
44 CHROOT_DIR="/chroot"
45 CHROOT=$1; shift
46 APTCMD=$1; shift
47 PACKAGES=$*
48
49
50 case "$APTCMD" in
51 install|remove|purge|build-dep)
52         # those are the allowed apt sub-commands
53         ;;
54 *)
55         echo "$APTCMD is not a valid apt sub-command"
56         exit 1
57         ;;
58 esac
59
60 grep -qFx $CHROOT <<< "$VALID_CHROOTS"
61
62 RET=$?
63
64 if [ "$RET" == "0" ]; then
65         # valid chroot
66         echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES'"
67         /usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES
68 else
69         echo "$CHROOT not in $VALID_CHROOTS_PARSE"
70         exit 1
71 fi
72
73