NFT链游项目系统开发模式方案丨NFT链游DAPP合约系统开发技术讲解
在内部无法获取容器名称时,如何操作? 当创建容器时,我们可以通过记录容器名称与ID到中。 在容器内部,通过 cat /proc/self/cgroup 获取容器ID。应用场景在容器内部,想要获取容器名称,替换容器内某些文件内的字符串, 代码如下:#-*-coding:utf-8-*-importosimportredisdefalter(file,new_str,old_str="abc_123abc"):"""替换文件中的字符串file:文件名old_str:就字符串new_str:新字符串"""file_data=""withopen(file,"r")asf:forlineinf:ifold_strinline:line=line.replace(old_str,new_str)file_data+=linewithopen(file,"w")asf:f.write(file_data)defget_container_name():db=redis.Redis(host="192.168.0.111",port=6380,decode_responses=False)#start:incontainer,runnextcode————————————————-cmd="cat/proc/self/cgroup"output=os.popen(cmd)rests=output.readlines()container_message=rests[-1]ifnotcontainer_message:container_id="abc"else:container_id=container_message.strip().split("docker-")[-1][:12]#end.———————————————————————————-container_name=Noneifcontainer_id!="abc":key_name="nm_"+container_idcontainer_name=db.hget("container_msg",key_name)ifcontainer_name:container_name=container_name.decode("utf-8")returncontainer_namedefrun():nginx_conf="/etc/nginx/nginx.conf"galaxy_yml="/galaxy-central/config/galaxy.yml"container_name=get_container_name()ifcontainer_nameisnotNone:alter(nginx_conf,container_name)os.popen("nginx-sreload")#os.popen("cp/galaxy-central/config/galaxy.yml.sample/galaxy-central/config/galaxy.yml")alter(galaxy_yml,container_name)print("Replacementstring'abc_123abc'succeeded")else:print("Replacementstring'abc_123abc'failed")if__name__=='__main__':run()
发表回复