rpmbuild.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. set -e
  3. BEECOMD=`which bee`
  4. show_usage="args: [--runmode=,--serve-addr=,--serve-port=,--etcd-addr=,--etcd-port=,--encrypt-key=,--log-dir=]"
  5. #-o或--options选项后面是可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,
  6. #其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
  7. #-l或--long选项后面是可接受的长选项,用逗号分开,冒号的意义同短选项。
  8. #-n选项后接选项解析错误时提示的脚本名字
  9. ARGS=`getopt -o -h --long help,runmode:,serve-addr:,serve-port:,etcd-addr:,etcd-port:,encrypt-key:,out-ip:,log-dir: -n "$0" -- "$@"`
  10. if [ $? != 0 ]; then
  11. exit 1
  12. fi
  13. #将规范化后的命令行参数分配至位置参数($1,$2,...)
  14. eval set -- "${ARGS}"
  15. while [ -n "$1" ]
  16. do
  17. case "$1" in
  18. -h|--help)
  19. echo "$show_usage"; exit 1 ;;
  20. --runmode)
  21. RUNMODE=$2; shift ;;
  22. --serve-addr)
  23. SERVE_ADDR=$2; shift ;;
  24. --serve-port)
  25. SERVE_PORT=$2; shift ;;
  26. --etcd-addr)
  27. ETCD_ADDR=$2; shift ;;
  28. --etcd-port)
  29. ETCD_PORT=$2; shift ;;
  30. --encrypt-key)
  31. ENCRYPT_KEY=$2; shift ;;
  32. --log-dir)
  33. LOG_DIR=$2; shift ;;
  34. --out-ip)
  35. OUT_IP=$2; shift ;;
  36. *) shift ;;
  37. esac
  38. done
  39. if [ -z $SERVE_PORT ];then
  40. echo "the server_port must be specified."
  41. exit 1
  42. fi
  43. if [ -z $OUT_IP ];then
  44. echo "out ip must be specified."
  45. fi
  46. if [ -z $RUNMODE ];then
  47. RUNMODE="dev"
  48. fi
  49. if [ -z $SERVE_ADDR ];then
  50. SERVE_ADDR="0.0.0.0"
  51. fi
  52. if [ -z $ETCD_ADDR ];then
  53. ETCD_ADDR="127.0.0.1"
  54. fi
  55. if [ -z $ETCD_PORT ];then
  56. ETCD_PORT="2379"
  57. fi
  58. if [ -z $ENCRYPT_KEY ];then
  59. ENCRYPT_KEY="a95cbb574bdc905f9bf457820f1fa602"
  60. fi
  61. if [ -z $LOG_DIR ];then
  62. LOG_DIR="logs"
  63. fi
  64. PROJECT_PATH=$(cd `dirname $0`; pwd)
  65. SERVICE_NAME="${PROJECT_PATH##*/}"
  66. APP_NAME="${PROJECT_PATH##*/}"
  67. APP_CONF=conf/app.conf
  68. GITLINE=`git log | head -1`
  69. GITLOG=gitlog
  70. DISCOVERY_TYPE="etcd"
  71. echo $GITLINE > $GITLOG
  72. #生成配置文件
  73. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}.in > ${APP_CONF}
  74. sed -i "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}
  75. sed -i "s/%RUNMODE%/$RUNMODE/g" ${APP_CONF}
  76. sed -i "s/%SERVE_ADDR%/$SERVE_ADDR/g" ${APP_CONF}
  77. sed -i "s/%SERVE_PORT%/$SERVE_PORT/g" ${APP_CONF}
  78. sed -i "s/%ETCD_ADDR%/$ETCD_ADDR/g" ${APP_CONF}
  79. sed -i "s/%ETCD_PORT%/$ETCD_PORT/g" ${APP_CONF}
  80. sed -i "s/%ENCRYPT_KEY%/$ENCRYPT_KEY/g" ${APP_CONF}
  81. sed -i "s/%LOG_DIR%/$LOG_DIR/g" ${APP_CONF}
  82. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${APP_CONF}
  83. sed -i "s/%DISCOVERY_TYPE%/$DISCOVERY_TYPE/g" ${APP_CONF}
  84. sed -i "s/%OUT_IP%/$OUT_IP/g" ${APP_CONF}
  85. SPEC_FILE=app.spec
  86. APP_SERVICE=conf/app.service
  87. SH_APP=sh/app
  88. TOOLS_ROOT=`pwd`
  89. BUILD="${TOOLS_ROOT}/build"
  90. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_SERVICE}.in > conf/${SERVICE_NAME}.service
  91. sed -e "s/%APP_NAME%/$APP_NAME/g" ${SH_APP}.in > ${SH_APP}
  92. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SH_APP}
  93. $BEECOMD generate docs
  94. VERSION=`cat VERSION`
  95. make version=$VERSION
  96. mkdir -p $BUILD/buildroot/{BUILD,RPMS,S{RPMS,PECS,OURCES}}
  97. rm -f $BUILD/buildroot/SOURCES/*.tgz
  98. tar czf $BUILD/buildroot/SOURCES/${SERVICE_NAME}-$VERSION.tgz * .[!.]*
  99. sed -e "s/%VERSION%/$VERSION/g" ${SPEC_FILE}.in > ${SPEC_FILE}
  100. sed -i "s/%APP_NAME%/$APP_NAME/g" ${SPEC_FILE}
  101. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SPEC_FILE}
  102. rpmbuild -bb ${SPEC_FILE} --define "_topdir $BUILD/buildroot/"
  103. rm -rf ./dist
  104. mkdir ./dist
  105. cp -rf $BUILD/buildroot/RPMS/* ./dist
  106. rm -rf $BUILD