diff --git a/.gitignore b/.gitignore index 2102bae..b5eeb2a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /file/build/ /waitfor/build/ /local-script/build/ +/command/build/ # Ignore Gradle GUI config gradle-app.setting diff --git a/file/contents/rotate b/file/contents/rotate index c9b81a1..aa13eda 100755 --- a/file/contents/rotate +++ b/file/contents/rotate @@ -4,11 +4,11 @@ set -eu [[ "${RD_JOB_LOGLEVEL:-}" == "DEBUG" ]] && set -vx syntax_error() { echo >&2 "SYNTAX: $*"; exit 2; } -(( $# != 3 )) && { - syntax_error "$0 <+%{date_format}>" +(( $# != 4 )) && { + syntax_error "$0 <+%{date_format}> " } -declare -r FILE=$1 COMPRESS=$2 TSTAMP_FORMAT=$3 +declare -r FILE=$1 COMPRESS=$2 TSTAMP_FORMAT=$3 RUNASSUDO=$4 [[ ! -f "$FILE" ]] && { echo "Nothing to rotate. File does not exist: $FILE" @@ -22,24 +22,40 @@ then syntax_error "date format '$TSTAMP_FORMAT' not supported by command: $(whereis date)." fi -# Create a copy of the original -FILE_COPY=$DIRNAME/$(basename "$FILE").$TSTAMP -cp -p "$FILE" "$FILE_COPY" -echo "Created file copy: $FILE_COPY" - -# Truncate the original file -> "$FILE" +function rotate() { + local DIRNAME=$1 + local FILE=$2 + local TSTAMP=$3 + local COMPRESS=$4 + + # Create a copy of the original + FILE_COPY=$DIRNAME/$(basename "$FILE").$TSTAMP + echo "FILE COPY: $FILE_COPY" + cp -p "$FILE" "$FILE_COPY" + echo "Created file copy: $FILE_COPY" + + # Truncate the original file + > "$FILE" + + # Compress the copy if specified + if [[ "${COMPRESS:-}" == "true" ]] + then + gzip -f "$FILE_COPY" + echo "Compressed rotated log: $FILE_COPY.gz" + fi +} +if [[ "${RUNASSUDO:-}" == "true" ]] +then +cat <&2 "SYNTAX: $*"; exit 2; } } # Read script arguments -declare -ar ARGUMENTS=($@) +declare -ar ARGUMENTS=("$@") [[ -z "${RD_CONFIG_SCRIPT:-}" ]] && { syntax_error "RD_CONFIG_SCRIPT environment variable unset." } +if [ -z "${RD_CONFIG_TMP:-}" ]; then + scriptfile=$(mktemp -t "script.in.XXXX") +else + tmp_folder="${RD_CONFIG_TMP:-}" + mkdir -p "$tmp_folder" + scriptfile="$tmp_folder/script-$RD_JOB_EXECID" +fi + # Write user defined script code to temp file. -scriptfile=$(mktemp -t "script.in.XXXX") cat > "$scriptfile" <<< "$RD_CONFIG_SCRIPT" trap 'rm "$scriptfile"' EXIT; # clean up on exit. diff --git a/local-script/plugin.yaml b/local-script/plugin.yaml index 2464686..0541f59 100644 --- a/local-script/plugin.yaml +++ b/local-script/plugin.yaml @@ -55,4 +55,10 @@ providers: name: arguments title: Arguments description: "optional command line arguments" - required: false + required: false + - type: String + name: tmp + title: Temporary path + description: "Temporary path where the script will be copied, default /tmp" + required: false +