Pull request: all: imp scripts

Merge in DNS/adguard-home from imp-sh to master

Squashed commit of the following:

commit 477832e11eca2ef7ac0071b5da938dacb2ed617d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 20:24:34 2021 +0300

    scripts: rm dbg

commit dbb4b8c783f607781b980dcd57d78085c9022e72
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 20:21:20 2021 +0300

    all: imp code, compat, docs

commit e6e4375d67ad1c213efb04411e3ba0bc6293f936
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 19:33:48 2021 +0300

    all: imp scripts
This commit is contained in:
Ainar Garipov
2021-05-19 20:31:20 +03:00
parent b3d28408cd
commit 6f7fd33afd
17 changed files with 473 additions and 321 deletions

View File

@@ -2,18 +2,20 @@
# AdGuard Home Release Script
#
# The commentary in this file is written with the assumption that the
# reader only has superficial knowledge of the POSIX shell language and
# alike. Experienced readers may find it overly verbose.
# The commentary in this file is written with the assumption that the reader
# only has superficial knowledge of the POSIX shell language and alike.
# Experienced readers may find it overly verbose.
# The default verbosity level is 0. Show log messages if the caller
# requested verbosity level greather than 0. Show every command that is
# run if the verbosity level is greater than 1. Show the environment if
# the verbosity level is greater than 2. Otherwise, print nothing.
# The default verbosity level is 0. Show log messages if the caller requested
# verbosity level greather than 0. Show every command that is run if the
# verbosity level is greater than 1. Show the environment if the verbosity
# level is greater than 2. Otherwise, print nothing.
#
# The level of verbosity for the build script is the same minus one
# level. See below in build().
readonly verbose="${VERBOSE:-0}"
# The level of verbosity for the build script is the same minus one level. See
# below in build().
verbose="${VERBOSE:-0}"
readonly verbose
if [ "$verbose" -gt '2' ]
then
env
@@ -24,14 +26,15 @@ then
fi
# By default, sign the packages, but allow users to skip that step.
readonly sign="${SIGN:-1}"
sign="${SIGN:-1}"
readonly sign
# Exit the script if a pipeline fails (-e), prevent accidental filename
# expansion (-f), and consider undefined variables as errors (-u).
set -e -f -u
# Function log is an echo wrapper that writes to stderr if the caller
# requested verbosity level greater than 0. Otherwise, it does nothing.
# Function log is an echo wrapper that writes to stderr if the caller requested
# verbosity level greater than 0. Otherwise, it does nothing.
log() {
if [ "$verbose" -gt '0' ]
then
@@ -42,85 +45,113 @@ log() {
log 'starting to build AdGuard Home release'
# Require the channel to be set. Additional validation is performed
# later by go-build.sh.
readonly channel="$CHANNEL"
# Require the channel to be set. Additional validation is performed later by
# go-build.sh.
channel="$CHANNEL"
readonly channel
# Check VERSION against the default value from the Makefile. If it is
# that, use the version calculation script.
# Check VERSION against the default value from the Makefile. If it is that, use
# the version calculation script.
if [ "${VERSION:-}" = 'v0.0.0' ] || [ "${VERSION:-}" = '' ]
then
readonly version="$( sh ./scripts/make/version.sh )"
version="$( sh ./scripts/make/version.sh )"
else
readonly version="$VERSION"
version="$VERSION"
fi
readonly version
log "channel '$channel'"
log "version '$version'"
# Check architecture and OS limiters. Add spaces to the local versions
# for better pattern matching.
# Check architecture and OS limiters. Add spaces to the local versions for
# better pattern matching.
if [ "${ARCH:-}" != '' ]
then
log "arches: '$ARCH'"
readonly arches=" $ARCH "
arches=" $ARCH "
else
readonly arches=''
arches=''
fi
readonly arches
if [ "${OS:-}" != '' ]
then
log "oses: '$OS'"
readonly oses=" $OS "
oses=" $OS "
else
readonly oses=''
oses=''
fi
readonly oses
readonly snap_enabled="${SNAP:-1}"
if [ "$snap_enabled" = '0' ]
snap_enabled="${SNAP:-1}"
readonly snap_enabled
if [ "$snap_enabled" -eq '0' ]
then
log 'snap: disabled'
fi
# Require the gpg key and passphrase to be set if the signing is
# required.
if [ "$sign" = '1' ]
# Require the gpg key and passphrase to be set if the signing is required.
if [ "$sign" -eq '1' ]
then
readonly gpg_key_passphrase="$GPG_KEY_PASSPHRASE"
readonly gpg_key="$GPG_KEY"
gpg_key_passphrase="$GPG_KEY_PASSPHRASE"
gpg_key="$GPG_KEY"
else
gpg_key_passphrase=''
gpg_key=''
fi
readonly gpg_key_passphrase gpg_key
# The default distribution files directory is dist.
readonly dist="${DIST_DIR:-dist}"
dist="${DIST_DIR:-dist}"
readonly dist
# Give users the ability to override the go command from environment.
# For example, to build two releases with two different Go versions and
# test the difference.
readonly go="${GO:-go}"
# Give users the ability to override the go command from environment. For
# example, to build two releases with two different Go versions and test the
# difference.
go="${GO:-go}"
readonly go
log "checking tools"
# Make sure we fail gracefully if one of the tools we need is missing.
for tool in gpg gzip sed sha256sum snapcraft tar zip
# Make sure we fail gracefully if one of the tools we need is missing. Use
# alternatives when available.
sha256sum_cmd='sha256sum'
for tool in gpg gzip sed "$sha256sum_cmd" snapcraft tar zip
do
which "$tool" >/dev/null\
|| { log "pieces don't fit, '$tool' not found"; exit 1; }
if ! which "$tool" > /dev/null
then
if [ "$tool" = "$sha256sum_cmd" ] && which 'shasum' > /dev/null
then
# macOS doesn't have sha256sum installed by default, but
# it does have shasum.
log 'replacing sha256sum with shasum -a 256'
sha256sum_cmd='shasum -a 256'
else
log "pieces don't fit, '$tool' not found"
exit 1
fi
fi
done
readonly sha256sum_cmd
# Data section. Arrange data into space-separated tables for read -r to
# read. Use 0 for missing values.
# Data section. Arrange data into space-separated tables for read -r to read.
# Use 0 for missing values.
readonly arms='5
arms='5
6
7'
readonly arms
readonly mipses='softfloat'
mipses='softfloat'
readonly mipses
# TODO(a.garipov): Remove armv6, because it was always overwritten by
# armv7. Rename armv7 to armhf. Rename the 386 snap to i386.
# TODO(a.garipov): Remove armv6, because it was always overwritten by armv7.
# Rename armv7 to armhf. Rename the 386 snap to i386.
# os arch arm mips snap
readonly platforms="\
platforms="\
darwin amd64 0 0 0
freebsd 386 0 0 0
freebsd amd64 0 0 0
@@ -141,12 +172,13 @@ linux mipsle 0 softfloat 0
linux ppc64le 0 0 0
windows 386 0 0 0
windows amd64 0 0 0"
readonly platforms
# Function build builds the release for one platform. It builds
# a binary, an archive and, if needed, a snap package.
# Function build builds the release for one platform. It builds a binary, an
# archive and, if needed, a snap package.
build() {
# Get the arguments. Here and below, use the "build_" prefix
# for all variables local to function build.
# Get the arguments. Here and below, use the "build_" prefix for all
# variables local to function build.
build_dir="${dist}/${1}/AdGuardHome"\
build_ar="$2"\
build_os="$3"\
@@ -156,8 +188,7 @@ build() {
build_snap="$7"\
;
# Use the ".exe" filename extension if we build a Windows
# release.
# Use the ".exe" filename extension if we build a Windows release.
if [ "$build_os" = 'windows' ]
then
build_output="./${build_dir}/AdGuardHome.exe"
@@ -169,11 +200,11 @@ build() {
# Build the binary.
#
# Set GOARM and GOMIPS to an empty string if $build_arm and
# $build_mips are zero by removing the zero as if it's a prefix.
# Set GOARM and GOMIPS to an empty string if $build_arm and $build_mips
# are zero by removing the zero as if it's a prefix.
#
# Don't use quotes with $build_par because we want an empty
# space if parallelism wasn't set.
# Don't use quotes with $build_par because we want an empty space if
# parallelism wasn't set.
env\
GOARCH="$build_arch"\
GOARM="${build_arm#0}"\
@@ -187,7 +218,7 @@ build() {
log "$build_output"
if [ "$sign" = '1' ]
if [ "$sign" -eq '1' ]
then
gpg\
--default-key "$gpg_key"\
@@ -202,8 +233,8 @@ build() {
# Prepare the build directory for archiving.
cp ./CHANGELOG.md ./LICENSE.txt ./README.md "$build_dir"
# Make archives. Windows and macOS prefer ZIP archives; the
# rest, gzipped tarballs.
# Make archives. Windows and macOS prefer ZIP archives; the rest,
# gzipped tarballs.
case "$build_os"
in
('darwin'|'windows')
@@ -220,7 +251,11 @@ build() {
log "$build_archive"
if [ "$build_snap" = '0' ] || [ "$snap_enabled" = '0' ]
# build_snap is a string, so use string comparison for it.
#
# TODO(a.garipov): Consider using a different empty value in the
# platforms table.
if [ "$build_snap" = '0' ] || [ "$snap_enabled" -eq '0' ]
then
return
fi
@@ -257,10 +292,10 @@ build() {
./scripts/snap/snap.tmpl.yaml\
>"${build_snap_dir}/meta/snap.yaml"
# TODO(a.garipov): The snapcraft tool will *always* write
# everything, including errors, to stdout. And there doesn't
# seem to be a way to change that. So, save the combined
# output, but only show it when snapcraft actually fails.
# TODO(a.garipov): The snapcraft tool will *always* write everything,
# including errors, to stdout. And there doesn't seem to be a way to
# change that. So, save the combined output, but only show it when
# snapcraft actually fails.
set +e
build_snapcraft_output="$(
snapcraft pack "$build_snap_dir" --output "$build_snap_output" 2>&1
@@ -278,20 +313,18 @@ build() {
log "starting builds"
# Go over all platforms defined in the space-separated table above,
# tweak the values where necessary, and feed to build.
# Go over all platforms defined in the space-separated table above, tweak the
# values where necessary, and feed to build.
echo "$platforms" | while read -r os arch arm mips snap
do
# See if the architecture or the OS is in the allowlist. To do
# so, try removing everything that matches the pattern (well,
# a prefix, but that doesn't matter here) containing the arch or
# the OS.
# See if the architecture or the OS is in the allowlist. To do so, try
# removing everything that matches the pattern (well, a prefix, but that
# doesn't matter here) containing the arch or the OS.
#
# For example, when $arches is " amd64 arm64 " and $arch is
# "amd64", then the pattern to remove is "* amd64 *", so the
# whole string becomes empty. On the other hand, if $arch is
# "windows", then the pattern is "* windows *", which doesn't
# match, so nothing is removed.
# For example, when $arches is " amd64 arm64 " and $arch is "amd64",
# then the pattern to remove is "* amd64 *", so the whole string becomes
# empty. On the other hand, if $arch is "windows", then the pattern is
# "* windows *", which doesn't match, so nothing is removed.
#
# See https://stackoverflow.com/a/43912605/1892060.
if [ "${arches##* $arch *}" != '' ]
@@ -333,17 +366,16 @@ log "$build_archive"
log "calculating checksums"
# Calculate the checksums of the files in a subshell with a different
# working directory. Don't use ls, because files matching one of the
# patterns may be absent, which will make ls return with a non-zero
# status code.
# Calculate the checksums of the files in a subshell with a different working
# directory. Don't use ls, because files matching one of the patterns may be
# absent, which will make ls return with a non-zero status code.
(
cd "./${dist}"
files="$( find . ! -name . -prune \( -name '*.tar.gz' -o -name '*.zip' \) )"
# Don't use quotes to get word splitting.
sha256sum $files > ./checksums.txt
$sha256sum_cmd $files > ./checksums.txt
)
log "writing versions"
@@ -351,20 +383,19 @@ log "writing versions"
echo "version=$version" > "./${dist}/version.txt"
# Create the verison.json file.
#
# TODO(a.garipov): Perhaps rewrite this as a go run program. Dealing
# with structured documents is really not a Shell's job.
readonly version_download_url="https://static.adguard.com/adguardhome/${channel}"
readonly version_json="./${dist}/version.json"
version_download_url="https://static.adguard.com/adguardhome/${channel}"
version_json="./${dist}/version.json"
readonly version_download_url version_json
# Point users to the master branch if the channel is edge.
if [ "$channel" = 'edge' ]
then
readonly version_history_url='https://github.com/AdguardTeam/AdGuardHome/commits/master'
version_history_url='https://github.com/AdguardTeam/AdGuardHome/commits/master'
else
readonly version_history_url='https://github.com/AdguardTeam/AdGuardHome/releases'
version_history_url='https://github.com/AdguardTeam/AdGuardHome/releases'
fi
readonly version_history_url
rm -f "$version_json"
echo "{
@@ -374,23 +405,11 @@ echo "{
\"selfupdate_min_version\": \"0.0\",
" >> "$version_json"
# Add the old object keys for compatibility with pre-v0.105.0 MIPS that
# did not mention the softfloat variant.
#
# TODO(a.garipov): Remove this around the time we hit v0.107.0.
echo "
\"download_linux_mips\": \"${version_download_url}/AdGuardHome_linux_mips_softfloat.tar.gz\",
\"download_linux_mipsle\": \"${version_download_url}/AdGuardHome_linux_mipsle_softfloat.tar.gz\",
\"download_linux_mips64\": \"${version_download_url}/AdGuardHome_linux_mips64_softfloat.tar.gz\",
\"download_linux_mips64le\": \"${version_download_url}/AdGuardHome_linux_mips64le_softfloat.tar.gz\",
" >> "$version_json"
# Same as with checksums above, don't use ls, because files matching one
# of the patterns may be absent.
readonly ar_files="$( \
find "./${dist}/" ! -name "${dist}" -prune \( -name '*.tar.gz' -o -name '*.zip' \)
)"
readonly ar_files_len="$( echo "$ar_files" | wc -l )"
# Same as with checksums above, don't use ls, because files matching one of the
# patterns may be absent.
ar_files="$( find "./${dist}/" ! -name "${dist}" -prune \( -name '*.tar.gz' -o -name '*.zip' \) )"
ar_files_len="$( echo "$ar_files" | wc -l )"
readonly ar_files ar_files_len
i='1'
# Don't use quotes to get word splitting.
@@ -408,7 +427,7 @@ do
# Use the filename's base path.
filename="${f#./${dist}/}"
if [ "$i" = "$ar_files_len" ]
if [ "$i" -eq "$ar_files_len" ]
then
echo " \"download_${platform}\": \"${version_download_url}/${filename}\"" >> "$version_json"
else