首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ruamel.yaml更新包含多个yaml的yaml文件中的yaml块

使用ruamel.yaml更新包含多个yaml的yaml文件中的yaml块
EN

Stack Overflow用户
提问于 2018-06-18 20:36:26
回答 1查看 1.9K关注 0票数 2

我有一个yaml文件,其中包含几个yaml块。这个文件可以看到这里

基本上,我试图更新文件的最后一个yaml块中的一个图像键(ctrl+f blockfreight/go-bftx:)的值。但是,除了我正在更新的一个值之外,我希望保留文件的所有内容(包括注释)。

我有以下代码:

代码语言:javascript
运行
复制
"""
Retrieves a truncated version of the latest git commit sha and updates 
the go-bftx container image tag in app.yaml
"""

import sys
import ruamel.yaml
from subprocess import check_output

yaml_path = 'app.yaml'
for yaml_block in ruamel.yaml.round_trip_load_all(stream=open(yaml_path)):
    pass

# parse the most recent git commit sha from command line
docker_image = 'blockfreight/go-bftx:ci-cd-' + check_output('git log -1 -- pretty=format:%h'.split()).decode()

# update go-bftx image with most recent git-commit-sha tag in the StatefulSet block
yaml_block['spec']['template']['spec']['containers'][1]['image'] = docker_image

ruamel.yaml.round_trip_dump(yaml_block, sys.stdout)

这成功地编辑了值,保持了注释的完整,但只将最后的yaml块发送到stdout。

有什么方法可以让我只编辑app.yaml中的第六个(也是最后一个) yaml块,并保持文件的其余部分完整(包括注释)?

我尝试用一个if语句替换上面代码中的pass,该语句将前五个yaml发送到stdout,然后编辑了第六个值中的值,并将其发送到stdout。我的想法是使用bash将所有的stdout发送到一个文件(例如python app_modifier.py > app1.yaml),但这只发送来自第六个yaml的输出。

该代码如下所示:

代码语言:javascript
运行
复制
for i, yaml_block in enumerate(ruamel.yaml.round_trip_load_all(stream=open(yaml_path))):
    if i != 5:
        ruamel.yaml.round_trip_dump(yaml_block, sys.stdout)
    else:
        # parse the most recent git commit sha from command line
        docker_image = 'blockfreight/go-bftx:ci-cd-' + check_output('git log -1 --pretty=format:%h'.split()).decode()

        # update go-bftx image with most recent git-commit-sha tag in the StatefulSet blocks
        yaml_block['spec']['template']['spec']['containers'][1]['image'] = docker_image

        ruamel.yaml.round_trip_dump(yaml_block, sys.stdout)

如能提供任何帮助,将不胜感激!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-23 13:02:33

您的文件由多个YAML文档组成,这就是使用round_trip_load_all读取的内容,它为您提供了一个生成器。

如果使用round_trip_dump()将其写回,则永远不会获得原始文件中的--- YAML文档分隔符。

您可能可以使用生成器和round_trip_dump_all,因为您知道要在哪个文档中进行更改,但可能更容易生成round_trip_load_all生成的内容并对其进行处理。我还会使用ruamel.yaml的新API来做到这一点:

代码语言:javascript
运行
复制
import sys
import ruamel.yaml

yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = list(yaml.load_all(open('app.yaml')))

# parse the most recent git commit sha from command line
# docker_image = 'blockfreight/go-bftx:ci-cd-' + check_output('git log -1 -- pretty=format:%h'.split()).decode()
docker_image = 'blockfreight/go-bftx:ci-cd-' + 'check_output_output'

# update go-bftx image with most recent git-commit-sha tag in the StatefulSet block
data[-1]['spec']['template']['spec']['containers'][1]['image'] = docker_image

with open('out.yaml', 'w') as ofp:
    yaml.dump_all(data, ofp)

上面的主要是工作。out.yaml的差异在于您想要进行的更改,以及一些空白标准([ "之间的缩进),这当然是一次性的。

这种方法有两个主要问题:

  • 文件中的第一个YAML文档没有内容,因此它的注释不能从任何映射/序列中挂起,也不能保存。
  • 第二个YAML文档有一个最后的注释( # // Initializes BFTX Service to interact with endpoints )被删除。这很可能是ruamel.yaml中的一个错误,它只出现在多文档文件中,而我必须对此进行调查。

要解决第一个问题,您只能在不使用ruamel.yaml的情况下将该文档作为普通行读取。当你这样做的时候,你可以做其他的事情,因为这也解决了第二个问题(也就是规避):

代码语言:javascript
运行
复制
import sys
import ruamel.yaml

with open('out.yaml', 'w') as ofp:
    lines = ''
    with open('app.yaml') as ifp:
        for line in ifp:
            lines += line
            if line == '---\n':
                ofp.write(lines)
                lines = ''
    # process lines from the last document
    # print(lines)
    yaml = ruamel.yaml.YAML()
    yaml.preserve_quotes = True
    data = yaml.load(lines)

    # parse the most recent git commit sha from command line
    # docker_image = 'blockfreight/go-bftx:ci-cd-' + check_output('git log -1 -- pretty=format:%h'.split()).decode()
    docker_image = 'blockfreight/go-bftx:ci-cd-' + 'check_output_output'

    # update go-bftx image with most recent git-commit-sha tag in the StatefulSet block
    data['spec']['template']['spec']['containers'][1]['image'] = docker_image

    yaml.dump(data, ofp)

除非您关心流样式序列中的前导尾随空间,或者使用不一致的缩进保留这三个位置,否则这应该是您想做的事情。diff -u app.yaml out.yaml输出

代码语言:javascript
运行
复制
--- app.yaml    2018-06-23 14:41:02.256290577 +0200
+++ out.yaml    2018-06-23 14:58:09.933991459 +0200
@@ -143,7 +143,7 @@
 spec:
   selector:
     matchLabels:
-       app: bftx
+      app: bftx
   serviceName: blockfreight
   replicas: 1
   template:
@@ -151,7 +151,7 @@
       labels:
         app: bftx
     spec:
-     containers:
+      containers:
       - name: tm
         imagePullPolicy: IfNotPresent
         image: tendermint/tendermint:0.20.0
@@ -199,7 +199,7 @@
           tendermint node --moniker="`hostname`" --p2p.seeds="aeabbf6b891435013f2a800fa9e22a1451ca90fd@bftx0.blockfreight.net:8888,6e9515c2cfed19464e6ce11ba2297ecdb411103b@bftx1.blockfreight.net:8888,b8b988370783bd0e58bf926d621a47160af2bdae@bftx2.blockfreight.net:8888,8c091f4e3dc4ac27db1efd38beee012d99967fd8@bftx3.blockfreight.net:8888" --proxy_app="tcp://localhost:46658" --consensus.create_empty_blocks=false
       - name: app
         imagePullPolicy: Always
-        image: blockfreight/go-bftx:rc1
+        image: blockfreight/go-bftx:ci-cd-check_output_output
         ports:
         - containerPort: 12345
         - containerPort: 46658
@@ -247,7 +247,7 @@
         - mountPath: /etc/nginx/conf.d/pub_key.conf
           name: tmconfigdir
           subPath: pub_key_nginx.conf
-     volumes:
+      volumes:
       - name: tmconfigdir
         configMap:
           name: bftx-config
@@ -262,7 +262,7 @@
       annotations:
         volume.alpha.kubernetes.io/storage-class: anything
     spec:
-      accessModes: [ "ReadWriteOnce" ]
+      accessModes: ["ReadWriteOnce"]
       resources:
         requests:
           storage: 2Gi
@@ -271,7 +271,7 @@
       annotations:
         volume.alpha.kubernetes.io/storage-class: anything
     spec:
-      accessModes: [ "ReadWriteOnce" ]
+      accessModes: ["ReadWriteOnce"]
       resources:
         requests:
           storage: 12Mi
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50917103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档