Skip to content

Commit de19709

Browse files
authored
Merge pull request #85 from jarrpa/build-scripts
Script tool installation and linting
2 parents 2016b60 + 34c7af9 commit de19709

File tree

10 files changed

+121
-199
lines changed

10 files changed

+121
-199
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ install:
1616

1717
script:
1818
# Lint text-like files
19-
- scripts/pre-commit.sh --require-all
19+
- scripts/lint-text.sh --require-all
2020
# Build container w/ Docker
21-
- docker version
2221
- ./build.sh

build.sh

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,53 @@
22

33
set -e
44

5+
# Set driver name
6+
DRIVER="${DRIVER:-glusterfs-csi-driver}"
7+
8+
# Set which docker repo to tag
9+
REPO="${REPO:-gluster/}"
10+
511
# Allow overriding default docker command
6-
DOCKER_CMD=${DOCKER_CMD:-docker}
12+
RUNTIME_CMD=${RUNTIME_CMD:-docker}
13+
14+
build="build"
15+
if [[ "${RUNTIME_CMD}" == "buildah" ]]; then
16+
build="bud"
17+
fi
718

819
# Allow disabling tests during build
920
RUN_TESTS=${RUN_TESTS:-1}
1021

1122
VERSION="$(git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')"
1223
BUILDDATE="$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')"
1324

14-
#-- Build final container
15-
$DOCKER_CMD build \
16-
-t glusterfs-csi-driver \
17-
--build-arg RUN_TESTS="$RUN_TESTS" \
18-
--build-arg version="$VERSION" \
19-
--build-arg builddate="$BUILDDATE" \
25+
GO_DEP_VERSION="${GO_DEP_VERSION}"
26+
GO_METALINTER_VERSION="${GO_METALINTER_VERSION:-v2.0.11}"
27+
GO_METALINTER_THREADS=${GO_METALINTER_THREADS:-4}
28+
29+
build_args=()
30+
build_args+=( --build-arg "RUN_TESTS=$RUN_TESTS" )
31+
build_args+=( --build-arg "GO_DEP_VERSION=$GO_DEP_VERSION" )
32+
build_args+=( --build-arg "GO_METALINTER_VERSION=$GO_METALINTER_VERSION" )
33+
build_args+=( --build-arg "GO_METALINTER_THREADS=$GO_METALINTER_THREADS" )
34+
build_args+=( --build-arg "version=$VERSION" )
35+
build_args+=( --build-arg "builddate=$BUILDDATE" )
36+
37+
# Print Docker version
38+
echo "=== $RUNTIME_CMD version ==="
39+
$RUNTIME_CMD version
40+
41+
#-- Build container
42+
$RUNTIME_CMD $build \
43+
-t "${REPO}${DRIVER}" \
44+
"${build_args[@]}" \
2045
-f pkg/glusterfs/Dockerfile \
2146
. \
2247
|| exit 1
2348

49+
# If running tests, extract profile data
2450
if [ "$RUN_TESTS" -ne 0 ]; then
2551
rm -f profile.cov
26-
$DOCKER_CMD build \
27-
-t glusterfs-csi-driver-build \
28-
--target build \
29-
--build-arg RUN_TESTS="$RUN_TESTS" \
30-
-f pkg/glusterfs/Dockerfile \
31-
. \
32-
&& \
33-
$DOCKER_CMD run --rm glusterfs-csi-driver-build \
34-
cat /profile.cov > profile.cov
52+
$RUNTIME_CMD run --entrypoint cat "${REPO}${DRIVER}" \
53+
/profile.cov > profile.cov
3554
fi

pkg/glusterfs/Dockerfile

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,55 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
#-- Create build environment
1314

14-
#-- Build phase
15-
FROM openshift/origin-release:golang-1.10 AS build
15+
FROM docker.io/openshift/origin-release:golang-1.10 as build
1616

