Fixing bug in docker-images-collector.sh
[oom/offline-installer.git] / build / build_nexus_blob.sh
index 4b1697e..09ed896 100755 (executable)
 ### This script prepares Nexus repositories data blobs for ONAP
 
 ## The script requires following dependencies are installed: nodejs, jq, docker
-## All required resources are expected in the upper directory
-## created during download procedure as DATA_DIR
-## All lists used must be in project data_lists directory
+## All required resources are expected in the upper directory created during
+## download procedure as DATA_DIR or in the directory given as --input-directory
+## All lists used must be in project data_lists directory or in the directory given
+## as --resource-list-directory
 
 # Fail fast settings
 set -e
@@ -34,18 +35,6 @@ SCRIPT_LOG="/tmp/$(basename $0)_$(eval ${TIMESTAMP}).log"
 # Log everything
 exec &> >(tee -a "${SCRIPT_LOG}")
 
-usage () {
-    echo "  This script is preparing Nexus data blob from docker images and npm and pypi packages"
-    echo "      Usage:"
-    echo "        ./$(basename $0) <project version> [<target>]"
-    echo "      "
-    echo "      Example: ./$(basename $0) onap_3.0.1 /root/nexus_data"
-    echo "      "
-    echo "      Dependencies: nodejs, jq, docker"
-    echo "      "
-    exit 1
-}
-
 # Nexus repository location
 NEXUS_DOMAIN="nexus"
 NEXUS_PORT="8081"
@@ -60,24 +49,53 @@ NEXUS_USERNAME=admin
 NEXUS_PASSWORD=admin123
 NEXUS_EMAIL=admin@example.org
 
-if [ "${1}" == "-h" ] || [ "${1}" == "--help"  ] || [ $# -eq 0 ]; then
-    usage
-else
-    TAG="${1}"
-fi
-
 # Setting paths
 LOCAL_PATH="$(readlink -f $(dirname ${0}))"
+
+#Defaults
 DATA_DIR="$(realpath ${LOCAL_PATH}/../../resources)"
+NEXUS_DATA_DIR="${DATA_DIR}/nexus_data"
+LISTS_DIR="${LOCAL_PATH}/data_lists"
 
-if [ -z "${2}" ]; then
-    NEXUS_DATA_DIR="${DATA_DIR}/nexus_data"
-else
-    NEXUS_DATA_DIR="${2}"
-fi
+usage () {
+    echo "   Example usage: build_nexus_blob.sh -t <tag> --input-directory </path/to/downloaded/files/dir>  --output-directory
+           </path/to/output/dir> --resource-list-directory </path/to/dir/with/resource/list>
+
+     -t | --tag release tag, taken from available on git or placed by data generating script (mandatory) must fallow scheme onap_<semver>
+     -i | --input-directory directory containing file needed to create nexus blob. The structure of this directory must organized as described in build guide
+     -o | --output-directory
+    -rl | --resource-list-directory directory with files containing docker, pypi and rpm lists
+    "
+    exit 1
+}
+
+while [ "$1" != "" ]; do
+    case $1 in
+        -t | --tag )                       shift
+                                           TAG=$1
+                                           ;;
+        -i | --input-directory )           shift
+                                           DATA_DIR=$1
+                                           ;;
+        -o | --output-directory )          shift
+                                           NEXUS_DATA_DIR=$1
+                                           ;;
+        -rl | --resource-list-directory )  shift
+                                           LISTS_DIR=$1
+                                           ;;
+        -h | --help )                      usage
+                                           ;;
+        *)                                 usage
+    esac
+    shift
+done
 
-# Setup directory with resources lists
-LISTS_DIR="${LOCAL_PATH}/data_lists"
+
+# exit if no tag given
+if [ -z ${TAG} ]; then
+    usage
+    exit 1
+fi
 
 # Setup directories with resources for docker, npm and pypi
 NXS_SRC_DOCKER_IMG_DIR="${DATA_DIR}/offline_data/docker_images_for_nexus"
@@ -284,7 +302,7 @@ for IMAGE in $(sed $'s/\r// ; /^#/d' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'
     if [[ ${IMAGE} != *"/"* ]]; then
         PUSH="${DOCKER_REGISTRY}/library/${IMAGE}"
     elif [[ ${IMAGE} == *"${DEFAULT_REGISTRY}"* ]]; then
-       if [[ ${IMAGE} == *"/"*"/"* ]]; then
+        if [[ ${IMAGE} == *"/"*"/"* ]]; then
             PUSH="$(sed 's/'"${DEFAULT_REGISTRY}"'/'"${DOCKER_REGISTRY}"'/' <<< ${IMAGE})"
         else
             PUSH="$(sed 's/'"${DEFAULT_REGISTRY}"'/'"${DOCKER_REGISTRY}"'\/library/' <<< ${IMAGE})"
@@ -311,10 +329,10 @@ echo "Stopping Nexus and returning backups"
 docker stop ${NEXUS_CONT_ID} > /dev/null
 
 # Return backed up configuration files
-mv -f "${HOSTS_BACKUP}" /etc/hosts
+mv -f "/etc/${HOSTS_BACKUP}" /etc/hosts
 
 if [ -f "~/.docker/${DOCKER_CONF_BACKUP}" ]; then
-    mv -f "${DOCKER_CONF_BACKUP}" ~/.docker/config.json
+    mv -f "~/.docker/${DOCKER_CONF_BACKUP}" ~/.docker/config.json
 fi
 
 # Return default settings
@@ -322,3 +340,4 @@ npm config set registry "https://registry.npmjs.org"
 
 echo "Nexus blob is built"
 exit 0
+