-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd2a6b1
commit bac8328
Showing
2 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#!/bin/sh | ||
|
||
set -e # Exit immediately if a command exits with a non-zero status. | ||
|
||
usage() { | ||
echo "usage: $(basename "$0") [--virtual NAME ] PKG [PKG...]" | ||
} | ||
|
||
trap : TERM QUIT INT | ||
trap "cleanup" EXIT | ||
|
||
# | ||
# Alpine functions | ||
# | ||
|
||
is_pkg_installed_alpine() { | ||
apk info 2>/dev/null | grep -v "^WARNING" | grep -q "^$1\$" | ||
} | ||
|
||
install_pkg_alpine() { | ||
VIRTUAL_PKG="$1" | ||
shift | ||
|
||
APK_EXTRA_OPTS= | ||
if [ "$VIRTUAL_PKG" -eq 1 ]; then | ||
APK_EXTRA_OPTS="--virtual" | ||
fi | ||
|
||
apk --no-cache add $APK_EXTRA_OPTS "$@" | ||
} | ||
|
||
cleanup_alpine() { | ||
rm -rf /var/cache/misc/* | ||
} | ||
|
||
# | ||
# Debian functions | ||
# | ||
|
||
is_pkg_installed_debian() { | ||
dpkg --status "$1" 2>&1 | grep -q "^Status: install ok installed" | ||
} | ||
|
||
install_pkg_debian() { | ||
VIRTUAL_PKG="$1" | ||
shift | ||
|
||
if [ "$VIRTUAL_PKG" -eq 1 ]; then | ||
VIRTUAL_PKG_NAME=$1 | ||
shift | ||
|
||
if [ -f /var/lib/apt/$VIRTUAL_PKG_NAME.virtual ]; then | ||
echo "ERROR: Virtual package '$VIRTUAL_PKG_NAME' already exists." | ||
exit 1 | ||
fi | ||
|
||
echo "$*" > /var/lib/apt/$VIRTUAL_PKG_NAME.virtual | ||
fi | ||
|
||
if [ -n "$1" ]; then | ||
apt-get -q update | ||
apt-get -q install -y --no-install-recommends "$@" | ||
fi | ||
} | ||
|
||
cleanup_debian() { | ||
apt-get -q clean | ||
rm -rf /var/lib/apt/lists/* \ | ||
/var/log/dpkg.log \ | ||
/var/log/alternatives.log \ | ||
/var/log/apt/* | ||
} | ||
|
||
# | ||
# OS agnostic functions | ||
# | ||
|
||
is_pkg_installed() { | ||
if [ -n "$(which apk)" ]; then | ||
is_pkg_installed_alpine "$@" | ||
else | ||
is_pkg_installed_debian "$@" | ||
fi | ||
} | ||
|
||
install_pkg() { | ||
if [ -n "$(which apk)" ]; then | ||
install_pkg_alpine "$@" | ||
else | ||
install_pkg_debian "$@" | ||
fi | ||
} | ||
|
||
cleanup() { | ||
if [ -n "$(which apk)" ]; then | ||
cleanup_alpine | ||
else | ||
cleanup_debian | ||
fi | ||
} | ||
|
||
# | ||
# Main | ||
# | ||
|
||
if [ -z "$1" ]; then | ||
usage | ||
exit 1 | ||
fi | ||
|
||
VIRTUAL_PKG= | ||
if [ "$1" = "--virtual" ]; then | ||
shift | ||
if [ -z "$1" ]; then | ||
echo "Virtual package name missing." | ||
usage | ||
exit 1 | ||
fi | ||
VIRTUAL_PKG="$1" | ||
shift | ||
fi | ||
|
||
if [ -n "$VIRTUAL_PKG" ]; then | ||
PKGS="$VIRTUAL_PKG" | ||
for PKG in "$@" | ||
do | ||
if ! is_pkg_installed $PKG; then | ||
PKGS="$PKGS $PKG" | ||
fi | ||
done | ||
install_pkg 1 $PKGS | ||
else | ||
install_pkg 0 "$@" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/bin/sh | ||
|
||
set -e # Exit immediately if a command exits with a non-zero status. | ||
|
||
usage() { | ||
echo "usage: $(basename "$0") PKG [PKG...]" | ||
} | ||
|
||
# | ||
# Alpine functions | ||
# | ||
|
||
del_pkg_alpine() { | ||
apk --no-cache del "$@" | ||
rm -rf /var/cache/misc/* | ||
} | ||
|
||
# | ||
# Debian functions | ||
# | ||
|
||
del_pkg_debian() { | ||
PKGS= | ||
for PKG in "$@" | ||
do | ||
if ! dpkg --status "$1" 2>&1 | grep -q "^Status: install ok installed"; then | ||
if [ -f /var/lib/apt/$PKG.virtual ]; then | ||
PKGS="$PKGS $(cat /var/lib/apt/$PKG.virtual)" | ||
rm /var/lib/apt/$PKG.virtual | ||
else | ||
PKGS="$PKGS $PKG" | ||
fi | ||
else | ||
PKGS="$PKGS $PKG" | ||
fi | ||
done | ||
|
||
if [ -n "$PKGS" ]; then | ||
apt-get -q purge -y $PKGS | ||
apt-get -q --purge autoremove -y | ||
rm -rf /var/log/dpkg.log \ | ||
/var/log/apt/* | ||
fi | ||
} | ||
|
||
# | ||
# OS agnostic functions | ||
# | ||
|
||
del_pkg() { | ||
if [ -n "$(which apk)" ]; then | ||
del_pkg_alpine "$@" | ||
else | ||
del_pkg_debian "$@" | ||
fi | ||
} | ||
|
||
# | ||
# Main | ||
# | ||
|
||
if [ -z "$1" ]; then | ||
usage | ||
exit 1 | ||
fi | ||
|
||
del_pkg "$@" |