1717
ENV GOPATH="/go/" \
18-
SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/"
18+
SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/" \
19+
SCRIPTSDIR="${SRCDIR}scripts/"
1920

2021
RUN yum install -y \
2122
git
2223

23-
# Install dep
24-
RUN mkdir -p /go/bin
25-
RUN curl -L https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
26-
27-
# Install gometalinter
28-
ARG GO_METALINTER_VERSION=v2.0.11
29-
RUN curl -L 'https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh' \
30-
| bash -s -- -b "${GOPATH}bin" "${GO_METALINTER_VERSION}"
24+
# Install go tools
25+
ARG GO_DEP_VERSION=
26+
ARG GO_METALINTER_VERSION=latest
27+
COPY scripts/install-go-tools.sh "${SCRIPTSDIR}"
28+
RUN mkdir -p /go/bin; ${SCRIPTSDIR}/install-go-tools.sh
3129

3230
# Vendor dependencies
3331
COPY Gopkg.lock Gopkg.toml "${SRCDIR}"
3432
WORKDIR "${SRCDIR}"
3533
RUN /go/bin/dep ensure -v -vendor-only
3634

37-
# Build executable
38-
COPY . "${SRCDIR}"
39-
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /glusterfs-csi-driver cmd/glusterfs/main.go
35+
# Copy source directories
36+
COPY cmd/ "${SRCDIR}/cmd"
37+
COPY pkg/ "${SRCDIR}/pkg"
38+
COPY scripts/ "${SCRIPTSDIR}"
4039

41-
# Ensure the binary is statically linked
42-
RUN ldd /glusterfs-csi-driver | grep -q "not a dynamic executable"
40+
#-- Test phase
4341

44-
# Run tests
4542
ARG RUN_TESTS=1
46-
RUN [ $RUN_TESTS -eq 0 ] || { \
47-
set -o pipefail \
48-
&& gometalinter -j4 --sort=path --sort=line --sort=column \
49-
--enable="gofmt" \
50-
--exclude="method NodeGetId should be NodeGetID" \
51-
--deadline 9m --vendor --debug ./... \
52-
|& stdbuf -oL awk '/linter took/ || !/^DEBUG/'; \
53-
}
54-
RUN [ $RUN_TESTS -eq 0 ] || { \
55-
GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)"; \
56-
go test -covermode=count -coverprofile=/profile.cov $GOPACKAGES; \
57-
}
43+
ARG GO_METALINTER_THREADS=1
44+
ENV TEST_COVERAGE=stdout \
45+
GO_COVER_DIR=/build/
46+
47+
RUN mkdir /build
48+
RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/lint-go.sh
49+
RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/test-go.sh
5850

51+
#-- Build phase
52+
53+
# Build executable
54+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /build/glusterfs-csi-driver cmd/glusterfs/main.go
55+
56+
# Ensure the binary is statically linked
57+
RUN ldd /build/glusterfs-csi-driver | grep -q "not a dynamic executable"
5958

6059
#-- Final container
61-
FROM centos:7.5.1804
60+
61+
FROM docker.io/centos:7.5.1804 as final
6262

6363
# Install dependencies
6464
RUN yum -y install centos-release-gluster && \
@@ -68,7 +68,7 @@ RUN yum -y install centos-release-gluster && \
6868
rpm -qa | grep gluster | tee /gluster-rpm-versions.txt
6969

7070
# Copy glusterfs-csi-driver from build phase
71-
COPY --from=build /glusterfs-csi-driver /glusterfs-csi-driver
71+
COPY --from=build /build /
7272

7373
# The version of the driver (git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')
7474
ARG version="(unknown)"

