发版应用机器多了,就偷个懒,简单做了个脚本通过docker实现同时发版


我准备一台服务器作为ansible控制台,将所有项目(下文脚本项目仅举例列举)所需的环境包准备好,通过脚本一键实现同一个项目所有服务器同时发版更新


主脚本如下:

#!/bin/bash

#Great by fanwanli04
function menu() {
    cat <<END
    1、casxx
    2、txtissap
    3、issap
    4、xxxap
END
}
function chose() {
echo "please input the project name"
read name
case "$name" in
    casxx | txtissap | issap)
        cd project/"$name"
        rm -rf tomcat.tar.gz
        tar -zcvf tomcat.tar.gz tomcat
        oldver=$(cat /opt/project/"$name"/id.id)
        echo "old version is $oldver"
        echo "please input a version"
        read answer
        docker build -t registry.xxx.xxx.com/"$name":"$answer" -f /opt/project/"$name"/Dockerfile /opt/project/"$name"
        docker push registry.xxx.xxx.com/"$name":"$answer"
        echo "$answer" > /opt/project/"$name"/id.id
        ;;
    xxxap)
        cd project/"$name"                \\这个是如果有项目特殊不一样的时候单独设置,这里都是复制上面的,仅示例而已
                rm -rf tomcat.tar.gz
                tar -zcvf tomcat.tar.gz tomcat
                echo "please input a version"
                read answer
               docker build -t registry.xxx.xxx.com/"$name":"$answer" -f /opt/project/"$name"/Dockerfile /opt/project/"$name"
               docker push registry.xxx.xxx.com/"$name":"$answer"
               ;;
    *)
        echo "this project name is wrong"
        exit;
esac
}
function implement() {
read -p "Which project to implement?" okanswer
/usr/bin/ansible "$okanswer" -a "docker-compose down"
/usr/bin/ansible "$okanswer" -a "docker rmi registry.xxx.xxx.com/$okanswer:$oldver"
/usr/bin/ansible "$okanswer" -a "docker pull registry.xxx.xxx.com/$okanswer:$answer"
sed -i "s/$okanswer:$oldver/$okanswer:$answer/g" /opt/project/"$okanswer"/docker-compose.yml
/usr/bin/ansible "$okanswer" -m copy -a "src=/opt/project/$okanswer/docker-compose.yml dest=/root/"
/usr/bin/ansible "$okanswer" -a "docker-compose up -d"
}
function main(){
    menu
    chose
    menu
    implement
}
main


在每个项目的目录中都有一个Dockerfile,示例如下

FROM registry.xxx.xxx.com/docker_centos7
LABEL "system"="casxx" "app"="AP"
MAINTAINER xxx <xxx@xxx.com>
USER root
ADD java.tar.gz /usr/local/
ADD tomcat.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/java/
ENV JAVA_BIN=/usr/local/java/bin
ENV CLASSPATH=/usr/local/java/lib
ENV PATH=$PATH:$JAVA_HOME/bin
VOLUME ["/usr/local/tomcat/logs/"]
CMD /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out
EXPOSE 8080


以及一个docker-compose.yml,示例如下


version: '2'
services:
  casxx:
    image: registry.xxx.xxx.com/casxx:1.1
    ports:
      - "8080:8080"
    volumes:
      - /home/logs/tomcat/:/usr/local/tomcat/logs/
    dns:
      - "10.100.31.1"


并包含 Dockerfile 所需要的tomcat java等包


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