shell动态修改yml配置⽂件
环境准备
使⽤python来对yml⽂件内容进⾏读写操作,然后在shell中调⽤python
编写python脚本
import yaml
with open("l",'r') as f:
result = f.read()
x=yaml.load(result,Loader=yaml.FullLoader)
print(x["spring"]["datasource"]["url"])
x["spring"]["datasource"]["url"]="wwww.baidu"
with open("l",'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:"
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 -ding=UTF-8 -sys=true -XX:+PrintGCDetails -#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/l",'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/l",'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(alpath(__file__))
cfgpath = os.path.join(curpath,"config.ini")
conf = MyConfigParser()
sections = conf.sections()
app_paths=["gateway-zuul/l"]
#app_paths=["admin/l","gateway-zuul/l","register-center/l","sdc-collect-config/l","sdc-es-service/l","sdc-rule-config/conf/app #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()
shell代码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]=int(section,key)
except:
yamlcon["spring"][section][key]=(section,key)
print("spring."+section+"."+key+" has changed in"+ymlfile)
(section) (section).get(key) is not None:
try:
yamlcon[section][key]=int(section,key)
except:
yamlcon[section][key]=(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不⽀持多线程,因⽽只能采⽤多进程的⽅式.具体的实现⽅法很简单,就是在要并发执⾏的命令后
⾯加上“&”,将其转⼊后台执⾏,这样就可以在执⾏完⼀条命令之后,不必等待其执⾏结束,就⽴即转去执⾏下⼀条命令
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论