Add "bastille tags" subcommand to add tag strings to jails

This commit is contained in:
Lars Engels
2023-03-14 21:34:07 +01:00
parent 7b63059d03
commit 09a1d306dc
2 changed files with 91 additions and 2 deletions

View File

@@ -73,7 +73,7 @@ bastille_perms_check() {
bastille_perms_check
## version
BASTILLE_VERSION="0.9.20220714"
BASTILLE_VERSION=
usage() {
cat << EOF
@@ -110,6 +110,7 @@ Available Commands:
stop Stop a running container.
sysrc Safely edit rc files within targeted container(s).
template Apply file templates to targeted container(s).
tags Add or remove tags to targeted container(s).
top Display and update information about the top(1) cpu processes.
umount Unmount a volume from within the targeted container(s).
update Update container base -pX release.
@@ -141,7 +142,7 @@ help|-h|--help)
bootstrap|create|destroy|export|import|list|rdr|restart|start|update|upgrade|verify)
# Nothing "extra" to do for these commands. -- cwells
;;
clone|config|cmd|console|convert|cp|edit|htop|limits|mount|pkg|rename|service|stop|sysrc|template|top|umount|zfs)
clone|config|cmd|console|convert|cp|edit|htop|limits|mount|pkg|rename|service|stop|sysrc|tags|template|top|umount|zfs)
# Parse the target and ensure it exists. -- cwells
if [ $# -eq 0 ]; then # No target was given, so show the command's help. -- cwells
PARAMS='help'

View File

@@ -0,0 +1,88 @@
#!/bin/sh
#
# Copyright (c) 2018-2023, Christer Edwards <christer.edwards@gmail.com>
# All rights reserved.
# Ressource limits added by Lars Engels github.com/bsdlme
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. /usr/local/share/bastille/common.sh
. /usr/local/etc/bastille/bastille.conf
usage() {
error_notify "Usage: bastille tags TARGET add tag1,tag2,..."
error_notify " bastille tags TARGET delete tag1,tag2,..."
error_notify " bastille tags TARGET list"
echo -e "Example: bastille tags JAILNAME add database,mysql"
echo -e " bastille tags JAILNAME delete mysql"
exit 1
}
# Handle special-case commands first.
case "$1" in
help|-h|--help)
usage
;;
esac
if [ $# -lt 1 -o $# -gt 2 ]; then
usage
fi
ACTION="${1}"
TAGS="${2}"
for _jail in ${JAILS}; do
bastille_jail_tags="${bastille_jailsdir}/${_jail}/tags"
if [ "${ACTION}" = "list" ]; then
[ -f "${bastille_jail_tags}" ] && cat "${bastille_jail_tags}"
continue
fi
for _tag in $(echo ${TAGS} | tr , ' '); do
case ${ACTION} in
add)
echo ${_tag} >> "${bastille_jail_tags}"
tmpfile="$(mktemp)"
sort "${bastille_jail_tags}" | uniq > "${tmpfile}"
mv "${tmpfile}" "${bastille_jail_tags}"
;;
del*)
if [ ! -f "${bastille_jail_tags}" ]; then
break
fi
tmpfile="$(mktemp)"
grep -Ev "^${_tag}\$" "${bastille_jail_tags}" > "${tmpfile}"
mv "${tmpfile}" "${bastille_jail_tags}"
# delete tags file if empty
[ ! -s "${bastille_jail_tags}" ] && rm "${bastille_jail_tags}"
;;
*)
usage
;;
esac
done
done