diff options
-rw-r--r-- | LICENSE.txt | 29 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | interface.d/nexus_raw.sh | 139 | ||||
-rw-r--r-- | nexus_bash.sh | 40 |
4 files changed, 210 insertions, 0 deletions
diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..fab45f8 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2019, Association of Universities for Research in Astronomy (AURA) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..73dd2ed --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# nexus_bash + diff --git a/interface.d/nexus_raw.sh b/interface.d/nexus_raw.sh new file mode 100644 index 0000000..5d6a559 --- /dev/null +++ b/interface.d/nexus_raw.sh @@ -0,0 +1,139 @@ +#!/bin/bash +# +# Nexus RAW repository interface +# + + +# Check existence of upstream file or directory +# @param url: path relative to NEXUS_URL (i.e. "repository/my_data", "repository/my_data/file.txt") +# @return non-zero indicates failure +nexus_raw_provides() { + local url + local response + + url="${NEXUS_URL}/$1" + if [[ -z $url ]]; then + echo "nexus_raw_provides: requires a path relative to $NEXUS_URL" >&2 + return 1 + fi + + response=$(curl -s --head "$url" | head -n 1 | awk '{print $2}') + if (( $response != 200 )); then + return 1 + fi + return 0 +} + +# Download a file +# @param url: +# @param filename: +# @param dest: +# @return +# 1: path to file +# 2: non-zero indicates failure +nexus_raw_download() { + local url + local filename + local dest + + url="${NEXUS_URL}/$1" + if [[ -z $url ]]; then + echo "nexus_raw_download: requires a path relative to $NEXUS_URL" >&2 + return 1 + fi + + filename=$(basename $url) + if ! nexus_raw_provides "$1"; then + echo "${url}: not found on remote server" >&2 + return 1 + fi + + dest="$2" + if [[ -z "${dest}" ]]; then + dest="." + elif [[ ! -d "${dest}" ]]; then + mkdir -p "${dest}" + fi + + if ! curl -L "$url" > "${dest}/${filename}"; then + echo "Could not download data" >&2 + return 1 + fi + + echo "$dest/$filename" + return 0 +} + +# Upload a file +# @param url: path relative to NEXUS_URL (i.e. "repository/my_data") +# @param filename: local file to upload +# @return non-zero indicates failure +nexus_raw_upload() { + local url + local filename + + url="${NEXUS_URL}/$2" + filename="$1" + + if [[ -z $url ]]; then + echo "nexus_raw_upload: requires a path relative to $NEXUS_URL" >&2 + return 1 + fi + + if (( NEXUS_BASH_VERBOSE )); then + /bin/echo -n "Uploading '$filename' => '${2}'... " + fi + if ! curl -s --user "${NEXUS_AUTH}" --upload-file "${filename}" "${url}/${filename}"; then + if (( NEXUS_BASH_VERBOSE )); then + echo "Failed" >&2 + fi + return 1 + fi + + if (( NEXUS_BASH_VERBOSE )); then + echo "done" + fi + return 0 +} + +# Upload a directory tree +# @param src: path to local directory +# @param dest: path relative to NEXUS_URL (i.e. "repository/my_data") +# @return non-zero indicates failure +nexus_raw_upload_dir() { + local src + local dest + local status_callback + src="$1" + dest="$2" + + if [[ ! -d "$src" ]]; then + echo "nexus_raw_upload_dir: source directory does not exist: ${src}" >&2 + return 1 + fi + + if [[ -z "$dest" ]]; then + echo "nexus_raw_upload_dir: requires a destination relative to ${NEXUS_URL}" >&2 + return 1 + fi + + local failures + local paths + paths=($(find "${src}" -type f -print)) + + failures=0 + for (( i=0; i < ${#paths[@]}; i++ )); do + local retval + local f + f="${paths[$i]}" + + if ! nexus_raw_upload "$f" "$dest"; then + (( failures++ )) + fi + done + + if (( failures )); then + return 1 + fi + return 0 +} diff --git a/nexus_bash.sh b/nexus_bash.sh new file mode 100644 index 0000000..79cbc0c --- /dev/null +++ b/nexus_bash.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# +# Global Variables +# +NEXUS_BASH_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +NEXUS_BASH_INTERFACE_ROOT="${NEXUS_BASH_ROOT}/interface.d" +NEXUS_BASH_VERBOSE=0 + +# +# Global Arguments +# +# NEXUS_URL: str +# HTTP[S] address of the Nexus server +# e.g. NEXUS_URL="https://nexus.server.tld" +# +NEXUS_URL=${NEXUS_URL:-} + +# NEXUS_AUTH: str +# Provide user credentials to the Nexus server. Username and password are delimited by ':' +# e.g. NEXUS_AUTH="username:password" +NEXUS_AUTH=${NEXUS_AUTH:-} + +# +# Common Functions +# + +# Check NEXUS_URL is a valid Nexus server +# @return non-zero indicates failure +nexus_available() { + response=$(curl --head -X GET "$NEXUS_URL/service/rest/v1/status" | head -n 1 | awk '{print $2}') + if (( $response != 200 )); then + return 1 + fi + return 0 +} + +# Populate interfaces +for iface in $(find "${NEXUS_BASH_INTERFACE_ROOT}" -type f -name '*.sh'); do + source "$iface" +done |