-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathentrypoint.sh
123 lines (109 loc) · 2.84 KB
/
entrypoint.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# vim: ts=2 sw=2 sts=2 et
set -e -u
: ${REPO_HOST:=}
function get-repo-file {
local repo_url="$1"
cd /etc/yum.repos.d
rm -f *.repo
curl --remote-name --silent "${repo_url}"
echo $(pwd -P)/$(basename ${repo_url})
}
function copy-packages {
local repo_url=$1
shift
local repo_file=$(get-repo-file "${repo_url}")
get-urls $(repotrack -n --urls "$@")
create-repos $(grep 'baseurl=' ${repo_file} | cut -f3- -d'/')
copy-gpg-keys "${repo_file}"
rm -f "${repo_file}"
[[ -z ${REPO_HOST} ]] || generate-local-repo-file ${REPO_HOST} ${repo_url}
}
function copy-repo {
for repo_url in "$@"; do
local repo_file=$(get-repo-file "${repo_url}")
for line in $(perl -wnl -00 -e '($name, $path) = $_ =~ /\[(\S+)\].*baseurl=https?:\/\/(\S+)/s and print "$name]$path"' "${repo_file}"); do
local repo="$(echo ${line} | cut -f1 -d']')"
local path="$(echo ${line} | cut -f2 -d']')"
reposync -n -p /var/repo/${path} --norepopath -r "${repo}"
create-repos ${path}
done
copy-gpg-keys "${repo_file}"
rm -f "${repo_file}"
[[ -z ${REPO_HOST} ]] || generate-local-repo-file ${REPO_HOST} ${repo_url}
done
}
function copy-gpg-keys {
local repo_file=$1
get-urls $(grep 'gpgkey=' ${repo_file} | cut -f2 -d'=')
}
function create-repos {
for dir in "$@"; do
[[ -d ${dir} ]] || continue
echo "Creating repo in ${dir}"
createrepo -q --update /var/repo/${dir}
done
}
function get-urls {
for url in "$@"; do
local path=$(echo ${url} | cut -f3- -d'/')
if [[ -r ${path} ]]; then
local -i local_size=$(wc -c < ${path})
if [[ $local_size -gt 0 ]]; then
local -i remote_size=$(curl --silent --head ${url} | fgrep 'Content-Length' | cut -f2 -d' ' | tr -d '\r')
if [[ $local_size -eq $remote_size ]]; then
echo "Skipping ${url}"
continue
fi
fi
fi
echo "Downloading ${url}"
curl --create-dirs --output ${path} --remote-time --silent ${url}
done
}
function generate-local-repo-file {
local host=$1
[[ -n ${host} ]] || return 1
local url=$2
[[ -n ${url} ]] || return 2
local path=$(echo ${url} | cut -f3- -d'/')
mkdir -p $(dirname ${path})
curl --create-dirs --remote-time --silent ${url} | perl -wpl -e "s@https?://@\$&${host}/@g" > ${path}
}
function usage {
echo "Usage:"
echo " repo <repo_url> [<repo_url> ...]"
echo " repo-file <repo_host> <repo_url>"
echo " pkg <repo_url> <pkg_name> [<pkg_name> ...]"
echo " file <url> [<url> ...]"
}
cmd=${1:-help}
case ${cmd} in
pkg)
shift
copy-packages "$@"
;;
repo)
shift
copy-repo "$@"
;;
http*)
# backwards compatibility
copy-repo "$@"
;;
repo-file)
shift
if [[ $# -lt 2 ]]; then
usage
exit 1
fi
generate-local-repo-file "$@"
;;
file)
shift
get-urls "$@"
;;
*)
usage
;;
esac