scripts/install-go-tools.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
GOPATH=$(go env GOPATH)
4+
GOBINDIR="${GOPATH}/bin"
5+
6+
install_dep() {
7+
DEPVER="${GO_DEP_VERSION}"
8+
if type dep >/dev/null 2>&1; then
9+
local version
10+
version=$(dep version | awk '/^ version/{print $3}')
11+
if [[ "${version}" == "${DEPVER}" || ${version} > ${DEPVER} ]]; then
12+
echo "dep ${DEPVER} or greater is already installed"
13+
return
14+
fi
15+
fi
16+
17+
echo "Installing dep. Version: ${DEPVER:-latest}"
18+
export INSTALL_DIRECTORY="${GOBINDIR}"
19+
export DEP_RELEASE_TAG="${DEPVER}"
20+
curl -L https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
21+
}
22+
23+
install_gometalinter() {
24+
GMLVER="${GO_METALINTER_VERSION}"
25+
if type gometalinter >/dev/null 2>&1; then
26+
echo "gometalinter already installed"
27+
return
28+
fi
29+
30+
echo "Installing gometalinter. Version: ${GMLVER}"
31+
curl -L https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh | bash -s -- -b "${GOBINDIR}" "${GMLVER}"
32+
}
33+
34+
install_dep
35+
install_gometalinter

scripts/lint-go.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
set -o pipefail
4+
5+
if [[ -x "$(command -v gometalinter)" ]]; then
6+
# shellcheck disable=SC2086,SC2068
7+
gometalinter -j ${GO_METALINTER_THREADS:-1} --sort path --sort line --sort column --deadline=9m \
8+
--enable="gofmt" --exclude "method NodeGetId should be NodeGetID" \
9+
--vendor --debug ${@-./...} |& stdbuf -oL grep "linter took\\|:warning:\\|:error:"
10+
else
11+
echo "WARNING: gometalinter not found, skipping lint tests" >&2
12+
fi
File renamed without changes.

test/100-gotest.sh renamed to scripts/test-go.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#!/bin/bash
22

33
GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)"
4+
COVERFILE="${GO_COVER_DIR}profile.cov"
5+
46
# no special options, exec to go test w/ all pkgs
5-
if [[ ${CSI_TEST_EXITFIRST} != "yes" && -z ${CSI_TEST_COVERAGE} ]]; then
7+
if [[ ${TEST_EXITFIRST} != "yes" && -z ${TEST_COVERAGE} ]]; then
68
# shellcheck disable=SC2086
79
exec go test ${GOPACKAGES}
810
fi
911

1012
# our options are set so we need to handle each go package one
1113
# at at time
12-
if [[ ${CSI_TEST_COVERAGE} ]]; then
14+
if [[ ${TEST_COVERAGE} ]]; then
1315
GOTESTOPTS="-covermode=count -coverprofile=cover.out"
14-
COVERFILE=packagecover.out
1516
echo "mode: count" > "${COVERFILE}"
1617
fi
1718

@@ -24,17 +25,17 @@ for gopackage in ${GOPACKAGES}; do
2425
# Append to coverfile
2526
grep -v "^mode: count" cover.out >> "${COVERFILE}"
2627
fi
27-
if [[ ${CSI_TEST_COVERAGE} = "stdout" && -f cover.out ]]; then
28+
if [[ ${TEST_COVERAGE} = "stdout" && -f cover.out ]]; then
2829
go tool cover -func=cover.out
2930
fi
30-
if [[ ${CSI_TEST_COVERAGE} = "html" && -f cover.out ]]; then
31+
if [[ ${TEST_COVERAGE} = "html" && -f cover.out ]]; then
3132
mkdir -p coverage
3233
fn="coverage/${gopackage////-}.html"
3334
echo " * generating coverage html: ${fn}"
3435
go tool cover -html=cover.out -o "${fn}"
3536
fi
3637
rm -f cover.out
37-
if [[ ${failed} -ne 0 && ${CSI_TEST_EXITFIRST} = "yes" ]]; then
38+
if [[ ${failed} -ne 0 && ${TEST_EXITFIRST} = "yes" ]]; then
3839
exit ${failed}
3940
fi
4041
done

test.sh

Lines changed: 0 additions & 115 deletions
This file was deleted.

test/001-testtest.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)