#!/bin/bash
# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

#
# Script to upload directory tarball of puppet modules to a swift container.
#
set -eu
set -o pipefail

SCRIPT_NAME=$(basename $0)
SCRIPT_HOME=$(cd $(dirname $0); pwd)
PUPPET_MODULES_TMP=''

function clean {
    [ -z "${PUPPET_MODULES_TMP}" ] || rm -rf ${PUPPET_MODULES_TMP}
}
trap clean SIGINT SIGTERM EXIT

function show_options {
    echo "Usage: $SCRIPT_NAME <directory>"
    echo
    echo "Options:"
    echo "    -h, --help                  -- print this help."
    echo "    -d <directory>              -- Puppet modules directory. Required."
    echo "    -c <container>              -- Swift container to use."
    echo "      Default: overcloud-artifacts"
    echo "    --environment <env>         -- Generate this heat <env>"
    echo "      Default: $HOME/.tripleo/environments/puppet-modules-url.yaml"
    echo "    --seconds <seconds>         -- Number of seconds for temp URL."
    echo "      Default: 31536000 (one year)"
    echo
    echo "Upload a directory tarball of puppet modules to a swift container"
    echo "and generate a heat environment file containing the required"
    echo "DeployArtifactURLs parameter so that it is used for TripleO deployment."
    echo
    exit $1
}

TEMP=`getopt -o h:ed:c:s: -l help,environment:,directory:,container:,seconds: -n $SCRIPT_NAME -- "$@"`
if [ $? != 0 ]; then
    echo "Terminating..." >&2
    exit 1
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

ENVIRONMENT_FILE="$HOME/.tripleo/environments/puppet-modules-url.yaml"
MODULES_DIRECTORY=
CONTAINER_NAME=overcloud-artifacts
SECONDS=31536000

while true ; do
    case "$1" in
        -h|--help) show_options 0 >&2;;
        -e|--environment) ENVIRONMENT_FILE=$2 ; shift 2;;
        -d|--directory) MODULES_DIRECTORY=$2 ; shift 2;;
        -c|--container) CONTAINER_NAME=$2 ; shift 2;;
        -s|--seconds) SECONDS=$2 ; shift 2;;
        --) shift ; break;;
        *) echo "Error: unsupported option $1." ; exit 1;;
    esac
done

: ${MODULES_DIRECTORY:?--directory is required}

modules_directory=${MODULES_DIRECTORY%/}
modules_directory=${modules_directory#/}

: ${CONTAINER_NAME:?No swift --container was specified}

echo "Creating tarball..."
PUPPET_MODULES_TMP=$(mktemp -d -t 'puppet-modules-XXXXXXX')
tar --transform "s|${modules_directory}|etc/puppet/modules|" \
    -czf "$PUPPET_MODULES_TMP/puppet-modules.tar.gz" $MODULES_DIRECTORY
echo "Tarball created."

upload-swift-artifacts -f "$PUPPET_MODULES_TMP/puppet-modules.tar.gz" \
                       -c "$CONTAINER_NAME" \
                       --environment "$ENVIRONMENT_FILE" \
                       --seconds "$SECONDS"
