-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapt-functions.sh
83 lines (73 loc) · 3.01 KB
/
apt-functions.sh
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
set -eo pipefail; [[ $TRACE ]] && set -x && export TRACE=$TRACE
export APTFILE_COLOR_OFF="\033[0m" # unsets color to term fg color
export APTFILE_RED="\033[0;31m" # red
export APTFILE_GREEN="\033[0;32m" # green
export APTFILE_YELLOW="\033[0;33m" # yellow
export APTFILE_MAGENTA="\033[0;35m" # magenta
export APTFILE_CYAN="\033[0;36m" # cyan
logfile=package-install
TMP_APTFILE_LOGFILE=$(mktemp "/tmp/${logfile}.XXXXXX") || {
log_fail "${APTFILE_RED}WARNING: Cannot create temp file using mktemp in /tmp dir ${APTFILE_COLOR_OFF}\n"
}
export TMP_APTFILE_LOGFILE="$TMP_APTFILE_LOGFILE"
trap 'rm -rf "$TMP_APTFILE_LOGFILE" > /dev/null' INT TERM EXIT
log_fail() {
[[ $TRACE ]] && set -x
echo -e "${APTFILE_RED}$*${APTFILE_COLOR_OFF}" 1>&2
[[ -f "$TMP_APTFILE_LOGFILE" ]] && echo -e "verbose logs:\n" 1>&2 && sed -e 's/^/ /' "$TMP_APTFILE_LOGFILE"
exit 1
}
log_info() {
[[ $TRACE ]] && set -x
echo -e "$@"
}
update() {
[[ $TRACE ]] && set -x
log_info "Running update"
sudo apt-get update > "$TMP_APTFILE_LOGFILE" 2>&1
[[ $? -eq 0 ]] || log_fail "Failed to run update"
}
package() {
[[ $TRACE ]] && set -x
[[ -z $1 ]] && log_fail "Please specify a package to install"
local pkg="$1"
sudo dpkg --force-confnew -s "$pkg" > "$TMP_APTFILE_LOGFILE" 2>&1 && log_info "${APTFILE_CYAN}[OK]${APTFILE_COLOR_OFF} package $pkg" && return 0
sudo apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" -qq -y install "$pkg"
[[ $? -eq 0 ]] || log_fail "${APTFILE_RED}[FAIL]${APTFILE_COLOR_OFF} package $pkg"
log_info "${APTFILE_GREEN}[NEW]${APTFILE_COLOR_OFF} package $pkg"
}
ppa() {
[[ $TRACE ]] && set -x
[[ -z $1 ]] && log_fail "Please specify a repository to setup"
local repo="$1"
if [[ -d /etc/apt/sources.list.d/ ]]; then
grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -q "$repo" && log_info "${APTFILE_CYAN}[OK]${APTFILE_COLOR_OFF} ppa $repo" && return 0
fi
repository "ppa:$1"
}
key() {
[[ $TRACE ]] && set -x
[[ -z $1 ]] && log_fail "Please specify a key to add"
local key="$1"
curl -fsSL "$key" | sudo apt-key add -
log_info "${APTFILE_CYAN}[OK]${APTFILE_COLOR_OFF} key $key"
}
repository() {
[[ $TRACE ]] && set -x
[[ -z $1 ]] && log_fail "Please specify a repository to setup"
local repo="$1"
if [[ -d /etc/apt/sources.list.d/ ]]; then
grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -Fq "$repo" && log_info "${APTFILE_CYAN}[OK]${APTFILE_COLOR_OFF} repository $repo" && return 0
fi
sudo add-apt-repository -y "$repo" > "$TMP_APTFILE_LOGFILE" 2>&1
[[ $? -eq 0 ]] || log_fail "${APTFILE_RED}[FAIL]${APTFILE_COLOR_OFF} repository $pkg"
update
log_info "${APTFILE_GREEN}[NEW]${APTFILE_COLOR_OFF} repository $repo"
}
debconf_selection() {
[[ $TRACE ]] && set -x
[[ -z $1 ]] && log_fail "Please specify a debconf line"
echo "$1" | sudo debconf-set-selections
[[ $? -eq 0 ]] || log_fail "${APTFILE_RED}[FAIL]${APTFILE_COLOR_OFF} debconf: $1"
log_info "${APTFILE_CYAN}[OK]${APTFILE_COLOR_OFF} set debconf line: $1"
}