-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathget_packages.sh
213 lines (168 loc) · 5.97 KB
/
get_packages.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
#
# Author: lework
# Desc: Download Packages With Dependencies Locally.
# Date: 2020/03/10
trap "echo -e '\n\033[0;31m[Error] stop container.\033[0m'; docker stop package; exit 1" ERR 2 3
set -o errexit # Exit on most errors (see the manual)
set -o errtrace # Make sure any error trap is inherited
set -o nounset # Disallow expansion of unset variables
set -o pipefail # Use last non-zero exit code in a pipeline
######################################################################################################
# environment configuration
######################################################################################################
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;36m'
PLAIN='\033[0m'
######################################################################################################
# function
######################################################################################################
echo_title() {
echo -e "${GREEN}$1${PLAIN}"
}
check_or_install_docker() {
if ! $(command -v docker > /dev/null 2>&1);then
[ ! -d "/etc/docker" ] && mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"live-restore": true,
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 10,
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": [
"http://dockerhub.azk8s.cn",
"http://hub-mirror.c.163.com"
]
}
EOF
echo_title "[Check] install docker-ce."
# curl -sSL https://get.docker.com/ | bash -s - --mirror AzureChinaCloud
if ! $(command -v curl > /dev/null 2>&1); then
echo -e "${RED}Error: curl command not found. You must be install curl command at first.${PLAIN}"
exit 1
fi
curl -sSL https://get.daocloud.io/docker | bash -s - --mirror AzureChinaCloud
fi
if ! $(docker version > /dev/null 2>&1); then
echo_title "[Check] start dockerd."
[ -f /var/run/docker.sock ] && rm -f /var/run/docker.sock
dockerd --config-file /etc/docker/daemon.json &> /dev/null &
sleep 5
fi
}
download_package_centos() {
docker_repo_mount="-v ${package_repo}:/etc/yum.repos.d/${package_name}.repo"
echo_title "[Docker] start container"
docker run --rm -tid --name ${docker_name} -v ${package_path}:${package_tmp_path} ${docker_repo_mount:-''} ${docker_image}
echo_title "\n[Docker] update repo cache"
$docker_exec sed -e 's!^#baseurl=!baseurl=!g' \
-e 's!^mirrorlist=!#mirrorlist=!g' \
-e 's!mirror.centos.org!mirrors.aliyun.com!g' \
-i /etc/yum.repos.d/CentOS-Base.repo
$docker_exec yum install -y epel-release >> /dev/null 2>&1
$docker_exec sed -e 's!^mirrorlist=!#mirrorlist=!g' \
-e 's!^#baseurl=!baseurl=!g' \
-e 's!^metalink!#metalink!g' \
-e 's!//download\.fedoraproject\.org/pub!//mirrors.aliyun.com!g' \
-e 's!http://mirrors\.aliyun!https://mirrors.aliyun!g' \
-i /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo
$docker_exec yum clean all > /dev/null 2>&1
$docker_exec yum makecache
echo_title "\n[Docker] download package"
$docker_exec yum install -y --downloadonly --downloaddir=${package_tmp_path} ${packages}
echo_title "\n[Docker] stop container"
$docker_stop
}
download_package_debian() {
docker_repo_mount="-v ${package_repo}:/etc/apt/sources.list.d/${package_name}.list"
echo_title "[Docker] start container"
docker run --rm -tid --name ${docker_name} -v ${package_path}:${package_tmp_path} ${docker_repo_mount:-''} ${docker_image}
echo_title "\n[Docker] update repo cache"
$docker_exec sed -e "s#http://deb.debian.org#http://mirrors.aliyun.com#g" \
-e "s#http://security.debian.org#http://mirrors.aliyun.com#g" \
-e "s#http://security-cdn.debian.org#http://mirrors.aliyun.com#g" \
-i /etc/apt/sources.list
$docker_exec apt-get update
$docker_exec rm -rf /var/cache/apt/archives/*
echo_title "\n[Docker] download package"
$docker_exec apt-get install --download-only -y ${packages}
$docker_exec find /var/cache/apt/archives/ -name "*.deb" -exec cp {} ${package_tmp_path} \;
echo_title "\n[Docker] stop container"
$docker_stop
}
download_package_centos6() {
docker_image="centos:6"
download_package_centos
}
download_package_centos7() {
docker_image="centos:7"
download_package_centos
}
download_package_centos8() {
docker_image="centos:8"
download_package_centos
}
download_package_debian8() {
docker_image="debian:8"
download_package_debian
}
download_package_debian9() {
docker_image="debian:9"
download_package_debian
}
download_package_debian10() {
docker_image="debian:10"
download_package_debian
}
usage_help() {
cat <<EOM
Download Packages With Dependencies Locally.
Usage:
$(basename $0) system package [package repo]
Support system:
$(echo ${support_system[@]})
Example:
$(basename $0) centos7 ansible
$(basename $0) centos7 "python36 python36-devel"
$(basename $0) centos7 ceph /root/ceph.repo
EOM
exit 1
}
######################################################################################################
# main
######################################################################################################
support_system=(centos6 centos7 centos8 debian8 debian9 debian10)
if [ $# -le 1 ]; then
usage_help
fi
system="${1}"
packages="${2}"
package_name="${packages%% *}"
package_repo="${3:-}"
package_path="$(pwd)/package_${system}_${package_name:-local}"
package_tmp_path="/tmp/package"
docker_name="package"
docker_exec="docker exec ${docker_name}"
docker_stop="docker stop ${docker_name}"
check_or_install_docker
case "${support_system[@]}" in
*"$system"*)
download_package_$system
echo_title "\n[Local] show file"
echo -e "Path: ${package_path}\n"
ls -al $package_path
exit 0;;
esac
usage_help