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不支持多线程,因而只能采用多进程的方式.具体的实现方法很简单,就是在要并发执行的命令后面加上“&”,将其转入后台执行,这样就可以在执行完一条命令之后,不必等待其执行结束,就立即转去执行下一条命令

        

     

posted @ 2021-03-26 15:53  不懂123  阅读(5854)  评论(0编辑  收藏  举报