Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -eu
- # working path
- wp=$(dirname $0)
- wp=$(readlink -f ${wp})
- # config
- config=${wp}/cluster.list
- # return 1 if string argument is natural number 0 otherwise
- function is_natural_number ()
- {
- case "$1" in
- ''|*[!0-9]*) return 1 ;;
- *) return 0 ;;
- esac
- }
- # return 1 if string argument is number and 0 otherwise
- function is_number ()
- {
- local string="$1"
- if [[ "$string" == "" ]]; then
- return 1
- fi
- if is_natural_number $string; then
- return 0
- fi
- # first symbol from string
- local first="${string:0:1}"
- # string without first symbol
- string="${string:1:${#string}-1}"
- if [[ $first == "-" ]]; then
- if is_natural_number "$string"; then
- return 0
- fi
- fi
- return 1
- }
- # Usage: <input> | drop <first|last> [-]<count>
- #
- # if count natural, drop first lines from <input> (<last> not supported)
- # if count negative, drop all except first or last <count> lines from <input>
- function drop ()
- {
- case $1 in
- first|last) ;;
- *)
- echo "drop: expected 'first' or 'last' as first argument. You provided '$1'"
- return 1
- ;;
- esac
- local order=$1
- if ! is_number $2; then
- echo "drop: expected number as second argument. You provided '$2'"
- return 1
- fi;
- local count=$2
- if [[ $count -eq 0 ]] ; then
- while read line; do
- echo $line
- done
- elif is_natural_number $count; then
- # drop all except $count
- case $order in
- first)
- # drop $count lines from begin
- while read line; do
- if [[ $count -eq 0 ]]; then
- echo $line
- else
- count=$((count-1))
- fi
- done
- ;;
- last)
- echo "last with natural count not supported yet"
- return 1
- ;;
- esac
- else
- # drop $count
- case $order in
- first)
- # drop all except $count lines from begin
- tail -n+$((-count+1))
- ;;
- last)
- # drop all except $count lines from end
- head -n-$((-count))
- ;;
- esac
- fi
- return 0
- }
- # Usage: <input> | join <delimeter>
- # Combine lines from <input> and insert <delimeter> between every pair
- function join ()
- {
- local delimeter="$1"
- sed -e "s/^/${delimeter}\n/g" | drop first 1 | xargs
- }
- function config_get_column ()
- {
- local column=$1
- cat ${config} | drop "first" 1 | grep -v "#" | awk "{ print \$${column} }"
- }
- function config_get_column_as_list ()
- {
- config_get_column $1 | sort -u | join "${2-,}" | tr -d ' '
- }
- function print_usage ()
- {
- echo "Script print machine names according to config file ${config}
- Usage ${0} <platform> <cluster_number> <vm_number>
- - platform can be
- 'c6' - CentOS 6.3
- 'r6' - RedHat 6.3
- 'u1204' - Ubuntu 12.04
- 'all' - all platforms
- - cluster_number is [$(config_get_column_as_list 3)] or 'all'
- - vm_number is [$(config_get_column_as_list 2)] or 'all'
- "
- if [ "${1-}" != "" ]; then
- local message="${1}"
- echo "error: ${message}"
- fi
- exit 1
- }
- function check_platform ()
- {
- case ${platform} in
- c6|r6|u1204|all) ;;
- *) print_usage "expected 'c6', 'r6', 'u1204' or 'all' for <platform>, you provided '${platform}'";;
- esac
- }
- function check_number ()
- {
- local column=${1}
- local actual=${2}
- local name=${3}
- local found=0
- for expected in $(config_get_column ${column} | sort -u); do
- if [[ "$expected" == "$actual" ]]; then
- found=1
- break
- fi
- done
- if [[ $found -ne 1 ]]; then
- print_usage "expected all|$(config_get_column_as_list ${column} \|) as '${3}', you provided '${2}'"
- fi
- }
- function check_cluster ()
- {
- if [ "${cluster}" != "all" ]; then
- check_number 2 "${cluster}" "cluster_number"
- fi;
- }
- function check_vm ()
- {
- if [ "${vm}" != "all" ]; then
- check_number 3 "${vm}" "vm_number"
- fi;
- }
- if [ $# -eq 0 ]; then
- print_usage
- fi;
- if [ $# -ne 3 ]; then
- print_usage "invalid argument count, expected 4, you provided $#"
- fi;
- platform=${1}
- check_platform
- cluster=${2}
- check_cluster
- vm=${3}
- check_vm
- awk_script='{ if ('
- if [ "$platform" == "all" ]; then
- awk_script="${awk_script}1"
- else
- awk_script="${awk_script}(\"$platform\" == \$1)"
- fi;
- awk_script="${awk_script} && "
- if [ "$cluster" == "all" ]; then
- awk_script="${awk_script}1"
- else
- awk_script="${awk_script}($cluster == \$2)"
- fi;
- awk_script="${awk_script} && "
- if [ "$vm" == "all" ]; then
- awk_script="${awk_script}1"
- else
- awk_script="${awk_script}($vm == \$3)"
- fi;
- awk_script="${awk_script}) print \$4 }"
- cat ${config} | tail -n+2 | grep -v "#" | awk "${awk_script}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement