Skip to content

Commit

Permalink
exit when no args are passed & improve usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjfocs committed Jul 19, 2019
1 parent e82b0cf commit 1ec0bae
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions docker/pipeline.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
#!/bin/bash
#!/usr/bin/env bash

PROGNAME=$0

usage() {
cat << EOF >&2
Usage: $PROGNAME [-s <dir>][-c <file>]
Usage: $PROGNAME [-c FILE] [-s DIRECTORY]
-s <dir>: ...
-c <file>: ...
-c FILE Credentials file. Default: soweego/importer/resources/credentials.json
-s DIRECTORY Output directory. Default: /tmp/soweego_shared/
EOF
exit 1
}

# Exit when no args are passed
if [ $# -eq 0 ]; then
usage
fi

export DOCKER_SHARED_FOLDER="/tmp/soweego_shared/"
export CREDENTIALS_PATH="soweego/importer/resources/credentials.json"

while getopts :s:c: o; do
while getopts :c:s: o; do
case $o in
(s) export DOCKER_SHARED_FOLDER="$OPTARG";;
(c) export CREDENTIALS_PATH="$OPTARG";;
(s) export DOCKER_SHARED_FOLDER="$OPTARG";;
(*) usage
esac
done
shift "$((OPTIND - 1))"


# Removes oldest backup
PARENT_FOLDER="$(dirname ${DOCKER_SHARED_FOLDER})"
FOLDER_NAME="$(basename ${DOCKER_SHARED_FOLDER})"
NUMBER_OF_BACKUPS=$(find ${PARENT_FOLDER} -maxdepth 1 -name "${FOLDER_NAME}*.tar.bzip2" | wc -l)
NUMBER_OF_BACKUPS="${NUMBER_OF_BACKUPS// /}"
echo "${NUMBER_OF_BACKUPS} backups available"

# Remove the oldest backup
if [[ ${NUMBER_OF_BACKUPS} = "3" ]]; then
to_rem=$(find ${PARENT_FOLDER} -maxdepth 1 -name "${FOLDER_NAME}*.tar.bzip2" | sort | head -n 1)
echo "Deleting older backup: ${to_rem}"
Expand All @@ -40,9 +46,9 @@ fi

(
cd "${PARENT_FOLDER}"
# Creates a backup and resets the docker shared folder
# Back up and reset the Docker shared folder
NOW=$(date +"%Y_%m_%d_%H_%M")
tar -cjvf "${FOLDER_NAME}_${NOW}.tar.bzip2" ${FOLDER_NAME}
tar -cvjf "${FOLDER_NAME}_${NOW}.tar.bzip2" ${FOLDER_NAME}
rm -rf ${FOLDER_NAME}
mkdir -p ${FOLDER_NAME}
)
Expand All @@ -52,12 +58,12 @@ git reset --hard HEAD
git clean -f -d
git pull

# Sets up the credentials file
# Set up the credentials file
if [[ -f "$CREDENTIALS_PATH" ]]; then
cp "${CREDENTIALS_PATH}" "${DOCKER_SHARED_FOLDER}/credentials.json"
fi

# Builds and runs docker
# Build and run Docker
docker build --rm -f "Dockerfile.pipeline" -t maxfrax/soweego:pipeline .
docker run -it --rm --name soweego-pipeline-$RANDOM --volume "${DOCKER_SHARED_FOLDER}":"/app/shared" maxfrax/soweego:pipeline "$@"

0 comments on commit 1ec0bae

Please sign in to comment.