mirror of
https://github.com/flynx/proxmox-utils.git
synced 2025-10-28 10:40:07 +00:00
129 lines
2.4 KiB
Bash
129 lines
2.4 KiB
Bash
#!/usr/bin/bash
|
|
#----------------------------------------------------------------------
|
|
|
|
CT_DIR=${CT_DIR:=/etc/pve/lxc/}
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# XXX this is quite generic, might be a good idea to move this to a
|
|
# seporate lib/file...
|
|
|
|
# Execute (optionally) and print a command.
|
|
#
|
|
#QUIET=
|
|
#DRY_RUN=
|
|
@(){
|
|
if [ -z $DRY_RUN ] ; then
|
|
! [ $QUIET ] \
|
|
&& echo "### $@"
|
|
$@
|
|
else
|
|
echo $@
|
|
fi
|
|
}
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# Fill section...
|
|
#
|
|
# XXX this is quite generic -- move to a more logical place...
|
|
|
|
fillsection(){ (
|
|
usage(){
|
|
echo "Usage:"
|
|
echo " ${FUNCNAME[0]} [-h]"
|
|
echo " ${FUNCNAME[0]} [-r] NAME FILE [CONTENT]"
|
|
echo
|
|
}
|
|
while true ; do
|
|
case $1 in
|
|
-h|--help)
|
|
usage
|
|
echo "Options:"
|
|
# . . .
|
|
echo " -h | --help print this help message and exit."
|
|
echo " -r | --return replace section markers with CONTENT."
|
|
echo
|
|
return 0
|
|
;;
|
|
-r|--replace)
|
|
local replace=1
|
|
shift
|
|
;;
|
|
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
if [[ $# < 2 ]] ; then
|
|
usage
|
|
return 1
|
|
fi
|
|
|
|
name=$1
|
|
file=$2
|
|
content=$3
|
|
content=${content:=/dev/stdin}
|
|
|
|
# print file upto section marker...
|
|
if [ $replace ] ; then
|
|
sed "/${name^^} BEGIN/q" "$file" | sed '$d'
|
|
else
|
|
sed "/${name^^} BEGIN/q" "$file"
|
|
fi
|
|
# print content...
|
|
cat $content
|
|
# print file from section end marker...
|
|
if [ $replace ] ; then
|
|
sed -ne "/${name^^} END/,$ p" "$file" | sed '1d'
|
|
else
|
|
sed -ne "/${name^^} END/,$ p" "$file"
|
|
fi
|
|
) }
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# CT hostname <-> CT id...
|
|
|
|
ct2hostname(){
|
|
local ct=${CT_DIR}/${1}.conf
|
|
local host=$(cat $ct | grep hostname | head -1)
|
|
echo ${host/hostname: /}
|
|
}
|
|
|
|
hostname2ct(){
|
|
if [ -e "${CT_DIR}/${1}.conf" ] ; then
|
|
echo $1
|
|
fi
|
|
local running=$2
|
|
running=${running:=any}
|
|
local ct
|
|
local host
|
|
for ct in "${CT_DIR}"/*.conf ; do
|
|
host=$(cat $ct | grep hostname | head -1)
|
|
host=${host/hostname: /}
|
|
if [ "$host" = $1 ] ; then
|
|
ct=${ct#${CT_DIR}}
|
|
ct=${ct%.conf}
|
|
ct=${ct#\/}
|
|
# filter results if needed...
|
|
if [ $running = "any" ] ; then
|
|
echo $ct
|
|
else
|
|
local status=`pct status $ct`
|
|
if [ "$running" = "${status/status: /}" ] ; then
|
|
echo $ct
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
}
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
# vim:set ts=4 sw=4 nowrap :
|