Skip to content

Commit

Permalink
sorted order execution of my_init.d and my_shutdown.d
Browse files Browse the repository at this point in the history
Also changed starting and shutdown of syslog-ng based to my_init.d and my_shutdown.d
  • Loading branch information
madharjan committed May 4, 2019
1 parent a86b10e commit ded9f30
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 78 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Makefile
Vagrantfile
.DS_Store
*.swp
bats
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Docker baseimage based on [phusion/baseimage-docker](https://github.com/phusion/
* Environment variables to disable services
* Using scripts in `my_init.d` to initialize services (e.g base-startup.sh, nginx-startup.sh .. etc)
* Using scripts in `my_shutdown.d` to cleanup services before container stop (e.g postfix-stop.sh ..etc)
* Bats ([sstephenson/bats](https://github.com/sstephenson/bats/)) based test cases
* Bats ([bats-core/bats-core](https://github.com/bats-core/bats-core)) based test cases

Example:

Expand Down
9 changes: 7 additions & 2 deletions bin/my_init
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,14 @@ def kill_all_processes(time_limit):
finally:
signal.alarm(0)

def sorted_aphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key)

def run_startup_files():
# Run /etc/my_init.d/*
for name in listdir("/etc/my_init.d"):
for name in sorted_aphanumeric(listdir("/etc/my_init.d")):
filename = "/etc/my_init.d/" + name
if is_exe(filename):
info("Running %s..." % filename)
Expand All @@ -232,7 +237,7 @@ def run_startup_files():

def run_shutdown_files():
# Run /etc/my_shutdown.d/*
for name in listdir("/etc/my_shutdown.d"):
for name in sorted_aphanumeric(listdir("/etc/my_shutdown.d")):
filename = "/etc/my_shutdown.d/" + name
if is_exe(filename):
info("Running %s..." % filename)
Expand Down
16 changes: 11 additions & 5 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ apt-get install -y --no-install-recommends \
ca-certificates \
language-pack-en \
software-properties-common \
apt-utils

locale-gen en_US
update-locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8
mkdir -p /etc/container_environment
echo -n en_US.UTF-8 > /etc/container_environment/LANG
echo -n en_US.UTF-8 > /etc/container_environment/LC_CTYPE

## Install init process.
cp /build/bin/my_init /sbin/
chmod 750 /sbin/my_init
mkdir -p /etc/my_init.d
mkdir -p /etc/container_environment

touch /etc/container_environment.sh
touch /etc/container_environment.json
chmod 700 /etc/container_environment
Expand All @@ -66,6 +67,9 @@ chown :docker_env /etc/container_environment.sh /etc/container_environment.json
chmod 640 /etc/container_environment.sh /etc/container_environment.json
ln -s /etc/container_environment.sh /etc/profile.d/

mkdir -p /etc/my_init.d
mkdir -p /etc/my_shutdown.d

## Install runit.
apt-get install -y --no-install-recommends runit

Expand All @@ -87,6 +91,8 @@ apt-get install -y --no-install-recommends \
cp /build/bin/setuser /sbin/setuser
chmod 750 /sbin/setuser

mkdir -p /etc/my_init.d
cp /build/services/base-startup.sh /etc/my_init.d
chmod 750 /etc/my_init.d/base-startup.sh
cp /build/services/10-startup.sh /etc/my_init.d
cp /build/services/90-shutdown.sh /etc/my_shutdown.d

chmod 750 /etc/my_init.d/10-startup.sh
chmod 750 /etc/my_shutdown.d/90-shutdown.sh
87 changes: 87 additions & 0 deletions services/10-startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash

set -e

if [ "${DEBUG}" = true ]; then
set -x
fi

DISABLE_SYSLOG=${DISABLE_SYSLOG:-0}
DISABLE_CRON=${DISABLE_CRON:-0}

syslogng_wait() {
if [ "$2" -ne 0 ]; then
return 1
fi

RET=1
for i in $(seq 1 30); do
status=0
syslog-ng-ctl stats >/dev/null 2>&1 || status=$?
if [ "$status" != "$1" ]; then
RET=0
break
fi
sleep 1s
done
return $RET
}

if [ "${DISABLE_SYSLOG}" -eq 0 ]; then
# start syslog-ng

# If /dev/log is either a named pipe or it was placed there accidentally,
# e.g. because of the issue documented at https://github.com/phusion/baseimage-docker/pull/25,
# then we remove it.
if [ ! -S /dev/log ]; then rm -f /dev/log; fi
if [ ! -S /var/lib/syslog-ng/syslog-ng.ctl ]; then rm -f /var/lib/syslog-ng/syslog-ng.ctl; fi

# determine output mode on /dev/stdout because of the issue documented at https://github.com/phusion/baseimage-docker/issues/468
if [ -p /dev/stdout ]; then
sed -i 's/##SYSLOG_OUTPUT_MODE_DEV_STDOUT##/pipe/' /etc/syslog-ng/syslog-ng.conf
else
sed -i 's/##SYSLOG_OUTPUT_MODE_DEV_STDOUT##/file/' /etc/syslog-ng/syslog-ng.conf
fi

# If /var/log is writable by another user logrotate will fail
/bin/chown root:root /var/log
/bin/chmod 0755 /var/log

SYSLOGNG_PIDFILE="/var/run/syslog-ng.pid"
SYSLOGNG_OPTS=""

[ -r /etc/default/syslog-ng ] && . /etc/default/syslog-ng

case "x$CONSOLE_LOG_LEVEL" in
x[1-8])
dmesg -n $CONSOLE_LOG_LEVEL
;;
x)
;;
*)
echo "CONSOLE_LOG_LEVEL is of unaccepted value."
;;
esac

if [ ! -e /dev/xconsole ]
then
mknod -m 640 /dev/xconsole p
chown root:adm /dev/xconsole
[ -x /sbin/restorecon ] && /sbin/restorecon $XCONSOLE
fi

exec syslog-ng -F -p $SYSLOGNG_PIDFILE $SYSLOGNG_OPTS &
syslogng_wait 1 $?
fi

if [ ! "${DISABLE_SYSLOG}" -eq 0 ]; then
touch /etc/service/syslog-forwarder/down
else
rm -f /etc/service/syslog-forwarder/down
fi

if [ ! "${DISABLE_CRON}" -eq 0 ]; then
touch /etc/service/cron/down
else
rm -f /etc/service/cron/down
fi
38 changes: 38 additions & 0 deletions services/90-shutdown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

set -e

if [ "${DEBUG}" = true ]; then
set -x
fi

DISABLE_SYSLOG=${DISABLE_SYSLOG:-0}

syslogng_wait() {
if [ "$2" -ne 0 ]; then
return 1
fi

RET=1
for i in $(seq 1 30); do
status=0
syslog-ng-ctl stats >/dev/null 2>&1 || status=$?
if [ "$status" != "$1" ]; then
RET=0
break
fi
sleep 1s
done
return $RET
}

SYSLOGNG_PIDFILE="/var/run/syslog-ng.pid"

if [ "${DISABLE_SYSLOG}" -eq 0 ]; then
# wait for syslog-ng to exit
if [ -f "$PIDFILE" ]; then
kill $(cat "$PIDFILE")
fi

syslogng_wait 0 $?
fi
24 changes: 0 additions & 24 deletions services/base-startup.sh

This file was deleted.

10 changes: 7 additions & 3 deletions services/syslog-ng/logrotate-syslog-ng
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
delaycompress
compress
postrotate
sv reload syslog-ng > /dev/null
if [ -f /var/run/syslog-ng.pid ]; then
kill -HUP `cat /var/run/syslog-ng.pid`
fi
endscript
}

