Merge pull request #3501 from dokku/3487-scheduler-logs

Move log retrieval to docker-local scheduler
This commit is contained in:
Jose Diaz-Gonzalez
2019-04-17 15:44:55 -04:00
committed by GitHub
5 changed files with 102 additions and 34 deletions

View File

@@ -43,12 +43,17 @@ This plugin implements various functionality through `plugn` triggers to integra
- `check-deploy`
- `core-post-deploy`
- `post-app-clone-setup`
- `post-create`
- `post-delete`
- `pre-deploy`
- `pre-restore`
- `scheduler-deploy`
- `scheduler-docker-cleanup`
- `scheduler-inspect`
- `scheduler-logs-failed`
- `scheduler-logs`
- `scheduler-retire`
- `scheduler-run`
- `scheduler-stop`
- `scheduler-tags-create`

View File

@@ -1305,6 +1305,25 @@ DOKKU_SCHEDULER="$1"; APP="$2";
# TODO
```
### `scheduler-logs`
> Warning: The scheduler plugin trigger apis are under development and may change
> between minor releases until the 1.0 release.
- Description: Allows you to run scheduler commands when retrieving container logs
- Invoked by: `dokku logs:failed`
- Arguments: `$DOKKU_SCHEDULER $APP $PROCESS_TYPE $TAIL $PRETTY_PRINT $NUM`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
DOKKU_SCHEDULER="$1"; APP="$2"; PROCESS_TYPE="$3"; TAIL="$4"; PRETTY_PRINT="$5"; NUM="$6"
# TODO
```
### `scheduler-logs-failed`
> Warning: The scheduler plugin trigger apis are under development and may change
@@ -1319,7 +1338,26 @@ DOKKU_SCHEDULER="$1"; APP="$2";
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
DOKKU_SCHEDULER="$1"; APP="$2"; ARGS="${@:3}";
DOKKU_SCHEDULER="$1"; APP="$2";
# TODO
```
### `scheduler-retire`
> Warning: The scheduler plugin trigger apis are under development and may change
> between minor releases until the 1.0 release.
- Description: Allows you to run scheduler commands when containers should be force retired from the system
- Invoked by: `dokku run`
- Arguments: `$DOKKU_SCHEDULER`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
DOKKU_SCHEDULER="$1";
# TODO
```

View File

