formal.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #安装maven
  2. if ! command -v mvn >/dev/null 2>&1; then
  3. echo "Installing Maven"
  4. # 下载Maven
  5. wget https://archive.apache.org/dist/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
  6. # 解压Maven
  7. tar -xzf apache-maven-3.8.1-bin.tar.gz -C /opt
  8. # 设置Maven环境变量
  9. echo 'export PATH=/opt/apache-maven-3.8.1/bin:$PATH' >> ~/.bashrc
  10. # 更改Maven镜像
  11. mkdir -p ~/.m2
  12. echo -e "<settings>\n <mirrors>\n <mirror>\n <id>aliyun-maven</id>\n <mirrorOf>*</mirrorOf>\n <name>Aliyun Maven</name>\n <url>https://maven.aliyun.com/repository/public</url>\n </mirror>\n </mirrors>\n</settings>" > ~/.m2/settings.xml
  13. source ~/.bashrc
  14. else
  15. echo "Maven is already installed"
  16. fi
  17. #安装git
  18. sudo yum install -y git
  19. #安装依赖
  20. sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  21. #设置源
  22. sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  23. sudo yum makecache fast
  24. # 检查Docker是否已安装
  25. if ! command -v docker &> /dev/null
  26. then
  27. # 如果Docker未安装,执行安装步骤
  28. sudo yum install -y docker-ce
  29. sudo systemctl start docker
  30. fi
  31. # 检查Docker Compose是否已安装
  32. if ! command -v docker-compose &> /dev/null
  33. then
  34. # 如果Docker Compose未安装,执行安装步骤
  35. sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.1/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
  36. sudo chmod +x /usr/local/bin/docker-compose
  37. fi
  38. # 显示版本号
  39. docker-compose -v
  40. #拉取代码打包
  41. APP_NAME="promotesai"
  42. mkdir /data/app/${APP_NAME}/${APP_NAME}
  43. mkdir /data/app/${APP_NAME}/config
  44. cd /data/app/${APP_NAME}
  45. git init
  46. # 从私有仓库拉取代码
  47. git clone http://124.222.247.184:3000/admin_lsl/patent.git
  48. git pull
  49. cd ${APP_NAME}
  50. # Maven打包
  51. echo 开始打包
  52. mvn clean package
  53. # 定义应用组名
  54. group_name="$1"
  55. # 定义应用名称
  56. app_name="$1"
  57. # 定义应用版本
  58. app_version='latest'
  59. # 定义应用环境
  60. profile_active='prod'
  61. echo '----copy jar----'
  62. docker stop ${app_name}
  63. echo '----stop container----'
  64. docker rm ${app_name}
  65. echo '----rm container----'
  66. docker rmi ${group_name}/${app_name}:${app_version}
  67. echo '----rm image----'
  68. cd ${app_name}
  69. echo "当前目录是:$PWD"
  70. docker build -t ${group_name}/${app_name}:${app_version} --build-arg JAR_FILE=./target/client-api-0.0.1-SNAPSHOT.jar .
  71. echo '----build image----'
  72. docker run -p $2:8881 --name ${app_name} \
  73. -e TZ="Asia/Shanghai" \
  74. -v /etc/localtime:/etc/localtime \
  75. -v /data/app/patent/config:/config \
  76. -d ${group_name}/${app_name}:${app_version}
  77. echo '----start container----'
  78. echo "拉取nginx镜像"
  79. docker pull nginx:latest
  80. echo "运行nginx容器"
  81. docker run --name nginx -p 80:80 -d nginx
  82. if [ ! -f /opt/docker/nginx/conf/nginx.conf ]; then
  83. echo "创建文件目录"
  84. mkdir -p /opt/docker/nginx/conf.d
  85. mkdir -p /opt/docker/nginx/html
  86. mkdir -p /opt/docker/nginx/logs
  87. mkdir -p /opt/docker/nginx/conf/nginx.conf
  88. echo "拷贝nginx容器对应的文件默认配置"
  89. docker cp nginx:/etc/nginx/nginx.conf /opt/docker/nginx/conf/nginx.conf
  90. docker cp nginx:/etc/nginx/conf.d /opt/docker/nginx/conf.d
  91. docker cp nginx:/usr/share/nginx/html /opt/docker/nginx
  92. fi
  93. echo "停止并移除容器"
  94. docker stop nginx
  95. docker rm nginx
  96. # 设置变量
  97. NGINX_CONF_DIR="/etc/nginx/conf.d"
  98. NGINX_CONF_FILE="example.conf"
  99. SERVER_NAME="example.com"
  100. UPSTREAM_NAME="app_server"
  101. UPSTREAM_SERVER="127.0.0.1:8000"
  102. # 创建配置文件
  103. echo "upstream ${UPSTREAM_NAME} {" > ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  104. echo " server ${UPSTREAM_SERVER};" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  105. echo "}" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  106. echo "server {" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  107. echo " listen 80;" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  108. echo " server_name ${SERVER_NAME};" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  109. echo " location / {" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  110. echo " proxy_pass http://${UPSTREAM_NAME};" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  111. echo " proxy_set_header Host \$host;" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  112. echo " proxy_set_header X-Real-IP \$remote_addr;" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  113. echo " }" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  114. echo "}" >> ${NGINX_CONF_DIR}/${NGINX_CONF_FILE}
  115. echo "重新启动nginx镜像重新新容器"
  116. docker run -p 80:80 --name nginx --restart=always
  117. -v /opt/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
  118. -v /opt/docker/nginx/conf.d:/etc/nginx/conf.d \
  119. -v /opt/docker/nginx/html:/usr/share/nginx/html \
  120. -v /opt/docker/nginx/logs:/var/log/nginx \
  121. -d nginx