Files
dokku/plugins/config/commands

212 lines
5.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(dirname $0)/../common/functions"
ENV_FILE="$DOKKU_ROOT/$2/ENV"
ENV_FILE_TEMP="$DOKKU_ROOT/$2/ENV.tmp"
config_create () {
[ -f $ENV_FILE ] || {
touch $ENV_FILE
}
}
config_styled_hash () {
2014-10-02 11:37:47 +02:00
vars="$1"
prefix="$2"
longest=""
2014-10-02 11:37:47 +02:00
while read -r word; do
2014-12-13 18:56:31 -08:00
KEY=$(echo $word | cut -d"=" -f1)
if [ ${#KEY} -gt ${#longest} ]; then
longest=$KEY
fi
2014-10-02 11:37:47 +02:00
done <<< "$vars"
2014-10-02 11:37:47 +02:00
while read -r word; do
2014-12-13 18:56:31 -08:00
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 "
2014-12-13 18:56:31 -08:00
num_zeros=$((num_zeros - 1))
done
echo "$prefix$KEY:$zeros$VALUE"
2014-10-02 11:37:47 +02:00
done <<< "$vars"
}
2014-06-06 14:45:36 +02:00
config_write() {
ENV_TEMP="$1"
echo "$ENV_TEMP" | sed '/^$/d' | sort > $ENV_FILE_TEMP
2014-06-06 14:45:36 +02:00
if ! cmp -s $ENV_FILE $ENV_FILE_TEMP; then
cp -f $ENV_FILE_TEMP $ENV_FILE
chmod 600 $ENV_FILE
2014-06-06 14:45:36 +02:00
fi
rm -f $ENV_FILE_TEMP
}
case "$1" in
config)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"
config_create
[[ ! -s $ENV_FILE ]] && echo "$APP has no config vars" && exit 1
2014-12-13 18:56:31 -08:00
VARS=$(grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" $ENV_FILE | cut -d" " -f2-)
for var in "$@"; do
if [[ "$var" == "--shell" ]]; then
echo $VARS
exit 0
fi
done
dokku_log_info2_quiet "$APP config vars"
config_styled_hash "$VARS"
2014-11-23 16:42:21 -05:00
;;
config:get)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"
if [[ -z $3 ]]; then
echo "Usage: dokku config:get APP KEY"
echo "Must specify KEY."
exit 1
fi
config_create
2014-11-23 23:01:08 -05:00
if [[ ! -s $ENV_FILE ]] ; then
exit 0
fi
KEY="$3"
2014-12-13 18:56:31 -08:00
grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" $ENV_FILE | grep "^export $KEY=" | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//"
2014-11-23 16:42:21 -05:00
;;
config:set)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"
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
shift 2
dokku config:set-norestart $APP "$@"
dokku_log_info1 "Restarting app $APP"
dokku ps:restart $APP
;;
config:set-norestart)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"
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=""
2014-12-13 18:56:31 -08:00
ENV_TEMP=$(cat "${ENV_FILE}")
RESTART_APP=false
2014-10-02 11:37:47 +02:00
shift 2
2014-10-02 11:37:47 +02:00
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
2014-10-02 11:37:47 +02:00
for var; do
2014-12-13 18:56:31 -08:00
KEY=$(echo ${var} | cut -d"=" -f1)
VALUE=$(echo ${var} | cut -d"=" -f2-)
2014-10-02 12:13:09 +02:00
if [[ $KEY =~ [a-zA-Z_][a-zA-Z0-9_]* ]]; then
RESTART_APP=true
ENV_TEMP=$(echo "${ENV_TEMP}" | sed "/^export $KEY=/ d")
ENV_TEMP="${ENV_TEMP}
export $KEY='$VALUE'"
ENV_ADD=$(echo -e "${ENV_ADD}" | sed "/^$KEY=/ d")
2014-10-02 11:37:47 +02:00
ENV_ADD="${ENV_ADD}$
${var}"
fi
done
2014-10-02 11:37:47 +02:00
ENV_ADD=$(echo "$ENV_ADD" | tail -n +2) #remove first empty line
if [ $RESTART_APP ]; then
dokku_log_info1 "Setting config vars"
config_styled_hash "$ENV_ADD" " "
2014-06-06 14:45:36 +02:00
config_write "$ENV_TEMP"
fi
2014-11-23 16:42:21 -05:00
;;
config:unset)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"
if [[ -z $3 ]]; then
echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
echo "Must specify KEY to unset."
exit 1
fi
shift 2
dokku config:unset-norestart $APP "$@"
dokku_log_info1 "Restarting app $APP"
dokku ps:restart $APP
;;
config:unset-norestart)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"
if [[ -z $3 ]]; then
echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
echo "Must specify KEY to unset."
exit 1
fi
config_create
2014-12-13 18:56:31 -08:00
ENV_TEMP=$(cat "${ENV_FILE}")
VARS="${*:3}"
for var in $VARS; do
dokku_log_info1 "Unsetting $var"
ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $var=/ d")
2014-06-06 14:45:36 +02:00
config_write "$ENV_TEMP"
done
2014-11-23 16:42:21 -05:00
;;
2014-11-22 17:52:46 -05:00
help | config:help)
cat && cat<<EOF
2014-09-28 01:03:07 +01:00
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
2014-11-23 16:42:21 -05:00
;;
*)
exit $DOKKU_NOT_IMPLEMENTED_EXIT
;;
esac