您如何中止/结束厨师跑步? [英] How do you abort/end a Chef run?

查看:168
本文介绍了您如何中止/结束厨师跑步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我需要中止/结束带有非零状态代码的Chef运行,然后再通过我们的部署链传播回该运行,并最终传播到Jenkins,从而产生一个大的胖红色球。

Under certain conditions, I need to abort/end a Chef run with a non-zero status code, that will then propagate back through our deployment chain and eventually to Jenkins resulting in a big, fat red ball.

执行此操作的最佳方法是什么?

What is the best way to do this?

推荐答案

对于以后可能不熟悉Chef的读者,此问题和答案是由Chef运行的。 节点,或使其与正在运行的配方中声明的策略保持一致。这也称为收敛。这有两个阶段,编译和执行。编译阶段是Chef评估(编译)配方的Ruby代码,寻找要添加到Resource Collection的资源。一旦完成,它将为每个资源执行操作以使其进入所需状态。系统命令正在运行,等等。

For the readers coming to this question and answer in the future that may not be familiar with Chef, a Chef run "converges" the node, or brings it in line with the policy declared in the recipe(s) it is running. This is also called "convergence." This has two phases, "compile" and "execute." The compile phase is when Chef evaluates ("compiles") the recipes' Ruby code, looking for resources to add to the Resource Collection. Once that is complete, it "executes" the actions for each resource to put it into the state desired. System commands are run, etc.

Erik Hollensbe写了优秀这在2013年如何工作

Erik Hollensbe wrote an excellent walk through of how this works in 2013.

现在,答案为

有几种方法可以终止厨师因为厨师的食谱是Ruby代码,所以根据您的操作方式运行或退出厨师的食谱。

There are several ways to end a Chef run, or exit a Chef recipe, depending on how you want to go about it, since Chef recipes are Ruby code.

如果您的目标是停止基于在一定条件下,但继续进行其余的运行,然后使用 return Ruby关键字。例如:

If your goal is to stop processing a recipe based on a condition, but continue with the rest of the run, then use the return Ruby keyword. For example:

file '/tmp/ponies' do
  action :create
end

return if platform?('windows')

package 'bunnies-and-flowers' do
  action :install
end

我们假设如果系统是Windows,则它没有可以安装兔子和鲜花软件包的软件包管理器,所以我们从那里回来。

We presume that if the system is Windows, it doesn't have a package manager that can install the bunnies-and-flowers package, so we return from whence we came.

如果您想中止厨师的整个运行

Tl; dr:使用提高。最好的方法是在出现错误情况时中止Chef运行。

Tl;dr: Use raise. It's the best practice to abort a Chef run in case of an error condition.

也就是说,chef-client如果在运行中的任何地方遇到未处理的异常,都会退出。例如,如果模板资源找不到其源文件,或者运行Chef-client的用户没有权限进行诸如创建目录之类的操作。这就是为什么使用 raise 也可以结束运行的原因。

That said, chef-client exits if it encounters an unhandled exception anywhere in the run. For example, if a template resource can't find its source file, or if the user running chef-client doesn't have permission to do something like make a directory. This is why using raise also works to end a run.

在何处放置 raise 很重要。如果在 ruby​​_block 资源中使用它,它将仅在执行阶段收敛。如果您在上面的 return 这样的资源之外使用它,它将在编译阶段发生。

Where you put raise matters. If you use it in a ruby_block resource, it will only raise during the execution phase in convergence. If you use it outside of a resource like the return example above, it will happen during the compile phase.

file '/tmp/ponies' do
  action :create
end

raise if platform?('windows')

package 'bunnies-and-flowers' do
  action :install
end

也许我们在Windows上确实有一个软件包管理器,并且我们想要安装此软件包。

Perhaps we do have a package manager on Windows, and we want this package installed. The raise will result in Chef fatally exiting and giving a stack trace.

在过去的几年中,另一种方法是使用 Chef :: Application.fatal !-正如我在此答案中所写。时间已经改变,这是不推荐。不要再这样做了。如果这样做,请切换到 raise ,并如前所述,如果您的需求更加复杂,请编写自己的异常处理程序(见下文)。

In years past, another approach was to use Chef::Application.fatal! - as written by me in this answer. Times have changed and this is NOT RECOMMENDED. Do not do this anymore. If you're doing it, switch to raise and as mentioned, write your own exception handler if your needs are more complicated (see below).

更多优美的错误处理

由于配方是Ruby,您还可以使用<$优美地处理错误情况c $ c>开始..救援块。

Since recipes are Ruby, you can also gracefully handle error conditions with a begin..rescue block.

begin
  dater = data_bag_item(:basket, "flowers")
rescue Net::HTTPServerException
  # maybe some retry code here?
  raise "Couldn't find flowers in the basket, need those to continue!"
end

data_bag_item 对Chef服务器上的数据包的HTTP请求,如果服务器出现问题(找不到404,未授权403等),则会返回 Net :: HTTPServerException 。我们可能会尝试重试或进行其他处理,然后退回到提高

data_bag_item makes an HTTP request for a data bag on the Chef Server, and will return a Net::HTTPServerException if there's a problem from the server (404 not found, 403 unauthorized, etc). We could possibly attempt to retry or do some other handling, and then fall back to raise.

报告错误

如果您是从命令行运行Chef,只需退出并扔掉堆栈跟踪记录就可以了。但是,如果您在 cron 中运行它,或者将其作为守护程序运行在几台甚至数十台或数百台计算机上,那么这并不是保持理智的好方法。

Simply exiting and tossing a stack trace is fine if you're running Chef from the command-line. However, if you're running it in cron or as a daemon across a few, or even dozens or hundreds of machines, this isn't a great way to keep sanity when there's problems.

输入厨师的报告/异常处理程序功能。您可以为Chef运行使用处理程序。所有报表处理程序在Chef运行结束时运行。异常处理程序在中止的Chef运行结束时运行。可以跟踪运行的状态,并可以在处理程序中对其进行检查,因此您可以编写处理两种运行(成功/完成或不成功/中止)的运行状态。

Enter Chef's report/exception handler feature. You can use a handler for your Chef runs. All report handlers are run at the end of a Chef run. Exception handlers are run at the end of an aborted Chef run. The status of the run is tracked, and can be checked in the handler, so you can write one that handles both kinds of run (successful/completed or unsuccessful/aborted).

文档告诉您如何编写。它还包括可用的开源处理程序的列表,您可以将其用于多种服务,包括:

The documentation tells you how to write one. It also includes a list of available open source handlers that you can use for a variety of services, including:


  • 通过SMTP发送电子邮件

  • IRC

  • 石墨

  • HipChat

  • Email over SMTP
  • IRC
  • Graphite
  • HipChat

还有更多。

这篇关于您如何中止/结束厨师跑步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