Skip to content

Commit 31677d1

Browse files
committed
make it easier to add new architectures to release.sh
1 parent 1ef6b32 commit 31677d1

File tree

1 file changed

+107
-37
lines changed

1 file changed

+107
-37
lines changed

release.sh

Lines changed: 107 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,126 @@ set -e
88
#========================================================================================
99

1010
cd $GOPATH/src/github.com/fstab/grok_exporter
11-
rm -rf dist
1211

1312
export VERSION=0.2.4-SNAPSHOT
1413

1514
export VERSION_FLAGS="\
16-
-X github.com/fstab/grok_exporter/exporter.Version=$VERSION \
17-
-X github.com/fstab/grok_exporter/exporter.BuildDate=$(date +%Y-%m-%d) \
18-
-X github.com/fstab/grok_exporter/exporter.Branch=$(git rev-parse --abbrev-ref HEAD) \
19-
-X github.com/fstab/grok_exporter/exporter.Revision=$(git rev-parse --short HEAD) \
15+
-X github.com/fstab/grok_exporter/exporter.Version=$VERSION
16+
-X github.com/fstab/grok_exporter/exporter.BuildDate=$(date +%Y-%m-%d)
17+
-X github.com/fstab/grok_exporter/exporter.Branch=$(git rev-parse --abbrev-ref HEAD)
18+
-X github.com/fstab/grok_exporter/exporter.Revision=$(git rev-parse --short HEAD)
2019
"
2120

2221
#--------------------------------------------------------------
2322
# Make sure all tests run.
2423
#--------------------------------------------------------------
2524

