Updated code to maintain symlink

This commit is contained in:
Darren 'Tadgy' Austin 2018-04-17 21:52:46 +01:00
commit 295a89d9af
2 changed files with 9 additions and 10 deletions

View file

@ -3,4 +3,3 @@
* Instead of requiring a fifo already exist, if the file doesn't already exist create the fifo. Would need to modify the trap for SIGTERM in order to clean up the file. * Instead of requiring a fifo already exist, if the file doesn't already exist create the fifo. Would need to modify the trap for SIGTERM in order to clean up the file.
* Figure out a way to check if the program is respawning too offten - this would indicate an error in the calling process and we don't want to just keep looping forever. * Figure out a way to check if the program is respawning too offten - this would indicate an error in the calling process and we don't want to just keep looping forever.
* Have an option to change UID and/or GID when running. Alternatively, use setpriv to drop capabilities. * Have an option to change UID and/or GID when running. Alternatively, use setpriv to drop capabilities.
* Make it so the symlink filename can contain the same escapes and {} as the template file.

View file

@ -52,7 +52,7 @@ display_help() {
Arguments (all of which are mandatory): Arguments (all of which are mandatory):
<basedir> The base directory of where to write the log files. <basedir> The base directory of where to write the log files.
<template> The filename template. When normal mode, the template must <template> The filename template. When normal mode, the template must
include at least one occurance of '{}', which is replaced with include at least one occurrance of '{}', which is replaced with
the site name from the VirtualHost identifier. In raw mode the site name from the VirtualHost identifier. In raw mode
(-r), '{}' should not be included in the template. The template (-r), '{}' should not be included in the template. The template
may also include any %-escaped format strings recognised by the may also include any %-escaped format strings recognised by the
@ -63,13 +63,13 @@ display_help() {
string: string:
"|$LJ_NAME '/path/to/logsdir' '{}/logs/access-log-%Y-%m'" "|$LJ_NAME '/path/to/logsdir' '{}/logs/access-log-%Y-%m'"
Where the httpd VirtualHost identifier is 'example.com', would write logs Where the httpd VirtualHost identifier is 'example.com', would write logs
(with the site identifier steipped) to the filename: (with the site identifier stripped) to the filename:
/path/to/logsdir/example.com/logs/access-log-<year>-<month> /path/to/logsdir/example.com/logs/access-log-<year>-<month>
"|$LJ_NAME '/path/to/logsdir' '{}/logs/()-access-log-%Y-%m'" "|$LJ_NAME '/path/to/logsdir' '{}/logs/()-access-log-%Y-%m'"
Where the httpd VirtualHost identifier is 'example.com', would write logs Where the httpd VirtualHost identifier is 'example.com', would write logs
(with the site identifier steipped) to the filename: (with the site identifier steipped) to the filename:
/path/to/logsdir/example.com/logs/example.com-access-log-<year>-<month> /path/to/logsdir/example.com/logs/example.com-access-log-<year>-<month>
When used with the httpd ErrorLog directive (both examples are equilivant): When used with the httpd ErrorLog directive (both examples are equilivent):
"|$LJ_NAME -r '/path/to/logsdir' 'logs/error-log-%Y-%m'" "|$LJ_NAME -r '/path/to/logsdir' 'logs/error-log-%Y-%m'"
Would write raw log lines to the filename: Would write raw log lines to the filename:
/path/to/logsdir/logs/error-log-<year>-<month> /path/to/logsdir/logs/error-log-<year>-<month>
@ -103,7 +103,6 @@ is_dir() {
make_dir() { make_dir() {
# $1 The directory to create. # $1 The directory to create.
# $2 The permissions to assign to the new directory.
[[ ! "$1" ]] && return 1 [[ ! "$1" ]] && return 1
[[ ! -e "$1" ]] && { [[ ! -e "$1" ]] && {
umask "$LJ_DIR_UMASK" umask "$LJ_DIR_UMASK"
@ -397,10 +396,11 @@ while :; do
# Fix the now broken symlink - point it to the currently active log file. # Fix the now broken symlink - point it to the currently active log file.
[[ "$LJ_LINKFILE" ]] && { [[ "$LJ_LINKFILE" ]] && {
LJ_LINKFILE_EXPANDED="$(printf "%($LJ_LINKFILE)T")"
# Note: This will clobber anything that already exists with the link name. # Note: This will clobber anything that already exists with the link name.
rm -rf "${LJ_FILENAME%/*}/$LJ_LINKFILE" rm -rf "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_SITE}"
ln -sf "${LJ_FILENAME##*/}" "${LJ_FILENAME%/*}/$LJ_LINKFILE" 2>/dev/null || { ln -sfr "$LJ_FILENAME" "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_SITE}" 2>/dev/null || {
syslog "error" "failed to fix link: ${LJ_FILENAME%/*}/$LJ_LINKFILE" syslog "error" "failed to fix link: $LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_SITE}"
} }
} }
@ -447,8 +447,8 @@ while :; do
# Create symlink to the currently active log file. # Create symlink to the currently active log file.
[[ "$LJ_LINKFILE" ]] && { [[ "$LJ_LINKFILE" ]] && {
LJ_LINKFILE_EXPANDED="$(printf "%($LJ_LINKFILE)T")" LJ_LINKFILE_EXPANDED="$(printf "%($LJ_LINKFILE)T")"
[[ "$(stat -L --printf="%m %i" "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_LOG_VHOST}" 2>/dev/null)" != \ [[ "$(stat -L --printf="%d:%i" "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_LOG_VHOST}" 2>/dev/null)" != \
"$(stat --printf="%m %i" "$LJ_FILENAME" 2>/dev/null)" ]] && { "$(stat --printf="%d:%i" "$LJ_FILENAME" 2>/dev/null)" ]] && {
# Note: This will clobber anything that already exists with the link name. # Note: This will clobber anything that already exists with the link name.
rm -rf "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_LOG_VHOST}" rm -rf "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_LOG_VHOST}"
ln -sfr "$LJ_FILENAME" "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_LOG_VHOST}" 2>/dev/null || { ln -sfr "$LJ_FILENAME" "$LJ_BASEDIR/${LJ_LINKFILE_EXPANDED//\{\}/$LJ_LOG_VHOST}" 2>/dev/null || {