dev/provisioning/modules/puppi/files/scripts/functions
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 #!/bin/bash
       
     2 # General Puppi functions
       
     3 
       
     4 BOOTUP=color
       
     5 RES_COL=75
       
     6 MOVE_TO_COL="echo -en \\033[${RES_COL}G"
       
     7 SETCOLOR_SUCCESS="echo -en \\033[0;32m"
       
     8 SETCOLOR_FAILURE="echo -en \\033[0;31m"
       
     9 SETCOLOR_WARNING="echo -en \\033[0;33m"
       
    10 SETCOLOR_NORMAL="echo -en \\033[0;39m"
       
    11 SETCOLOR_TITLE="echo -en \\033[0;35m"
       
    12 SETCOLOR_BOLD="echo -en \\033[0;1m"
       
    13 
       
    14 echo_success() {
       
    15   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
       
    16   echo -n "["
       
    17   [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
       
    18   echo -n $"  OK  "
       
    19   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       
    20   echo -n "]"
       
    21   echo -ne "\r"
       
    22   return 0
       
    23 }
       
    24 
       
    25 echo_dontdeploy() {
       
    26   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
       
    27   echo -n "["
       
    28   [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
       
    29   echo -n $" NO NEED TO DEPLOY "
       
    30   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       
    31   echo -n "]"
       
    32   echo -ne "\r"
       
    33   return 0
       
    34 }
       
    35 
       
    36 echo_failure() {
       
    37   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
       
    38   echo -n "["
       
    39   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
       
    40   echo -n $"FAILED"
       
    41   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       
    42   echo -n "]"
       
    43   echo -ne "\r"
       
    44   return 1
       
    45 }
       
    46 
       
    47 echo_passed() {
       
    48   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
       
    49   echo -n "["
       
    50   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
       
    51   echo -n $"PASSED"
       
    52   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       
    53   echo -n "]"
       
    54   echo -ne "\r"
       
    55   return 1
       
    56 }
       
    57 
       
    58 echo_warning() {
       
    59   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
       
    60   echo -n "["
       
    61   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
       
    62   echo -n $"WARNING"
       
    63   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       
    64   echo -n "]"
       
    65   echo -ne "\r"
       
    66   return 1
       
    67 }
       
    68 
       
    69 echo_title () {
       
    70   echo
       
    71   echo
       
    72   [ "$BOOTUP" = "color" ] && $SETCOLOR_TITLE
       
    73   echo "$1"
       
    74   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       
    75 }
       
    76 
       
    77 check_retcode () {
       
    78     if [ $? = "0" ] ; then
       
    79         true
       
    80     else
       
    81         exit 2
       
    82     fi
       
    83 }
       
    84 
       
    85 handle_result () {
       
    86         RETVAL=$?
       
    87         if [ "$RETVAL" = "0" ] ; then
       
    88             showresult="echo_success"
       
    89             result="OK"
       
    90         fi
       
    91         if [ "$RETVAL" = "1" ] ; then
       
    92             showresult="echo_warning"
       
    93             EXITWARN="1"
       
    94             result="WARNING"
       
    95         fi
       
    96         if [ "$RETVAL" = "2" ] ; then
       
    97             showresult="echo_failure"
       
    98             EXITCRIT="1"
       
    99             result="CRITICAL"
       
   100         fi
       
   101         if [ "$RETVAL" = "99" ] ; then
       
   102             showresult="echo_dontdeploy"
       
   103             DONTDEPLOY="1"
       
   104             result="OK"
       
   105         fi
       
   106         if [ x$show == "xyes" ] ; then
       
   107             $showresult
       
   108             echo
       
   109             echo -e "$output"
       
   110             echo
       
   111         elif [ x$show == "xfail" ] && [ x$RETVAL != "x0" ] ; then
       
   112             $showresult
       
   113             echo
       
   114             echo -e "$output"
       
   115             echo
       
   116         fi
       
   117 
       
   118         # Output to file
       
   119         if [ ! -d $logdir/$project/$tag ] ; then
       
   120             mkdir -p $logdir/$project/$tag
       
   121         fi
       
   122         let counter=counter+1
       
   123         echo $title > $logdir/$project/$tag/$counter-$command
       
   124         echo $code >> $logdir/$project/$tag/$counter-$command
       
   125         echo $result >> $logdir/$project/$tag/$counter-$command
       
   126         echo $output >> $logdir/$project/$tag/$counter-$command
       
   127 
       
   128 }
       
   129 
       
   130 
       
   131 # Function taken from http://www.threadstates.com/articles/parsing_xml_in_bash.html
       
   132 xml_parse () {
       
   133     local tag=$1
       
   134     local xml=$2
       
   135 
       
   136     # Find tag in the xml, convert tabs to spaces, remove leading spaces, remove the tag.
       
   137     grep $tag $xml | \
       
   138         tr '\011' '\040' | \
       
   139         sed -e 's/^[ ]*//' \
       
   140             -e 's/^<.*>\([^<].*\)<.*>$/\1/'
       
   141 }
       
   142 
       
   143 # Stores the passed arguments in Project runtime config file
       
   144 # Only if the parameter is not already defined
       
   145 # Usage:
       
   146 # save_runtime_config parameter=value # Sets or overrides parameter
       
   147 # save_runtime_config parameter=value notforce # Sets parameters only if is not already set
       
   148 save_runtime_config () {
       
   149     parameter=$(echo $1 | cut -d '=' -f1)
       
   150     value=$(echo $1 | cut -d '=' -f2-)
       
   151     force=$2
       
   152 
       
   153     if [[ ! $(grep $parameter $workdir/$project/config) ]] ; then
       
   154         echo  >> $workdir/$project/config
       
   155         echo "# Added by $0" >> $workdir/$project/config
       
   156         echo "$parameter=\"$value\"" >> $workdir/$project/config
       
   157     else
       
   158 #        sed -i "/^$parameter=/d" $workdir/$project/config # No real need to remove lines with old configs
       
   159         if [[ x$force == xnotforce ]] ; then
       
   160             echo  >> $workdir/$project/config
       
   161             echo "# CHANGE NOT FORCED by $0" >> $workdir/$project/config
       
   162             echo "# $parameter=\"$value\"" >> $workdir/$project/config
       
   163         else
       
   164             echo  >> $workdir/$project/config
       
   165             echo "# CHANGED by $0" >> $workdir/$project/config
       
   166             echo "$parameter=\"$value\"" >> $workdir/$project/config
       
   167        fi
       
   168     fi
       
   169     
       
   170 }
       
   171 
       
   172 # Adds a runtime comment to Project runtime config file
       
   173 save_runtime_comment () {
       
   174     echo  >> $workdir/$project/config
       
   175     echo "# Added by $0" >> $workdir/$project/config
       
   176     echo "  ## $1" >> $workdir/$project/config
       
   177 }
       
   178 
       
   179 
       
   180 # Stores the passed arguments in Project runtime config file
       
   181 # Forces parameter overwrite if already defined
       
   182 overwrite_runtime_config () {
       
   183     echo "$1" >> $workdir/$project/config
       
   184 }
       
   185 
       
   186 ask_interactive () {
       
   187     if [ x$show == "xyes" ] ; then
       
   188         echo -n $title
       
   189     fi
       
   190 
       
   191     if [ "$interactive" = "yes" ] ; then
       
   192         echo 
       
   193         echo "INTERACTIVE MODE: Press 'x' to exit or just return to go on" 
       
   194         read press
       
   195         case $press in 
       
   196             x) exit 2 ;;
       
   197             *) return
       
   198         esac
       
   199     fi
       
   200 }
       
   201 
       
   202 # Shows or executes a command
       
   203 show_command () {
       
   204    echo
       
   205    $SETCOLOR_BOLD ; echo "$HOSTNAME: $*" ; $SETCOLOR_NORMAL
       
   206 
       
   207    bash -c "$*"
       
   208 
       
   209 # Grep filter at show_command level
       
   210 #   if [ ! -z "$greppattern" ] ; then
       
   211 #       bash -c "$*" | grep $greppattern
       
   212 #   else
       
   213 #       bash -c "$*"
       
   214 #   fi
       
   215 }
       
   216 
       
   217 # Filtering out only:  $ ; ` | < >
       
   218 shell_filter () {
       
   219     echo $1 | sed 's/\$//g' | sed 's/;//g' | sed 's/`//g' | sed 's/|//g' | sed 's/<//g' | sed 's/>//g'
       
   220 }
       
   221 
       
   222 shell_filter_strict () {
       
   223 # Filtering out:  $ ; ` | < > = ! { } [ ] / \ # &
       
   224     echo $1 | sed 's/\$//g' | sed 's/;//g' | sed 's/`//g' | sed 's/|//g' | sed 's/<//g' | sed 's/>//g'  | sed 's/=//g' | sed 's/!//g' | sed 's/{//g' | sed 's/}//g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/\///g' | sed 's/\\//g' | sed 's/#//g' | sed 's/&//g'
       
   225 
       
   226 # Filtering out: all but accepted chars
       
   227 #     echo $1 | sed "s/[^a-Z0-9_\-]//Ig"
       
   228 }
       
   229