前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >溯源‘NFT链游系统开发’源码技术搭建

溯源‘NFT链游系统开发’源码技术搭建

原创
作者头像
系统_I8O28578624
发布2023-03-24 11:04:47
2970
发布2023-03-24 11:04:47
举报
文章被收录于专栏:YYDSYYDS

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

应用场景

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

代码语言:javascript
复制
# -*-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()

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 应用场景
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档