最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【基本解决】Makefile中从独立文件比如json中读取配置变量

makefile crifan 4591浏览 0评论

自己弄的Gitbook的发布脚本中,用到了rsync时借助sshpass去传递密码

之前做法是用f=file,一个txt文件去传递

属于deploy的其中一个配置

其他还有用户名,ip等配置:

################################################################################

# Upload to server

################################################################################

PASSWORD_FILE=../sshpass_password.txt

REMOTE_USER=root

# REMOTE_SERVER=80.85.87.205

# REMOTE_SERVER=150.109.113.228

REMOTE_SERVER=149.28.136.189

REMOTE_BOOKS_PATH=/data/wwwroot/book.crifan.com/books

# REMOTE_PATH=$(REMOTE_BOOKS_PATH)/$(BOOK_NAME)

REMOTE_PATH=$(REMOTE_BOOKS_PATH)

## Upload all genereted website/pdf/epub/mobi files to remote server using rsync. Create sshpass_password.txt file to contain password before use this

upload: all

    @echo ================================================================================

ifeq ($(SHOULD_IGNORE), true)

    @echo Ignore upload $(BOOK_NAME) to book.crifan.com

else

    @echo Upload for $(BOOK_NAME)

    sshpass -f $(PASSWORD_FILE) rsync -avzh –progress –stats –delete –force $(OUTPUT_PATH) $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH)

endif

现有外部独立文件:

sshpass_password.txt

存储密码:

现在希望:

把外部密码文件去掉,新增一个json配置文件,加上其他参数:

  • deploy_dest_server_user:root

  • deploy_dest_server_ip:149.28.136.189

  • deploy_dest_server_path:/data/wwwroot/book.crifan.com/books

都放到json中

然后makefile中去调用

现在需要去高清楚,如何从makefile中读取和解析json参数

实在要是不行,就再去借助于python脚本实现?

makefile load json

makefile – Read JSON and assign to a list of make variables – Stack Overflow

怎么会有个node?

python – Using variables in a makefile – Stack Overflow

Make a makefile for cjson code with -lcjson for json parser in c – Stack Overflow

shell – Makefile to generate JSON from Python scripts in separate directories – Stack Overflow

How to read from our JSON config in the Makefile? · Issue #7 · commonsearch/cosr-ops

COSR_AWS_STACKNAME := $(shell python config.py AWS_STACKNAME)
COSR_AWS_REGION := $(shell python config.py AWS_REGION)

有机会试试

cosr-ops/Makefile at master · commonsearch/cosr-ops

makefile read json

python aws/config.py AWS_STACKNAME

这句本身 难道是 从py中读取内容 赋值给AWS_STACKNAME,而AWS_STACKNAME是环境变量?

makefile variable from json

bash – Exporting JSON to environment variables – Stack Overflow

for s in $(echo $values | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ); do
    export $s
done

抽空试试

Makefile variable not set from grep output – Stack Overflow

COGLINE := $(shell grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:)

all:
        COGLINE=$$(grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:); \
        echo "$${COGLINE}"

environment variables – Makefile include env file – Unix & Linux Stack Exchange

导入另外一个文件

后缀设置为.env(实际上也是makefile(的普通文本)文件?)

include .env
export

抽空试试

【总结】

最后直接include另外一个makefile文件,即可实现导入其中的变量配置了

DeployServerInfo.mk

DEPLOY_SERVER_USER=root
DEPLOY_SERVER_IP=149.28.136.189
DEPLOY_SERVER_PATH=/data/wwwroot/book.crifan.com/books

GitbookCommon.mk

include DeployServerInfo.mk

.PHONY : debug_inlcude

## Debug include file

debug_include:

  @echo DEPLOY_SERVER_USER=$(DEPLOY_SERVER_USER)

  @echo DEPLOY_SERVER_IP=$(DEPLOY_SERVER_IP)

  @echo DEPLOY_SERVER_PATH=$(DEPLOY_SERVER_PATH)

然后就可以打印出变量了:

<p>➜  xxx git:(master) make debug_include</p><p>...</p>
DEPLOY_SERVER_USER=root
DEPLOY_SERVER_IP=149.28.136.189
DEPLOY_SERVER_PATH=/data/wwwroot/book.crifan.com/books

不过,在把密码加过来后,还出错了:

【已解决】mac中用sshpass通过-p传递密码包含特殊字符导致rsync报错:/bin/sh: -c: line 0: syntax error near unexpected token `)’

然后再去:

【基本解决】Makefile中从独立文件比如json中读取配置变量

【总结】

至此,算是基本解决问题了:

单独弄了个配置文件:

DeployServerInfo.mk

DEPLOY_SERVER_IP=149.28.136.189
DEPLOY_SERVER_USER=root
DEPLOY_SERVER_PASSWORD=your_password
DEPLOY_SERVER_PATH=/data/wwwroot/book.crifan.com/books

然后如果是密码包含特殊字符,比如

问号?

井号#

右括号)

等等,都要加上反斜杠去转义才可以:

DEPLOY_SERVER_PASSWORD=xxx\)xxx\#\?xxx

然后主makefile

GitbookCommon.mk

-include ../DeployServerInfo.mk
# sinclude ../DeployServerInfo.mk

## Upload all genereted website/pdf/epub/mobi files to remote server using rsync. Create DeployServerInfo.mk which contain deploy server IP+User+Password+Path before use this

upload: all

  @echo ================================================================================

ifeq ($(SHOULD_IGNORE), true)

  @echo Ignore upload $(BOOK_NAME) to book.crifan.com

else

  @echo Upload for $(BOOK_NAME)

  sshpass -p $(DEPLOY_SERVER_PASSWORD) rsync -avzh –progress –stats –delete –force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH)

endif

就可以支持通过配置文件传入相关deploy的参数了。

注:但是不方便用json保存配置信息,所以选用了另外的makefile。

转载请注明:在路上 » 【基本解决】Makefile中从独立文件比如json中读取配置变量

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.187 seconds, using 22.09MB memory