繁体   English   中英

更改 docker-compose.yml 以使用 python 添加端口

[英]Alter docker-compose.yml to add ports with python

def make_file():
    
    read_file = r'C:\Users\~\Desktop\sample.yml'
    write_file = r'C:\Users\~\Desktop\sample_out.yml'    
    
    f = open(read_file, 'r')
    lst = [line for line in f]            
    f.close()

    ports = 'ports:\n'
    for index in range(len(lst)):
        if "system_frontend:" in lst[index]:            
            count_spaces = len(lst[index+1]) - len(lst[index+1].lstrip(' '))
            
            lst.insert(index+1, ports)
            lst[index+1] = lst[index+1].rjust(count_spaces)            
            # lst.insert(index+1, " "*count_spaces + ports + " "*count_spaces + "- \"0.0.0.0:5000:80\"\n") ## Did it this way, but it's not the best option.

    with open(write_file, 'w', encoding='utf-8') as file:
        for line in lst:
            file.write(line) 

任务是在需要的行中放置一定数量的空格(在 str 'ports:' 之前,所以它看起来像 'ports:')。

来自 .yml 的片段

services:
  system_frontend:
    image: ${DOCKER_REGISTRY}/frontend:${CONTAINER_VERSION}
    logging: *id001
    environment:
      SSL_CERTIFICATE: ${SSL_CERTIFICATE:-}
      SSL_CERTIFICATE_KEY: ${SSL_CERTIFICATE_KEY:-}
      ENABLE_CORS: ${ENABLE_CORS:-}
      FRONTEND_URL: ${FRONTEND_URL:-}
      METRICS_PUSHGATEWAY: ${METRICS_PUSHGATEWAY:-}
    volumes:
      - ssl-volume:/etc/nginx/ssl
    networks:
      system_network: null
    restart: unless-stopped
    depends_on:
      - webapi

不能成功。

我猜您正在尝试更改docker-compose.yaml以添加端口。 有 2 个选项,您可以通过处理文本来做这件事,或者使用yaml.safe_load来处理内容

这是我对如何做到这一点的看法。 试一试,看看它是否符合您的需求

import yaml


# manually processing text file
def update_port_value_manual(read_file: str, write_file: str, ports: list):
    f = open(read_file, 'r')
    lst = [line for line in f]
    f.close()

    for index in range(len(lst)):
        if "system_frontend:" in lst[index]:
            count_spaces = len(lst[index + 1]) - len(lst[index + 1].lstrip(" "))
            inserted_content = count_spaces * " " + "ports:\n"
            inserted_content += "".join([f"{' ' * count_spaces}- {host_port}:{container_port}\n"
                                         for host_port, container_port in ports])
            lst.insert(index + 1, inserted_content)
            break

    with open(write_file, 'w', encoding='utf-8') as file:
        for line in lst:
            file.write(line)


# use yaml for easier processing
def update_port_value_yaml(read_file: str, write_file: str, ports: list):
    with open(read_file, "r") as read_yml:
        content = yaml.safe_load(read_yml)

    content.get("services").get("system_frontend").update(ports=[f"{host_port}:{container_port}"
                                                                 for host_port, container_port in ports])

    with open(write_file, "w") as write_yml:
        yaml.dump(content, write_yml)


def main():
    read_file = "sample.yml"
    write_file = "sample_out.yaml"

    ports = [(8081, 8081), (8080, 8080)]

    update_port_value_manual(read_file, write_file, ports)
    # update_port_value_yaml(read_file, write_file, ports)

我认为这就是您要实现的目标。 您的代码与此代码之间的主要区别是我添加了一个 break 语句,因此在找到目标行后循环不会继续执行操作。

def make_file():

    read_file = r'sample.yml'
    write_file = r'sample_out.yml'
    f = open(read_file, 'r')
    lst = [line for line in f]
    f.close()
    ports = 'ports:\n'
    spaces = 0
    for index in range(len(lst)):
        if "system_frontend:" in lst[index]:
            spaces += lst[index+1].count(" ") - 1
            break
    lst.insert(index + 1, (" "* spaces) + ports)
    with open(write_file, 'w', encoding='utf-8') as file:
        for line in lst:
            file.write(line)
make_file()

请注意,有更好的方法来处理 yaml 文件。 pyyaml中查看 pyyaml。

output

services:
  system_frontend:
    ports:
    image: ${DOCKER_REGISTRY}/frontend:${CONTAINER_VERSION}
    logging: *id001
    environment:
      SSL_CERTIFICATE: ${SSL_CERTIFICATE:-}
      SSL_CERTIFICATE_KEY: ${SSL_CERTIFICATE_KEY:-}
      ENABLE_CORS: ${ENABLE_CORS:-}
      FRONTEND_URL: ${FRONTEND_URL:-}
      METRICS_PUSHGATEWAY: ${METRICS_PUSHGATEWAY:-}
    volumes:
      - ssl-volume:/etc/nginx/ssl
    networks:
      bimeister_network: null
    restart: unless-stopped
    depends_on:
      - webapi

您在循环中插入一行,您应该保留原始行并使用另一个列表来存储输出,这就是我要做的:

def make_file():
    read_file = r'sample.yml.txt'
    write_file = r'output.yml.txt'    
    
    ports = 'ports:\n'

    with open(read_file, 'r', encoding='UTF-8') as f, open(write_file, 'w', encoding='utf-8') as o:
        while (line := f.readline()):

            o.write( line )

            if "system_frontend:" in line:
                next_line = f.readline()
                count_spaces = next_line.count(" ") - 1
                next_line = next_line.rjust(count_spaces)
                
                o.write(ports)
                o.write(next_line)

make_file()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM