厨师:延迟服务通知

时间:2017-03-06 11:06:57

标签: chef

这是我的mongodb食谱食谱:

node.default['mongodb3']['version'] = '3.4.2'
node.default['mongodb3']['repo'] = 'https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/'

node.default['mongodb3']['config']['mongod']['net']['port'] = 30158
node.default['mongodb3']['config']['mongod']['net']['bindIp'] = 'localhost'
node.default['mongodb3']['config']['mongod']['security']['authorization'] = 'enabled'

include_recipe 'mongodb3::default'

cookbook_file "/tmp/setupUsers.js" do
  source "mongo/setupUsers.js"
  mode 0755
end

execute "Add Mongo Users" do
  command "mongo localhost:30158 /tmp/setupUsers.js"
end

如你所见:

  1. 我正在安装mongo 3.4 and,
  2. 我正在尝试执行mongo localhost:30158 /tmp/setupUsers.js
  3. 但是,我收到了厨师的这条消息:

    ==> default:   * execute[Add Mongo Users] action run
    ==> default:
    ==> default:     [execute] MongoDB shell version v3.4.2
    ==> default:               connecting to: localhost:30158
    ==> default:               2017-03-06T10:56:56.875+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:30158, in(checking socket
    for error after poll), reason: Connection refused
    ==> default:               2017-03-06T10:56:56.879+0000 E QUERY    [thread1] Error: couldn't connect to server localhost:30158, connec
    tion attempt failed :
    ==> default:               connect@src/mongo/shell/mongo.js:237:13
    ==> default:               @(connect):1:6
    ==> default:               exception: connect failed
    ==> default:
    ==> default:     ================================================================================
    ==> default:     Error executing action `run` on resource 'execute[Add Mongo Users]'
    ==> default:     ================================================================================
    ==> default:
    ==> default:     Mixlib::ShellOut::ShellCommandFailed
    ==> default:     ------------------------------------
    ==> default:     Expected process to exit with [0], but received '1'
    ==> default:     ---- Begin output of mongo localhost:30158 /tmp/setupUsers.js ----
    ==> default:     STDOUT: MongoDB shell version v3.4.2
    ==> default:     connecting to: localhost:30158
    ==> default:     2017-03-06T10:56:56.875+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:30158, in(checking socket for error
    after poll), reason: Connection refused
    ==> default:     2017-03-06T10:56:56.879+0000 E QUERY    [thread1] Error: couldn't connect to server localhost:30158, connection attem
    pt failed :
    ==> default:     connect@src/mongo/shell/mongo.js:237:13
    ==> default:     @(connect):1:6
    ==> default:     STDERR: exception: connect failed
    ==> default:     ---- End output of mongo localhost:30158 /tmp/setupUsers.js ----
    ==> default:     Ran mongo localhost:30158 /tmp/setupUsers.js returned 1
    ==> default:
    ==> default:     Resource Declaration:
    ==> default:     ---------------------
    ==> default:     # In /var/chef/cache/cookbooks/berk/recipes/security.rb
    ==> default:
    ==> default:       6: execute "Add Mongo Users" do
    ==> default:       7:   command "mongo localhost:30158 /tmp/setupUsers.js"
    ==> default:       8: end
    ==> default:
    ==> default:     Compiled Resource:
    ==> default:     ------------------
    ==> default:     # Declared in /var/chef/cache/cookbooks/berk/recipes/security.rb:6:in `from_file'
    ==> default:
    ==> default:     execute("Add Mongo Users") do
    ==> default:       action [:run]
    ==> default:       retries 0
    ==> default:       retry_delay 2
    ==> default:       default_guard_interpreter :execute
    ==> default:       command "mongo localhost:30158 /tmp/setupUsers.js"
    ==> default:       backup 5
    ==> default:       returns 0
    ==> default:       user nil
    ==> default:       declared_type :execute
    ==> default:       cookbook_name "berk"
    ==> default:       recipe_name "security"
    ==> default:     end
    ==> default:
    ==> default:     Platform:
    ==> default:     ---------
    ==> default:     x86_64-linux
    ==> default:
    

    正如您所看到的,似乎mongod服务尚未运行,但在输出消息的最后,Chef告诉我正在尝试重新启动延迟服务通知。

    ==> default: [2017-03-06T10:56:56+00:00] INFO: Running queued delayed notifications before re-raising exception
    ==> default: [2017-03-06T10:56:56+00:00] INFO: template[/etc/mongod.conf] sending restart action to service[mongod] (delayed)
    ==> default: Recipe: mongodb3::default
    ==> default:   * service[mongod] action restart
    ==> default: [2017-03-06T10:56:57+00:00] INFO: service[mongod] restarted
    ==> default:
    ==> default:     - restart service service[mongod]
    ==> default: [2017-03-06T10:56:57+00:00] INFO: template[/opt/wildfly/standalone/configuration/standalone-full.xml] sending restart act
    ion to service[wildfly] (delayed)
    

    为什么在Chef配置结束之前不启动此服务?我需要在mongod服务启动后连接到mongo

    我看过mongodb3食谱。 According to this line(mongodb3配方的默认配方),服务应该立即开始。

    修改

    我正在使用mongodb3配方。 mongodb3默认配方:

    service 'mongod' do
      case node['platform']
        when 'ubuntu'
          if node['platform_version'].to_f >= 15.04
            provider Chef::Provider::Service::Systemd
          elsif node['platform_version'].to_f >= 14.04
            provider Chef::Provider::Service::Upstart
          end
      end
      supports :start => true, :stop => true, :restart => true, :status => true
      action :enable
      subscribes :restart, "template[#{node['mongodb3']['mongod']['config_file']}]", :delayed
      subscribes :restart, "template[#{node['mongodb3']['config']['mongod']['security']['keyFile']}", :delayed
    end
    

1 个答案:

答案 0 :(得分:2)

我没有在你的食谱中看到service[mongod],但我想你在某处有以下几行:

 service 'mongod' do
   action [:enable, :start]
 end

此时Chef会启动该服务,但可能需要一些时间才能完全运行并响应请求。厨师不等待,继续运行食谱。

您可以多次尝试execute[Add Mongo Users],直到成功为止:

execute "Add Mongo Users" do
  command "mongo localhost:30158 /tmp/setupUsers.js"
  retries 6 #times
  retry_delay 10 #seconds
end

在Chef失败之前,这将使您的服务开始一分钟。

重要提示:您还应该为execute[Add Mongo Users]提供一些保护,否则它将在每次主厨运行时运行。

编辑(显示service[mongod]资源后。)

它没有 start 动作,这就是它没有启动的原因。所以添加

service 'mongod' do
  action :start
end

execute[Add Mongo Users]资源之前的某个地方。