Skip to content

Commit

Permalink
- for local-script: add the option to set a temporary path
Browse files Browse the repository at this point in the history
- for rotate: add the option to run it as sudo
  • Loading branch information
ltamaster committed Oct 14, 2019
1 parent 2c00b77 commit 65d53fc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/file/build/
/waitfor/build/
/local-script/build/
/command/build/

# Ignore Gradle GUI config
gradle-app.setting
Expand Down
50 changes: 33 additions & 17 deletions file/contents/rotate
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ set -eu
[[ "${RD_JOB_LOGLEVEL:-}" == "DEBUG" ]] && set -vx
syntax_error() { echo >&2 "SYNTAX: $*"; exit 2; }

(( $# != 3 )) && {
syntax_error "$0 <file> <gzip?> <+%{date_format}>"
(( $# != 4 )) && {
syntax_error "$0 <file> <gzip?> <+%{date_format}> <sudo?>"
}

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"
Expand All @@ -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 <<EOS | sudo bash
$(declare -f rotate)
rotate $DIRNAME $FILE $TSTAMP $COMPRESS
EOS

# Compress the copy if specified
if [[ "${COMPRESS:-}" == "true" ]]
then
gzip -f "$FILE_COPY"
echo "Compressed rotated log: $FILE_COPY.gz"
else
rotate $DIRNAME $FILE $TSTAMP $COMPRESS
fi


# Done. Exit with last command exit status.
exit $?

6 changes: 5 additions & 1 deletion file/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ providers:
plugin-type: script
script-interpreter: /bin/bash
script-file: rotate
script-args: ${config.file} ${config.compress} ${config.tstamp-format}
script-args: ${config.file} ${config.compress} ${config.tstamp-format} ${config.sudo}
config:
- type: String
name: file
Expand All @@ -106,6 +106,10 @@ providers:
description: "the timestamp in date format"
default: '+%Y%m%d'
required: true
- type: Boolean
name: sudo
title: Run as sudo?
description: "Run command as sudo?."
- name: nixy-file-truncate
service: RemoteScriptNodeStep
title: '*nixy / file / truncate'
Expand Down
11 changes: 9 additions & 2 deletions local-script/contents/local-script
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ syntax_error() { echo >&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.

Expand Down
8 changes: 7 additions & 1 deletion local-script/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 65d53fc

Please sign in to comment.