From 304f4e12ddcb11417d470ba81349ac07fb84e939 Mon Sep 17 00:00:00 2001 From: Andrew Seigner Date: Fri, 23 Feb 2018 10:02:14 -0800 Subject: [PATCH] Make build scripts location-agnostic (#409) The build scripts assume they are executed from the root of this repo. This prevents running scripts from other locations, for example, `cd web && ../bin/go-run .`. Modify the build scripts to work regardless of current directory. Fixes #301 Signed-off-by: Andrew Seigner --- BUILD.md | 40 +++++++++++++++++++++---------------- bin/_docker.sh | 15 +++++++------- bin/_gcp.sh | 5 ----- bin/_log.sh | 2 -- bin/_tag.sh | 12 +++++------ bin/conduit | 11 ++++++---- bin/docker-build | 14 +++++++------ bin/docker-build-base | 9 ++++++--- bin/docker-build-cli | 13 +++++++----- bin/docker-build-cli-bin | 17 +++++++++------- bin/docker-build-controller | 15 ++++++++------ bin/docker-build-go-deps | 11 ++++++---- bin/docker-build-proxy | 11 ++++++---- bin/docker-build-proxy-init | 15 ++++++++------ bin/docker-build-web | 15 ++++++++------ bin/docker-images | 8 +++++--- bin/docker-pull | 6 ++++-- bin/docker-pull-deps | 8 +++++--- bin/docker-push | 6 ++++-- bin/docker-push-deps | 8 +++++--- bin/docker-retag-all | 6 ++++-- bin/go-run | 6 ++++-- bin/mkube | 2 +- bin/protoc | 2 +- bin/root-tag | 6 ++++-- bin/update-go-deps-shas | 8 +++++--- 26 files changed, 158 insertions(+), 113 deletions(-) diff --git a/BUILD.md b/BUILD.md index d06e558e6..f802ef956 100644 --- a/BUILD.md +++ b/BUILD.md @@ -189,13 +189,13 @@ build/run/debug loop faster. In general, replace commands like this: ```bash -go run web/main.go +go run cli/main.go ``` with this: ```bash -bin/go-run web +bin/go-run cli ``` You may also leverage `go-run` to execute our `conduit` cli command. While in a @@ -234,8 +234,8 @@ yarn ```bash cd web/app yarn && yarn webpack -cd ../.. -bin/go-run web +cd .. +../bin/go-run . ``` The web server will be running on `localhost:8084`. @@ -256,7 +256,8 @@ address of the public API server that's running in your docker environment: ```bash docker-compose stop web -bin/go-run web --api-addr=$DOCKER_IP:8085 +cd web +../bin/go-run . --api-addr=$DOCKER_IP:8085 ``` #### 3. Connect to `public-api` in Kubernetes @@ -272,7 +273,8 @@ kubectl -n conduit port-forward $POD_NAME 8085:8085 Then connect the local web process to the forwarded port: ```bash -bin/go-run web --api-addr=localhost:8085 +cd web +../bin/go-run . --api-addr=localhost:8085 ``` ### Webpack dev server @@ -287,7 +289,8 @@ yarn webpack-dev-server And then set the `--webpack-dev-server` flag when running the web server: ```bash -bin/go-run web --webpack-dev-server=http://localhost:8080 +cd web +../bin/go-run . --webpack-dev-server=http://localhost:8080 ``` To add a JS dependency: @@ -368,6 +371,7 @@ cargo check If you make Protobuf changes, run: ```bash +bin/dep ensure bin/protoc-go.sh ``` @@ -410,6 +414,8 @@ build_architecture "_log.sh"; "_tag.sh"; + "dep"; + "docker-build" -> "docker-build-controller"; "docker-build" -> "docker-build-web"; "docker-build" -> "docker-build-proxy"; @@ -442,7 +448,6 @@ build_architecture "docker-build-proxy" -> "_docker.sh"; "docker-build-proxy" -> "_tag.sh"; - "docker-build-proxy" -> "docker-build-base"; "docker-build-proxy" -> "proxy/Dockerfile"; "docker-build-proxy-init" -> "_docker.sh"; @@ -473,6 +478,7 @@ build_architecture "docker-retag-all" -> "_docker.sh"; "go-run" -> ".gorun"; + "go-run" -> "root-tag"; "minikube-start-hyperv.bat"; @@ -484,15 +490,15 @@ build_architecture "root-tag" -> "_tag.sh"; - "travis.yml" -> "_gcp.sh"; - "travis.yml" -> "docker-build"; - "travis.yml" -> "docker-pull"; - "travis.yml" -> "docker-pull-deps"; - "travis.yml" -> "docker-push"; - "travis.yml" -> "docker-push-deps"; - "travis.yml" -> "docker-retag-all"; - "travis.yml" -> "protoc-go.sh"; - "travis.yml" -> "root-tag"; + ".travis.yml" -> "_gcp.sh"; + ".travis.yml" -> "dep"; + ".travis.yml" -> "docker-build"; + ".travis.yml" -> "docker-pull"; + ".travis.yml" -> "docker-pull-deps"; + ".travis.yml" -> "docker-push"; + ".travis.yml" -> "docker-push-deps"; + ".travis.yml" -> "docker-retag-all"; + ".travis.yml" -> "protoc-go.sh"; "update-go-deps-shas" -> "_tag.sh"; "update-go-deps-shas" -> "cli/Dockerfile-bin"; diff --git a/bin/_docker.sh b/bin/_docker.sh index 346e1e567..ed957a775 100644 --- a/bin/_docker.sh +++ b/bin/_docker.sh @@ -1,11 +1,8 @@ -#!/bin/sh -# -# docker -# - set -eu -. bin/_log.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_log.sh # TODO this should be set to the canonical public docker regsitry; we can override this # docker regsistry in, for instance, CI. @@ -42,8 +39,10 @@ docker_build() { output="/dev/stderr" fi - log_debug " :; docker build . -t $repo:$tag -f $file $extra" - docker build . \ + rootdir="$( cd $bindir/.. && pwd )" + + log_debug " :; docker build $rootdir -t $repo:$tag -f $file $extra" + docker build $rootdir \ -t "$repo:$tag" \ -f "$file" \ $extra \ diff --git a/bin/_gcp.sh b/bin/_gcp.sh index 3165e1972..0d42c9bff 100644 --- a/bin/_gcp.sh +++ b/bin/_gcp.sh @@ -1,8 +1,3 @@ -#!/bin/sh -# -# gcp -- mostly for CI -# - set -eu get_k8s_ctx() { diff --git a/bin/_log.sh b/bin/_log.sh index c9baea1df..9c9abe90e 100644 --- a/bin/_log.sh +++ b/bin/_log.sh @@ -1,5 +1,3 @@ -#!/bin/sh - set -eu # build debug logging is disabled by default; enable with BUILD_DEBUG=1 diff --git a/bin/_tag.sh b/bin/_tag.sh index 8b7cba12a..013d8802c 100644 --- a/bin/_tag.sh +++ b/bin/_tag.sh @@ -1,5 +1,3 @@ -#!/bin/sh - set -eu git_sha_head() { @@ -7,7 +5,9 @@ git_sha_head() { } go_deps_sha() { - cat Gopkg.lock Dockerfile-go-deps | shasum - | awk '{print $1}' |cut -c 1-8 + bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + rootdir="$( cd $bindir/.. && pwd )" + cat $rootdir/Gopkg.lock $rootdir/Dockerfile-go-deps | shasum - | awk '{print $1}' |cut -c 1-8 } clean_head() { @@ -59,9 +59,9 @@ validate_tag() { fi } -# These functions should be called by any docker-build-* script that relies on -# Go or Rust dependencies. To confirm the set of scripts that should call this -# function, run: +# This function should be called by any docker-build-* script that relies on Go +# dependencies. To confirm the set of scripts that should call this function, +# run: # $ grep -ER 'docker-build-go-deps' . validate_go_deps_tag() { diff --git a/bin/conduit b/bin/conduit index a4feefb8c..4e1ff2564 100755 --- a/bin/conduit +++ b/bin/conduit @@ -1,13 +1,16 @@ -#!/bin/sh +#!/bin/bash set -eu +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" + system=$(uname -s) if [ "$system" = "Darwin" ]; then - bin=target/cli/darwin/conduit + bin=$rootdir/target/cli/darwin/conduit elif [ "$system" = "Linux" ]; then - bin=target/cli/linux/conduit + bin=$rootdir/target/cli/linux/conduit else echo "unknown system: $system" >&2 exit 1 @@ -15,7 +18,7 @@ fi # build conduit executable if it does not exist if [ ! -f $bin ]; then - bin/docker-build-cli-bin >/dev/null + $bindir/docker-build-cli-bin >/dev/null fi exec $bin "$@" diff --git a/bin/docker-build b/bin/docker-build index 8e3b817b1..a71c0a5cb 100755 --- a/bin/docker-build +++ b/bin/docker-build @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,9 +7,11 @@ if [ $# -ne 0 ]; then exit 64 fi -bin/docker-build-controller -bin/docker-build-web -bin/docker-build-proxy-init -bin/docker-build-cli +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -bin/docker-build-proxy +$bindir/docker-build-controller +$bindir/docker-build-web +$bindir/docker-build-proxy-init +$bindir/docker-build-cli + +$bindir/docker-build-proxy diff --git a/bin/docker-build-base b/bin/docker-build-base index 032dbb5d2..d32348fc6 100755 --- a/bin/docker-build-base +++ b/bin/docker-build-base @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Builds (or pulls) our base runtime docker image. @@ -9,12 +9,15 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" + +. $bindir/_docker.sh tag="2017-10-30.01" if (docker_pull base "${tag}"); then echo "$(docker_repo base):${tag}" else - docker_build base "${tag}" Dockerfile-base + docker_build base "${tag}" $rootdir/Dockerfile-base fi diff --git a/bin/docker-build-cli b/bin/docker-build-cli index 1d9eba089..4274406d1 100755 --- a/bin/docker-build-cli +++ b/bin/docker-build-cli @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,10 +7,13 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" + +. $bindir/_docker.sh +. $bindir/_tag.sh # Build gcr.io/runconduit/cli-bin, which is used by cli/Dockerfile. -bin/docker-build-cli-bin >/dev/null +$bindir/docker-build-cli-bin >/dev/null -docker_build cli "$(head_root_tag)" cli/Dockerfile +docker_build cli "$(head_root_tag)" $rootdir/cli/Dockerfile diff --git a/bin/docker-build-cli-bin b/bin/docker-build-cli-bin index c701d3451..b89b76df3 100755 --- a/bin/docker-build-cli-bin +++ b/bin/docker-build-cli-bin @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,16 +7,19 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" -dockerfile=cli/Dockerfile-bin +. $bindir/_docker.sh +. $bindir/_tag.sh + +dockerfile=$rootdir/cli/Dockerfile-bin validate_go_deps_tag $dockerfile ( - bin/docker-build-base - bin/docker-build-go-deps + $bindir/docker-build-base + $bindir/docker-build-go-deps ) >/dev/null tag="$(head_root_tag)" @@ -26,7 +29,7 @@ ID=$(docker create "$IMG") # copy the newly built conduit cli binaries to the local system for OS in darwin linux windows ; do - DIR="target/cli/${OS}" + DIR="${rootdir}/target/cli/${OS}" mkdir -p "$DIR" if docker cp "$ID:/out/conduit-${OS}" "$DIR/conduit" ; then diff --git a/bin/docker-build-controller b/bin/docker-build-controller index 44d8a91b2..a3717c321 100755 --- a/bin/docker-build-controller +++ b/bin/docker-build-controller @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,16 +7,19 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" -dockerfile=controller/Dockerfile +. $bindir/_docker.sh +. $bindir/_tag.sh + +dockerfile=$rootdir/controller/Dockerfile validate_go_deps_tag $dockerfile ( - bin/docker-build-base - bin/docker-build-go-deps + $bindir/docker-build-base + $bindir/docker-build-go-deps ) >/dev/null tag="$(head_root_tag)" diff --git a/bin/docker-build-go-deps b/bin/docker-build-go-deps index 172c1556e..b01778779 100755 --- a/bin/docker-build-go-deps +++ b/bin/docker-build-go-deps @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Builds (or pulls) our go-deps docker image. @@ -9,13 +9,16 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh +. $bindir/_tag.sh tag=$(go_deps_sha) if (docker_pull go-deps "${tag}"); then echo "$(docker_repo go-deps):${tag}" else - docker_build go-deps "${tag}" Dockerfile-go-deps + rootdir="$( cd $bindir/.. && pwd )" + docker_build go-deps "${tag}" $rootdir/Dockerfile-go-deps fi diff --git a/bin/docker-build-proxy b/bin/docker-build-proxy index 51632b2ae..b370281f0 100755 --- a/bin/docker-build-proxy +++ b/bin/docker-build-proxy @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,9 +7,12 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" -docker_build proxy "$(head_root_tag)" proxy/Dockerfile \ +. $bindir/_docker.sh +. $bindir/_tag.sh + +docker_build proxy "$(head_root_tag)" $rootdir/proxy/Dockerfile \ --build-arg="PROXY_SKIP_TESTS=${PROXY_SKIP_TESTS:-}" \ --build-arg="PROXY_UNOPTIMIZED=${PROXY_UNOPTIMIZED:-}" diff --git a/bin/docker-build-proxy-init b/bin/docker-build-proxy-init index d5954fbfd..ee5d76b40 100755 --- a/bin/docker-build-proxy-init +++ b/bin/docker-build-proxy-init @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,16 +7,19 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" -dockerfile=proxy-init/Dockerfile +. $bindir/_docker.sh +. $bindir/_tag.sh + +dockerfile=$rootdir/proxy-init/Dockerfile validate_go_deps_tag $dockerfile ( - bin/docker-build-base - bin/docker-build-go-deps + $bindir/docker-build-base + $bindir/docker-build-go-deps ) >/dev/null docker_build proxy-init "$(head_root_tag)" $dockerfile diff --git a/bin/docker-build-web b/bin/docker-build-web index 0201dafd0..23d4789c2 100755 --- a/bin/docker-build-web +++ b/bin/docker-build-web @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -7,16 +7,19 @@ if [ $# -ne 0 ]; then exit 64 fi -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +rootdir="$( cd $bindir/.. && pwd )" -dockerfile=web/Dockerfile +. $bindir/_docker.sh +. $bindir/_tag.sh + +dockerfile=$rootdir/web/Dockerfile validate_go_deps_tag $dockerfile ( - bin/docker-build-base - bin/docker-build-go-deps + $bindir/docker-build-base + $bindir/docker-build-go-deps ) >/dev/null tag="$(head_root_tag)" diff --git a/bin/docker-images b/bin/docker-images index a3435ff9f..a29acdbf7 100755 --- a/bin/docker-images +++ b/bin/docker-images @@ -1,9 +1,11 @@ -#!/bin/sh +#!/bin/bash set -eu -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh +. $bindir/_tag.sh docker_image() { repo="$(docker_repo "$1")" diff --git a/bin/docker-pull b/bin/docker-pull index f3ceb8f73..9fef5f2bb 100755 --- a/bin/docker-pull +++ b/bin/docker-pull @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -9,7 +9,9 @@ else exit 64 fi -. bin/_docker.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh docker_pull proxy "${tag}" || true docker_pull proxy-init "${tag}" || true diff --git a/bin/docker-pull-deps b/bin/docker-pull-deps index a39d3fb63..4070deaf6 100755 --- a/bin/docker-pull-deps +++ b/bin/docker-pull-deps @@ -1,9 +1,11 @@ -#!/bin/sh +#!/bin/bash set -eu -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh +. $bindir/_tag.sh docker_pull base 2017-10-30.01 || true docker_pull go-deps "$(go_deps_sha)" || true diff --git a/bin/docker-push b/bin/docker-push index 49745389f..e133f57b5 100755 --- a/bin/docker-push +++ b/bin/docker-push @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -9,7 +9,9 @@ else exit 64 fi -. bin/_docker.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh docker_push proxy "${tag}" docker_push proxy-init "${tag}" diff --git a/bin/docker-push-deps b/bin/docker-push-deps index 068d6d5d6..5165c740e 100755 --- a/bin/docker-push-deps +++ b/bin/docker-push-deps @@ -1,9 +1,11 @@ -#!/bin/sh +#!/bin/bash set -eu -. bin/_docker.sh -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh +. $bindir/_tag.sh docker_push base 2017-10-30.01 docker_push go-deps "$(go_deps_sha)" diff --git a/bin/docker-retag-all b/bin/docker-retag-all index c29727457..aec8d4f2c 100755 --- a/bin/docker-retag-all +++ b/bin/docker-retag-all @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu @@ -9,7 +9,9 @@ fi from="${1}" to="${2}" -. bin/_docker.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_docker.sh docker_retag proxy "$from" "$to" docker_retag proxy-init "$from" "$to" diff --git a/bin/go-run b/bin/go-run index a38b3a85a..e71e72472 100755 --- a/bin/go-run +++ b/bin/go-run @@ -1,13 +1,15 @@ -#!/bin/sh +#!/bin/bash set -eu +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + if [ "$#" -eq 0 ]; then echo "Usage: bin/go-run path/to/main [args]" >&2 exit 1 fi -ldflags="-X github.com/runconduit/conduit/pkg/version.Version=$(bin/root-tag)" +ldflags="-X github.com/runconduit/conduit/pkg/version.Version=$($bindir/root-tag)" go build -v -i -race -o .gorun -ldflags "$ldflags" "./$1" shift exec ./.gorun "$@" diff --git a/bin/mkube b/bin/mkube index e23b19be9..6b702a770 100755 --- a/bin/mkube +++ b/bin/mkube @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # A wrapper for interacting with minikube. # diff --git a/bin/protoc b/bin/protoc index ec6721f01..5cb9f6717 100755 --- a/bin/protoc +++ b/bin/protoc @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -eu diff --git a/bin/root-tag b/bin/root-tag index ba28a7193..c805d46c0 100755 --- a/bin/root-tag +++ b/bin/root-tag @@ -1,5 +1,7 @@ -#!/bin/sh +#!/bin/bash -. bin/_tag.sh +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +. $bindir/_tag.sh head_root_tag diff --git a/bin/update-go-deps-shas b/bin/update-go-deps-shas index ca485aa9e..5baf4657c 100755 --- a/bin/update-go-deps-shas +++ b/bin/update-go-deps-shas @@ -1,12 +1,14 @@ -#!/bin/sh +#!/bin/bash set -eu # Updates the tag for `runconduit/go-deps` across all Dockerfiles in this repository. -sha=$(. bin/_tag.sh ; go_deps_sha) +bindir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -for f in $( grep -lR --include=Dockerfile\* go-deps: . ) ; do +sha=$(. $bindir/_tag.sh ; go_deps_sha) + +for f in $( grep -lR --include=Dockerfile\* go-deps: $bindir/.. ) ; do sed -E -i.bak -e "s|runconduit/go-deps:[^ ]+|runconduit/go-deps:${sha}|" "$f" rm "$f".bak done