Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Update snap build scripts
Browse files Browse the repository at this point in the history
* make SNAP_PATH optional.
* remove options around build-plugins.
* use test as branch name when checking out git ref.
* fix docker test so it works regardless of directory.
* simplify travis.ci config.
  • Loading branch information
nanliu committed Jun 24, 2016
1 parent 902b9ae commit 9399ef1
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 216 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ go:
- 1.6.2
before_install:
- bash scripts/gitcookie.sh
- go get github.com/tools/godep
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/goimports
- go get github.com/smartystreets/goconvey/convey
- if [ ! -d $SNAP_SOURCE ]; then mkdir -p $HOME/gopath/src/github.com/intelsdi-x; ln -s $TRAVIS_BUILD_DIR $SNAP_SOURCE; fi # CI for forks not from intelsdi-x
- go get golang.org/x/tools/cmd/cover
env:
global:
- SNAP_SOURCE=/home/travis/gopath/src/github.com/intelsdi-x/snap
- SNAP_PATH=/home/travis/gopath/src/github.com/intelsdi-x/snap/build
- GO15VENDOREXPERIMENT=1
matrix:
- SNAP_TEST_TYPE=legacy
Expand Down
14 changes: 6 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ default:
deps:
bash -c "./scripts/deps.sh"
test:
export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh $(SNAP_TEST_TYPE)"
bash -c "./scripts/test.sh $(SNAP_TEST_TYPE)"
test-legacy:
export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh legacy"
bash -c "./scripts/test.sh legacy"
test-small:
export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh small"
bash -c "./scripts/test.sh small"
test-medium:
export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh medium"
bash -c "./scripts/test.sh medium"
test-large:
export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh large"
bash -c "./scripts/test.sh large"
check:
$(MAKE) test
all:
bash -c "./scripts/build.sh $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) true"
snap:
bash -c "./scripts/build.sh $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))"
bash -c "./scripts/build_snap.sh"
install:
cp build/bin/snapd /usr/local/bin/
cp build/bin/snapctl /usr/local/bin/
Expand Down
10 changes: 2 additions & 8 deletions scripts/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
FROM golang:latest
FROM golang:latest
ENV GOPATH=$GOPATH:/app
ENV SNAP_PATH=/go/src/github.com/intelsdi-x/snap/build
RUN apt-get update && \
apt-get -y install facter
RUN apt-get update
WORKDIR /go/src/github.com/intelsdi-x/
RUN git clone https://github.com/intelsdi-x/gomit.git
WORKDIR /go/src/github.com/intelsdi-x/snap
ADD . /go/src/github.com/intelsdi-x/snap
RUN go get github.com/tools/godep && \
go get golang.org/x/tools/cmd/goimports && \
go get golang.org/x/tools/cmd/cover && \
go get github.com/smartystreets/goconvey
RUN scripts/deps.sh
RUN make
88 changes: 0 additions & 88 deletions scripts/build.sh

This file was deleted.

26 changes: 20 additions & 6 deletions scripts/build-plugin.sh → scripts/build_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@
#See the License for the specific language governing permissions and
#limitations under the License.

BUILDCMD='go build -a -ldflags "-w"'
BUILDDIR=$1
PLUGIN=$2
PLUGINNAME=`echo $PLUGIN | grep -oh "snap-.*"`
set -e
set -u
set -o pipefail

echo " $PLUGINNAME => $BUILDDIR"
$BUILDCMD -o $BUILDDIR/$PLUGINNAME $PLUGIN || exit 2
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__proj_dir="$(dirname "$__dir")"

# shellcheck source=scripts/common.sh
. "${__dir}/common.sh"

build_dir="${__proj_dir}/build"
plugin_dir="${build_dir}/plugin"

plugin_src_path=$1
plugin_name=$(basename "${plugin_src_path}")
go_build=(go build -a -ldflags "-w")

_debug "plugin source: ${plugin_src_path}"
_info "building ${plugin_name}"

(cd "${plugin_src_path}" && "${go_build[@]}" -o "${plugin_dir}/${plugin_name}" . || exit 1)
67 changes: 67 additions & 0 deletions scripts/build_snap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

#http://www.apache.org/licenses/LICENSE-2.0.txt
#
#
#Copyright 2015 Intel Corporation
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

set -x

git_branch=$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)
git_branch="${git_branch:-test}"
git_sha=$(git log --pretty=format:"%h" -1)
git_version=$(git describe --always --exact-match 2> /dev/null || echo "${git_branch}-${git_sha}")

set -e
set -u
set -o pipefail

__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__proj_dir="$(dirname "$__dir")"

# shellcheck source=scripts/common.sh
. "${__dir}/common.sh"

