shell動態修改yml配置例項

不懂123發表於2024-11-15

yq安裝

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

每個yq版本支援的選項引數都不一樣

修改Dockerfile

shell動態修改yml配置例項
[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"]

shell動態修改yml配置例項
#!/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配置檔案裡面的配置專案

相關文章