rpmbuild.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. set -e
  3. show_usage="args: [--runmode=,--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:,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. --log-dir)
  22. LOG_DIR=$2; shift ;;
  23. *) shift ;;
  24. esac
  25. done
  26. if [ -z $RUNMODE ];then
  27. RUNMODE="dev"
  28. fi
  29. if [ -z $LOG_DIR ];then
  30. LOG_DIR="logs"
  31. fi
  32. PROJECT_PATH=$(cd `dirname $0`; pwd)
  33. SERVICE_NAME="${PROJECT_PATH##*/}"
  34. APP_NAME="${PROJECT_PATH##*/}"
  35. #生成配置文件
  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. COMMONYAML=conf/common.yaml
  42. sed -e "s/%RUNMODE%/$RUNMODE/g" ${COMMONYAML}.in > ${COMMONYAML}
  43. echo ${LOG_DIR}
  44. sed -i "s#%LOGDIR%#${LOG_DIR}#g" ${COMMONYAML}
  45. sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_SERVICE}.in > conf/${SERVICE_NAME}.service
  46. sed -e "s/%APP_NAME%/$APP_NAME/g" ${SH_APP}.in > ${SH_APP}
  47. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SH_APP}
  48. VERSION=`cat VERSION`
  49. make version=$VERSION
  50. mkdir -p $BUILD/buildroot/{BUILD,RPMS,S{RPMS,PECS,OURCES}}
  51. rm -f $BUILD/buildroot/SOURCES/*.tgz
  52. tar czf $BUILD/buildroot/SOURCES/${SERVICE_NAME}-$VERSION.tgz * .[!.]*
  53. sed -e "s/%VERSION%/$VERSION/g" ${SPEC_FILE}.in > ${SPEC_FILE}
  54. sed -i "s/%APP_NAME%/$APP_NAME/g" ${SPEC_FILE}
  55. sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SPEC_FILE}
  56. rpmbuild -bb ${SPEC_FILE} --define "_topdir $BUILD/buildroot/"
  57. rm -rf ./dist
  58. mkdir ./dist
  59. cp -rf $BUILD/buildroot/RPMS/* ./dist
  60. rm -rf $BUILD