rpmbuild.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. set -e
  3. show_usage="args: [--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,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. *) shift ;;
  22. esac
  23. done
  24. if [ -z $ETCD_ADDRS ];then
  25. ETCD_ADDRS="127.0.0.1:2379"
  26. fi
  27. PROJECT_PATH=$(cd `dirname $0`; pwd)
  28. SERVICE_NAME="${PROJECT_PATH##*/}"
  29. APP_NAME="${PROJECT_PATH##*/}"
  30. APP_CONF=conf/app.conf
  31. DISCOVERY_TYPE="etcd"
  32. #生成配置文件
  33. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}.in > ${APP_CONF}
  34. sed -i "s/%ETCD_ADDRS%/$ETCD_ADDRS/g" ${APP_CONF}
  35. sed -i "s/%DISCOVERY_TYPE%/$DISCOVERY_TYPE/g" ${APP_CONF}
  36. SPEC_FILE=app.spec
  37. APP_SERVICE=conf/app.service
  38. SH_APP=sh/app
  39. TOOLS_ROOT=`pwd`
  40. BUILD="${TOOLS_ROOT}/build"
  41. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_SERVICE}.in > conf/${SERVICE_NAME}.service
  42. sed -e "s/%APP_NAME%/$APP_NAME/g" ${SH_APP}.in > ${SH_APP}
  43. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SH_APP}
  44. VERSION=`cat VERSION`
  45. make version=$VERSION
  46. mkdir -p $BUILD/buildroot/{BUILD,RPMS,S{RPMS,PECS,OURCES}}
  47. rm -f $BUILD/buildroot/SOURCES/*.tgz
  48. set +e
  49. tar czf $BUILD/buildroot/SOURCES/${SERVICE_NAME}-$VERSION.tgz * .[!.]*
  50. exitcode=$?
  51. if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then
  52. exit $exitcode
  53. fi
  54. set -e
  55. sed -e "s/%VERSION%/$VERSION/g" ${SPEC_FILE}.in > ${SPEC_FILE}
  56. sed -i "s/%APP_NAME%/$APP_NAME/g" ${SPEC_FILE}
  57. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SPEC_FILE}
  58. rpmbuild -bb ${SPEC_FILE} --define "_topdir $BUILD/buildroot/"
  59. rm -rf ./dist
  60. mkdir ./dist
  61. cp -rf $BUILD/buildroot/RPMS/* ./dist
  62. rm -rf $BUILD