shell動態修改yml配置文件


環境准備

     使用python來對yml文件內容進行讀寫操作,然后在shell中調用python

     

    

 編寫python腳本

import yaml

with open("config/application.yml",'r') as f:
        result = f.read()
        x=yaml.load(result,Loader=yaml.FullLoader)
        print(x["spring"]["datasource"]["url"])

        x["spring"]["datasource"]["url"]="http://wwww.baidu.com"
        with open("config/application.yml",'w') as w_f:
                yaml.dump(x,w_f)
python2

    

 shell調用python並傳遞參數

#!/bin/bash


#change config
pname=`env | grep podname | cut -d"=" -f2 | cut -d"-" -f1`
podid=`env | grep podname | cut -d"=" -f2 | cut -d"-" -f2`
datasoureurl=`env | grep datasoureurl`
dburl=${datasoureurl#*=}
redishost=`env | grep redishost | cut -d"=" -f2`

#python /jlogstash/changeconfig.py jdbc://mysql@192.168.30.99
python /jlogstash/changeconfig.py $dburl


tail -f /dev/null

# start jlogstash
cd /jlogstash/
JAVA_OPTS="$JAVA_OPTS -Xmx3000m -Xms3000m -server"
JAVA_OPTS="$JAVA_OPTS -Xloggc:logs/jlogstash.gc"
JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=logs/heapdump.hprof"

#-XX:MaxDirectMemorySize=16M According to owner memory
JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps"

#java $JAVA_OPTS -jar lib/TLog-master.jar "$@"
java $JAVA_OPTS -jar lib/TLog_web-1.0.0.jar -name work$podid "$@"
啟動shell
import yaml
import sys

dburl=sys.argv[1]

with open("/jlogstash/config/application.yml",'r') as f:
        result = f.read()
        x=yaml.load(result,Loader=yaml.FullLoader)
        print(x["spring"]["datasource"]["url"])

        x["spring"]["datasource"]["url"]=dburl
        with open("/jlogstash/config/application.yml",'w') as w_f:
                yaml.dump(x,w_f)
python腳本

      

     

ConfigParser和Yaml的結合使用  

import ConfigParser
import yaml
import os


class MyConfigParser(ConfigParser.ConfigParser):
    """
    set ConfigParser options for case sensitive.
    """
    def __init__(self, defaults=None):
        ConfigParser.ConfigParser.__init__(self, defaults=defaults)
 
    def optionxform(self, optionstr):
        return optionstr


curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath,"config.ini")

conf = MyConfigParser()
conf.read(cfgpath)
sections = conf.sections()


app_paths=["gateway-zuul/conf/application-prod.yml"]
#app_paths=["admin/conf/application-prod.yml","gateway-zuul/conf/application-prod.yml","register-center/conf/application-prod.yml","sdc-collect-config/conf/application-prod.yml","sdc-es-service/conf/application-prod.yml","sdc-rule-config/conf/application-prod.yml","sdc-schedule/conf/application-prod.yml","sdc-web/conf/application-prod.yml","jlogstash/config/application.yml"]

#1.配置項目字符的大小寫
#2.配置值的數字和字符串類型
#3.配置值為0

for yml in app_paths:
    ymlfile = os.path.join(curpath,"app",yml)    
    with open(ymlfile,'r') as f:
        result = f.read()
        yamlcon = yaml.load(result,Loader=yaml.FullLoader)
        for section in sections:
            items = conf.items(section)
            for item in items:
                key=item[0]
                if yamlcon["spring"].get(section) and yamlcon["spring"].get(section).get(key) is not None:
                    try:
                        yamlcon["spring"][section][key]=conf.getint(section,key)
                    except:
                        yamlcon["spring"][section][key]=conf.get(section,key)
                    print("spring."+section+"."+key+" has changed in"+ymlfile)
                                elif yamlcon.get(section) and yamlcon.get(section).get(key) is not None:
                    try:
                        yamlcon[section][key]=conf.getint(section,key)
                    except:
                        yamlcon[section][key]=conf.get(section,key)
                    print(section+"."+key+" has changed"+ymlfile)
                else:
                    print("spring."+section+"."+key+" has no match keys in"+ymlfile)        
                    print(section+"."+key+" has no match keys. in"+ymlfile)
                print("********************************************************************************************************************************")
                with open(ymlfile,'w') as w_f:
                     yaml.dump(yamlcon,w_f)
代碼

shell的jq和yq工具使用

       jq是shell對json格式數據進行操作的工具

       yq是shell對yaml格式數據進行操作的工具

shell創建映射目錄

        把/根分區的目錄映射到其他分區(/home)

         

         

          

          /app/data01就可以把/home/taishidata/data02下的所有文件映射到自己的目錄下

shell讀取配置文件的值設置為執行變量

function __ReadINI()
{
    #讀取之前修改IP地址
    while read line
    do
        if [[ ! ${line} =~ ^[\[|#].* ]];then
            eval "${line}"
        fi
    done < $1
}
View Code
[INSTALL_CONFIG]
MODULES=java,system_tools_config,mysql,flink,elasticsearch,kafka,nginx,zookeeper,redis,app
#安裝根目錄
INSTALL_DIR=/home/admin/taishi
MODE_DIR=/home/admin
#運行維護賬號,該賬號默認會加入到wheel sudo組內
USER=admin
DATA_DIR=/home/admin/taishidata
IP=192.168.19.201
Time_Server=192.168.19.201
配置文件
source ./utils.sh
__ReadINI ../conf/install_config.ini
echo ${IP}
引入變量

 

shell實現並發執行

       Shell不支持多線程,因而只能采用多進程的方式.具體的實現方法很簡單,就是在要並發執行的命令后面加上“&”,將其轉入后台執行,這樣就可以在執行完一條命令之后,不必等待其執行結束,就立即轉去執行下一條命令

        

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM