如何在厨师的Linux上创build一个后台服务?

我有一个Python脚本,我在ubuntu主机上使用screen执行,我想用一个Chef食谱把它变成适当的后台服务。 python脚本启动一个SMTP服务器,丢弃所有的电子邮件(一个“黑洞”服务器,用于testing),然后调用python的asyncore.loop()永远运行。

我怎么把这变成一个厨师的食谱? 我正在使用cookbook_file资源上传我的Python脚本,我知道我需要使用service 资源来启动服务,但是我不确定如何实际创build服务。 我需要写一个文件到/etc/init.d/etc/systemd来在操作系统级创build服务,或者是否有一些我可以调用的Chef recipe / resource来为我创build一个后台服务,给定的命令?

我使用的厨师11.18.12 ,不能升级,所以这就排除了一些食谱(如runit ),需要12+。 主机操作系统是Ubuntu 14.04.3

这是迄今为止我所得到的:

 include_recipe 'apt' include_recipe 'python' # the python script to run as a background service cookbook_file '/usr/local/bin/smtp_server_blackhole.py' do source 'smtp_blackhole_server.py' owner 'root' group 'root' mode '0755' end # the init.d file which defines the service? Not sure about this part... cookbook_file "init.d_smtp_server_blackhole" do path "/etc/init.d/smtp_server_blackhole.py" source "init.d_smtp_server_blackhole" owner "root" group "root" mode "0755" end service "smtp_blackhole_server.py" do # this doesn't work, because the command doesn't reference an existing service? start_command 'python /usr/local/bin/smtp_blackhole_server.py -c bounce' action [ :enable, :start ] # I think I need to do something like this, to wait for the init.d file to be written subscribes :restart, "cookbook_file[init.d_smtp_server_blackhole]", :immediately end 

潜在的解决scheme

  1. daemontools是一个LWRP,允许您使用运行命令的模板文件创build服务。 但是,它在几年之内还没有更新,并且需要为“合适的init系统”设置svscan服务。
  2. 对于如何使用各种代码示例和Python库(例如python-daemon和daemonize )来编写Python守护进程服务, 这个问题有一些很好的答案,但是它并不包括如何在主厨配方中以幂等的方式执行OS命令。
  3. https://github.com/poise/poise-service看起来像我需要的,但它不支持厨师11.18,因为poise 2.0需要厨师12+。

对于Ubuntu 14.04,你可能想从Upstart开始,因为这是系统初始化框架。 查看poise-service中的暴发户Erb模板,了解如何设置或查看Canonical的Upstart Cookbook。 如果你想使用除了Upstary以外的东西,我会推荐Supervisord,因为它的简单性,因为它在Python社区很受欢迎。