Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions amazon-efs-utils.spec
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ mkdir -p %{buildroot}%{_unitdir}
install -p -m 644 %{_builddir}/%{name}/dist/amazon-efs-mount-watchdog.service %{buildroot}%{_unitdir}
%else
mkdir -p %{buildroot}%{_sysconfdir}/init
mkdir -p %{buildroot}%{_sysconfdir}/init.d
install -p -m 644 %{_builddir}/%{name}/dist/amazon-efs-mount-watchdog.conf %{buildroot}%{_sysconfdir}/init
install -p -m 755 %{_builddir}/%{name}/dist/amazon-efs-mount-watchdog.sysvinit %{buildroot}%{_sysconfdir}/init.d/amazon-efs-mount-watchdog
%endif

mkdir -p %{buildroot}%{efs_bindir}
Expand All @@ -101,6 +103,7 @@ install -p -m 644 %{_builddir}/%{name}/man/mount.efs.8 %{buildroot}%{_mandir}/ma
%{_unitdir}/amazon-efs-mount-watchdog.service
%else
%config(noreplace) %{_sysconfdir}/init/amazon-efs-mount-watchdog.conf
%config(noreplace) %{_sysconfdir}/init.d/amazon-efs-mount-watchdog
%endif
%{_sysconfdir}/amazon/efs/efs-utils.crt
%{efs_bindir}/mount.efs
Expand All @@ -124,12 +127,20 @@ install -p -m 644 %{_builddir}/%{name}/man/mount.efs.8 %{buildroot}%{_mandir}/ma

%preun
if [ $1 -eq 0 ]; then
/sbin/stop amazon-efs-mount-watchdog &> /dev/null || true
if [ -f /sbin/init.sysvinit ]; then
service amazon-efs-mount-watchdog stop &> /dev/null || true
else
/sbin/stop amazon-efs-mount-watchdog &> /dev/null || true
fi
fi

%postun
if [ $1 -eq 1 ]; then
/sbin/restart amazon-efs-mount-watchdog &> /dev/null || true
if [ -f /sbin/init.sysvinit ]; then
service amazon-efs-mount-watchdog restart &> /dev/null || true
else
/sbin/restart amazon-efs-mount-watchdog &> /dev/null || true
fi
fi

%endif
Expand Down
2 changes: 2 additions & 0 deletions build-deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mkdir -p ${BUILD_ROOT}
echo 'Creating application directories'
mkdir -p ${BUILD_ROOT}/etc/amazon/efs
mkdir -p ${BUILD_ROOT}/etc/init/
mkdir -p ${BUILD_ROOT}/etc/init.d/
mkdir -p ${BUILD_ROOT}/etc/systemd/system
mkdir -p ${BUILD_ROOT}/sbin
mkdir -p ${BUILD_ROOT}/usr/bin
Expand All @@ -30,6 +31,7 @@ mkdir -p ${BUILD_ROOT}/usr/share/man/man8

echo 'Copying application files'
install -p -m 644 dist/amazon-efs-mount-watchdog.conf ${BUILD_ROOT}/etc/init
install -p -m 755 dist/amazon-efs-mount-watchdog.sysvinit ${BUILD_ROOT}/etc/init.d/amazon-efs-mount-watchdog
install -p -m 644 dist/amazon-efs-mount-watchdog.service ${BUILD_ROOT}/etc/systemd/system
install -p -m 444 dist/efs-utils.crt ${BUILD_ROOT}/etc/amazon/efs
install -p -m 644 dist/efs-utils.conf ${BUILD_ROOT}/etc/amazon/efs
Expand Down
99 changes: 99 additions & 0 deletions dist/amazon-efs-mount-watchdog.sysvinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/sh
#
# Copyright 2017-2018 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
# the License.
#
### BEGIN INIT INFO
# Provides: amazon-efs-mount-watchdog
# Required-Start: $network
# Required-Stop: $network
# X-Start-Before: $remote_fs
# X-Stop-After: $remote_fs
# Default-Stop: 016
# Short-Description: Amazon EFS Mount Watchdog
# Description: Amazon EFS Mount Watchdog
### END INIT INFO

name=$(basename "$0")
pid_file="/var/run/$name.pid"
cmd="/usr/bin/env amazon-efs-mount-watchdog"

get_pid() {
cat "$pid_file" 2>/dev/null
}

is_running() {
[ -f "$pid_file" ] && ps -p "$(get_pid)" >/dev/null 2>&1
}

root_required() {
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: root privileges are required"
exit 2
fi
}

case "$1" in
start)
if is_running; then
echo "Already started"
else
root_required

echo "Starting $name..."
$cmd &
echo $! > "$pid_file"
if ! is_running; then
echo "ERROR: Failed to start"
exit 1
fi
echo "Running"
fi
;;
stop)
if is_running; then
root_required

echo "Stopping $name..."
kill "$(get_pid)"
for i in $(seq 10); do
if ! is_running; then
break
fi
sleep 1
done

if is_running; then
echo "ERROR: Failed to stop"
exit 1
else
echo "Stopped"
rm -f "$pid_file"
fi
else
echo "Not running"
fi
;;
restart)
$0 stop || exit $?
sleep 1
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 3
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac

exit 0
6 changes: 5 additions & 1 deletion dist/scriptlets/after-install-upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ set -e

if [ -n $2 ]; then
if [ "$(cat /proc/1/comm)" = "init" ]; then
/sbin/restart amazon-efs-mount-watchdog &> /dev/null || true
if [ -f /sbin/init.sysvinit ]; then
service amazon-efs-mount-watchdog restart &> /dev/null || true
else
/sbin/restart amazon-efs-mount-watchdog &> /dev/null || true
fi
elif [ "$(cat /proc/1/comm)" = "systemd" ]; then
if systemctl is-active --quiet amazon-efs-mount-watchdog; then
systemctl try-restart amazon-efs-mount-watchdog.service &> /dev/null || true
Expand Down
6 changes: 5 additions & 1 deletion dist/scriptlets/before-remove
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ set -e

if [ "$1" = "remove" ] || [ "$1" = "purge" ]; then
if [ "$(cat /proc/1/comm)" = "init" ]; then
/sbin/stop amazon-efs-mount-watchdog &> /dev/null || true
if [ -f /sbin/init.sysvinit ]; then
service amazon-efs-mount-watchdog stop &> /dev/null || true
else
/sbin/stop amazon-efs-mount-watchdog &> /dev/null || true
fi
elif [ "$(cat /proc/1/comm)" = "systemd" ]; then
if systemctl is-active --quiet amazon-efs-mount-watchdog; then
systemctl --no-reload disable amazon-efs-mount-watchdog.service &> /dev/null || true
Expand Down
29 changes: 26 additions & 3 deletions src/mount_efs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,14 +1375,20 @@ def poll_tunnel_process(tunnel_proc, fs_id, mount_completed):

def get_init_system(comm_file="/proc/1/comm"):
init_system = DEFAULT_UNKNOWN_VALUE
if not check_if_platform_is_mac():
if check_if_platform_is_mac():
init_system = "launchd"
else:
try:
with open(comm_file) as f:
init_system = f.read().strip()
except IOError:
logging.warning("Unable to read %s", comm_file)
else:
init_system = "launchd"

# Handle other init systems
if init_system == "init":
# Sysvinit
if os.path.isfile("/sbin/init.sysvinit"):
init_system = "sysvinit"

logging.debug("Identified init system: %s", init_system)
return init_system
Expand Down Expand Up @@ -1445,6 +1451,23 @@ def start_watchdog(init_system):
elif "start" in str(status):
logging.debug("%s is already running", WATCHDOG_SERVICE)

elif init_system == "sysvinit":
rc = subprocess.call(
["service", WATCHDOG_SERVICE, "status"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
close_fds=True
)
if rc != 0:
subprocess.Popen(
["service", WATCHDOG_SERVICE, "start"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
close_fds=True,
)
else:
logging.debug("%s is already running", WATCHDOG_SERVICE)

elif init_system == "systemd":
rc = subprocess.call(
["systemctl", "is-active", "--quiet", WATCHDOG_SERVICE], close_fds=True
Expand Down