*: do not require env.sh

This removes the need to source env.{sh,fish} when working with hscloud.

This is done by:

 1. Implementing a Go library to reliably detect the location of the
    active hscloud checkout. That in turn is enabled by
    BUILD_WORKSPACE_DIRECTORY being now a thing in Bazel.
 2. Creating a tool `hscloud`, with a command `hscloud workspace` that
    returns the workspace path.
 3. Wrapping this tool to be accessible from Python and Bash.
 4. Bumping all users of hscloud_root to use either the Go library or
    one of the two implemented wrappers.

We also drive-by replace tools/install.sh to be a proper sh_binary, and
make it yell at people if it isn't being ran as `bazel run
//tools:install`.

Finally, we also drive-by delete cluster/tools/nixops.sh which was never used.

Change-Id: I7873714319bfc38bbb930b05baa605c5aa36470a
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1169
Reviewed-by: informatic <informatic@hackerspace.pl>
diff --git a/tools/install.sh b/tools/install.sh
index 6151a38..04b4f79 100755
--- a/tools/install.sh
+++ b/tools/install.sh
@@ -1,14 +1,52 @@
 #!/usr/bin/env bash
+source tools/hscloud/lib.sh || exit 1
 
-set -e -o pipefail
+function main() {
+    # If we're not running from `bazel run/buld`, complain and re-execute
+    # ourselves.
+    #
+    # We do the check fairly low level, as //tools/hscloud:lib.sh will just
+    # fail in this case. We want to be nice.
+    #
+    # This is all mostly copied from the runfiles.bash snippet in
+    # tools/hscloud/lib.sh.
+    f=bazel_tools/tools/bash/runfiles/runfiles.bash
+    if [ ! -e "${RUNFILES_DIR:-/dev/null}/$f" ] && \
+       [ ! -e "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" ] && \
+       [ ! -e "$0.runfiles/$f" ] && \
+       [ ! -e "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" ] && \
+       [ ! -e "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" ]; then
+        echo "Uh-oh, looks like you didn't run this as 'bazel run //tools:install'.">&2
+        echo "Let me fix that for you in a few seconds - but work on your muscle memory, as we'll stop supporting this by some point.">&2
+        sleep 2
+        bazel run //tools:install -- "$@"
+        exit $?
+    fi
 
-if [ -z "$hscloud_root" ]; then
-    echo 2>&1 "Please first source env.sh"
-    exit 1
-fi
+    cd $(hscloud::workspace_location)
+    echo "Building hscloud tools and cluster tools..."
+    bazel build //tools/... //cluster/tools/...
 
-cd "${hscloud_root}"
+    local path_missing=""
+    local path="$(hscloud::workspace_location)/bazel-bin/tools"
+    if [[ ":$PATH:" == *":$path:"* ]]; then
+        path_missing="$path"
+    fi
+    local path="$(hscloud::workspace_location)/bazel-bin/cluster/tools"
+    if [[ ":$PATH:" == *":$path:"* ]]; then
+        if [ -z "$path_missing" ]; then
+            path_missing="$path"
+        else
+            path_missing="$path_missing:$path"
+        fi
+    fi
+    if [ -z "$path_missing" ]; then
+        echo "Tools built correctly, but your PATH should be updated to access them:">&2
+        echo '   PATH="$PATH:'$path_missing'"'
+        echo 'Add the above line to your shell profile, or source env.sh from the root of hscloud.'
+    else
+        echo "Tools built correctly and in PATH. Happy hsclouding!"
+    fi
+}
 
-bazel build //tools/...
-
-cluster/tools/install.sh
+main "$@"