ITPub博客

首页 > 区块链 > 区块链 > NFT链游项目系统开发模式方案丨NFT链游DAPP合约系统开发技术讲解

NFT链游项目系统开发模式方案丨NFT链游DAPP合约系统开发技术讲解

原创 区块链 作者:Tg_StPv888 时间:2023-03-24 11:42:12 0 删除 编辑

在 容器内部无法获取容器名称时,如何操作? 当创建容器时,我们可以通过记录容器名称与ID到 redis中。 在容器内部,通过 cat /proc/self/cgroup 获取容器ID。

应用场景

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

# -*-coding:utf-8-*-import osimport 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()


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70016646/viewspace-2941421/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
专业区块项目开发团队:18028578624微電

注册时间:2022-04-13

  • 博文量
    158
  • 访问量
    95686