rpmbuild.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. set -e
  3. show_usage="args: [--serve-addr=,--etcd-addrs=]"
  4. #-o或--options选项后面是可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,
  5. #其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
  6. #-l或--long选项后面是可接受的长选项,用逗号分开,冒号的意义同短选项。
  7. #-n选项后接选项解析错误时提示的脚本名字
  8. ARGS=`getopt -o -h --long help,serve-addr:,etcd-addrs: -n "$0" -- "$@"`
  9. if [ $? != 0 ]; then
  10. exit 1
  11. fi
  12. #将规范化后的命令行参数分配至位置参数($1,$2,...)
  13. eval set -- "${ARGS}"
  14. while [ -n "$1" ]
  15. do
  16. case "$1" in
  17. -h|--help)
  18. echo "$show_usage"; exit 1 ;;
  19. --etcd-addrs)
  20. ETCD_ADDRS=$2; shift ;;
  21. --serve-addr)
  22. SERVE_ADDR=$2; shift ;;
  23. *) shift ;;
  24. esac
  25. done
  26. if [ -z $ETCD_ADDRS ];then
  27. ETCD_ADDRS="127.0.0.1:2379"
  28. fi
  29. if [ -z $SERVE_ADDR ];then
  30. SERVE_ADDR="0.0.0.0:7004"
  31. fi
  32. PROJECT_PATH=$(cd `dirname $0`; pwd)
  33. SERVICE_NAME="${PROJECT_PATH##*/}"
  34. APP_NAME="${PROJECT_PATH##*/}"
  35. APP_CONF=conf/app.conf
  36. DISCOVERY_TYPE="etcd"
  37. #生成配置文件
  38. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}.in > ${APP_CONF}
  39. sed -i "s/%ETCD_ADDRS%/$ETCD_ADDRS/g" ${APP_CONF}
  40. sed -i "s/%DISCOVERY_TYPE%/$DISCOVERY_TYPE/g" ${APP_CONF}
  41. sed -i "s/%SERVE_ADDR%/$SERVE_ADDR/g" ${APP_CONF}
  42. SPEC_FILE=app.spec
  43. APP_SERVICE=conf/app.service
  44. SH_APP=sh/app
  45. TOOLS_ROOT=`pwd`
  46. BUILD="${TOOLS_ROOT}/build"
  47. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_SERVICE}.in > conf/${SERVICE_NAME}.service
  48. sed -e "s/%APP_NAME%/$APP_NAME/g" ${SH_APP}.in > ${SH_APP}
  49. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SH_APP}
  50. VERSION=`cat VERSION`
  51. make version=$VERSION
  52. mkdir -p $BUILD/buildroot/{BUILD,RPMS,S{RPMS,PECS,OURCES}}
  53. rm -f $BUILD/buildroot/SOURCES/*.tgz
  54. set +e
  55. tar czf $BUILD/buildroot/SOURCES/${SERVICE_NAME}-$VERSION.tgz * .[!.]*
  56. exitcode=$?
  57. if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then
  58. exit $exitcode
  59. fi
  60. set -e
  61. sed -e "s/%VERSION%/$VERSION/g" ${SPEC_FILE}.in > ${SPEC_FILE}
  62. sed -i "s/%APP_NAME%/$APP_NAME/g" ${SPEC_FILE}
  63. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SPEC_FILE}
  64. rpmbuild -bb ${SPEC_FILE} --define "_topdir $BUILD/buildroot/"
  65. rm -rf ./dist
  66. mkdir ./dist
  67. cp -rf $BUILD/buildroot/RPMS/* ./dist
  68. rm -rf $BUILD