我试图在程序中读取一个docker文件作为模板,并在发生新情况时替换细节。但是当我使用dump
函数时,它失败了。
我的代码就像:
let yaml = require('js-yaml');
let filePath = **/docker-compos-template.yaml';
let savePath = '**/docker-compose-save.yaml';
try{
let doc = yaml.safeLoad(fs.readFileSync(filePath),'utf8');
console.log(doc);
fs.writeFileSync(savePath, yaml.dump(doc), 'utf8');
}catch(e) {
console.log(e);
}
我的模板文件的一部分是:
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-santai
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default
- FABRIC_CA_SERVER_TLS_ENABLED=true
- FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.santai.chembean.io-cert.pem
,翻译后的文件是:
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-santai
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default
- FABRIC_CA_SERVER_TLS_ENABLED=true
- >-
FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.santai.chembean.io-cert.pem
符号>-
未在模板中定义。
然后将在翻译文件中为''
。
e.g我的模板文件是:
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb-santai:5984
- CORE_PEER_ID=peer0.santai.chembean.io
- CORE_PEER_NETWORKID=chembean
- CORE_PEER_ADDRESS=peer0.santai.chembean.io:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.santai.chembean.io:7051
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_PEER_CHAINCODEADDRESS=peer0.santai.chembean.io:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_CHAINCODE_STARTUPTIMEOUT=1200s
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.santai.chembean.io:7051
- CORE_PEER_LOCALMSPID=SantaiMSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default
- CORE_LOGGING_LEVEL=INFO
#- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
但翻译文件是:
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- 'CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb-santai:5984'
- CORE_PEER_ID=peer0.santai.chembean.io
- CORE_PEER_NETWORKID=chembean
- 'CORE_PEER_ADDRESS=peer0.santai.chembean.io:7051'
- 'CORE_PEER_GOSSIP_BOOTSTRAP=peer0.santai.chembean.io:7051'
- CORE_PEER_ADDRESSAUTODETECT=true
- 'CORE_PEER_CHAINCODEADDRESS=peer0.santai.chembean.io:7052'
- 'CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052'
- CORE_CHAINCODE_STARTUPTIMEOUT=1200s
- 'CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.santai.chembean.io:7051'
- CORE_PEER_LOCALMSPID=SantaiMSP
- 'CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock'
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=chembean_default
- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
似乎符号''
,随机出现。
有没有什么好主意,帮我解决这些问题呢?
答案 0 :(得分:1)
解析和转储yaml文件时可能会出现问题。如果您可以使用replace-in-files替换yaml文件中的某些字符串。只需将<>放在需要替换文本的位置并相应地放置它们。