diff --git a/.circleci/config.yml b/.circleci/config.yml index 9d6c2df50..a1bbc99af 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -55,26 +55,29 @@ jobs: name: detect deadcode without test folder command: | gometalinter --disable-all --skip vendor --skip test -E deadcode ./... - unit-test-golang: docker: - image: circleci/golang:1.10.4 working_directory: /go/src/github.com/dragonflyoss/Dragonfly steps: - checkout + - run: + name: build client + command: | + cd cmd + cd dfget && go build + cd .. + cd dfdaemon && go build - run: name: unit test - command: (cd ./build/client && ./configure && make unit-test) + command: make unit-test - run: name: upload code coverage report command: bash <(curl -s https://codecov.io/bash) - run: name: rm coverage.txt command: rm coverage.txt - - run: - name: build client - command: ./build/build.sh client - + api-integration-test: docker: - image: circleci/golang:1.10.4 diff --git a/.gitignore b/.gitignore index b6119e93f..2ff1d161c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ target **/.DS_Store *.swo *.swp +.cache/ +.go/ +release/ #df-daemon diff --git a/.travis.yml b/.travis.yml index c74e7b21d..d3c85026b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,9 @@ language: java jdk: - oraclejdk8 script: - - export LOG_HOME=/tmp/supernode && ./build/build.sh supernode + - ./hack/check-supernode.sh + - ./hack/compile-supernode.sh + - ./hack/build-supernode-image.sh # - (cd ./build/client && ./configure && make unit-test) && export LOG_HOME=/tmp/supernode && ./build/build.sh # after_success: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4413742bb..6b906e5e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,7 @@ Since you are ready to improve Dragonfly with a PR, we suggest you could take a * [Branch Definition](#branch-definition) * [Commit Rules](#commit-rules) * [PR Description](#pr-description) +* [Developing Environment](#developing-environment) ### Workspace Preparation @@ -139,6 +140,20 @@ No matter commit message, or commit content, we do take more emphasis on code re PR is the only way to make change to Dragonfly project files. To help reviewers better get your purpose, PR description could not be too detailed. We encourage contributors to follow the [PR template](./.github/PULL_REQUEST_TEMPLATE.md) to finish the pull request. +### Developing Environment + +As a contributor, if you want to make any contribution to Dragonfly project, we should reach an agreement on the version of tools used in the development environment. +Here are some dependents with specific version: + +* golang : v1.10.4 +* swagger : 0.17.1 +* markdownlint : v0.5.0 +* misspell : latest +* shellCheck : latest +* docker: latest + +When you develop the Dragonfly project at the local environment, you should use subcommands of Makefile to help yourself to check and build the latest version of Dragonfly. For the convenience of developers, we use the docker to build Dragonfly. It can reduce problems of the developing environment. + ## Engage to help anything We choose GitHub as the primary place for Dragonfly to collaborate. So the latest updates of Dragonfly are always here. Although contributions via PR is an explicit way to help, we still call for any other ways. diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..06988e672 --- /dev/null +++ b/Makefile @@ -0,0 +1,107 @@ +# Copyright The Dragonfly Authors. +# +# 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. + +PKG := github.com/dragonflyoss/Dragonfly +SUPERNODE_SOURCE_HOME="${curDir}/../../src/supernode" +BUILD_IMAGE ?= golang:1.10.4 +GOARCH := $(shell go env GOARCH) +GOOS := $(shell go env GOOS) +BUILD := $(shell git rev-parse HEAD) +BUILD_PATH := release/${GOOS}_${GOARCH} + +LDFLAGS_DFGET = -ldflags "-X ${PKG}/cmd/dfget/app.Build=${BUILD}" +LDFLAGS_DFDAEMON = -ldflags "-X ${PKG}/cmd/dfdaemon/app.Build=${BUILD}" + +ifeq ($(GOOS),darwin) + BUILD_PATH := release +endif + +clean: + @echo "Begin to clean redundant files." + @rm -rf ./release +.PHONY: clean + +check-client: + @echo "Begin to check client code formats." + ./hack/check-client.sh +.PHONY: check + +check-supernode: + @echo "Begin to check supernode code formats." + ./hack/check-supernode.sh + +build-dirs: + @mkdir -p .go/src/$(PKG) .go/bin .cache + @mkdir -p $(BUILD_PATH) +.PHONY: build-dirs + +build-dfget: build-dirs + @echo "Begin to build dfget." + @docker run \ + --rm \ + -ti \ + -u $$(id -u):$$(id -g) \ + -v $$(pwd)/.go:/go \ + -v $$(pwd):/go/src/$(PKG) \ + -v $$(pwd)/$(BUILD_PATH):/go/bin \ + -v $$(pwd)/.cache:/.cache \ + -e GOOS=$(GOOS) \ + -e GOARCH=$(GOARCH) \ + -e CGO_ENABLED=0 \ + -w /go/src/$(PKG) \ + $(BUILD_IMAGE) \ + go install -v -pkgdir /go/pkg $(LDFLAGS_DFGET) ./cmd/dfget +.PHONY: build-dfget + +build-dfdaemon: build-dirs + @echo "Begin to build dfdaemon." + @docker run \ + --rm \ + -ti \ + -u $$(id -u):$$(id -g) \ + -v $$(pwd)/.go:/go \ + -v $$(pwd):/go/src/$(PKG) \ + -v $$(pwd)/$(BUILD_PATH):/go/bin \ + -v $$(pwd)/.cache:/.cache \ + -e GOOS=$(GOOS) \ + -e GOARCH=$(GOARCH) \ + -e CGO_ENABLED=0 \ + -w /go/src/$(PKG) \ + $(BUILD_IMAGE) \ + go install -v -pkgdir /go/pkg $(LDFLAGS_DFDAEMON) ./cmd/dfdaemon +.PHONY: build-dfdaemon + +build-supernode: + ./hack/compile-supernode.sh + ./hack/build-supernode-image.sh +.PHONY: build-supernode + +unit-test: build-dirs + ./hack/unit-test.sh +.PHONY: unit-test + +install: + @echo "Begin to install dfget and dfdaemon." + ./hack/install-client.sh install +.PHONY: install + +uninstall: + @echo "Begin to uninstall dfget and dfdaemon." + ./hack/install-client.sh uninstall +.PHONY: uninstall + +build-client: check-client build-dfget build-dfdaemon +.PHONY: build-client + +build: check-client build-dfget build-dfdaemon check-supernode build-supernode \ No newline at end of file diff --git a/build/build.sh b/build/build.sh deleted file mode 100755 index 719e2c257..000000000 --- a/build/build.sh +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/env bash - -# Copyright The Dragonfly Authors. -# -# 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. - -curDir=$(cd "$(dirname "$0")" && pwd) -BASE_HOME=$(cd "${curDir}"/.. && pwd) - -BUILD_DIR="${BASE_HOME}/build" - -INSTALL_PREFIX="/opt/dragonfly" - -. "${BUILD_DIR}/log.sh" - -printResult() { - filed=$1 - result=$2 - if [ "${result}" -eq 0 ]; then - info "${filed}" "SUCCESS" - else - error "${filed}" "FAILURE" - fi -} - -buildClient() { - field="dfdaemon&dfget" - echo "=====================================================================" - info "${field}" "compiling dfdaemon and dfget..." - cd "${BUILD_DIR}/client" - ./configure --prefix="${INSTALL_PREFIX}" - make - retCode=$? - printResult "${field}" ${retCode} - return ${retCode} -} - -installClient() { - field="install client" - echo "=====================================================================" - info "${field}" "install dfdaemon and dfget..." - cd "${BUILD_DIR}/client" - make install - retCode=$? - test ${retCode} -eq 0 && make clean - printResult "${field}" ${retCode} - return ${retCode} -} - -buildSupernode() { - field="supernode" - echo "=====================================================================" - info "${field}" "compiling source and building image..." - "${BUILD_DIR}/supernode/supernode-build.sh" all - retCode=$? - printResult "${field}" ${retCode} - return ${retCode} -} - -main() { - case "$1" in - install) - installClient - ;; - client) - buildClient - ;; - supernode) - buildSupernode - ;; - *) - buildClient && buildSupernode - ;; - esac -} - -main "$@" - diff --git a/build/client/Makefile b/build/client/Makefile deleted file mode 100644 index 2a47ada04..000000000 --- a/build/client/Makefile +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright The Dragonfly Authors. -# -# 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. - -# build client -# NOTE: All actions of every rule must be started with tabs(not 4 spaces). -# Using command 'cat -e -t Makefile' to show the presence of tabs with `^I`. - -BUILD_CMD=./run.sh - -.PHONY: build -build: pre check dfdaemon dfget - - -.PHONY: pre -pre: - @${BUILD_CMD} pre - -.PHONY: check -check: - @${BUILD_CMD} check - -.PHONY: dfget -dfget: - @${BUILD_CMD} dfget-go - - -.PHONY: dfdaemon -dfdaemon: - @${BUILD_CMD} dfdaemon - -.PHONY: unit-test -unit-test: - @${BUILD_CMD} unit-test - -.PHONY: package -package: - @${BUILD_CMD} package - - -.PHONY: install -install: - @${BUILD_CMD} install - - -.PHONY: uninstall -uninstall: - @${BUILD_CMD} uninstall - - -.PHONY: clean -clean: - @${BUILD_CMD} clean - diff --git a/build/client/configure b/build/client/configure deleted file mode 100755 index cd68ebcb0..000000000 --- a/build/client/configure +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env bash - -# Copyright The Dragonfly Authors. -# -# 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. - -curDir=`cd $(dirname $0) && pwd` -cd ${curDir} - -# -# init build environment variables -# -. ./env.sh - - -# -# set prefix if configured -# -if [ $# -eq 1 ] ;then - prefix=${1#--prefix=} - if [ -n "${prefix}" -a ${prefix:0:1} = "/" ] ;then - INSTALL_HOME=${prefix} - fi -fi -echo "install home: ${INSTALL_HOME}" - -# -# link source directory to temporary build directory -# -test -d ${BUILD_SOURCE_HOME%Dragonfly*} || mkdir -p ${BUILD_SOURCE_HOME%Dragonfly*} -test -L ${BUILD_SOURCE_HOME} && rm -f ${BUILD_SOURCE_HOME} -echo "link ${BUILD_SOURCE_HOME} -> ${DRAGONFLY_HOME}" -ln -s ${DRAGONFLY_HOME} ${BUILD_SOURCE_HOME} - -# -# generate ${CONFIGURED_VARIABLES_FILE} -# -echo "#!/usr/bin/env bash" > ${CONFIGURED_VARIABLES_FILE} -echo "INSTALL_HOME=${INSTALL_HOME}" >> ${CONFIGURED_VARIABLES_FILE} -chmod +x ${CONFIGURED_VARIABLES_FILE} - - diff --git a/build/client/env.sh b/build/client/env.sh deleted file mode 100755 index b5d42b870..000000000 --- a/build/client/env.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# Copyright The Dragonfly Authors. -# -# 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. - -# -# env.sh must be executed in its parent directory -# -curDir=$(pwd) - -export DRAGONFLY_HOME=${curDir%/build/client*} -export BUILD_GOPATH=/tmp/dragonfly/build -export BUILD_SOURCE_HOME="${BUILD_GOPATH}/src/github.com/dragonflyoss/Dragonfly" - -export INSTALL_HOME="/opt/dragonfly" - -export CONFIGURED_VARIABLES_FILE="${BUILD_GOPATH}/configured_variables.sh" - -# -# source directories -# -export GO_SOURCE_DIRECTORIES=( \ - "dfdaemon" \ - "dfget" \ - "version" \ -) - diff --git a/build/client/run.sh b/build/client/run.sh deleted file mode 100755 index 19e39d721..000000000 --- a/build/client/run.sh +++ /dev/null @@ -1,195 +0,0 @@ -#!/usr/bin/env bash - -# Copyright The Dragonfly Authors. -# -# 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 -e - -curDir=$(cd "$(dirname "$0")" && pwd) -cd "${curDir}" - -# -# init build environment variables -# -. ./env.sh - -# -# init configured variables -# -test -e "${CONFIGURED_VARIABLES_FILE}" || (echo "ERROR: must execute './configure' before '$0'" && exit 2) -. "${CONFIGURED_VARIABLES_FILE}" - -# -# ============================================================================= -# scripts' variables - -export GOPATH=${BUILD_GOPATH}:${GOPATH} - -BIN_DIR=${BUILD_GOPATH}/bin -PKG_DIR=${BUILD_GOPATH}/package - -DFDAEMON_BINARY_NAME=dfdaemon -DFGET_BINARY_NAME=dfget-go - -PKG_NAME=df-client - -# -# ============================================================================= -# build commands - -pre() { - echo "PRE: clean and create ${BIN_DIR}" - createDir "${BIN_DIR}" -} - -check() { - cd "${BUILD_SOURCE_HOME}" - exclude="vendor/" - - # gofmt - echo "CHECK: gofmt, check code formats" - result=$(find . -name '*.go' | grep -vE "${exclude}" | xargs gofmt -s -l -d 2>/dev/null) - [ ${#result} -gt 0 ] && (echo "${result}" \ - && echo "CHECK: please format Go code with 'gofmt -s -w .'" && false) - - # golint - which golint > /dev/null || export PATH=${BUILD_GOPATH}:$PATH - which golint > /dev/null || (echo "CHECK: install golint" \ - && go get -u golang.org/x/lint/golint; \ - cp "${BUILD_GOPATH}/bin/golint" "${BUILD_GOPATH}"/) - - echo "CHECK: golint, check code style" - result=$(go list ./... | grep -vE "${exclude}" | sed 's/^_//' | xargs golint) - [ ${#result} -gt 0 ] && (echo "${result}" && false) - - # go vet check - echo "CHECK: go vet, check code syntax" - packages=$(go list ./... | grep -vE "${exclude}" | sed 's/^_//') - echo "${packages}" | xargs go vet 2>&1 -} - -dfdaemon() { - echo "BUILD: dfdaemon" - test -f "${BIN_DIR}/${DFDAEMON_BINARY_NAME}" && rm -f "${BIN_DIR}/${DFDAEMON_BINARY_NAME}" - cd "${BUILD_SOURCE_HOME}/cmd/dfdaemon" - go build -o "${BIN_DIR}/${DFDAEMON_BINARY_NAME}" - chmod a+x "${BIN_DIR}/${DFDAEMON_BINARY_NAME}" -} - -dfget() { - echo "BUILD: dfget" - dfgetDir="${BIN_DIR}/${PKG_NAME}" - createDir "${dfgetDir}" - cp -r "${BUILD_SOURCE_HOME}/src/getter/*" "${dfgetDir}" - find "${dfgetDir}" -name '*.pyc' -exec rm -f {} \; - chmod a+x "${dfgetDir}/dfget" -} - -dfget-go() { - echo "BUILD: dfget-go" - test -f "${BIN_DIR}/${DFGET_BINARY_NAME}" && rm -f "${BIN_DIR}/${DFGET_BINARY_NAME}" - cd "${BUILD_SOURCE_HOME}/cmd/dfget" - go build -o "${BIN_DIR}/${DFGET_BINARY_NAME}" - chmod a+x "${BIN_DIR}/${DFGET_BINARY_NAME}" -} - -unit-test() { - echo "TEST: unit test" - cd "${BUILD_SOURCE_HOME}" - go test -i ./... - - cmd="go list ./... | grep 'github.com/dragonflyoss/Dragonfly/'" - sources="${GO_SOURCE_DIRECTORIES[*]}" - sources="${sources// /|}" - test -n "${sources}" && cmd+=" | grep -E '${sources}'" - - for d in $(eval "${cmd}") - do - go test -race -coverprofile=profile.out -covermode=atomic "${d}" - if [ -f profile.out ] ; then - cat profile.out >> coverage.txt - rm profile.out > /dev/null 2>&1 - fi - done -} - -package() { - createDir "${PKG_DIR}"/"${PKG_NAME}" - cp -r "${BIN_DIR}/${PKG_NAME}/*" "${PKG_DIR}/${PKG_NAME}/" - cp "${BIN_DIR}"/"${DFDAEMON_BINARY_NAME}" "${PKG_DIR}/${PKG_NAME}/" - cp "${BIN_DIR}"/"${DFGET_BINARY_NAME}" "${PKG_DIR}/${PKG_NAME}/" - - cd "${PKG_DIR}" && tar czf "${INSTALL_HOME}"/"${PKG_NAME}".tar.gz ./${PKG_NAME} - rm -rf "${PKG_DIR}" -} - -install() { - installDir="${INSTALL_HOME}/${PKG_NAME}" - echo "INSTALL: ${installDir}" - createDir "${installDir}" - # cp -r ${BIN_DIR}/${PKG_NAME}/* ${installDir} - cp "${BIN_DIR}"/${DFDAEMON_BINARY_NAME} "${installDir}" - cp "${BIN_DIR}"/${DFGET_BINARY_NAME} "${installDir}" - - createLink "${installDir}/${DFDAEMON_BINARY_NAME}" /usr/local/bin/dfdaemon - createLink "${installDir}/${DFGET_BINARY_NAME}" /usr/local/bin/dfget -} - -uninstall() { - echo "unlink /usr/local/bin/dfdaemon" - test -e /usr/local/bin/dfdaemon && unlink /usr/local/bin/dfdaemon - echo "unlink /usr/local/bin/dfget" - test -e /usr/local/bin/dfget && unlink /usr/local/bin/dfget - - echo "uninstall dragonfly: ${INSTALL_HOME}" - test -d "${INSTALL_HOME}" && rm -rf "${INSTALL_HOME}" -} - -clean() { - echo "delete ${BUILD_GOPATH}" - test -d "${BUILD_GOPATH}" && rm -rf "${BUILD_GOPATH}" -} - -# -# ============================================================================= -# - -createLink() { - srcPath="$1" - linkPath="$2" - - echo "create link ${linkPath} to ${srcPath}" - test -e "${linkPath}" && unlink "${linkPath}" - ln -s "${srcPath}" "${linkPath}" -} - -createDir() { - test -e "$1" && rm -rf "$1" - mkdir -p "$1" -} - -COMMANDS="pre|check|dfdaemon|dfget|dfget-go|unit-test|package|install|uninstall|clean" -usage() { - echo "Usage: $0 [${COMMANDS}]" - exit 1 -} - - -main() { - cmd="${COMMANDS}" - test -z "$1" && usage - $1 -} - -main "$@" diff --git a/build/supernode/supernode-build.sh b/build/supernode/supernode-build.sh deleted file mode 100755 index 28f8a8d0f..000000000 --- a/build/supernode/supernode-build.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env bash - -# Copyright The Dragonfly Authors. -# -# 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. - -curDir=$(cd "$(dirname "$0")" && pwd) -SUPERNODE_SOURCE_HOME="${curDir}/../../src/supernode" - -ACTION=$1 - -. "${curDir}/../log.sh" - -usage() { - echo "Usage: $0 {source|image|all}" - echo " source compile supernode's source" - echo " image build docker image of supernode" - echo " all compile source and build image" - exit 2; # bad usage -} - -if [ $# -lt 1 ]; then - usage -fi - -compileSupernode() { - echo "=====================================================================" - info "supernode:source" "compiling source..." - mvn clean package -} - -buildDockerImage() { - echo "=====================================================================" - info "supernode:image" "building image..." - mvn clean package -DskipTests docker:build -} - -check() { - which docker > /dev/null && docker ps > /dev/null 2>&1 - if [ $? != 0 ]; then - echo "Please install docker and start docker daemon first." && exit 3 - fi -} - -main() { - cd "${SUPERNODE_SOURCE_HOME}" - case "${ACTION}" in - source) - compileSupernode - ;; - image) - check && buildDockerImage - ;; - all) - check && compileSupernode && buildDockerImage - ;; - *) - usage - ;; - esac -} - -main - diff --git a/hack/build-supernode-image.sh b/hack/build-supernode-image.sh new file mode 100755 index 000000000..43b4f2aad --- /dev/null +++ b/hack/build-supernode-image.sh @@ -0,0 +1,10 @@ +#!/bin/bash +curDir=$(cd "$(dirname "$0")" && pwd) +SUPERNODE_SOURCE_HOME="${curDir}/../src/supernode" + +buildDockerImage() { + cd "${SUPERNODE_SOURCE_HOME}" || return + mvn clean package -DskipTests docker:build +} + +buildDockerImage "$@" diff --git a/hack/check-client.sh b/hack/check-client.sh new file mode 100755 index 000000000..0f8a1db9c --- /dev/null +++ b/hack/check-client.sh @@ -0,0 +1,28 @@ +#!/bin/bash +curDir=$(cd "$(dirname "$0")" && pwd) +cd "${curDir}/../" || return + +check() { + exclude="vendor/" + + # gofmt + echo "CHECK: gofmt, check code formats" + result=$(find . -name '*.go' | grep -vE "${exclude}" | xargs gofmt -s -l -d 2>/dev/null) + [ ${#result} -gt 0 ] && (echo "${result}" \ + && echo "CHECK: please format Go code with 'gofmt -s -w .'" && false) + + # golint + which golint > /dev/null || (echo "CHECK: install golint" \ + && go get -u golang.org/x/lint/golint ) + + echo "CHECK: golint, check code style" + result=$(go list ./... | grep -vE "${exclude}" | sed 's/^_//' | xargs golint) + [ ${#result} -gt 0 ] && (echo "${result}" && false) + + # go vet check + echo "CHECK: go vet, check code syntax" + packages=$(go list ./... | grep -vE "${exclude}" | sed 's/^_//') + echo "${packages}" | xargs go vet 2>&1 +} + +check "$@" diff --git a/hack/check-supernode.sh b/hack/check-supernode.sh new file mode 100755 index 000000000..b70b9d2a6 --- /dev/null +++ b/hack/check-supernode.sh @@ -0,0 +1,9 @@ +#!/bin/bash +check() { + which docker > /dev/null && docker ps > /dev/null 2>&1 + if [ $? != 0 ]; then + echo "Please install docker and start docker daemon first." && exit 3 + fi +} + +check "$@" diff --git a/hack/compile-supernode.sh b/hack/compile-supernode.sh new file mode 100755 index 000000000..53c2bb426 --- /dev/null +++ b/hack/compile-supernode.sh @@ -0,0 +1,10 @@ +#!/bin/bash +curDir=$(cd "$(dirname "$0")" && pwd) +SUPERNODE_SOURCE_HOME="${curDir}/../src/supernode" + +compileSupernode() { + cd "${SUPERNODE_SOURCE_HOME}"|| return + mvn clean package +} + +compileSupernode "$@" diff --git a/build/log.sh b/hack/env.sh similarity index 67% rename from build/log.sh rename to hack/env.sh index 492482d3e..9a2f8adbf 100755 --- a/build/log.sh +++ b/hack/env.sh @@ -1,5 +1,4 @@ -#!/usr/bin/env bash - +#!/bin/bash # Copyright The Dragonfly Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,23 +13,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -# color: normal, red, yellow, green -CN="\033[0;;m" -CR="\033[1;31;m" -CY="\033[1;33;m" -CG="\033[1;32;m" - -log() { - filed=$1 - msg=$2 - echo -e "${CY}BUILD(${filed})${CN}: ${msg}" -} - -info() { - log "$1" "${CG}$2${CN}" -} - -error() { - log "$1" "${CR}$2${CN}" -} +export INSTALL_HOME=/opt/dragonfly +export INSTALL_CLIENT_PATH=df-client +export GO_SOURCE_DIRECTORIES=( \ + "dfdaemon" \ + "dfget" \ + "version" \ +) +GOOS=$(go env GOOS) +GOARCH=$(go env GOARCH) +export GOOS +export GOARCH diff --git a/hack/install-client.sh b/hack/install-client.sh new file mode 100755 index 000000000..bf103f3c0 --- /dev/null +++ b/hack/install-client.sh @@ -0,0 +1,60 @@ +#!/bin/bash +BIN_DIR="../release" +DFDAEMON_BINARY_NAME=dfdaemon +DFGET_BINARY_NAME=dfget + +curDir=$(cd "$(dirname "$0")" && pwd) +cd "${curDir}" || return + +. ./env.sh + +install() { + installDir="${INSTALL_HOME}/${INSTALL_CLIENT_PATH}" + echo "install: ${installDir}" + createDir "${installDir}" + cp "${BIN_DIR}/${GOOS}_${GOARCH}/${DFDAEMON_BINARY_NAME}" "${installDir}" + cp "${BIN_DIR}/${GOOS}_${GOARCH}/${DFGET_BINARY_NAME}" "${installDir}" + + createLink "${installDir}/${DFDAEMON_BINARY_NAME}" /usr/local/bin/dfdaemon + createLink "${installDir}/${DFGET_BINARY_NAME}" /usr/local/bin/dfget +} + +uninstall() { + echo "unlink /usr/local/bin/dfdaemon" + test -e /usr/local/bin/dfdaemon && unlink /usr/local/bin/dfdaemon + echo "unlink /usr/local/bin/dfget" + test -e /usr/local/bin/dfget && unlink /usr/local/bin/dfget + + echo "uninstall dragonfly: ${INSTALL_HOME}" + test -d "${INSTALL_HOME}" && rm -rf "${INSTALL_HOME}" +} + +createLink() { + srcPath="$1" + linkPath="$2" + + echo "create link ${linkPath} to ${srcPath}" + test -e "${linkPath}" && unlink "${linkPath}" + ln -s "${srcPath}" "${linkPath}" +} + +createDir() { + test -e "$1" && rm -rf "$1" + mkdir -p "$1" +} + +main() { + case "$1" in + install) + install + ;; + uninstall) + uninstall + ;; + *) + echo "You must specify the subcommand 'install' or 'uninstall'." + ;; + esac +} + +main "$@" diff --git a/hack/unit-test.sh b/hack/unit-test.sh new file mode 100755 index 000000000..293972a45 --- /dev/null +++ b/hack/unit-test.sh @@ -0,0 +1,27 @@ +#!/bin/bash +curDir=$(cd "$(dirname "$0")" && pwd) +cd "${curDir}" || return + +. ./env.sh + +unit_test() { + echo "TEST: unit test" + cd ../ || return + go test -i ./... + + cmd="go list ./... | grep 'github.com/dragonflyoss/Dragonfly/'" + sources="${GO_SOURCE_DIRECTORIES[*]}" + sources="${sources// /|}" + test -n "${sources}" && cmd+=" | grep -E '${sources}'" + + for d in $(eval "${cmd}") + do + go test -race -coverprofile=profile.out -covermode=atomic "${d}" + if [ -f profile.out ] ; then + cat profile.out >> coverage.txt + rm profile.out > /dev/null 2>&1 + fi + done +} + +unit_test "$@"