【问题标题】:Vagrant: easy way to define custom Chef service or boot scriptVagrant:定义自定义 Chef 服务或启动脚本的简单方法
【发布时间】:2013-08-19 01:53:55
【问题描述】:

我有一个 Vagrant 项目,它定义了几个虚拟机。在其中一个上,我需要运行一些命令来调整内核参数、从远程位置下载文件并启动守护程序。目前我在cookbooks/*/recipes/default.rb中定义:

execute "sudo tune-kernel-parameter"
execute "curl http://something/ > /tmp/some-file"
execute "sudo some-daemon --config /tmp/some-file"

当我第一次vagrant up 时,一切正常。但是当然,如​​果机器重新启动,这些命令都不会再次运行,因为它们不是任何 init 脚本的一部分。

我想我总是可以运行 vagrant reload 来重新启动 VM,但我希望它能够直接重新启动。换句话说,有一个持久的“服务”。

现在一些上面给出的命令(内核​​调整)可以被/etc中的文件修改替换,如果我想花时间查找这些文件的正确语法(在我已经完成验证立即命令是否执行我想要的工作之后)。对于其他人(启动守护进程),我可能能够使用预定义服务,即定义服务的 Ubuntu 软件包,我可以在 /etc 中配置并开始使用 service 资源。

但是还有一些特殊的命令,比如上面提到的下载(必须在守护进程启动之前完成)。大概我可以使用template 资源在/etc/init.d/ 中定义一个脚本,然后在配置时使用service 启动它(尽管Chef, how to run a template that creates a init.d script before the service is created 暗示这比听起来还要工作)。

但是,是否没有标准的方法来简单地要求在配置期间以及随后的重新启动时运行一个简短的内联脚本?介于executeservice 之间的东西,例如

init_script <<-END
    sudo tune-kernel-parameter
    curl http://something/ > /tmp/some-file
    sudo some-daemon --config /tmp/some-file
END

(我假设脚本是幂等的,即vagrant provision 可以在同一个VM 会话中再次运行修改后的版本;如果我需要先killall some-daemon,那很容易。)

【问题讨论】:

  • 您找到解决此问题的方法了吗?可以回答你自己的问题。请不要忘记将答案标记为正确! :)

标签: service chef-infra vagrant init.d cookbook


【解决方案1】:

您可能会为此使用/etc/rc.local

file "/etc/rc.local" do
  content <<-EOC
    sudo tune-kernel-parameter
    curl http://something/ > /tmp/some-file
    sudo some-daemon --config /tmp/some-file
  EOC
  mode 0755
  notifies :run, "execute[rc.local]"
end

execute "rc.local"
  command "/etc/rc.local"
  action :nothing
end

execute 块只声明了一个resource,但使用它来声明:nothing。当file 运行时,如果它修改磁盘上的文件,那么它会将notifies execute 资源转换为:run

【讨论】:

    猜你喜欢
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2013-01-26
    相关资源
    最近更新 更多