From 575b328f325bd2055383577c9a7596cab5b57bd2 Mon Sep 17 00:00:00 2001 From: lework Date: Tue, 10 Mar 2020 18:42:34 +0800 Subject: [PATCH] add get_packages.sh --- shell/get_packages.sh | 211 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 shell/get_packages.sh diff --git a/shell/get_packages.sh b/shell/get_packages.sh new file mode 100644 index 0000000..4a99c7d --- /dev/null +++ b/shell/get_packages.sh @@ -0,0 +1,211 @@ +#!/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 < /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} ${package_name} + 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 ${package_name} + $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 <