博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Puppet实战手册》——1.10 利用Git钩子自动进行语法检查
阅读量:7225 次
发布时间:2019-06-29

本文共 2719 字,大约阅读时间需要 9 分钟。

本节书摘来自异步社区《Puppet实战手册》一书中的第1章,第1.10节,作者:【英】John Arundel著,更多章节内容可以访问云栖社区“异步社区”公众号查看

1.10 利用Git钩子自动进行语法检查

如果能够在交付配置清单之前发现配置清单中的语法错误,那么将是一件非常好的事。可以使用puppet parser validate命令检查配置清单中的语法问题。

ubuntu@cookbook:~/puppet$ puppet parser validate manifests/nodes.ppError: Could not parse for environment production: Syntax error at end of file; expected '}' at /home/ubuntu/puppet/manifests/nodes.pp:3Error: Try 'puppet help parser validate' for usage

这是非常有用的。如果清单中的任何一个位置存在语法错误,那么都会停止Puppet在所有节点上运行,即使节点中没有使用那部分有错误的配置清单。因此,检查配置清单中的错误,直到问题被发现,可能引起一段时间内Puppet在生产环境中无法应用变更,这可能会带来严重的后果。要避免这种情况的发生,最好的方法就是在版本控制系统中使用预提交的钩子自动进行语法检查。

操作步骤

具体步骤如下。

1. 在Puppet仓库目录创建一个新的hooks目录。

ubuntu@cookbook:~/puppet$ mkdir hooks

2. 参考下面的内容,创建hooks/check_syntax.sh文件(基于由Puppet Labs提供的脚本)。

#!/bin/shsyntax_errors=0error_msg=$(mktemp /tmp/error_msg.XXXXXX)if git rev-parse --quiet --verify HEAD > /dev/nullthen   against=HEADelse   # Initial commit: diff against an empty tree object   against=4b825dc642cb6eb9a060e54bf8d69288fbee4904fi# Get list of new/modified manifest and template files to check (in git index)for indexfile in `git diff-index --diff-filter=AM --  name-only --cached $against | egrep '\.(pp|erb)'`do   # Don't check empty files   if [ `git cat-file -s :0:$indexfile` -gt 0 ]   then     case $indexfile in       *.pp )          # Check puppet manifest syntax          git cat-file blob :0:$indexfile |            puppet parser validate > $error_msg ;;       *.erb )          # Check ERB template syntax          git cat-file blob :0:$indexfile |            erb -x -T - | ruby -c 2> $error_msg >            /dev/null ;;     esac     if [ "$?" -ne 0 ]     then        echo -n "$indexfile: "        cat $error_msg        syntax_errors=`expr $syntax_errors + 1`     fi  fidonerm -f $error_msgif [ "$syntax_errors" -ne 0 ]then   echo "Error: $syntax_errors syntax errors found,    aborting commit."   exit 1fi

3. 使用下面的命令给钩子脚本设置执行权限:

ubuntu@cookbook:~/puppet$ chmod a+x .hooks/check_syntax.sh

4. 将下面的任务添加到Rakefile文件中:

desc "Add syntax check hook to your git repo"task :add_check do  here = File.dirname(__FILE__) sh "ln -s #{here}/hooks/check_syntax.sh  #{here}/.git/hooks/pre-commit" puts "Puppet syntax check hook added"end

5. 运行下面的命令:

ubuntu@cookbook:~/puppet$ rake add_checkln -s /home/ubuntu/puppet/hooks/check_syntax.sh /home/ubuntu/puppet/.git/hooks/pre-commit

Puppet语法检查的钩子已添加完成。

工作原理

该check_syntax.sh脚本会阻止用户提交任何带有语法错误的文件。

ubuntu@cookbook:~/puppet$ git commit -m "test commit"Error: Could not parse for environment production: Syntax error at   '}' at line 3Error: Try 'puppet help parser validate' for usagemanifests/nodes.pp: Error: 1 syntax errors found, aborting commit.

如果将hooks目录添加到Git仓库中,任何检出了副本的人都可以运行rake add_check任务,检查配置清单中的语法。

转载地址:http://ydufm.baihongyu.com/

你可能感兴趣的文章
设计模式走一遍---观察者模式
查看>>
Docker 笔记(2):Dockerfile
查看>>
聊聊sentinel的DegradeSlot
查看>>
不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
查看>>
E-HPC支持多队列管理和自动伸缩
查看>>
Java编程基础24——递归练习
查看>>
基于web的全景—— Pannellum小试
查看>>
因为阿里,他们成了“杭漂”
查看>>
jQuery(一)
查看>>
前端存储 - localStorage
查看>>
express + mock 让前后台并行开发
查看>>
30天自制操作系统-2
查看>>
[LeetCode/LintCode] Largest Palindrome Product
查看>>
小程序开发之路(一)
查看>>
携程小程序初体验
查看>>
Odoo domain写法及运用
查看>>
JS进阶 - JS 、JS-Web-API与DOM、BOM
查看>>
JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
查看>>
猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
查看>>
面试题:给你个id,去拿到name,多叉树遍历
查看>>