_info "project path: ${__proj_dir}"

build_dir="${__proj_dir}/build"
bin_dir="${build_dir}/bin"
plugin_dir="${build_dir}/plugin"
go_build=(go build -ldflags "-w -X main.gitversion=${git_version}")

_info "snap build version: ${git_version}"
_info "git commit: $(git log --pretty=format:"%H" -1)"

# Disable CGO for builds.
export CGO_ENABLED=0

# rebuild binaries:
_debug "removing: ${bin_dir:?}/*"
rm -rf "${bin_dir:?}/"*
mkdir -p "${bin_dir}"

_info "building snapd"
"${go_build[@]}" -o "${bin_dir}/snapd" . || exit 1

_info "building snapctl"
(cd "${__proj_dir}/cmd/snapctl" && "${go_build[@]}" -o "${bin_dir}/snapctl" . || exit 1)

# rebuild plugins:
_debug "removing: ${plugin_dir:?}/*"
rm -rf "${plugin_dir:?}/"*
mkdir -p "${plugin_dir}"

_info "building plugins"
find "${__proj_dir}/plugin/" -type d -iname "snap-*" -print0 | xargs -0 -n 1 -I{} "${__dir}/build_plugin.sh" {}
105 changes: 105 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash

set -e
set -u
set -o pipefail

LOG_LEVEL="${LOG_LEVEL:-6}"
NO_COLOR="${NO_COLOR:-}"

trap_exitcode() {
exit $?
}

trap trap_exitcode SIGINT

_go_get() {
local _url=$1
local _util

_util=$(basename "${_url}")

type -p "${_util}" > /dev/null || go get "${_url}" && _debug "go get ${_util} ${_url}"
}

_fmt () {
local color_debug="\x1b[35m"
local color_info="\x1b[32m"
local color_notice="\x1b[34m"
local color_warning="\x1b[33m"
local color_error="\x1b[31m"
local colorvar=color_$1

local color="${!colorvar:-$color_error}"
local color_reset="\x1b[0m"
if [ "${NO_COLOR}" = "true" ] || [[ "${TERM:-}" != "xterm"* ]] || [ -t 1 ]; then
# Don't use colors on pipes or non-recognized terminals
color=""; color_reset=""
fi
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" "${1}")${color_reset}";
}

_debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${*}" 1>&2 || true; }
_info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${*}" 1>&2 || true; }
_notice () { [ "${LOG_LEVEL}" -ge 5 ] && echo "$(_fmt notice) ${*}" 1>&2 || true; }
_warning () { [ "${LOG_LEVEL}" -ge 4 ] && echo "$(_fmt warning) ${*}" 1>&2 || true; }
_error () { [ "${LOG_LEVEL}" -ge 3 ] && echo "$(_fmt error) ${*}" 1>&2 || true; exit 1; }

test_dirs=$(find . -type f -name '*.go' -not -path "./.*" -not -path "*/_*" -not -path "./Godeps/*" -not -path "./vendor/*" -print0 | xargs -0 -n1 dirname| sort -u)

_debug "go code directories:
${test_dirs}"

_go_get() {
local _url=$1
local _util

_util=$(basename "${_url}")

type -p "${_util}" > /dev/null || go get "${_url}" && _debug "go get ${_util} ${_url}"
}

_goimports() {
_go_get golang.org/x/tools/cmd/goimports
test -z "$(goimports -l -d $(find . -type f -name '*.go' -not -path "./vendor/*") | tee /dev/stderr)"
}

_gofmt() {
test -z "$(gofmt -l -d $(find . -type f -name '*.go' -not -path "./vendor/*") | tee /dev/stderr)"
}

_golint() {
_go_get github.com/golang/lint/golint
golint ./...
}

_go_vet() {
go vet ${test_dirs}
}

_go_race() {
go test -race ./...
}

_go_test() {
_info "running test type: ${TEST_TYPE}"
# Standard go tooling behavior is to ignore dirs with leading underscors
for dir in $test_dirs;
do
if [[ -z ${go_cover+x} ]]; then
_debug "running go test with cover in ${dir}"
go test --tags="${TEST_TYPE}" -covermode=count -coverprofile="${dir}/profile.tmp" "${dir}"
if [ -f "${dir}/profile.tmp" ]; then
tail -n +2 "${dir}/profile.tmp" >> "profile-${TEST_TYPE}.cov"
rm "${dir}/profile.tmp"
fi
else
_debug "running go test without cover in ${dir}"
go test --tags="${TEST_TYPE}" "${dir}"
fi
done
}

_go_cover() {
go tool cover -func "profile-${TEST_TYPE}.cov"
}
Loading

0 comments on commit 9399ef1

Please sign in to comment.