diff --git a/compose/run.sh b/compose/run.sh index e08ef12336b..f9456c2fcc7 100755 --- a/compose/run.sh +++ b/compose/run.sh @@ -1,10 +1,29 @@ #!/bin/bash -readonly ME=${0##*/} -DGRAPH_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. -readonly COMPOSE_FILE="./docker-compose.yml" +main() { + setup $@ -if [[ $1 == "-h" || $1 == "--help" ]]; then + set -e + build_compose_tool $@ + build_dgraph_docker_image + launch_environment +} + +setup() { + readonly ME=${0##*/} + DGRAPH_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. + readonly COMPOSE_FILE="./docker-compose.yml" + + if [[ $1 == "-h" || $1 == "--help" ]]; then usage; fi + + check_environment +} + +Info() { + echo -e "INFO: $*" +} + +usage() { cat < /dev/null || \ + { echo "ERROR: 'make' command not not found" 1>&2; exit 1; } + command -v go > /dev/null || \ + { echo "ERROR: 'go' command not not found" 1>&2; exit 1; } + command -v docker-compose > /dev/null || \ + { echo "ERROR: 'docker-compose' command not not found" 1>&2; exit 1; } + ## GOPATH required for locally built docker images + [[ -z "${GOPATH}" ]] && \ + { echo "ERROR: The env var of 'GOPATH' was not defined. Exiting" 1>&2; exit 1; } } -# -# MAIN -# +build_compose_tool() { + ## Always make compose if it doesn't exist + make compose -set -e + ## Create compose file if it does not exist or compose parameters passed + if [[ $# -gt 0 ]] || ! [[ -f $COMPOSE_FILE ]]; then + Info "creating compose file ..." + ./compose "$@" + fi -if [[ $# -gt 0 ]]; then - Info "creating compose file ..." - go build compose.go - ./compose "$@" -fi + if [[ ! -e $COMPOSE_FILE ]]; then + echo >&2 "$ME: no '$COMPOSE_FILE' found" + exit 1 + fi +} -if [[ ! -e $COMPOSE_FILE ]]; then - echo >&2 "$ME: no $COMPOSE_FILE found" +build_dgraph_docker_image() { + ## linux binary required for docker image + export GOOS=linux + Info "rebuilding dgraph ..." + ( cd $DGRAPH_ROOT/dgraph && make install ) +} + +launch_environment() { + # Detect if $GOPATH/bin/$GOOS_$GOARCH path + if [[ -f $GOPATH/bin/linux_amd64/dgraph ]]; then + Info "Found '$GOPATH/bin/linux_amd64/dgraph'. Updating $COMPOSE_FILE." + sed -i 's/\$GOPATH\/bin$/\$GOPATH\/bin\/linux_amd64/' $COMPOSE_FILE + # if no dgraph binary found, abort + elif ! [[ -f $GOPATH/bin/dgraph ]]; then + echo "ERROR: '$GOPATH/bin/dgraph' not found. Exiting" 1>&2 exit 1 -fi + else + Info "Found '$GOPATH/bin/dgraph' + fi -Info "rebuilding dgraph ..." -( cd $DGRAPH_ROOT/dgraph && make install ) + # No need to down existing containers, if any. + # The up command handles that automatically -# No need to down existings containers, if any. -# The up command handles that automatically + Info "Bringing up containers" + docker-compose -p dgraph down + docker-compose --compatibility -p dgraph up --force-recreate --remove-orphans +} -Info "bringing up containers" -docker-compose -p dgraph down -docker-compose --compatibility -p dgraph up --force-recreate --remove-orphans +main $@