0

我尝试创建一个厨师食谱以在我的 vagrant 虚拟框中启动多个 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. /etc/mpd_mix.conf它在启动 mpd 时不使用配置文件,因为它仍然调用/etc/init.d/mpd startwhich uses /etc/mpd.conf. 我该如何更改它,以便它为每个 mpd 实例使用正确的配置文件?
  3. 为创建配置文件调整属性不能按预期工作(请参阅上面代码中的 node.set 部分)。两个配置文件,/etc/mpd_tech.conf/etc/mpd_mix.conf使用技术通道属性。看起来混音设置不知何故被覆盖了?我该如何解决?

我真的很感激这方面的一些帮助,因为我对厨师食谱很陌生。

4

1 回答 1

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

于 2014-05-29T13:27:20.513 回答