@@ -264,6 +264,7 @@ get_app_scheduler() {
declare APP="$1"
local DOKKU_APP_SCHEDULER DOKKU_GLOBAL_SCHEDULER DOKKU_SCHEDULER
source "$PLUGIN_AVAILABLE_PATH/config/functions"
[[ -n "$APP" ]] && DOKKU_APP_SCHEDULER="$(config_get "$APP" DOKKU_SCHEDULER || true)"
DOKKU_GLOBAL_SCHEDULER="$(config_get --global DOKKU_SCHEDULER || true)"

View File

@@ -10,8 +10,6 @@ logs_default_cmd() {
[[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
local APP="$2"
verify_app_name "$2"
local APP_ROOT="$DOKKU_ROOT/$APP"
local COLORS=(36 33 32 35 31)
! (is_deployed "$APP") && dokku_log_fail "App $APP has not been deployed"
@@ -24,23 +22,23 @@ logs_default_cmd() {
fi
eval set -- "$TEMP"
local DOKKU_LOGS_LINE_NUMBERS="100"
local NUM="100"
while true; do
case "$1" in
-t | --tail)
local DOKKU_LOGS_ARGS+="--follow "
local TAIL=true
shift
;;
-n | --num)
local DOKKU_LOGS_LINE_NUMBERS="$2"
local NUM="$2"
shift 2
;;
-p | --ps)
local DOKKU_LOGS_ONLY_PROCESS="$2"
local PROCESS_TYPE="$2"
shift 2
;;
-q | --quiet)
local DOKKU_LOGS_NO_PRETTY_PRINT="true"
local PRETTY_PRINT=true
shift
;;
--)
@@ -51,32 +49,8 @@ logs_default_cmd() {
esac
done
if [[ -n $DOKKU_LOGS_ONLY_PROCESS ]]; then
local CONTAINERS=("$APP_ROOT/CONTAINER.$DOKKU_LOGS_ONLY_PROCESS".*)
else
local CONTAINERS=("$APP_ROOT"/CONTAINER.*)
fi
[[ -z $(stat -t "${CONTAINERS[0]}" 2>/dev/null) ]] && exit 0
local DOKKU_LOGS_ARGS+="--tail $DOKKU_LOGS_LINE_NUMBERS"
((MAX_INDEX = ${#CONTAINERS[*]} - 1)) || true
for i in ${!CONTAINERS[*]}; do
local DYNO=$(echo "${CONTAINERS[i]}" | sed -r 's/.*CONTAINER\.(.*)/\1/')
local CID=$(<"${CONTAINERS[i]}")
local COLOR=${COLORS[i % ${#COLORS[*]}]}
if [[ $DOKKU_LOGS_NO_PRETTY_PRINT == "true" ]]; then
local DOKKU_LOGS_CMD+="(docker logs $DOKKU_LOGS_ARGS $CID 2>&1)"
else
local DOKKU_LOGS_PRETTY_PRINT_CMD="sed -r 's/^([^Z]+Z )/\x1b[${COLOR}m\1app[$DYNO]:\x1b[0m /gm'"
local DOKKU_LOGS_CMD+="(docker logs -t $DOKKU_LOGS_ARGS $CID 2>&1 | $DOKKU_LOGS_PRETTY_PRINT_CMD)"
fi
if [[ $i != "$MAX_INDEX" ]]; then
local DOKKU_LOGS_CMD+="& "
else
local DOKKU_LOGS_CMD+="; "
fi
done
bash -c "($DOKKU_LOGS_CMD)"
DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
plugn trigger scheduler-logs "$DOKKU_SCHEDULER" "$APP" "$PROCESS_TYPE" "$TAIL" "$PRETTY_PRINT" "$NUM"
}
logs_default_cmd "$@"

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
scheduler-docker-local-scheduler-logs() {
declare desc="scheduler-docker-local scheduler-logs plugin trigger"
declare trigger="scheduler-docker-local scheduler-logs"
declare DOKKU_SCHEDULER="$1" APP="$2" PROCESS_TYPE="$3" TAIL="$4" PRETTY_PRINT="$5" NUM="$6"
local DOKKU_LOGS_ARGS=""
if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
return
fi
if [[ "$TAIL" == "true" ]]; then
DOKKU_LOGS_ARGS="--follow "
fi
local APP_ROOT="$DOKKU_ROOT/$APP"
local COLORS=(36 33 32 35 31)
if [[ -n "$PROCESS_TYPE" ]]; then
local CONTAINERS=("$APP_ROOT/CONTAINER.$PROCESS_TYPE".*)
else
local CONTAINERS=("$APP_ROOT"/CONTAINER.*)
fi
[[ -z $(stat -t "${CONTAINERS[0]}" 2>/dev/null) ]] && exit 0
local DOKKU_LOGS_ARGS+="--tail $NUM"
((MAX_INDEX = ${#CONTAINERS[*]} - 1)) || true
for i in ${!CONTAINERS[*]}; do
local DYNO=$(echo "${CONTAINERS[i]}" | sed -r 's/.*CONTAINER\.(.*)/\1/')
local CID=$(<"${CONTAINERS[i]}")
local COLOR=${COLORS[i % ${#COLORS[*]}]}
if [[ $PRETTY_PRINT == "true" ]]; then
local DOKKU_LOGS_CMD+="(docker logs $DOKKU_LOGS_ARGS $CID 2>&1)"
else
local DOKKU_LOGS_PRETTY_PRINT_CMD="sed -r 's/^([^Z]+Z )/\x1b[${COLOR}m\1app[$DYNO]:\x1b[0m /gm'"
local DOKKU_LOGS_CMD+="(docker logs -t $DOKKU_LOGS_ARGS $CID 2>&1 | $DOKKU_LOGS_PRETTY_PRINT_CMD)"
fi
if [[ $i != "$MAX_INDEX" ]]; then
local DOKKU_LOGS_CMD+="& "
else
local DOKKU_LOGS_CMD+="; "
fi
done
bash -c "($DOKKU_LOGS_CMD)"
}
scheduler-docker-local-scheduler-logs "$@"