#!/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= ECHO_PREFIX="### " @(){ if [ -z $DRY_RUN ] ; then ! [ $QUIET ] \ && echo "${ECHO_PREFIX}$@" $@ else echo $@ fi return $? } #---------------------------------------------------------------------- # 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 } #---------------------------------------------------------------------- pct-push-r(){ local id=$1 local from=$2 local to=$3 local IFS=$'\n' local dirs=($(find "$from" -type d)) for dir in "${dirs[@]}" ; do if [[ "$dir" == "${to}" ]] ; then continue fi dir=${dir#${from}} lxc-attach $id -- mkdir -p "${to}/${dir}" done local files=($(find "$from" -type f)) for file in "${files[@]}" ; do file=${file#${from}} [ $QUIET ] \ || echo "copy: \"${from}/${file}\" -> $id:\"${to}/${file}\"" pct push $id "${from}/${file}" "${to}/${file}" done } #---------------------------------------------------------------------- # vim:set ts=4 sw=4 nowrap :