groupon/ansible-silo

View on GitHub
silo/bundle/bin/starter

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash
#
# Starts the ansible-silo bundle {{ BUNDLE_IMAGE_SHORT }}
#
# Copyright (c) 2017, Groupon, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 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.
#
# Neither the name of GROUPON 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.

#######################################
# Generates the docker-run command to start the ansible-silo bundle
# Globals:
#   None
# Arguments:
#   $@ - Anything that was passed to the script
# Returns:
#   None
#######################################
ansible_silo_bundle_starter() {
  local command runner_path tty

  # SILO_DEBUG can be set by the user to enable debug output
  if [[ ! -z "${SILO_DEBUG}" ]]; then
    # Output all defined evn vars starting with SILO_
    echo "SILO vars:"
    # shellcheck disable=SC2030
    for var in $( (set -o posix; set) | grep = | cut -d '=' -f 1 ); do
      if [[ "${var}" == "SILO_"* ]]; then
        echo " - ${var}=${!var}"
      fi
    done
    echo ""
  fi

  # SILO_IMAGE is replaced with the value defined in Dockerfile
  BUNDLE_IMAGE="{{ BUNDLE_IMAGE }}"
  BUNDLE_IMAGE_SHORT="${BUNDLE_IMAGE##*/}"
  BUNDLE_VERSION="{{ BUNDLE_VERSION }}"

  if [ -t 0 ]; then
    tty="--tty"
  fi

  runner_path="/tmp/${BUNDLE_IMAGE_SHORT}-runner-${BUNDLE_VERSION}"
  if [[ -f "${runner_path}" ]]; then
    if [[ ! -z "${SILO_DEBUG}" ]]; then
      echo -e "Runner file already exists.\\n"
    fi
    command="${runner_path}"
    # shellcheck disable=SC2031
    for var in "$@"; do
      command+=" \"${var}\""
    done
  else
    if [[ ! -z "${SILO_DEBUG}" ]]; then
      echo -e "Requesting runner creation. Starting container with --run.\\n"
    fi
    # shellcheck disable=SC2031
    command="$(docker run --interactive ${tty} --rm --volume "/tmp:/tmp" \
      "${BUNDLE_IMAGE}:${BUNDLE_VERSION}" --run "$@")"
  fi

  if [[ ! -z "${SILO_DEBUG}" ]]; then
    echo "Executing: ${command}"
  fi

  bash -c "$(echo "${command}" | tr -d "\\r")"
  exit
}

# shellcheck disable=SC2031
ansible_silo_bundle_starter "$@"