26-
go fmt $(go list ./... | grep -v /vendor/)
27-
go test $(go list ./... | grep -v /vendor/)
28-
29-
function make_release {
30-
MACHINE=$1
31-
ARCH=$2
32-
EXTENSION=$3
33-
echo "Building grok_exporter-$VERSION.$ARCH"
34-
mkdir -p dist/grok_exporter-$VERSION.$ARCH
35-
if [[ $ARCH == darwin* ]] || [[ $ARCH == linux* ]] ; then
36-
# The compile script in the Docker image sets CGO_LDFLAGS to libonig.a, which should make grok_exporter
37-
# statically linked with the Oniguruma library. However, this doesn't work on Darwin and CentOS 6.
38-
# As a workaround, we set LDFLAGS directly in the header of oniguruma.go.
39-
sed -i.bak 's;#cgo LDFLAGS: -L/usr/local/lib -lonig;#cgo LDFLAGS: /usr/local/lib/libonig.a;' exporter/oniguruma.go
40-
fi
41-
if [ $MACHINE = "docker" ] ; then
42-
docker run -v $GOPATH/src/github.com/fstab/grok_exporter:/root/go/src/github.com/fstab/grok_exporter --net none --rm -ti fstab/grok_exporter-compiler compile-$ARCH.sh -ldflags "$VERSION_FLAGS" -o dist/grok_exporter-$VERSION.$ARCH/grok_exporter$EXTENSION
43-
else
44-
# export CGO_LDFLAGS=/usr/local/lib/libonig.a
45-
go build -ldflags "$VERSION_FLAGS" -o dist/grok_exporter-$VERSION.$ARCH/grok_exporter .
46-
fi
47-
if [[ $ARCH == darwin* ]] || [[ $ARCH == linux* ]] ; then
48-
# Undo workaround with LDFLAGS in oniguruma.go
25+
function run_tests {
26+
go fmt $(go list ./... | grep -v /vendor/)
27+
go test $(go list ./... | grep -v /vendor/)
28+
}
29+
30+
#--------------------------------------------------------------
31+
# Helper functions
32+
#--------------------------------------------------------------
33+
34+
function enable_legacy_static_linking {
35+
# The compile script in the Docker image sets CGO_LDFLAGS to libonig.a, which should make grok_exporter
36+
# statically linked with the Oniguruma library. However, this doesn't work on Darwin and CentOS 6.
37+
# As a workaround, we set LDFLAGS directly in the header of oniguruma.go.
38+
sed -i.bak 's;#cgo LDFLAGS: -L/usr/local/lib -lonig;#cgo LDFLAGS: /usr/local/lib/libonig.a;' exporter/oniguruma.go
39+
}
40+
41+
function revert_legacy_static_linking {
42+
if [ -f exporter/oniguruma.go.bak ] ; then
4943
mv exporter/oniguruma.go.bak exporter/oniguruma.go
5044
fi
51-
cp -a logstash-patterns-core/patterns dist/grok_exporter-$VERSION.$ARCH
52-
cp -a example dist/grok_exporter-$VERSION.$ARCH
45+
}
46+
47+
# Make sure revert_legacy_static_linking is called even if a compile error makes this script terminate early
48+
trap revert_legacy_static_linking EXIT
49+
50+
function create_zip_file {
51+
OUTPUT_DIR=$1
52+
cp -a logstash-patterns-core/patterns dist/$OUTPUT_DIR
53+
cp -a example dist/$OUTPUT_DIR
5354
cd dist
54-
sed -i.bak s,/logstash-patterns-core/patterns,/patterns,g grok_exporter-$VERSION.$ARCH/example/*.yml
55-
rm grok_exporter-$VERSION.$ARCH/example/*.yml.bak
56-
zip --quiet -r grok_exporter-$VERSION.$ARCH.zip grok_exporter-$VERSION.$ARCH
57-
rm -r grok_exporter-$VERSION.$ARCH
55+
sed -i.bak s,/logstash-patterns-core/patterns,/patterns,g $OUTPUT_DIR/example/*.yml
56+
rm $OUTPUT_DIR/example/*.yml.bak
57+
zip --quiet -r $OUTPUT_DIR.zip $OUTPUT_DIR
58+
rm -r $OUTPUT_DIR
5859
cd ..
5960
}
6061

61-
make_release native darwin-amd64
62-
make_release docker linux-amd64
63-
make_release docker windows-amd64 .exe
62+
function run_docker_amd64 {
63+
COMPILE_SCRIPT=$1
64+
OUTPUT_DIR=$2
65+
docker run \
66+
-v $GOPATH/src/github.com/fstab/grok_exporter:/root/go/src/github.com/fstab/grok_exporter \
67+
--net none \
68+
--rm -ti fstab/grok_exporter-compiler-amd64 \
69+
"$COMPILE_SCRIPT" -ldflags "$VERSION_FLAGS" -o "dist/$OUTPUT_DIR/grok_exporter"
70+
}
71+
72+
#--------------------------------------------------------------
73+
# Release functions
74+
#--------------------------------------------------------------
75+
76+
function release_linux_amd64 {
77+
echo "Building dist/grok_exporter-$VERSION.linux-amd64.zip"
78+
enable_legacy_static_linking
79+
run_docker_amd64 ./compile-linux.sh grok_exporter-$VERSION.linux-amd64
80+
revert_legacy_static_linking
81+
create_zip_file grok_exporter-$VERSION.linux-amd64
82+
}
83+
84+
function release_windows_amd64 {
85+
echo "Building dist/grok_exporter-$VERSION.windows-amd64.zip"
86+
run_docker_amd64 ./compile-windows-amd64.sh grok_exporter-$VERSION.windows-amd64
87+
create_zip_file grok_exporter-$VERSION.windows-amd64
88+
}
89+
90+
function release_darwin_amd64 {
91+
echo "Building dist/grok_exporter-$VERSION.darwin-amd64.zip"
92+
enable_legacy_static_linking
93+
go build -ldflags "$VERSION_FLAGS" -o dist/grok_exporter-$VERSION.darwin-amd64/grok_exporter .
94+
revert_legacy_static_linking
95+
create_zip_file grok_exporter-$VERSION.darwin-amd64
96+
}
97+
98+
#--------------------------------------------------------------
99+
# main
100+
#--------------------------------------------------------------
101+
102+
case $1 in
103+
linux-amd64)
104+
rm -rf dist/*
105+
run_tests
106+
release_linux_amd64
107+
;;
108+
darwin-amd64)
109+
rm -rf dist/*
110+
run_tests
111+
release_darwin_amd64
112+
;;
113+
windows-amd64)
114+
rm -rf dist/*
115+
run_tests
116+
release_windows_amd64
117+
;;
118+
all)
119+
rm -rf dist/*
120+
run_tests
121+
release_linux_amd64
122+
release_darwin_amd64
123+
release_windows_amd64
124+
;;
125+
*)
126+
echo 'Usage: ./release.sh <arch>' >&2
127+
echo 'where <arch> can be:' >&2
128+
echo ' - linux-amd64' >&2
129+
echo ' - darwin-amd64' >&2
130+
echo ' - windows-amd64' >&2
131+
echo ' - all' >&2
132+
exit -1
133+
esac

0 commit comments

Comments
 (0)