首页后端开发JAVA交易所开发/(源码搭建)交易所系统开发/(逻辑成熟)(交易所源码 开源)

交易所开发/(源码搭建)交易所系统开发/(逻辑成熟)(交易所源码 开源)

时间2023-04-03 15:06:39发布访客分类JAVA浏览465
导读:应用场景在容器内部,想要获取容器名称,替换容器内某些文件内的字符串, 代码如下:# -*-coding:utf-8-*- import os import redis def alter(file, new_str, old_str="...

应用场景

在容器内部,想要获取容器名称,替换容器内某些文件内的字符串, 代码如下:

# -*-coding:utf-8-*-
import os
import redis


def alter(file, new_str, old_str="abc_123abc"):
    """
    替换文件中的字符串
    file:文件名
    old_str:就字符串
    new_str:新字符串
    """

    file_data = ""
    with open(file, "r") as f:
        for line in f:
            if old_str in line:
                line = line.replace(old_str, new_str)
            file_data += line
    with open(file, "w") as f:
        f.write(file_data)


def get_container_name():
    db = redis.Redis(host="192.168.0.111", port=6380, decode_responses=False)
   
    # start: in container, run next code  -------------------------------------------------
    cmd = "cat /proc/self/cgroup"
    output = os.popen(cmd)
    rests = output.readlines()
    container_message= rests[-1]
    if not container_message:
        container_id = "abc"
    else:
        container_id =  container_message.strip().split("docker-")[-1][:12]
    # end. ----------------------------------------------------------------------------------

    container_name = None
    if container_id != "abc":
        key_name = "nm_" + container_id
        container_name = db.hget("container_msg", key_name)

    if container_name:
        container_name = container_name.decode("utf-8")

    return container_name


def run():
    nginx_conf = "/etc/nginx/nginx.conf"
    galaxy_yml = "/galaxy-central/config/galaxy.yml"

    container_name = get_container_name()
    if container_name is not None:
        alter(nginx_conf, container_name)
        os.popen("nginx -s reload")
        # os.popen("cp /galaxy-central/config/galaxy.yml.sample /galaxy-central/config/galaxy.yml")
        alter(galaxy_yml, container_name)
        print("Replacement string 'abc_123abc' succeeded")
    else:
        print("Replacement string 'abc_123abc' failed")
        

if __name__ == '__main__':
    run()

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

php

若转载请注明出处: 交易所开发/(源码搭建)交易所系统开发/(逻辑成熟)(交易所源码 开源)
本文地址: https://pptw.com/jishu/802.html
如何使用nginx作为docker容器中ASP.NET应用的反向代理 java逻辑运算符(java逻辑运算符优先级)

游客 回复需填写必要信息