git、nagios 和 hooks、损坏的 git repo

EEA*_*EAA 13 git

背景

我们正在使用 nagios 来监控我们的基础设施。我们目前没有版本控制下的 nagios 配置,我们有两个人管理 nagios 配置。因此,我正在努力将我们的 nagios 配置放入中央 git 存储库中,使用一些钩子进行语法检查,然后如果配置看起来不错,则使它们“处于活动状态”。我正在使用这个人的帖子作为起点。

我试图实现的一般工作流程是:

  1. 编辑 nagios 配置的本地 git repo。添加编辑过的文件,在本地提交。
  2. git push origin master 到远程仓库。
  3. Push 被 pre-receive hook 拦截,它获取文件,将它们移动到服务器上的临时目录,并通过 nagios 语法检查器运行它们。
  4. 如果语法检查器通过,则接受推送,然后使用 post-commit hookgit pull将新代码放入 live nagios 配置目录中,然后重新启动 nagios。
  5. 如果语法检查器失败,则拒绝推送,向用户显示 nagios 语法错误。

但是,当我由于 nagios 配置中的语法错误而拒绝 git push 时,我遇到了一个奇怪的行为。我期望发生的是,如果我拒绝钩子,则尝试推送应该使存储库保持原样,原样。不过,情况似乎并非如此。以下是我所看到的详细信息:

问题

我在本地编辑 nagios 配置,故意包含一个语法错误,添加,然后在本地提交:

host:nagios erik$ vi nagios.cfg
host:nagios erik$ git add nagios.cfg
host:nagios erik$ git commit -m "syntax error"
[master da71aed] syntax error
 1 files changed, 1 insertions(+), 0 deletions(-)
Run Code Online (Sandbox Code Playgroud)

现在我将这些更改推送到主存储库。由于语法错误,这将被拒绝:

host:nagios erik$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 12.74 KiB, done.
Total 3 (delta 1), reused 2 (delta 1)
remote: Previous HEAD position was 3ddc880... removed syntax error
remote: HEAD is now at da71aed... syntax error
remote: Nagios Config Check Exit Status: 254
remote: Your configs did not parse correctly, there was an error. Output follows.
remote:
remote: Nagios Core 3.2.3
remote: Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors
remote: Copyright (c) 1999-2009 Ethan Galstad
remote: Last Modified: 10-03-2010
remote: License: GPL
remote:
remote: Website: http://www.nagios.org
remote: Reading configuration data...
remote: Error in configuration file '/tmp/nagiosworkdir/nagios.cfg' - Line 23 (NULL value)
remote:    Error processing main config file!
remote:
remote:
remote:
remote: ***> One or more problems was encountered while processing the config files...
remote:
remote:      Check your configuration file(s) to ensure that they contain valid
remote:      directives and data defintions.  If you are upgrading from a previous
remote:      version of Nagios, you should be aware that some variables/definitions
remote:      may have been removed or modified in this version.  Make sure to read
remote:      the HTML documentation regarding the config files, as well as the
remote:      'Whats New' section to find out what has changed.
remote:
To git@remote-server.example.com:nagios
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@remote-server.example.com:nagios'
Run Code Online (Sandbox Code Playgroud)

应该触及远程仓库,但确实如此。如果我更改到另一个本地临时目录并尝试克隆存储库,我会得到:

host:temp erik$ git clone git@remote-server.example.com:nagios
Cloning into nagios...
remote: Counting objects: 30, done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 30 (delta 12), reused 0 (delta 0)
Receiving objects: 100% (30/30), 29.81 KiB, done.
Resolving deltas: 100% (12/12), done.
error: Trying to write ref HEAD with nonexistant object da71aedfde2e0469288acd9e45bb8b57a6e5a7b3
fatal: Cannot update the ref 'HEAD'.
Run Code Online (Sandbox Code Playgroud)

现在我回到原来的工作目录,修复语法错误,添加、提交和推送:

host:nagios erik$ vi nagios.cfg
host:nagios erik$ git add nagios.cfg
host:nagios erik$ git commit -m "removing syntax error, push should succeed this time"
[master f147ded] removing syntax error, push should succeed this time
 1 files changed, 0 insertions(+), 2 deletions(-)
host:nagios erik$ git push origin master
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 487 bytes, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Previous HEAD position was 4c80d45... syntax error
remote: HEAD is now at f147ded... removing syntax error, push should succeed this time
remote: Nagios Config Check Exit Status: 0
remote: Your configs look good and parsed correctly.
To git@remote-server.example.com:nagios
   3ddc880..f147ded  master -> master
Run Code Online (Sandbox Code Playgroud)

此时,存储库很好,我可以切换到临时目录并再次克隆存储库:

host:temp erik$ git clone git@remote-server.example.com:nagios
Cloning into nagios...
remote: Counting objects: 34, done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 34 (delta 14), reused 0 (delta 0)
Receiving objects: 100% (34/34), 30.22 KiB, done.
Resolving deltas: 100% (14/14), done.
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的预接收钩子。

我在客户端使用 git v1.7.5.4,在服务器上使用 v1.7.2.3。

那么,问题是:当我拒绝推送时,为什么存储库会处于不一致的状态?我的 git hook 有什么问题,或者我对 git 的理解不够?

nic*_*rim 7

你正在做的:

export GIT_WORK_TREE=/tmp/nagiosworkdir
/usr/bin/git checkout -f $NEW_SHA1
Run Code Online (Sandbox Code Playgroud)

在你的钩子里。虽然它没有触及您通常的工作副本,但它正在更新 git-dir 中的HEAD引用(特别是引用),如您的错误所示:

...
remote: HEAD is now at da71aed... syntax error
...
Run Code Online (Sandbox Code Playgroud)

您的钩子正在exit 1拒绝更新,但它不会HEAD在失败后(重新)重置引用。

我认为您需要像这样更新钩子中的故障分支:

...
if [ "$NAGIOS_CHECK_STATUS" -ne 0 ]
   then
   echo "Your configs did not parse correctly, there was an error. Output follows."
   cat $GIT_WORK_TREE/check.out
   /usr/bin/git reset --hard $OLD_SHA1    # <-- Add This
   exit 1
else
   ...
Run Code Online (Sandbox Code Playgroud)