Skip to content

Instantly share code, notes, and snippets.

@LeileiChui
Last active February 28, 2023 19:30
Show Gist options
  • Save LeileiChui/5a7e216ae4c7bf04cf3805629118c993 to your computer and use it in GitHub Desktop.
Save LeileiChui/5a7e216ae4c7bf04cf3805629118c993 to your computer and use it in GitHub Desktop.
Auto Build Latest FastDFS With Docker(使用 Docker 自动构建最新版本 FastDFS)
#/bin/bash
set +x
################
# Install dependencies
################
cat >/etc/apt/sources.list <<EOF
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# 预发布软件源,不建议启用
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
EOF
apt update
apt install -y curl git build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev
################
# Utils
################
function empty_dir() {
# $1: dir
if [ -d $1 ]; then
rm -rf $1
fi
mkdir -p $1
}
function tar_ball() {
# $1: save path
# $2: file name
# $3: version
# $4: source path
# covert version to lower case
version=$(echo $3 | tr '[:upper:]' '[:lower:]')
# add v prefix
if [[ $version != v* ]]; then
version=v$version
fi
tar -czf $1/$2-$version.tar.gz -C $4 . --transform "s,^./,,"
}
function any_copyfile() {
# $1: source path
# $2: target path
target_dir=$(dirname $2)
mkdir -p $target_dir
cp $1 $2
}
function folder_mirror() {
# $1: source path root
# $2: file list root
# $3: target path root
# special for root
source_root=$1
if [ $source_root = "/" ]; then
source_root=""
fi
empty_dir $3
find $2 -type f | while read file; do
# 去掉前缀
file=${file#$2}
any_copyfile $source_root$file $3$file
done
}
################
# Make Directories
################
cur_path=$(
cd "$(dirname "$0")"
pwd
)
work_path=$(mktemp -d)
dist_dir=$cur_path/dist
empty_dir $dist_dir
################
# Download Source Code
################
cd $work_path
fdfs_repo=(fastdfs-nginx-module.git libfastcommon.git libserverframe.git fastdfs.git)
nginx_ver=1.16.1
for repo in ${fdfs_repo[@]}; do
git clone https://github.com/happyfish100/$repo $repo
done
curl -Ls http://nginx.org/download/nginx-$nginx_ver.tar.gz | tar -xz
fastdfs_version=""
################
# Build FastDFS
################
for repo in ${fdfs_repo[@]:1}; do
cd $work_path/$repo
latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1))
# Save FastDFS version
if [ $repo = "fastdfs.git" ]; then
fastdfs_version=$latest_tag
fi
git checkout $latest_tag
# 先正常编译
export DESTDIR=""
./make.sh clean && ./make.sh && ./make.sh install
# 再编译到指定目录,目的是获取文件列表
export DESTDIR=$work_path/build-$repo-$latest_tag
empty_dir $DESTDIR
./make.sh clean && ./make.sh && ./make.sh install
# 依照文件列表复制到 dist 目录
export DISTDIR=$work_path/dist-$repo-$latest_tag
folder_mirror "/" $DESTDIR $DISTDIR
# 打包
tar_ball $dist_dir $repo $latest_tag $DISTDIR
done
################
# Build Nginx
################
cd $work_path/nginx-$nginx_ver
export DESTDIR=""
export CUSTOMDIR=""
sed -i "s|/usr/local/include|/usr/include|g" $work_path/fastdfs-nginx-module.git/src/config
# 先正常编译
./configure \
--prefix=$CUSTOMDIR/usr/local/nginx \
--error-log-path=$CUSTOMDIR/usr/local/nginx/logs/error.log \
--http-log-path=$CUSTOMDIR/usr/local/nginx/logs/access.log \
--pid-path=$CUSTOMDIR/var/run/nginx/nginx.pid \
--lock-path=$CUSTOMDIR/var/lock/nginx.lock \
--http-client-body-temp-path=$CUSTOMDIR/var/temp/nginx/client \
--http-proxy-temp-path=$CUSTOMDIR/var/temp/nginx/proxy \
--http-fastcgi-temp-path=$CUSTOMDIR/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=$CUSTOMDIR/var/temp/nginx/uwsgi \
--http-scgi-temp-path=$CUSTOMDIR/var/temp/nginx/scgi \
--with-http_gzip_static_module \
--add-module=$work_path/fastdfs-nginx-module.git/src &&
make -j$(nproc) && make install
# 再编译到指定目录,目的是获取文件列表
export CUSTOMDIR=$work_path/build-nginx-v$nginx_ver
empty_dir $CUSTOMDIR
./configure \
--prefix=$CUSTOMDIR/usr/local/nginx \
--error-log-path=$CUSTOMDIR/usr/local/nginx/logs/error.log \
--http-log-path=$CUSTOMDIR/usr/local/nginx/logs/access.log \
--pid-path=$CUSTOMDIR/var/run/nginx/nginx.pid \
--lock-path=$CUSTOMDIR/var/lock/nginx.lock \
--http-client-body-temp-path=$CUSTOMDIR/var/temp/nginx/client \
--http-proxy-temp-path=$CUSTOMDIR/var/temp/nginx/proxy \
--http-fastcgi-temp-path=$CUSTOMDIR/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=$CUSTOMDIR/var/temp/nginx/uwsgi \
--http-scgi-temp-path=$CUSTOMDIR/var/temp/nginx/scgi \
--with-http_gzip_static_module \
--add-module=$work_path/fastdfs-nginx-module.git/src &&
make -j$(nproc) && make install
# 依照文件列表复制到 dist 目录
export DISTDIR=$work_path/dist-nginx-v$nginx_ver
folder_mirror "/" $CUSTOMDIR $DISTDIR
# 拷贝配置文件
any_copyfile $work_path/fastdfs-nginx-module.git/src/mod_fastdfs.conf $DISTDIR/etc/fdfs/mod_fastdfs.conf
# 打包
tar_ball $dist_dir nginx $nginx_ver $DISTDIR
################
# Mix All Packages
################
mix_dir=$work_path/mix
empty_dir $mix_dir
for ball in $dist_dir/*.tar.gz; do
tar -xzf $ball -C $mix_dir
done
tar_ball $dist_dir fastdfs-full $fastdfs_version $mix_dir
################
# Clean
################
cd $cur_path
rm -rf $work_path
set +x
docker run -it --rm \
-v $(pwd):/workspace \
-e "HTTP_PROXY=http://<proxy ip>:<proxy port>" \
-e "HTTPS_PROXY=http://<proxy ip>:<proxy port>" \
-e "NO_PROXY=localhost,192.*,127.*,10.*" \
ubuntu:latest \
/bin/bash /workspace/build_fastdfs.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment