-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add disable-units and enable-units hooks
Add the hook scripts `disable-units` and `enable-units` for disabling and enabling services / systemd units. Signed-off-by: Benjamin Drung <[email protected]>
- Loading branch information
Showing
10 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/sh | ||
set -eu | ||
|
||
# Copyright (C) 2021, Benjamin Drung <[email protected]> | ||
# SPDX-License-Identifier: ISC | ||
|
||
# Disable services / systemd units in a chroot environment. | ||
|
||
if test "$#" = 0; then | ||
echo "${0##*/}: Called without required CHROOT_DIR argument." >&2 | ||
echo "usage: ${0##*/} CHROOT_DIR service/systemd_unit [service/system_unit...]" >&2 | ||
exit 1 | ||
fi | ||
|
||
chroot_directory="$1" | ||
shift | ||
|
||
for unit in "$@"; do | ||
unit_files=$(test ! -d "${chroot_directory}/etc/systemd/system" || | ||
find "${chroot_directory}"/etc/systemd/system/*.wants -name "$unit" -o -name "${unit}.service") | ||
if test -n "$unit_files"; then | ||
echo "${0##*/}: Disabling unit $unit..." >&2 | ||
rm "$unit_files" | ||
elif test -e "${chroot_directory}/etc/init.d/$unit"; then | ||
echo "${0##*/}: Disabling SysV init.d service $unit..." >&2 | ||
echo "${0##*/}: Calling chroot \"${chroot_directory}\" /usr/sbin/update-rc.d \"$unit\" disable" >&2 | ||
chroot "${chroot_directory}" /usr/sbin/update-rc.d "$unit" disable | ||
else | ||
echo "${0##*/}: Unit $unit not found. Skipping disabling it." >&2 | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/sh | ||
set -eu | ||
|
||
# Copyright (C) 2021, Benjamin Drung <[email protected]> | ||
# SPDX-License-Identifier: ISC | ||
|
||
# Enable services / systemd units in a chroot environment. | ||
|
||
if test "$#" = 0; then | ||
echo "${0##*/}: Called without required CHROOT_DIR argument." >&2 | ||
echo "usage: ${0##*/} CHROOT_DIR service/systemd_unit [service/system_unit...]" >&2 | ||
exit 1 | ||
fi | ||
|
||
chroot_directory="$1" | ||
shift | ||
|
||
for unit in "$@"; do | ||
if test -e "${chroot_directory}/etc/systemd/system/${unit}" || \ | ||
test -e "${chroot_directory}/lib/systemd/system/${unit}" || \ | ||
test -e "${chroot_directory}/usr/lib/systemd/system/${unit}" || \ | ||
test -e "${chroot_directory}/etc/systemd/system/${unit}.service" || \ | ||
test -e "${chroot_directory}/lib/systemd/system/${unit}.service" || \ | ||
test -e "${chroot_directory}/usr/lib/systemd/system/${unit}.service" || \ | ||
test -e "${chroot_directory}/etc/init.d/$unit"; then | ||
echo "${0##*/}: Enabling unit $unit..." >&2 | ||
echo "${0##*/}: Calling chroot \"${chroot_directory}\" /bin/systemctl enable \"$unit\"" >&2 | ||
chroot "${chroot_directory}" /bin/systemctl enable "$unit" | ||
else | ||
echo "${0##*/}: Unit $unit not found. Skipping enabling it." >&2 | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (C) 2021, Benjamin Drung <[email protected]> | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
"""Run shellcheck on Shell code.""" | ||
|
||
import subprocess | ||
import sys | ||
import unittest | ||
|
||
from . import get_path, unittest_verbosity | ||
|
||
SHELL_SCRIPTS = ["hooks/disable-units", "hooks/enable-units"] | ||
|
||
|
||
class ShellcheckTestCase(unittest.TestCase): | ||
""" | ||
This unittest class provides a test that runs the shellcheck | ||
on Shell source code. | ||
""" | ||
|
||
def test_flake8(self): | ||
"""Test: Run shellcheck on Shell source code.""" | ||
cmd = ["shellcheck"] + [get_path(s) for s in SHELL_SCRIPTS] | ||
if unittest_verbosity() >= 2: | ||
sys.stderr.write(f"Running following command:\n{' '.join(cmd)}\n") | ||
with subprocess.Popen( | ||
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True | ||
) as process: | ||
out, err = process.communicate() | ||
|
||
if process.returncode != 0: # pragma: no cover | ||
msgs = [] | ||
if err: | ||
msgs.append( | ||
f"shellcheck exited with code {process.returncode} " | ||
f"and has unexpected output on stderr:\n{err.decode().rstrip()}" | ||
) | ||
if out: | ||
msgs.append(f"shellcheck found issues:\n{out.decode().rstrip()}") | ||
if not msgs: | ||
msgs.append( | ||
f"shellcheck exited with code {process.returncode} " | ||
"and has no output on stdout or stderr." | ||
) | ||
self.fail("\n".join(msgs)) |