我正试图使用yq
(https://github.com/mikefarah/yq)使用asciidoc自动生成文档。
我的docker-compose.yml看起来相当简单,没有做任何不同寻常的事情:
---
version: "3.3"
services:
# prometheus metrics
node_exporter:
image: prom/node-exporter:latest
container_name: node_exporter
labels:
description: Prometheus exporter to monitor system metrics
restart: always
command:
- --path.rootfs=/host
pid: host
network_mode: host
# ports:
# - 9100:9100
# The network_mode: host tells docker to run the container as if it was running on the
# server itself, so all ports exports by the container will directly be mapped to the server.
volumes:
- /:/host:ro,rslave
- /etc/timezone:/etc/timezone:ro
# prometheus metrics
cadvisor:
image: gcr.io/cadvisor/cadvisor:latest
container_name: cadvisor
restart: always
expose:
- 9110
ports:
- 9110:8080
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
# Mange containers
portainer:
image: portainer/portainer-ce:alpine
container_name: portainer
command: -H unix:///var/run/docker.sock --admin-password-file /tmp/portainer_passwords
restart: always
ports:
- 9990:9000
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
- ./assets/portainer.passwd:/tmp/portainer_passwords
- /etc/timezone:/etc/timezone:ro
volumes:
portainer_data:
我想要每项服务的一些信息。对我来说最重要的是形象。容器名称重新启动和端口。加上可能的标签->描述,这是我用于某些文档的字段(各个服务实际做什么(。
我不知道如何将各个服务的这些字段组合起来。当我运行yq eval '.services.[] | .container_name, .services.[] | .image' $composeFile
时,我首先得到3行容器名称,然后得到3行图像。
node_exporter
cadvisor
portainer
prom/node-exporter:latest
gcr.io/cadvisor/cadvisor:latest
portainer/portainer-ce:alpine
此结果未按服务分组。我更喜欢这样的东西:
node_exporter
prom/node-exporter:latest
cadvisor
gcr.io/cadvisor/cadvisor:latest
portainer
portainer/portainer-ce:alpine
或者,既然我想生成asciidoc,那么完美的解决方案就是:
|node_exporter |prom/node-exporter:latest
|cadvisor |gcr.io/cadvisor/cadvisor:latest
|portainer |portainer/portainer-ce:alpine
通过这种方式,我可以生成一个ASCII表的主体,其中包含有关我的文档服务的信息。
有人知道我如何让yq
在我结束时工作吗?
每个.services.[]
开始一个新的迭代。只做一次,然后从中提取所有您需要的内容:
yq eval '.services.[] | "|" + .container_name + "| " + .image'
|node_exporter| prom/node-exporter:latest
|cadvisor| gcr.io/cadvisor/cadvisor:latest
|portainer| portainer/portainer-ce:alpine