厨师:延迟服务通知

这是我的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

如你看到的:

  • 我正在安装mongo 3.4,
  • 我正在尝试执行mongo localhost:30158 /tmp/setupUsers.js
  • 不过,我收到了厨师的这个消息:

    ==> 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服务还没有运行,但在输出消息的最后,厨师告诉我,这是尝试重新启动延迟服务通知。

    ==> 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)
    

    为什么这项服务直到厨师配置结束才开始? 在Mongo服务启动后,我需要连接到mongo

    我看了一下mongodb3食谱。 根据这条线(mongodb3 recipe的默认配方),服务应立即启动。

    编辑

    我正在使用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
    

    我没有在你的食谱中的任何地方看到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]提供一些execute[Add Mongo Users] ,否则它会在每次Chef运行时运行。

    编辑 (在你展示service[mongod]资源之后。)

    它没有启动动作,这就是为什么它没有启动。 所以添加

    service 'mongod' do
      action :start
    end
    

    execute[Add Mongo Users]资源之前的某处。

    链接地址: http://www.djcxy.com/p/78315.html

    上一篇: Chef: delayed service notification

    下一篇: Restart Nginx service on Centos 7 via chef