#!/bin/bash
# ************************ USEAGE **********************************
# program:mapurl.sh
# Time:2017/12/14
# version:V 3.0.0
# auth: Eden
# content:user to map or unmap url
# note:
# 1.only suitable for cloudfoundry appname's url
# ******************************************************************
###url map function for cloudfoundry
function mapurl(){
#a="one,two,three,four"
a=$(cf a|grep $1|tr -s ' '|cut -d ' ' -f 6-|cut -d ',' -f 1-)
OLD_IFS="$IFS"
IFS=","
arr=($a)
IFS="$OLD_IFS"
for s in ${arr[@]}
do
echo "$s"
echo ${s}|grep -q "/"
RTN=`echo $?`
if [[ ${RTN} -eq 0 ]];then
rurl=$(echo $s|cut -d '/' -f 1)
rpath=$(echo $s|cut -d '/' -f 2)
cf map-route $2 $(echo "$rurl"|cut -d '.' -f 2-) -n $(echo "$rurl"|cut -d '.' -f 1) --path ${rpath}
else
cf map-route $2 $(echo "$s"|cut -d '.' -f 2-) -n $(echo "$s"|cut -d '.' -f 1)
fi
done
}
###url unmap function for cloudfoundry
function unmapurl(){
a=$(cf a|grep $1|tr -s ' '|cut -d ' ' -f 6-|cut -d ',' -f 1-)
OLD_IFS="$IFS"
IFS=","
arr=($a)
IFS="$OLD_IFS"
for s in ${arr[@]}
do
echo "$s"
echo ${s}|grep -q "/"
RTN=`echo $?`
if [[ ${RTN} -eq 0 ]];then
rurl=$(echo $s|cut -d '/' -f 1)
rpath=$(echo $s|cut -d '/' -f 2)
cf unmap-route $1 $(echo "$rurl"|cut -d '.' -f 2-) -n $(echo "$rurl"|cut -d '.' -f 1) --path ${rpath}
else
cf unmap-route $1 $(echo "$s"|cut -d '.' -f 2-) -n $(echo "$s"|cut -d '.' -f 1)
fi
done
}
#########################################################
MAP_FLAG=FALSE;
ARGS=`getopt -o at:c:: --long all,type:,clong:: -n 'example.sh' -- "$@"`
if [ $? != 0 ]; then
echo "Terminating..."
exit 1
fi
#echo $ARGS
#将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "${ARGS}"
while true
do
case "$1" in
-a|--all)
echo "map and unmap url";
MAP_FLAG=TRUE;
shift
;;
-t|--type)
echo "Option t, argument $2";
MAP_TYPE=$2
shift 2
;;
-c|--clong)
case "$2" in
"")
echo "Option c, no argument";
shift 2
;;
*)
echo "Option c, argument $2";
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
#处理剩余的参数
for arg in $@
do
echo "appname: $arg"
done
#handles the domain name of the application process
SOURCE_APPNAME=$1;
DEST_APPNAME=$2;
if [[ "_$MAP_FLAG" == "_TRUE" ]];then
mapurl $SOURCE_APPNAME $DEST_APPNAME;
unmapurl $SOURCE_APPNAME;
echo $MAP_FLAG
else
if [ "_$(echo $MAP_TYPE|tr 'a-z' 'A-Z')" == "_MAP" ];then
echo $MAP_TYPE|tr 'a-z' 'A-Z'
mapurl $SOURCE_APPNAME $DEST_APPNAME ;
echo "$SOURCE_APPNAME, $DEST_APPNAME" ;
elif [ "_$(echo $MAP_TYPE|tr 'a-z' 'A-Z')" == "_UNMAP" ];then
echo $MAP_TYPE|tr 'a-z' 'A-Z'
unmapurl $SOURCE_APPNAME;
echo $SOURCE_APPNAME;
else
echo "please input url map type:map or unmap!"
fi
fi