rpmbuild.sh 3.3 KB

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