Expand All @@ -32,7 +34,9 @@
delaycompress
sharedscripts
postrotate
sv reload syslog-ng > /dev/null
sv restart cron-log-forwarder > /dev/null
if [ -f /var/run/syslog-ng.pid ]; then
kill -HUP `cat /var/run/syslog-ng.pid`
fi
sv restart syslog-forwarder > /dev/null
endscript
}
12 changes: 8 additions & 4 deletions services/syslog-ng/syslog-ng.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@version: 3.5
@include "scl.conf"
@include "`scl-root`/system/tty10.conf"

# Syslog-ng configuration file, compatible with default Debian syslogd
# installation.
Expand All @@ -18,7 +17,7 @@ options { chain_hostnames(off); flush_lines(0); use_dns(no); use_fqdn(no);
# Logs may come from unix stream, but not from another machine.
#
source s_src {
unix-stream("/dev/log");
unix-dgram("/dev/log");
internal();
};

Expand Down Expand Up @@ -54,7 +53,7 @@ destination d_newscrit { file("/var/log/news/news.crit"); };
destination d_newserr { file("/var/log/news/news.err"); };
destination d_newsnotice { file("/var/log/news/news.notice"); };

# Some `catch-all' logfiles.
# Some 'catch-all' logfiles.
#
destination d_debug { file("/var/log/debug"); };
destination d_error { file("/var/log/error"); };
Expand All @@ -74,6 +73,9 @@ destination d_xconsole { pipe("/dev/xconsole"); };
# Debian only
destination d_ppp { file("/var/log/ppp.log"); };

# stdout for docker
destination d_stdout { ##SYSLOG_OUTPUT_MODE_DEV_STDOUT##("/dev/stdout"); };

########################
# Filters
########################
Expand Down Expand Up @@ -119,7 +121,7 @@ log { source(s_src); filter(f_cron); destination(d_cron); };
log { source(s_src); filter(f_daemon); destination(d_daemon); };
log { source(s_src); filter(f_kern); destination(d_kern); };
log { source(s_src); filter(f_lpr); destination(d_lpr); };
log { source(s_src); filter(f_syslog3); destination(d_syslog); };
log { source(s_src); filter(f_syslog3); destination(d_syslog); destination(d_stdout); };
log { source(s_src); filter(f_user); destination(d_user); };
log { source(s_src); filter(f_uucp); destination(d_uucp); };

Expand All @@ -131,6 +133,8 @@ log { source(s_src); filter(f_mail); destination(d_mail); };
log { source(s_src); filter(f_news); filter(f_crit); destination(d_newscrit); };
log { source(s_src); filter(f_news); filter(f_err); destination(d_newserr); };
log { source(s_src); filter(f_news); filter(f_notice); destination(d_newsnotice); };
#log { source(s_src); filter(f_cnews); destination(d_console_all); };
#log { source(s_src); filter(f_cother); destination(d_console_all); };

#log { source(s_src); filter(f_ppp); destination(d_ppp); };

Expand Down
32 changes: 0 additions & 32 deletions services/syslog-ng/syslog-ng.runit

This file was deleted.

11 changes: 4 additions & 7 deletions services/syslog-ng/syslog-ng.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ SYSLOG_NG_BUILD_PATH=/build/services/syslog-ng

## Install a syslog daemon.
apt-get install -y --no-install-recommends syslog-ng-core
mkdir -p /etc/service/syslog-ng
cp $SYSLOG_NG_BUILD_PATH/syslog-ng.runit /etc/service/syslog-ng/run
chmod 750 /etc/service/syslog-ng/run

mkdir -p /var/lib/syslog-ng
cp $SYSLOG_NG_BUILD_PATH/syslog-ng-default /etc/default/syslog-ng
touch /var/log/syslog
chmod u=rw,g=r,o= /var/log/syslog
cp $SYSLOG_NG_BUILD_PATH/syslog-ng.conf /etc/syslog-ng/syslog-ng.conf

## Install logrotate.
apt-get install -y --no-install-recommends logrotate
cp $SYSLOG_NG_BUILD_PATH/logrotate-syslog-ng /etc/logrotate.d/syslog-ng

## Install syslog to "docker logs" forwarder.
mkdir -p /etc/service/syslog-forwarder
cp $SYSLOG_NG_BUILD_PATH/syslog-forwarder.runit /etc/service/syslog-forwarder/run
chmod 750 /etc/service/syslog-forwarder/run

## Install logrotate.
apt-get install -y --no-install-recommends logrotate
cp $SYSLOG_NG_BUILD_PATH/logrotate-syslog-ng /etc/logrotate.d/syslog-ng

0 comments on commit ded9f30

Please sign in to comment.