All checks are now done within the commands, and we only need to ensure the config file exists.
215 lines
4.9 KiB
Bash
Executable File
215 lines
4.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
|
|
|
ENV_FILE="$DOKKU_ROOT/$2/ENV"
|
|
ENV_FILE_TEMP="$DOKKU_ROOT/$2/ENV.tmp"
|
|
config_create () {
|
|
[ -f $ENV_FILE ] || {
|
|
echo "-----> Creating $ENV_FILE"
|
|
touch $ENV_FILE
|
|
}
|
|
}
|
|
|
|
config_styled_hash () {
|
|
vars="$1"
|
|
prefix="$2"
|
|
|
|
longest=""
|
|
while read -r word; do
|
|
KEY=`echo $word | cut -d"=" -f1`
|
|
if [ ${#KEY} -gt ${#longest} ]; then
|
|
longest=$KEY
|
|
fi
|
|
done <<< "$vars"
|
|
|
|
while read -r word; do
|
|
KEY=`echo $word | cut -d"=" -f1`
|
|
VALUE=`echo $word | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//"`
|
|
|
|
num_zeros=$((${#longest} - ${#KEY}))
|
|
zeros=" "
|
|
while [ $num_zeros -gt 0 ]; do
|
|
zeros="$zeros "
|
|
num_zeros=$(($num_zeros - 1))
|
|
done
|
|
echo "$prefix$KEY:$zeros$VALUE"
|
|
done <<< "$vars"
|
|
}
|
|
|
|
config_restart_app() {
|
|
APP="$1";
|
|
|
|
if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then
|
|
echo "-----> Releasing $APP ..."
|
|
dokku release $APP
|
|
echo "-----> Release complete!"
|
|
echo "-----> Deploying $APP ..."
|
|
dokku deploy $APP
|
|
echo "-----> Deploy complete!"
|
|
fi
|
|
}
|
|
|
|
config_write() {
|
|
ENV_TEMP="$1"
|
|
echo -e "$ENV_TEMP" | sed '/^$/d' | sort > $ENV_FILE_TEMP
|
|
if ! cmp -s $ENV_FILE $ENV_FILE_TEMP; then
|
|
cp -f $ENV_FILE_TEMP $ENV_FILE
|
|
chmod 600 $ENV_FILE
|
|
config_restart_app $APP
|
|
fi
|
|
rm -f $ENV_FILE_TEMP
|
|
}
|
|
|
|
case "$1" in
|
|
config)
|
|
if [[ -z $2 ]]; then
|
|
echo "Please specify an app to run the command on"
|
|
exit 1
|
|
fi
|
|
APP="$2"
|
|
if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then
|
|
echo "App $APP does not exist"
|
|
exit 1
|
|
fi
|
|
|
|
config_create
|
|
if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then
|
|
echo "$APP has no config vars"
|
|
exit 1
|
|
fi
|
|
|
|
VARS=`cat $ENV_FILE | grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" | cut -d" " -f2-`
|
|
|
|
for var in "$@"; do
|
|
if [[ "$var" == "--shell" ]]; then
|
|
echo $VARS
|
|
exit 0
|
|
fi
|
|
done
|
|
|
|
echo "=== $APP config vars ==="
|
|
config_styled_hash "$VARS"
|
|
;;
|
|
|
|
config:get)
|
|
if [[ -z $2 ]]; then
|
|
echo "Please specify an app to run the command on"
|
|
exit 1
|
|
fi
|
|
APP="$2"
|
|
if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then
|
|
echo "App $APP does not exist"
|
|
exit 1
|
|
fi
|
|
if [[ -z $3 ]]; then
|
|
echo "Usage: dokku config:get APP KEY"
|
|
echo "Must specify KEY."
|
|
exit 1
|
|
fi
|
|
|
|
config_create
|
|
if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then
|
|
exit 1
|
|
fi
|
|
|
|
KEY="$3"
|
|
|
|
cat $ENV_FILE | grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" | grep "^export $KEY=" | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//"
|
|
;;
|
|
|
|
config:set)
|
|
if [[ -z $2 ]]; then
|
|
echo "Please specify an app to run the command on"
|
|
exit 1
|
|
fi
|
|
APP="$2"
|
|
if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then
|
|
echo "App $APP does not exist"
|
|
exit 1
|
|
fi
|
|
if [[ -z "${*:3}" ]]; then
|
|
echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
|
|
echo "Must specify KEY and VALUE to set."
|
|
exit 1
|
|
fi
|
|
|
|
config_create
|
|
ENV_ADD=""
|
|
ENV_TEMP=`cat "${ENV_FILE}"`
|
|
RESTART_APP=false
|
|
shift 2
|
|
|
|
for var; do
|
|
if [[ $var != *"="* ]]; then
|
|
echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
|
|
echo "Must specify KEY and VALUE to set."
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
for var; do
|
|
KEY=`echo ${var} | cut -d"=" -f1`
|
|
VALUE=`echo ${var} | cut -d"=" -f2-`
|
|
|
|
if [[ $KEY =~ [a-zA-Z_][a-zA-Z0-9_]* ]]; then
|
|
RESTART_APP=true
|
|
ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $KEY=/ d")
|
|
ENV_TEMP="${ENV_TEMP}\nexport $KEY='$VALUE'"
|
|
ENV_ADD=$(echo -e "${ENV_ADD}" | sed "/^$KEY=/ d")
|
|
ENV_ADD="${ENV_ADD}$
|
|
${var}"
|
|
fi
|
|
done
|
|
ENV_ADD=$(echo "$ENV_ADD" | tail -n +2) #remove first empty line
|
|
|
|
if [ $RESTART_APP ]; then
|
|
echo "-----> Setting config vars and restarting $APP"
|
|
config_styled_hash "$ENV_ADD" " "
|
|
|
|
config_write "$ENV_TEMP"
|
|
fi
|
|
;;
|
|
|
|
config:unset)
|
|
if [[ -z $2 ]]; then
|
|
echo "Please specify an app to run the command on"
|
|
exit 1
|
|
fi
|
|
APP="$2"
|
|
if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then
|
|
echo "App $APP does not exist"
|
|
exit 1
|
|
fi
|
|
if [[ -z $3 ]]; then
|
|
echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
|
|
echo "Must specify KEY to unset."
|
|
exit 1
|
|
fi
|
|
|
|
config_create
|
|
ENV_TEMP=`cat "${ENV_FILE}"`
|
|
VARS="${*:3}"
|
|
|
|
for var in $VARS; do
|
|
echo "-----> Unsetting $var and restarting $APP"
|
|
ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $var=/ d")
|
|
|
|
config_write "$ENV_TEMP"
|
|
done
|
|
;;
|
|
|
|
help | config:help)
|
|
cat && cat<<EOF
|
|
config <app> Display the config vars for an app
|
|
config:get <app> KEY Display a config value for an app
|
|
config:set <app> KEY1=VALUE1 [KEY2=VALUE2 ...] Set one or more config vars
|
|
config:unset <app> KEY1 [KEY2 ...] Unset one or more config vars
|
|
EOF
|
|
;;
|
|
|
|
*)
|
|
exit $DOKKU_NOT_IMPLEMENTED_EXIT
|
|
;;
|
|
|
|
esac
|