Skip to content

Commit

Permalink
Merge pull request #85 from jarrpa/build-scripts
Browse files Browse the repository at this point in the history
Script tool installation and linting
  • Loading branch information
JohnStrunk committed Nov 5, 2018
2 parents 2016b60 + 34c7af9 commit de19709
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 199 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ install:

script:
# Lint text-like files
- scripts/pre-commit.sh --require-all
- scripts/lint-text.sh --require-all
# Build container w/ Docker
- docker version
- ./build.sh
51 changes: 35 additions & 16 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,53 @@

set -e

# Set driver name
DRIVER="${DRIVER:-glusterfs-csi-driver}"

# Set which docker repo to tag
REPO="${REPO:-gluster/}"

# Allow overriding default docker command
DOCKER_CMD=${DOCKER_CMD:-docker}
RUNTIME_CMD=${RUNTIME_CMD:-docker}

build="build"
if [[ "${RUNTIME_CMD}" == "buildah" ]]; then
build="bud"
fi

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

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

#-- Build final container
$DOCKER_CMD build \
-t glusterfs-csi-driver \
--build-arg RUN_TESTS="$RUN_TESTS" \
--build-arg version="$VERSION" \
--build-arg builddate="$BUILDDATE" \
GO_DEP_VERSION="${GO_DEP_VERSION}"
GO_METALINTER_VERSION="${GO_METALINTER_VERSION:-v2.0.11}"
GO_METALINTER_THREADS=${GO_METALINTER_THREADS:-4}

build_args=()
build_args+=( --build-arg "RUN_TESTS=$RUN_TESTS" )
build_args+=( --build-arg "GO_DEP_VERSION=$GO_DEP_VERSION" )
build_args+=( --build-arg "GO_METALINTER_VERSION=$GO_METALINTER_VERSION" )
build_args+=( --build-arg "GO_METALINTER_THREADS=$GO_METALINTER_THREADS" )
build_args+=( --build-arg "version=$VERSION" )
build_args+=( --build-arg "builddate=$BUILDDATE" )

# Print Docker version
echo "=== $RUNTIME_CMD version ==="
$RUNTIME_CMD version

#-- Build container
$RUNTIME_CMD $build \
-t "${REPO}${DRIVER}" \
"${build_args[@]}" \
-f pkg/glusterfs/Dockerfile \
. \
|| exit 1

# If running tests, extract profile data
if [ "$RUN_TESTS" -ne 0 ]; then
rm -f profile.cov
$DOCKER_CMD build \
-t glusterfs-csi-driver-build \
--target build \
--build-arg RUN_TESTS="$RUN_TESTS" \
-f pkg/glusterfs/Dockerfile \
. \
&& \
$DOCKER_CMD run --rm glusterfs-csi-driver-build \
cat /profile.cov > profile.cov
$RUNTIME_CMD run --entrypoint cat "${REPO}${DRIVER}" \
/profile.cov > profile.cov
fi
62 changes: 31 additions & 31 deletions pkg/glusterfs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,55 @@
# See the License for the specific language governing permissions and
# limitations under the License.

#-- Create build environment

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

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

RUN yum install -y \
git

# Install dep
RUN mkdir -p /go/bin
RUN curl -L https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

# Install gometalinter
ARG GO_METALINTER_VERSION=v2.0.11
RUN curl -L 'https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh' \
| bash -s -- -b "${GOPATH}bin" "${GO_METALINTER_VERSION}"
# Install go tools
ARG GO_DEP_VERSION=
ARG GO_METALINTER_VERSION=latest
COPY scripts/install-go-tools.sh "${SCRIPTSDIR}"
RUN mkdir -p /go/bin; ${SCRIPTSDIR}/install-go-tools.sh

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

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

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

# Run tests
ARG RUN_TESTS=1
RUN [ $RUN_TESTS -eq 0 ] || { \
set -o pipefail \
&& gometalinter -j4 --sort=path --sort=line --sort=column \
--enable="gofmt" \
--exclude="method NodeGetId should be NodeGetID" \
--deadline 9m --vendor --debug ./... \
|& stdbuf -oL awk '/linter took/ || !/^DEBUG/'; \
}
RUN [ $RUN_TESTS -eq 0 ] || { \
GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)"; \
go test -covermode=count -coverprofile=/profile.cov $GOPACKAGES; \
}
ARG GO_METALINTER_THREADS=1
ENV TEST_COVERAGE=stdout \
GO_COVER_DIR=/build/

RUN mkdir /build
RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/lint-go.sh
RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/test-go.sh

#-- Build phase

# Build executable
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /build/glusterfs-csi-driver cmd/glusterfs/main.go

# Ensure the binary is statically linked
RUN ldd /build/glusterfs-csi-driver | grep -q "not a dynamic executable"

#-- Final container
FROM centos:7.5.1804

FROM docker.io/centos:7.5.1804 as final

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

# Copy glusterfs-csi-driver from build phase
COPY --from=build /glusterfs-csi-driver /glusterfs-csi-driver
COPY --from=build /build /

# The version of the driver (git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')
ARG version="(unknown)"
Expand Down
35 changes: 35 additions & 0 deletions scripts/install-go-tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

GOPATH=$(go env GOPATH)
GOBINDIR="${GOPATH}/bin"

install_dep() {
DEPVER="${GO_DEP_VERSION}"
if type dep >/dev/null 2>&1; then
local version
version=$(dep version | awk '/^ version/{print $3}')
if [[ "${version}" == "${DEPVER}" || ${version} > ${DEPVER} ]]; then
echo "dep ${DEPVER} or greater is already installed"
return
fi
fi

echo "Installing dep. Version: ${DEPVER:-latest}"
export INSTALL_DIRECTORY="${GOBINDIR}"
export DEP_RELEASE_TAG="${DEPVER}"
curl -L https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
}

install_gometalinter() {
GMLVER="${GO_METALINTER_VERSION}"
if type gometalinter >/dev/null 2>&1; then
echo "gometalinter already installed"
return
fi

echo "Installing gometalinter. Version: ${GMLVER}"
curl -L https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh | bash -s -- -b "${GOBINDIR}" "${GMLVER}"
}

install_dep
install_gometalinter
12 changes: 12 additions & 0 deletions scripts/lint-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -o pipefail

if [[ -x "$(command -v gometalinter)" ]]; then
# shellcheck disable=SC2086,SC2068
gometalinter -j ${GO_METALINTER_THREADS:-1} --sort path --sort line --sort column --deadline=9m \
--enable="gofmt" --exclude "method NodeGetId should be NodeGetID" \
--vendor --debug ${@-./...} |& stdbuf -oL grep "linter took\\|:warning:\\|:error:"
else
echo "WARNING: gometalinter not found, skipping lint tests" >&2
fi
File renamed without changes.
13 changes: 7 additions & 6 deletions test/100-gotest.sh → scripts/test-go.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/bin/bash

GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)"
COVERFILE="${GO_COVER_DIR}profile.cov"

# no special options, exec to go test w/ all pkgs
if [[ ${CSI_TEST_EXITFIRST} != "yes" && -z ${CSI_TEST_COVERAGE} ]]; then
if [[ ${TEST_EXITFIRST} != "yes" && -z ${TEST_COVERAGE} ]]; then
# shellcheck disable=SC2086
exec go test ${GOPACKAGES}
fi

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

Expand All @@ -24,17 +25,17 @@ for gopackage in ${GOPACKAGES}; do
# Append to coverfile
grep -v "^mode: count" cover.out >> "${COVERFILE}"
fi
if [[ ${CSI_TEST_COVERAGE} = "stdout" && -f cover.out ]]; then
if [[ ${TEST_COVERAGE} = "stdout" && -f cover.out ]]; then
go tool cover -func=cover.out
fi
if [[ ${CSI_TEST_COVERAGE} = "html" && -f cover.out ]]; then
if [[ ${TEST_COVERAGE} = "html" && -f cover.out ]]; then
mkdir -p coverage
fn="coverage/${gopackage////-}.html"
echo " * generating coverage html: ${fn}"
go tool cover -html=cover.out -o "${fn}"
fi
rm -f cover.out
if [[ ${failed} -ne 0 && ${CSI_TEST_EXITFIRST} = "yes" ]]; then
if [[ ${failed} -ne 0 && ${TEST_EXITFIRST} = "yes" ]]; then
exit ${failed}
fi
done
Expand Down
115 changes: 0 additions & 115 deletions test.sh

This file was deleted.

8 changes: 0 additions & 8 deletions test/001-testtest.sh

This file was deleted.

Loading

0 comments on commit de19709

Please sign in to comment.