Advertisement
zamotivator

Untitled

Jun 14th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.55 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -eu
  4.  
  5. # working path
  6. wp=$(dirname $0)
  7. wp=$(readlink -f ${wp})
  8.  
  9. # config
  10. config=${wp}/cluster.list
  11.  
  12. # return 1 if string argument is natural number 0 otherwise
  13. function is_natural_number ()
  14. {
  15.     case "$1" in
  16.         ''|*[!0-9]*) return 1 ;;
  17.         *)           return 0 ;;
  18.     esac
  19. }
  20.  
  21. # return 1 if string argument is number and 0 otherwise
  22. function is_number ()
  23. {
  24.     local string="$1"
  25.     if [[ "$string" == "" ]]; then
  26.     return 1
  27.     fi
  28.     if is_natural_number $string; then
  29.     return 0
  30.     fi
  31.     # first symbol from string
  32.     local first="${string:0:1}"
  33.     # string without first symbol
  34.     string="${string:1:${#string}-1}"
  35.     if [[ $first == "-" ]]; then
  36.     if is_natural_number "$string"; then
  37.         return 0
  38.     fi
  39.     fi
  40.     return 1
  41. }
  42.  
  43. # Usage: <input> | drop <first|last> [-]<count>
  44. #
  45. # if count natural, drop first lines from <input> (<last> not supported)
  46. # if count negative, drop all except first or last <count> lines from <input>
  47. function drop ()
  48. {
  49.     case $1 in
  50.     first|last) ;;
  51.     *)
  52.         echo "drop: expected 'first' or 'last' as first argument. You provided '$1'"
  53.         return 1
  54.         ;;
  55.     esac
  56.     local order=$1
  57.     if ! is_number $2; then
  58.     echo "drop: expected number as second argument. You provided '$2'"
  59.     return 1
  60.     fi;
  61.     local count=$2
  62.     if [[ $count -eq 0 ]] ; then
  63.     while read line; do
  64.         echo $line
  65.     done
  66.     elif is_natural_number $count; then
  67.     # drop all except $count
  68.     case $order in
  69.         first)
  70.         # drop $count lines from begin
  71.         while read line; do
  72.             if [[ $count -eq 0 ]]; then
  73.             echo $line
  74.             else
  75.             count=$((count-1))
  76.             fi
  77.         done
  78.         ;;
  79.         last)
  80.         echo "last with natural count not supported yet"
  81.         return 1
  82.         ;;
  83.     esac
  84.     else
  85.     # drop $count
  86.     case $order in
  87.         first)
  88.         # drop all except $count lines from begin
  89.         tail -n+$((-count+1))
  90.         ;;
  91.         last)
  92.         # drop all except $count lines from end
  93.         head -n-$((-count))
  94.         ;;
  95.     esac
  96.     fi
  97.     return 0
  98. }
  99.  
  100. # Usage: <input> | join <delimeter>
  101. # Combine lines from <input> and insert <delimeter> between every pair
  102. function join ()
  103. {
  104.     local delimeter="$1"
  105.     sed -e "s/^/${delimeter}\n/g" | drop first 1 | xargs
  106. }
  107.  
  108. function config_get_column ()
  109. {
  110.     local column=$1
  111.     cat ${config} | drop "first" 1 | grep -v "#" | awk "{ print \$${column} }"
  112. }
  113.  
  114. function config_get_column_as_list ()
  115. {
  116.     config_get_column $1 | sort -u | join "${2-,}" | tr -d ' '
  117. }
  118.  
  119. function print_usage ()
  120. {
  121.     echo "Script print machine names according to config file ${config}
  122.  
  123. Usage ${0} <platform> <cluster_number> <vm_number>
  124.  
  125.  - platform can be
  126.        'c6' - CentOS 6.3
  127.        'r6' - RedHat 6.3
  128.        'u1204' - Ubuntu 12.04
  129.     'all' - all platforms
  130.  - cluster_number is [$(config_get_column_as_list 3)] or 'all'
  131.  - vm_number is [$(config_get_column_as_list 2)] or 'all'
  132. "
  133.     if [ "${1-}" != "" ]; then
  134.     local message="${1}"
  135.     echo "error: ${message}"
  136.     fi
  137.     exit 1
  138. }
  139.  
  140. function check_platform ()
  141. {
  142.     case ${platform} in
  143.     c6|r6|u1204|all) ;;
  144.     *) print_usage "expected 'c6', 'r6', 'u1204' or 'all' for <platform>, you provided '${platform}'";;
  145.     esac
  146. }
  147.  
  148. function check_number ()
  149. {
  150.     local column=${1}
  151.     local actual=${2}
  152.     local name=${3}
  153.     local found=0
  154.     for expected in $(config_get_column ${column} | sort -u); do
  155.     if [[ "$expected" == "$actual" ]]; then
  156.         found=1
  157.         break
  158.     fi
  159.     done
  160.     if [[ $found -ne 1 ]]; then
  161.     print_usage "expected all|$(config_get_column_as_list ${column} \|) as '${3}', you provided '${2}'"
  162.     fi
  163. }
  164.  
  165. function check_cluster ()
  166. {
  167.     if [ "${cluster}" != "all" ]; then
  168.     check_number 2 "${cluster}" "cluster_number"
  169.     fi;
  170. }
  171.  
  172. function check_vm ()
  173. {
  174.     if [ "${vm}" != "all" ]; then
  175.     check_number 3 "${vm}" "vm_number"
  176.     fi;
  177. }
  178.  
  179. if [ $# -eq 0 ]; then
  180.     print_usage
  181. fi;
  182.  
  183. if [ $# -ne 3 ]; then
  184.     print_usage "invalid argument count, expected 4, you provided $#"
  185. fi;
  186.  
  187. platform=${1}
  188. check_platform
  189. cluster=${2}
  190. check_cluster
  191. vm=${3}
  192. check_vm
  193.  
  194. awk_script='{ if ('
  195. if [ "$platform" == "all" ]; then
  196.     awk_script="${awk_script}1"
  197. else
  198.     awk_script="${awk_script}(\"$platform\" == \$1)"
  199. fi;
  200. awk_script="${awk_script} && "
  201. if [ "$cluster" == "all" ]; then
  202.     awk_script="${awk_script}1"
  203. else
  204.     awk_script="${awk_script}($cluster == \$2)"
  205. fi;
  206. awk_script="${awk_script} && "
  207. if [ "$vm" == "all" ]; then
  208.     awk_script="${awk_script}1"
  209. else
  210.     awk_script="${awk_script}($vm == \$3)"
  211. fi;
  212. awk_script="${awk_script}) print \$4 }"
  213. cat ${config} | tail -n+2 | grep -v "#" | awk "${awk_script}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement