shell动态修改yml配置实例

yq安装

      https://github.com/mikefarah/yq/tree/master

       

      每个yq版本支持的选项参数都不一样

     

 修改Dockerfile

[root@instance base-tlog-test]# vi Dockerfile 

FROM 192.168.30.113/library/java:latest
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone
VOLUME /tmp
ADD license.json license.json
ADD ca.crt ca.crt
ADD tlog tlog
ADD yq /usr/bin/yq
WORKDIR /tlog
ADD start_tlog.sh /tlog/start_tlog.sh

ENTRYPOINT ["sh","/tlog/start_tlog.sh"]
View Code

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","./lib/TLog.jar","/license.json"]

#!/bin/sh
configpath="./config/application.yml"

datasoureurl=`env | grep spring.datasource.url`
dburl=${datasoureurl#*=}
datausername=`env | grep spring.datasource.username | cut -d"=" -f2`
datapassword=`env | grep spring.datasource.password | cut -d"=" -f2`
redisdatabase=`env | grep  spring.redis.database | cut -d"=" -f2`
redishost=`env | grep spring.redis.host | cut -d"=" -f2`
redisport=`env | grep spring.redis.port | cut -d"=" -f2`
redispassword=`env | grep spring.redis.password | cut -d"=" -f2`
promhost=`env | grep Prometheus.host | cut -d"=" -f2`
promport=`env | grep Prometheus.port | cut -d"=" -f2`
tlogport=`env | grep server.port | cut -d"=" -f2`

[ ${dburl} ] && dburl=${dburl} yq  -i '.spring.datasource.url=strenv(dburl)' ${configpath}
[ ${datausername} ] && datausername=${datausername} yq  -i '.spring.datasource.username=strenv(datausername)' ${configpath}
[ ${datapassword} ] && datapassword=${datapassword} yq -i '.spring.datasource.password=strenv(datapassword)' ${configpath}
[ ${redisdatabase} ] && redisdatabase=${redisdatabase} yq  -i '.spring.redis.database=strenv(redisdatabase)' ${configpath}
[ ${redishost} ] && redishost=${redishost} yq -i '.spring.redis.host=strenv(redishost)' ${configpath}
[ ${redisport} ] && redisport=${redisport} yq  -i '.spring.redis.port=strenv(redisport)' ${configpath}
[ ${redispassword} ] && redispassword=${redispassword}  yq -i '.spring.redis.password=strenv(redispassword)' ${configpath}
[ ${promhost} ] && promhost=${promhost} yq  -i '.Prometheus.host=strenv(promhost)' ${configpath}
[ ${promport} ] && promport=${promport} yq -i '.Prometheus.port=strenv(promport)' ${configpath}
[ ${tlogport} ] && tlogport=${tlogport} yq -i '.server.port=strenv(tlogport)' ${configpath}

java -Djava.security.egd=file:/dev/./urandom -jar ./lib/TLog.jar /license.json
start_tlog.sh

 

设置环境变量

         

      

       通过jq这个shell工具可以动态修改yml配置文件里面的配置项目 

 Dockerfile拷贝并解压

         ADD test1.jar /usr   直接把test1.jar 解压到/usr目录下

         

       

        无需要再使用其它命令进行操作

posted @ 2024-11-15 21:37  不懂123  阅读(2)  评论(0编辑  收藏  举报