#!/bin/bash # Copyright 2015 Jelastic, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. [ -n "${VZEXECLIB_VERSION:-}" ] && return 0; VZEXECLIB_VERSION="0.1"; declare __CTID="" #stdout from last command declare __STDOUT #stderr from last command declare __STDERR declare __ERRCODE $PROGRAM 'vzctl'; $PROGRAM 'vzlist'; $PROGRAM 'prlctl'; #__EXEC="$VZCTL exec2" __EXEC="/bin/bash -c " function vzexecSetCTID() { __CTID=$1 if [ -z "$__CTID" ] ; then __EXEC="/bin/bash -c " else __EXEC="$VZCTL exec2" fi } vzexecSetENVID(){ __ENVID=$1 if [ -z "$__ENVID" ] ; then __EXEC="/bin/bash -c " else __EXEC="$PRLCTL exec" fi } # return 0 if success # return 99 if missing param # otherwise returns a code from called program function vzexecRun() { local buffer; local _tmperr _tmperr=$(mktemp -u --tmpdir=/tmp) [[ "$VERBOSE" -gt 0 ]] && echo -e "`date +%D.%k:%M:%S.%N`: vzexec command start ($__CTID) (PID: $$): $*" >> ${JEM_CALLS_LOG} __STDOUT=$( set -f ; $__EXEC $__CTID "$*" 2>$_tmperr; ); __ERRCODE=$? __STDERR=$(<$_tmperr) rm -f $_tmperr [[ "$VERBOSE" -gt 0 ]] && echo -e "`date +%D.%k:%M:%S.%N`: vzexec command done ($__CTID) (PID: $$): $*\n`date +%D.%k:%M:%S.%N`: vzexec stdout ($__CTID):${__STDOUT}\n`date +%D.%k:%M:%S.%N`: vzexec stderr ($__CTID):${__STDERR}" >> ${JEM_CALLS_LOG} return $__ERRCODE } function prlExecRun() { local buffer; local _tmperr _tmperr=$(mktemp -u --tmpdir=/tmp) [[ "$VERBOSE" -gt 0 ]] && echo -e "`date +%D.%k:%M:%S.%N`: prlexec command start ($__CTID) (PID: $$): $*" >> ${JEM_CALLS_LOG} __STDOUT=$( set -f ; $__EXEC $__ENVID "$*" 2>$_tmperr; ); __ERRCODE=$? __STDERR=$(<$_tmperr) rm -f $_tmperr [[ "$VERBOSE" -gt 0 ]] && echo -e "`date +%D.%k:%M:%S.%N`: prlexec command done ($__CTID) (PID: $$): $*\n`date +%D.%k:%M:%S.%N`: prlexec stdout ($__CTID):${__STDOUT}\n`date +%D.%k:%M:%S.%N`: prlexec stderr ($__CTID):${__STDERR}" >> ${JEM_CALLS_LOG} return $__ERRCODE } function VEExecRun() { if isUUID "$VEID"; then prlExecRun "$@" return $? else vzexecRun "$@" return $? fi } function VEExecRunInteractive() { if isUUID "$VEID"; then $__EXEC $__ENVID "$@" return $? else $__EXEC $__CTID "$@" return $? fi } #All needed defined variables should be passed #example: #> vzexecFunction function_ExtendPerm #or #> vzexecFunction function_ExtendPerm prefix=$prefix JEM_CALLS_LOG=/tmp/tmp.jem.log vzexecFunction(){ local functionname=${1//function_} shift; if isFunction "$functionname"; then local funct="$(declare -f "$functionname")" ( vzexecRun "$@; $funct; $functionname" ) else return 255 fi } function vzexecGetLastStdErr() { echo "${__STDERR}" } function VEexecGetLastStdErr() { echo "${__STDERR}" } function vzexecGetLastStdOut() { echo "${__STDOUT}" } function VEexecGetLastStdOut() { echo "${__STDOUT}" } function vzexecGetLastErrCode() { echo $__ERRCODE } function VEexecGetLastErrCode() { echo $__ERRCODE } function vzexecRunStdOut() { local buffer; local _tmperr _tmperr=$(mktemp -u --tmpdir=/tmp) [[ "$VERBOSE" -gt 0 ]] && echo -e "`date +%D.%k:%M:%S.%N`: vzexec command start ($__CTID) (PID: $$): $*" >> ${JEM_CALLS_LOG} __STDOUT=$( set -f ; $__EXEC $__CTID "$*" 2>$_tmperr; ); __ERRCODE=$? __STDERR=$(<$_tmperr) rm -f $_tmperr [[ "$VERBOSE" -gt 0 ]] && echo -e "`date +%D.%k:%M:%S.%N`: vzexec command done ($__CTID) (PID: $$): $*\n`date +%D.%k:%M:%S.%N`: vzexec stdout ($__CTID):${__STDOUT}\n`date +%D.%k:%M:%S.%N`: vzexec stderr ($__CTID):${__STDERR}" >> ${JEM_CALLS_LOG} echo $__STDOUT return $__ERRCODE } function vzctPath() { local _root=$1 shift set -- "$@" echo $(vzreadlink -q --vzroot="$_root" $* 2>/dev/null) }