imagebuild.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. set -e
  3. show_usage="args: [--runmode=,--serve-addr=,--serve-port=,--etcd-addr=,--etcd-port=,--encrypt-key=,--log-dir=,--image-name=]"
  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:,out-ip:,log-dir:,image-name: -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. --image-name)
  32. IMAGE_NAME=$2; shift ;;
  33. --log-dir)
  34. LOG_DIR=$2; shift ;;
  35. --out-ip)
  36. OUT_IP=$2; shift ;;
  37. *) shift ;;
  38. esac
  39. done
  40. DISCOVERY_TYPE="k8s"
  41. if [ -z $OUT_IP ];then
  42. echo "out ip must be specified."
  43. fi
  44. if [ -z $SERVE_PORT ];then
  45. SERVE_PORT=60001
  46. fi
  47. if [ -z $RUNMODE ];then
  48. RUNMODE="dev"
  49. fi
  50. if [ -z $SERVE_ADDR ];then
  51. SERVE_ADDR="0.0.0.0"
  52. fi
  53. if [ -z $ETCD_ADDR ];then
  54. ETCD_ADDR="127.0.0.1"
  55. fi
  56. if [ -z $ETCD_PORT ];then
  57. ETCD_PORT="2379"
  58. fi
  59. if [ -z $ENCRYPT_KEY ];then
  60. ENCRYPT_KEY="a95cbb574bdc905f9bf457820f1fa602"
  61. fi
  62. if [ -z $LOG_DIR ];then
  63. LOG_DIR="logs"
  64. fi
  65. SERVICE_NAME="gd-management-gateway"
  66. if [ -z $IMAGE_NAME ];then
  67. IMAGE_NAME=$SERVICE_NAME
  68. fi
  69. PROJECT_PATH=$(cd `dirname $0`; pwd)
  70. APP_NAME="${PROJECT_PATH##*/}"
  71. APP_CONF=conf/app.conf
  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. VERSION=`cat VERSION`
  86. make version=$VERSION
  87. DOCKERFILE=Dockerfile
  88. DOCKERCOMPOSE=docker/compose/docker-compose.yaml
  89. K8SFILE=docker/kubernetes/$SERVICE_NAME.yaml
  90. sed -e "s/%APP_NAME%/$APP_NAME/g" ${DOCKERFILE}.in > ${DOCKERFILE}
  91. sed -i "s/%SERVE_PORT%/$SERVE_PORT/g" ${DOCKERFILE}
  92. sed -e "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${DOCKERCOMPOSE}.in > ${DOCKERCOMPOSE}
  93. sed -e "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${K8SFILE}.in > ${K8SFILE}
  94. sed -i "s/%SERVE_PORT%/$SERVE_PORT/g" ${K8SFILE}
  95. sed -i "s/%RUNMODE%/$RUNMODE/g" ${K8SFILE}
  96. docker build . -t $IMAGE_NAME