Chef Recipe配置多个mpd实例

时间:2014-05-29 07:55:19

标签: vagrant chef multiple-instances chef-recipe mpd

我尝试创建一个厨师食谱,在我的流浪汉虚拟盒中启动多个mpd实例(使用chef-solo)。

我想在我的Vagrantfile中配置每个实例,如下所示:

mpd: {
  channels: {
    mix: {
      name: 'mpd_mix',
      bind: '0.0.0.0',
      socket: '/home/vagrant/.mpd/socket/mix',
      port: '6600'
    },
    tech: {
      name: 'mpd_tech',
      bind: '0.0.0.0',
      socket: '/home/vagrant/.mpd/socket/tech',
      port: '6601'
    }
  }
}

因此配方应该采用这些设置并循环遍历它们(为每个通道创建一个mpd实例)。

这是我目前的配方:

package "mpd"

node.normal[:mpd][:channels].each_value do |channel|
    # create socket
    file channel[:socket] do
      action :touch
    end

    # trying to set the attributes for the config file and the service
    node.set[:mpd][:port] = channel[:port]
    node.set[:mpd][:db_file] = "/var/lib/mpd/tag_cache_" + channel[:name]
    node.set[:mpd][:bind_2] = channel[:socket]
    node.set[:mpd][:icecast_mountpoint] = "/" + channel[:name] + ".mp3"
    node.set[:mpd][:channel_name] = channel[:name]

    # create service
    service channel[:name] do
      service_name "mpd" # linux service command
      action :enable
    end

    # create the corresponding config file
    config_filename = "/etc/" + channel[:name] + ".conf"
    template config_filename do
      source "mpd.conf.erb"
      mode "0644"
      notifies :restart, resources(:service => channel[:name])
    end
end

我有几个问题:

  1. Ist没有为每个mpd实例创建系统服务,所以我可以sudo service mpd_mix start。为什么?
  2. 启动mpd时不会使用/etc/mpd_mix.conf配置文件,因为它仍会调用使用/etc/init.d/mpd start的{​​{1}}。我该怎么改变它,所以它为每个mpd实例使用正确的配置文件?
  3. 调整创建配置文件的属性不能按预期工作(请参阅上面代码中的node.set部分)。配置文件/etc/mpd.conf/etc/mpd_tech.conf都使用技术渠道属性。看起来混合设置会以某种方式被覆盖?我该如何解决这个问题?
  4. 我真的很感激这方面的帮助,因为我对厨师烹饪书很新。

1 个答案:

答案 0 :(得分:0)

我想出了怎么做。以下是相关的代码部分:

node[:mpd][:channels].each_value do |channel|

    # create socket
    file channel[:socket] do
      action :touch
    end

    # create init file
    init_filename = "/etc/init.d/" + channel[:name]
    template init_filename do
      variables :channel => channel
      source "mpd.init.erb"
      mode "0755"
    end

    # create service
    service channel[:name] do
      service_name channel[:name] # linux service command
      action :enable
    end

    # create config file
    config_filename = "/etc/" + channel[:name] + ".conf"
    template config_filename do
      variables :channel => channel
      source "mpd.conf.erb"
      mode "0644"
      notifies :restart, resources(:service => channel[:name])
    end

end

如果您想仔细看看,请查看github上的完整食谱库:https://github.com/i42n/chef-cookbook-mpd/blob/master/recipes/default.rb