Skip to content

Instantly share code, notes, and snippets.

@stevecrozz
Last active March 11, 2016 23:37
Show Gist options
  • Select an option

  • Save stevecrozz/955b12d0c8824e95dfef to your computer and use it in GitHub Desktop.

Select an option

Save stevecrozz/955b12d0c8824e95dfef to your computer and use it in GitHub Desktop.
#! /bin/bash
CONSUL_ENV_CMD=$(cat <<EOF
bundle exec cmdb shim --env \
--user www-data \
--consul-url=http://consul:8500 \
--consul-prefix ${RS_DEPLOYMENT_UNIT}/library \
--consul-prefix ${RS_DEPLOYMENT_UNIT}/common
EOF
)
if getent hosts consul
then
CMD_PREFIX="$CONSUL_ENV_CMD -- "
else
CMD_PREFIX=""
fi
print-inputs() {
declare desc="Print the ENV"
env | sort
echo
}
yaml-esque-get() {
declare desc="Get key value from colon-separated structure"
declare key="$1"
local inputkey
local cmd
while read line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.* ]] && continue
inputkey=${line%%:*}
cmd=${line#*:}
[[ "$inputkey" == "$key" ]] && echo "$cmd"
done <<< "$(cat)"
}
procfile-parse() {
declare desc="Get command string for a process type from .cmd"
declare type="$1"
cat ".cmd" | yaml-esque-get "$type"
}
get-command() {
declare desc="Get the command mapping for a command word"
declare type="$1"
if [ -z "$1" ]
then word="$(procfile-parse "web")"
else word="$(procfile-parse "$1")"
fi
if [ -z "$word" ]
then cmd="$1"
else cmd="$word"
fi
echo "$cmd"
}
run-commands() {
declare desc="Run command words"
declare type="$1"
echo "command.sh: Printing inputs"
print-inputs
# eval all the words, but exec the last one
until [ -z "$1" ]
do
cmd="$CMD_PREFIX$(eval echo $(get-command "$1"))"
echo "command.sh: Command word is '"$1"', running '$cmd'"
if [ "$#" -gt "1" ]
then eval "$(eval echo "$cmd")" || echo "Process exited with status $?" && exit 1
else exec $cmd
fi
shift
done
}
main() {
declare desc="Kick off the entrypoint script"
declare type="$@"
if [ $# -eq 0 ]; then
echo "command.sh: No command words specified. Exiting"
else
run-commands "$@"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment