diff --git a/amazon-efs-utils.spec b/amazon-efs-utils.spec index 40ae8774..9377c416 100644 --- a/amazon-efs-utils.spec +++ b/amazon-efs-utils.spec @@ -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} @@ -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 @@ -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 diff --git a/build-deb.sh b/build-deb.sh index 2a70d8f0..95273ede 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -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 @@ -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 diff --git a/dist/amazon-efs-mount-watchdog.sysvinit b/dist/amazon-efs-mount-watchdog.sysvinit new file mode 100644 index 00000000..61c9119d --- /dev/null +++ b/dist/amazon-efs-mount-watchdog.sysvinit @@ -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 diff --git a/dist/scriptlets/after-install-upgrade b/dist/scriptlets/after-install-upgrade index 65bbff6d..c1092915 100644 --- a/dist/scriptlets/after-install-upgrade +++ b/dist/scriptlets/after-install-upgrade @@ -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 diff --git a/dist/scriptlets/before-remove b/dist/scriptlets/before-remove index 248c97b7..637f5364 100644 --- a/dist/scriptlets/before-remove +++ b/dist/scriptlets/before-remove @@ -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 diff --git a/src/mount_efs/__init__.py b/src/mount_efs/__init__.py index a95c7e57..b7cef95a 100755 --- a/src/mount_efs/__init__.py +++ b/src/mount_efs/__init__.py @@ -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 @@ -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