Skip to content

Commit

Permalink
Refactor build.sh script; no functional change
Browse files Browse the repository at this point in the history
  • Loading branch information
sumgarg committed Dec 19, 2024
1 parent e90ea54 commit 7b87cbc
Showing 1 changed file with 107 additions and 77 deletions.
184 changes: 107 additions & 77 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,107 +8,137 @@
# The extr_version is the version used when we make Extreme specific changes on
# a particular telegraf branch.
#
set -e

# Default ARM type for arm architecture
arm_type=5

usage()
show_usage()
{
echo "usage: $0 arch {build | upload}"
echo " . arch : valid architectures: arm, arm64, x86_64, mips"
echo " . build : build and tar utility for specified architecture"
echo " . upload: upload specified architecture's tar to Artifactory"
echo "usage: ${0} <arch> {build | upload}"
echo " . arch : valid architectures: arm, arm64, x86_64, mips"
echo " . build : build and tar utility for specified architecture"
echo " . upload : upload specified architecture's tar to Artifactory"
}

build()
do_build()
{
local opts=""
local _extr_arch="${1}"
local _target_tarball="${2}"

# set the target architecture for the executable
local _target_arch="${_extr_arch}"
if [[ "${_target_arch}" = "x86_64" ]]; then
_target_arch="amd64"
fi

opts+="${opts:+ }GOOS=linux"
opts+="${opts:+ }GOARCH=${bld_arch}"
opts+="${opts:+ }GOARM=${arm_type}"
opts+="${opts:+ }GOPROXY=https://proxy.golang.org,direct"
opts+="${opts:+ }GOSUMDB=sum.golang.org"
opts+="${opts:+ }CGO_ENABLED=0"
opts+="${opts:+ }GOCACHE=${gocache@Q}"
opts+="${opts:+ }GOMODCACHE=${gomodcache@Q}"
# options passed to GO
local _opts=""
_opts+="${_opts:+ }GOOS=linux"
_opts+="${_opts:+ }GOARCH=${_target_arch}"
if [[ "${_target_arch}" == "arm" ]]; then
# set GOARM only when building for 32 bit ARM
_opts+="${_opts:+ }GOARM=${arm_type}"
fi

# force GO to use alternate location of cache and modules
local _gocache="$(pwd -P)/.tmp/go-cache"
local _gomodcache="$(pwd -P)/.tmp/go-mod"
_opts+="${_opts:+ }GOCACHE=${_gocache@Q}"
_opts+="${_opts:+ }GOMODCACHE=${_gomodcache@Q}"

make clean
rm -f ${target}
LDFLAGS="-w -s" make ${opts} telegraf
tar -cf ${target} telegraf MIT generic_MIT
rm -f "${_target_tarball}"
env LDFLAGS="-w -s" CGO_ENABLED=0 ${_opts} make telegraf
tar -cf "${_target_tarball}" telegraf MIT generic_MIT
rm -f telegraf
}

upload()
do_upload()
{
if [ ! -f ${target} ]; then
echo "info: ${target} not found; building first..."
build
if [ ! -f ${target} ]; then
echo "error: could not find or build '${target}' tarball"
local _extr_arch="${1}"
local _target_tarball="${2}"

if [[ ! -f "${_target_tarball}" ]]; then
echo "info: ${_target_tarball} not found; building first..."
do_build "${_extr_arch}" "${_target_tarball}"
if [[ ! -f "${_target_tarball}" ]]; then
echo "error: could not find or build '${_target_tarball}' tarball"
exit 1
fi
fi

# make sure jfrog config is set up
if ! jfrog config show ${salem} &>/dev/null ; then
echo "Could not find Salem Artifactory config. Let's create one..."
echo "Accept defaults and use your corporate password."; echo ""
jfrog config add --url http://engartifacts1.extremenetworks.com:8081 --user $(whoami) ${salem}
if ! jfrog config show ${salem} &>/dev/null ; then
echo "error: failed to configure ${salem} Artifactory server access"
if ! jfrog config show "${afy_server_name}" &>/dev/null ; then
echo "Could not find ${afy_server_name} Artifactory config. Let's create one..."
echo "Accept defaults and use your corporate password."; echo
jfrog config add --url "${afy_server_url}" --user "${USER}" "${afy_server_name}"
if ! jfrog config show "${afy_server_name}" &>/dev/null ; then
echo "error: failed to configure ${afy_server_name} Artifactory server access"
exit 1
fi
fi
jfrog rt upload ${target} xos-binaries-local-release/telegraf/${arch}/${target} --server-id ${salem}

jfrog rt upload "${_target_tarball}" "${aft_repo}/${_extr_arch}/${_target_tarball}" --server-id "${afy_server_name}"
}

#######################
# execution starts here
#######################
if [[ -z "$1" || "$1" == "--help" || "$1" == "-h" || "$1" == "?" ]]; then
usage
exit 0
fi

# force go to use alternate location of modules
gocache="$(pwd -P)/.tmp/go-cache"
gomodcache="$(pwd -P)/.tmp/go-mod"
# grab version strings
telegraf_version=$(cat build_version.txt)
extr_version=$(cat extr_version.txt)
salem=Salem

# set architecture name and build architecture as used by golang
arch=$1
if [ "$arch" = "x86_64" ]; then
bld_arch=amd64
else
bld_arch=${arch}
fi
target=telegraf_${arch}_${telegraf_version}.${extr_version}.tar

# check action argument
case $2 in
build | upload)
action=$2
;;
*)
echo "error: invalid action '$2'"
usage
exit 1
esac

# perform action
case $1 in
arm64 | mips | x86_64 | arm)
$action
;;
*)
echo "error: invalid architecture '$1'"
usage

main()
{
# all hard-coded globals go here
arm32_type="5" # default ARM type for "arm" architecture
afy_server_name="Salem"
afy_server_url="http://engartifacts1.extremenetworks.com:8081"
aft_repo="xos-binaries-local-release/telegraf"

local _extr_arch="${1}"
local _script_action="${2}"

# verify inputs - count of args
if [[ ${#} -ne 2 ]]; then
echo "error: incorrect number of arguments '${#}'"
show_usage
exit 1
esac
fi

# verify inputs - architecture
case "${_extr_arch}" in
arm64 | mips | x86_64 | arm)
:
;;
--help | -h | -?)
show_usage
exit 0
;;
*)
echo "error: invalid architecture '${1}'"
show_usage
exit 1
esac

# verify inputs - action
case "${_script_action}" in
build | upload)
:
;;
--help | -h | -?)
show_usage
exit 0
;;
*)
echo "error: invalid action '$2'"
show_usage
exit 1
esac

# grab version strings and set the name of the target tarball
local _telegraf_version="$(< build_version.txt)"
local _extr_version="$(< extr_version.txt)"
local _target_tarball="telegraf_${_extr_arch}_${_telegraf_version}.${_extr_version}.tar"

"do_${_script_action}" "${_extr_arch}" "${_target_tarball}"
}

set -o errexit
set -o pipefail

main "${@}"

0 comments on commit 7b87cbc

Please sign in to comment.