Baker是一种基于文件的分散配置管理
bakercm的Python项目详细描述
bakercm是一种基于文件的分散式配置管理。bakercm是一个开箱即用的工具,可以使用配方文件自动配置环境。
我为什么要用这个?
bakercm是一种配置管理,不需要集中式服务器来配置环境。bakercm是基于python(版本3)构建的轻量级工具,它从模板配置文件。
其次,bakercm可以使用配方文件中的秘密部分对值进行加密和解密。bakercm关心在正确的环境中解密值的安全性,因此您的配置文件可以与源代码一起公开存在,并且您的机密值仍然是安全的。
最后,可以从最常用的版本控制服务器(如github、bitbucket或其他文件服务器)存储和下载菜谱文件,其中可以对配置文件进行版本控制,bakercm会注意使用正确的版本设置您的环境。
功能
- 在每个环境的模板文件中配置动态值
- 加密和解密值以保证其安全
- 在文件系统中移动或复制文件
- 更改权限、所有者和文件组
- 从最常用的版本控制服务器管理配方文件的版本
- 自定义BAKERCM设置
安装
bakercm必须安装在要自行配置的环境中。一旦安装了python,就很容易了。
$ pip install bakercm
使用贝克
- 创建一个类似simple.cfg的配方
[appdb:template]template=app.conf.tpl[appdb:variables]HOST=dev.host.dbPORT=9000
- 创建一个模板app.conf.tpl
database:engine: 'postgres'host: '{{ HOST }}'port: '{{ PORT }}'
- 运行bakercm
$ baker run --path simple.cfg
- 完成!文件已配置。
秘密
Secret部分保存配方中的加密值。它的工作原理与其他变量类似,但不是明文值而是加密的,并且只有当配方运行以在环境中设置模板时才会解密。
配方中的秘密部分
...[appdb:secrets]PASSWORD=cfce1f5e82798a7fca808d8acae50baa\c092ca0bbc873e99d0a2318efa381355\6e9b48...
在模板中,秘密和其他变量一样
database:...password: '{{ PASSWORD }}'
加密和解密值是生成通过密钥传递的运行genkey命令的密钥所必需的。
$ baker genkey myKeyPass
要加密要保存在配方中的值,可以使用encrypt命令。
$ baker encrypt valueToEncrypt or to encrypt all values in secret section from a recipe $ baker encrypt --file recipe-to-encrypt.cfg
文件系统操作
要更改文件系统上的文件选项,可以在模板部分的配方上添加选项。模板部分支持的外观选项:
[appdb:template]template=/path/to/template.conf.tpl # Template location, it can be a URL toopath=/path/to/save/replaced/config.conf # Target location to save replaced file, # you also can rename the fileuser=owner # Set what user will be the file ownergroup=group-of-onwer # Set group that this file will belongmode=0755 # Set permission of file using the number format
上面的所有选项都适用于unix操作系统。对于windows,还不支持选项user、group、mode。
远程配方
远程配方是存储在版本控制服务器中的文件,bakercm让它们配置环境。当您想存储环境配置并对其进行版本控制时,它非常有用,bakercm会注意使用正确的配置来管理您想要的任何环境。
存储库设置
储存库应该设置在贝克知道食谱存放在哪里。为此,请使用存储库设置更改~/.bakerc文件。
REPOSITORY='https://raw.githubusercontent.com/lucasb/BakerCM/' # Repository urlREPOSITORY_TYPE='github' # Repository pattern like: 'github', 'bitbucket' or 'custom'# if authorization is necessary to read files from repository you can# add authorization in this setting.REPOSITORY_AUTH='Basic YmFrZXI6YmFrZXJjbQ=='# if REPOSITORY_TYPE='custom', REPOSITORY_CUSTOM_PATTERN should be set# using special keys: repository, path, ext and versionREPOSITORY_CUSTOM_PATTERN='%(repository)s/%(path)s.%(ext)s/%(version)s'
远程配方命令
要从存储库中获取配方,请使用带有name参数的命令pull,name格式为<;path>;:<;version>;,其中path是存储库中配方文件和配方版本的位置。
$ baker pull example/dev2:0.4.2 to force donwload of recipe use option -f $ baker pull -f example/dev:0.4.2
要列出环境中保存的所有配方和版本,请使用命令recipes。
$ baker recipes RECIPE_ID REMOTE VERSION FILENAME CREATED af33908tg example/dev2 0.4.2 dev2.cfg 2018-06-03 06:18
要删除本地存储的某些配方,请使用命令rm和RECIPE_ID。
$ baker rm af33908tg
此外,还可以使用命令run提取配方并使用name参数运行它。
$ baker run example/dev2:0.4.2
选项
了解更多t bakercm options只需运行--help-h,对于特定命令的帮助,同样的选项也可以工作。
$ baker -h usage: baker [-h] [-v] [--verbose] <COMMAND> ... Baker is a decentralized configuration management based on files. <:::> optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit --verbose increase output verbosity commands: <COMMAND> Run 'baker COMMAND --help' for more info on a command configs list of configs encrypt encrypt values using secret key genkey generate a secret key from a key pass pull pull a recipe with configurations recipes list recipes locally rm remove recipes locally run run configurations from a recipe
设置
您可以通过设置自定义bakercm选项。为此,您需要在主目录中创建一个.bakerc:
$ vim ~/.bakerc
DEBUG=False # Verbose mode, the default is falseENCODING=utf-8 # Encode of files and secretsRECIPE_CASE_SENSITIVE=False # The default config keys are case insensitiveREPOSITORY=None # Repository url including protocol http/httpsREPOSITORY_TYPE=None # Repository pattern like: 'github', 'bitbucket' or 'custom'REPOSITORY_AUTH=None # Authorization to read files from repository. Value is set as a header. # e.g.: 'Basic YmFrZXI6YmFrZXJjbQ=='REPOSITORY_CUSTOM_PATTERN=None # Custom repository url for others pattern. # e.g.: '%(repository)s/%(path)s.%(ext)s/%(version)s'STORAGE_RECIPE=~/.baker/recipes/ # Remote recipes are storageSTORAGE_RECIPE_INDEX=~/.baker/index # Baker index itemsSTORAGE_RECIPE_META=~/.baker/meta # Baker matadataSTORAGE_KEY_PATH=~/.baker/baker.key # Store secret key to encrypt and decrypt secret valuesSTORAGE_TEMPLATES=~/.baker/templates/ # Remote templates are storageTEMPLATE_EXT=tpl # Extension for template files. Set 'None' for no extension
列出bakercm的所有设置(自定义和默认设置)。
$ baker configs --all
其他
转义变量
如何转义模板中的变量:
escape-conn: '{{\ connection }}'
配方的多个模板
在一个配方中可以进行多个模板管理。为此,请为要配置的每个模板文件使用不同的名称。使用格式<name>:<section>。
[name1:template]...[name1:variable]...[name2:template]...[name2:secrets]