-
Notifications
You must be signed in to change notification settings - Fork 2
/
yum-repo
executable file
·109 lines (81 loc) · 1.9 KB
/
yum-repo
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
set -e
err_report() {
echo "errexit on line $(caller)" >&2
}
trap err_report ERR
CURL_FLAGS="-LSsf"
SOURCES_DIR=/etc/yum.repos.d
GPGKEY_DIR=/etc/pki/rpm-gpg
BASENAME=$(basename $0)
usage() {
cat <<EOF
Usage: $BASENAME add REPO_NAME REPO_URL GPGKEY_URL
$BASENAME remove REPO_NAME
A utility to safely add a yum repository with a GPG signing key.
REPO_NAME is a unique local identifier for the repo.
REPO_URL is the URL of the repo.
GPGKEY_URL is a URL from which to download the signing key of the repo.
EOF
}
if [[ ! $2 ]]; then
usage
exit 1
fi
if ! which curl >&/dev/null; then
echo "This utility requires curl"
exit 1
fi
REPO_NAME="$2"
SOURCES_FILE="$SOURCES_DIR/$REPO_NAME".repo
GPGKEY_FILE="$GPGKEY_DIR/RPM-GPG-KEY-$REPO_NAME".gpg
case "$1" in
"add")
if [[ ! $4 || $5 ]]; then
usage
exit 1
fi
REPO_URL="$3"
GPGKEY_URL="$4"
if [[ -f $SOURCES_FILE || -f $GPGKEY_FILE ]]; then
echo "Unable to create files; repo already configured!"
exit 2
fi
DIST=$(yum repolist | awk '/^!?base/ {print $2}')
cat <<EOF >$SOURCES_FILE
# Created by $0 with: $BASENAME add $2 "$3" $4
# To clean up use: $BASENAME remove $2
[${REPO_NAME}]
name=${REPO_NAME} for ${DIST}
baseurl=${REPO_URL}
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file://${GPGKEY_FILE}
EOF
TMPFILE=$(mktemp)
curl $CURL_FLAGS -o $TMPFILE $GPGKEY_URL
gpg --no-default-keyring --keyring=$GPGKEY_FILE --import $TMPFILE
# This might leave a backup file; clean it up
rm "$GPGKEY_FILE~" || true
rm $TMPFILE
# fix permissions
chown root:root $GPGKEY_FILE $SOURCES_FILE
chmod 644 $GPGKEY_FILE $SOURCES_FILE
;;
"remove")
if [[ ! $4 || $5 ]]; then
usage
exit 1
fi
if [[ ! -f $SOURCES_FILE && ! -f $GPGKEY_FILE ]]; then
echo "Unable to delete files; repo not configured!"
exit 2
fi
rm $SOURCES_FILE $GPGKEY_FILE || true
;;
* )
usage
exit 2
;;
esac