#!/bin/bash set -e show_usage="args: [--serve-addr=,--etcd-addrs=]" #-o或--options选项后面是可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c, #其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的 #-l或--long选项后面是可接受的长选项,用逗号分开,冒号的意义同短选项。 #-n选项后接选项解析错误时提示的脚本名字 ARGS=`getopt -o -h --long help,serve-addr:,etcd-addrs: -n "$0" -- "$@"` if [ $? != 0 ]; then exit 1 fi #将规范化后的命令行参数分配至位置参数($1,$2,...) eval set -- "${ARGS}" while [ -n "$1" ] do case "$1" in -h|--help) echo "$show_usage"; exit 1 ;; --etcd-addrs) ETCD_ADDRS=$2; shift ;; --serve-addr) SERVE_ADDR=$2; shift ;; *) shift ;; esac done if [ -z $ETCD_ADDRS ];then ETCD_ADDRS="127.0.0.1:2379" fi if [ -z $SERVE_ADDR ];then SERVE_ADDR="0.0.0.0:7004" fi PROJECT_PATH=$(cd `dirname $0`; pwd) SERVICE_NAME="${PROJECT_PATH##*/}" APP_NAME="${PROJECT_PATH##*/}" APP_CONF=conf/app.conf DISCOVERY_TYPE="etcd" #生成配置文件 sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}.in > ${APP_CONF} sed -i "s/%ETCD_ADDRS%/$ETCD_ADDRS/g" ${APP_CONF} sed -i "s/%DISCOVERY_TYPE%/$DISCOVERY_TYPE/g" ${APP_CONF} sed -i "s/%SERVE_ADDR%/$SERVE_ADDR/g" ${APP_CONF} SPEC_FILE=app.spec APP_SERVICE=conf/app.service SH_APP=sh/app TOOLS_ROOT=`pwd` BUILD="${TOOLS_ROOT}/build" sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_SERVICE}.in > conf/${SERVICE_NAME}.service sed -e "s/%APP_NAME%/$APP_NAME/g" ${SH_APP}.in > ${SH_APP} sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SH_APP} VERSION=`cat VERSION` make version=$VERSION mkdir -p $BUILD/buildroot/{BUILD,RPMS,S{RPMS,PECS,OURCES}} rm -f $BUILD/buildroot/SOURCES/*.tgz set +e tar czf $BUILD/buildroot/SOURCES/${SERVICE_NAME}-$VERSION.tgz * .[!.]* exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then exit $exitcode fi set -e sed -e "s/%VERSION%/$VERSION/g" ${SPEC_FILE}.in > ${SPEC_FILE} sed -i "s/%APP_NAME%/$APP_NAME/g" ${SPEC_FILE} sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SPEC_FILE} rpmbuild -bb ${SPEC_FILE} --define "_topdir $BUILD/buildroot/" rm -rf ./dist mkdir ./dist cp -rf $BUILD/buildroot/RPMS/* ./dist rm -rf $BUILD