222 lines
6.3 KiB
Bash
222 lines
6.3 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
function build() {
|
|
ROOT=$(dirname "$0")
|
|
NAME="edge-api"
|
|
DIST=$ROOT/"../dist/${NAME}"
|
|
OS=${1}
|
|
ARCH=${2}
|
|
TAG=${3}
|
|
NODE_ARCHITECTS=("amd64")
|
|
#NODE_ARCHITECTS=("amd64" "arm64")
|
|
|
|
if [ -z "$OS" ]; then
|
|
echo "usage: build.sh OS ARCH"
|
|
exit
|
|
fi
|
|
if [ -z "$ARCH" ]; then
|
|
echo "usage: build.sh OS ARCH"
|
|
exit
|
|
fi
|
|
if [ -z "$TAG" ]; then
|
|
TAG="community"
|
|
fi
|
|
|
|
VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
|
|
# 生成 zip 文件名时不包含 plus 标记
|
|
if [ "${TAG}" = "plus" ]; then
|
|
ZIP="${NAME}-${OS}-${ARCH}-v${VERSION}.zip"
|
|
else
|
|
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
|
|
fi
|
|
|
|
# build edge-node
|
|
NodeVersion=$(lookup-version "$ROOT""/../../EdgeNode/internal/const/const.go")
|
|
echo "building edge-node v${NodeVersion} ..."
|
|
EDGE_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeNode/build/build.sh"
|
|
if [ ! -f "$EDGE_NODE_BUILD_SCRIPT" ]; then
|
|
echo "unable to find edge-node build script 'EdgeNode/build/build.sh'"
|
|
exit
|
|
fi
|
|
cd "$ROOT""/../../EdgeNode/build" || exit
|
|
echo "=============================="
|
|
for arch in "${NODE_ARCHITECTS[@]}"; do
|
|
# 查找 zip 文件时不包含 plus 标记
|
|
if [ "${TAG}" = "plus" ]; then
|
|
NODE_ZIP_FILE="$ROOT""/../../EdgeNode/dist/edge-node-linux-${arch}-v${NodeVersion}.zip"
|
|
else
|
|
NODE_ZIP_FILE="$ROOT""/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip"
|
|
fi
|
|
if [ ! -f "$NODE_ZIP_FILE" ]; then
|
|
./build.sh linux "$arch" $TAG
|
|
else
|
|
echo "use built node linux/$arch/v${NodeVersion}"
|
|
fi
|
|
done
|
|
echo "=============================="
|
|
cd - || exit
|
|
|
|
rm -f "$ROOT"/deploy/*.zip
|
|
for arch in "${NODE_ARCHITECTS[@]}"; do
|
|
# 复制 zip 文件时不包含 plus 标记
|
|
if [ "${TAG}" = "plus" ]; then
|
|
NODE_ZIP_FILE="$ROOT""/../../EdgeNode/dist/edge-node-linux-${arch}-v${NodeVersion}.zip"
|
|
else
|
|
NODE_ZIP_FILE="$ROOT""/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip"
|
|
fi
|
|
cp "$NODE_ZIP_FILE" "$ROOT"/deploy/edge-node-linux-"${arch}"-v"${NodeVersion}".zip
|
|
done
|
|
|
|
# build edge-dns
|
|
if [ "$TAG" = "plus" ]; then
|
|
DNS_ROOT=$ROOT"/../../EdgeDNS"
|
|
if [ -d "$DNS_ROOT" ]; then
|
|
DNSNodeVersion=$(lookup-version "$ROOT""/../../EdgeDNS/internal/const/const.go")
|
|
echo "building edge-dns ${DNSNodeVersion} ..."
|
|
EDGE_DNS_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeDNS/build/build.sh"
|
|
if [ ! -f "$EDGE_DNS_NODE_BUILD_SCRIPT" ]; then
|
|
echo "unable to find edge-dns build script 'EdgeDNS/build/build.sh'"
|
|
exit
|
|
fi
|
|
cd "$ROOT""/../../EdgeDNS/build" || exit
|
|
echo "=============================="
|
|
architects=("amd64")
|
|
#architects=("amd64" "arm64")
|
|
for arch in "${architects[@]}"; do
|
|
./build.sh linux "$arch" $TAG
|
|
done
|
|
echo "=============================="
|
|
cd - || exit
|
|
|
|
for arch in "${architects[@]}"; do
|
|
cp "$ROOT""/../../EdgeDNS/dist/edge-dns-linux-${arch}-v${DNSNodeVersion}.zip" "$ROOT"/deploy/edge-dns-linux-"${arch}"-v"${DNSNodeVersion}".zip
|
|
done
|
|
fi
|
|
fi
|
|
|
|
# build sql
|
|
if [ $TAG = "plus" ]; then
|
|
echo "building sql ..."
|
|
"${ROOT}"/sql.sh
|
|
fi
|
|
|
|
# copy files
|
|
echo "copying ..."
|
|
if [ ! -d "$DIST" ]; then
|
|
mkdir "$DIST"
|
|
mkdir "$DIST"/bin
|
|
mkdir "$DIST"/configs
|
|
mkdir "$DIST"/logs
|
|
mkdir "$DIST"/data
|
|
fi
|
|
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs/
|
|
cp "$ROOT"/configs/db.template.yaml "$DIST"/configs/
|
|
# 复制 EdgeCommon 的配置文件(如果存在)
|
|
if [ -f "$ROOT"/../../EdgeCommon/build/configs/ip_library.yaml ]; then
|
|
cp "$ROOT"/../../EdgeCommon/build/configs/ip_library.yaml "$DIST"/configs/
|
|
fi
|
|
if [ -f "$ROOT"/../../EdgeCommon/build/configs/brand.yaml ]; then
|
|
cp "$ROOT"/../../EdgeCommon/build/configs/brand.yaml "$DIST"/configs/
|
|
fi
|
|
cp -R "$ROOT"/deploy "$DIST/"
|
|
rm -f "$DIST"/deploy/.gitignore
|
|
cp -R "$ROOT"/installers "$DIST"/
|
|
|
|
# copy fluent-bit templates and local packages from repo root
|
|
FLUENT_ROOT="$ROOT/../../deploy/fluent-bit"
|
|
FLUENT_DIST="$DIST/deploy/fluent-bit"
|
|
if [ -d "$FLUENT_ROOT" ]; then
|
|
rm -rf "$FLUENT_DIST"
|
|
mkdir -p "$FLUENT_DIST"
|
|
|
|
FLUENT_FILES=(
|
|
"fluent-bit.conf"
|
|
"fluent-bit-dns.conf"
|
|
"fluent-bit-https.conf"
|
|
"fluent-bit-dns-https.conf"
|
|
"fluent-bit-windows.conf"
|
|
"fluent-bit-windows-https.conf"
|
|
"parsers.conf"
|
|
"clickhouse-upstream.conf"
|
|
"clickhouse-upstream-windows.conf"
|
|
"logrotate.conf"
|
|
"README.md"
|
|
)
|
|
for file in "${FLUENT_FILES[@]}"; do
|
|
if [ -f "$FLUENT_ROOT/$file" ]; then
|
|
cp "$FLUENT_ROOT/$file" "$FLUENT_DIST/"
|
|
fi
|
|
done
|
|
|
|
if [ -d "$FLUENT_ROOT/packages" ]; then
|
|
cp -R "$FLUENT_ROOT/packages" "$FLUENT_DIST/"
|
|
fi
|
|
|
|
# remove local runtime artifacts if present
|
|
rm -f "$FLUENT_DIST/.gitignore"
|
|
rm -f "$FLUENT_DIST"/logs.db*
|
|
rm -rf "$FLUENT_DIST/storage"
|
|
fi
|
|
|
|
# building edge installer
|
|
echo "building node installer ..."
|
|
architects=("amd64")
|
|
#architects=("amd64" "arm64")
|
|
for arch in "${architects[@]}"; do
|
|
# TODO support arm, mips ...
|
|
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-helper-linux-"${arch}" "$ROOT"/../cmd/installer-helper/main.go
|
|
done
|
|
|
|
# building edge dns installer
|
|
if [ $TAG = "plus" ]; then
|
|
echo "building dns node installer ..."
|
|
architects=("amd64")
|
|
#architects=("amd64" "arm64")
|
|
for arch in "${architects[@]}"; do
|
|
# TODO support arm, mips ...
|
|
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-dns-helper-linux-"${arch}" "$ROOT"/../cmd/installer-dns-helper/main.go
|
|
done
|
|
fi
|
|
|
|
# building api node
|
|
env GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$DIST/bin/$NAME" "$ROOT"/../cmd/edge-api/main.go
|
|
if [ ! -f "${DIST}/bin/${NAME}" ]; then
|
|
echo "build failed!"
|
|
exit
|
|
fi
|
|
|
|
# delete hidden files
|
|
find "$DIST" -name ".DS_Store" -delete
|
|
find "$DIST" -name ".gitignore" -delete
|
|
|
|
# 删除 MaxMind 数据库文件(使用嵌入的数据库,不需要外部文件)
|
|
find "$DIST" -name "*.mmdb" -type f -delete
|
|
find "$DIST" -type d -name "iplibrary" -empty -delete
|
|
|
|
echo "zip files"
|
|
cd "${DIST}/../" || exit
|
|
if [ -f "${ZIP}" ]; then
|
|
rm -f "${ZIP}"
|
|
fi
|
|
zip -r -X -q "${ZIP}" ${NAME}/
|
|
rm -rf ${NAME}
|
|
cd - || exit
|
|
|
|
echo "[done]"
|
|
}
|
|
|
|
function lookup-version() {
|
|
FILE=$1
|
|
VERSION_DATA=$(cat "$FILE")
|
|
re="Version[ ]+=[ ]+\"([0-9.]+)\""
|
|
if [[ $VERSION_DATA =~ $re ]]; then
|
|
VERSION=${BASH_REMATCH[1]}
|
|
echo "$VERSION"
|
|
else
|
|
echo "could not match version"
|
|
exit
|
|
fi
|
|
}
|
|
|
|
build "$1" "$2" "$3"
|