如何在使用 chef 部署 war 文件后重新启动 tomcat?

How to restart tomcat after deploying war file with chef?

我有自己的食谱,它从 AWS S3 存储桶下载文件并尝试重新启动 tomcat 服务。

我正在使用来自 https://github.com/opscode-cookbooks/tomcat

的 "official" Tomcat 食谱
FILE: myapp/recipes/default.rb

include_recipe 's3_file'
include_recipe 'tomcat::default'

s3_file "/var/lib/tomcat6/webapps/ROOT.war" do
    remote_path node['myapp']['packages_s3_bucket_path'] + 'myapp.war'
    bucket node['myapp']['packages_s3_bucket']
    aws_access_key_id node['myapp']['packages_s3_bucket_aws_access_key']
    aws_secret_access_key node['myapp']['packages_s3_bucket_aws_secret_key']
    notifies :restart, "service[tomcat]", :immediately
end

问题是编译时没有找到service[tomcat](tomcat在myapp的metadata.rb中也被引用了)

Compiling Cookbooks...
[2015-01-14T00:56:14+00:00] WARN: Using java::default instead is recommended.
Converging 112 resources
[2015-01-14T00:56:15+00:00] INFO: Running queued delayed notifications before re-raising exception
[2015-01-14T00:56:15+00:00] ERROR: Running exception handlers
[2015-01-14T00:56:15+00:00] ERROR: Exception handlers complete
[2015-01-14T00:56:15+00:00] FATAL: Stacktrace dumped to /var/cache/chef/chef-stacktrace.out
Chef Client failed. 0 resources updated
[2015-01-14T00:56:15+00:00] ERROR: resource s3_file[/var/lib/tomcat6/webapps/ROOT.war] is configured to notify resource service[tomcat] with action restart, but service[tomcat] cannot be found in the resource collection. s3_file[/var/lib/tomcat6/webapps/ROOT.war] is defined in /var/cache/chef/cookbooks/uaa/recipes/default.rb:69:in `from_file'

[2015-01-14T00:56:15+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

有什么想法吗? 谢谢

假设您使用的是默认设置的基本实例,那应该是 service[tomcat6]。服务资源是defined inside the LWRP。这只有效,因为提供不 use_inline_resources,所以在未来的某个时候,这可能不再有效。

编辑:

我在下面提到的技巧。

ruby_block 'restart tomcat' do
  action :nothing
  block do
    resources('service[tomcat6]').run_action(:restart)
  end
end

或类似的东西。通知 ruby 块,它将查找真正的资源并重新启动它。