2013-08-24 42 views
2

我是一名廚師菜鳥。我想創建一個在後臺運行jar的配方。如何在廚師配方的背景下運行java程序

bash 'run_jar' do 
    code <<-EOH 
    wget https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar -O hello.jar 
    java -jar hello.jar & 
    EOH 
end 

helloworld-1.0.jar是程序第一次打印「Hello World」,然後執行一段時間(true)循環。

我希望當我登錄到廚師客戶機。它應該表明有一個jar使用「jps」命令運行。但是沒有這樣的jar運行。

我可以看到hello.jar被下載,表明代碼塊已經被執行。

這個食譜有什麼問題?

回答

7

建議您將代碼配置爲作爲服務運行。幾個可用的包裝,例如:

一旦做到這一點,你可以配置廚師來管理新的服務:

service "myapp_service" do 
    supports :status => true, :restart => true 
    start_command "/usr/lib/myapp/bin/myapp start" 
    restart_command "/usr/lib/myapp/bin/myapp restart" 
    status_command "/usr/lib/myapp/bin/myapp status" 
    action [ :enable, :start ] 
end 
+0

馬克,感謝您的回覆。有用!我想創建一個微服務。如果我必須自己編寫服務,我寧願使用現有的Web容器,如tomcat,jetty。有沒有其他可能性以最簡單的方式部署微服務? – kiwisw

+0

@kiwisw不是以跨平臺的方式。正如你所說的將應用程序部署到Jetty或Tomcat中可能是實現你的願望的最簡單的方法。謝天謝地,廚師可以很容易地設置。 –

+0

你知道dropwizard嗎?我已經通過dropwizard提供服務,這正是我想由廚師部署的。 – kiwisw

3

您應該使用remote_file資源來獲取文件而不是在bash中使用wget這裏的文檔塊。

調試是非常有幫助的,啓用它,如果你不知道;-)(假設你正在使用的廚師獨奏)

chef-solo -c solo.rb -j node.json -l debug

注:我建議將文件下載到Chef::Config[:file_cache_path]/tmp

我能夠使用remote_file實現你想要的。配方看上去象下面這樣:

temp=Chef::Config[:file_cache_path] 

remote_file "#{Chef::Config[:file_cache_path]}/hello.jar" do 
    source "https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar" 
    mode 00644 
end 

bash 'run_jar' do 
    code <<-EOF 
     /opt/jdk1.6.0_37/bin/java -jar #{temp}/hello.jar > #{temp}/hello.log 2>&1 & 
    EOF 
end 

例如在一個EC2測試盒上運行(性能很爛...):

# chef-solo -c solo.rb -j node.json -l debug 
[2013-08-26T02:51:08+00:00] INFO: Forking chef instance to converge... 
[2013-08-26T02:51:08+00:00] DEBUG: Fork successful. Waiting for new chef pid: 18991 
[2013-08-26T02:51:08+00:00] DEBUG: Forked instance now converging 
Starting Chef Client, version 11.6.0 
[2013-08-26T02:51:13+00:00] INFO: *** Chef 11.6.0 *** 
[2013-08-26T02:51:37+00:00] DEBUG: Building node object for cloud.terry.im 
[2013-08-26T02:51:37+00:00] DEBUG: Extracting run list from JSON attributes provided on command line 
[2013-08-26T02:51:37+00:00] INFO: Setting the run_list to ["recipe[main]"] from JSON 
[2013-08-26T02:51:37+00:00] DEBUG: Applying attributes from json file 
[2013-08-26T02:51:37+00:00] DEBUG: Platform is ubuntu version 12.04 
[2013-08-26T02:51:37+00:00] INFO: Run List is [recipe[main]] 
[2013-08-26T02:51:37+00:00] INFO: Run List expands to [main] 
[2013-08-26T02:51:37+00:00] INFO: Starting Chef Run for cloud.terry.im 
[2013-08-26T02:51:37+00:00] INFO: Running start handlers 
[2013-08-26T02:51:37+00:00] INFO: Start handlers complete. 
[2013-08-26T02:51:37+00:00] DEBUG: No chefignore file found at /tmp/chef/cookbooks/chefignore no files will be ignored 
Compiling Cookbooks... 
[2013-08-26T02:51:37+00:00] DEBUG: Cookbooks to compile: [:main] 
[2013-08-26T02:51:37+00:00] DEBUG: Loading Recipe main via include_recipe 
[2013-08-26T02:51:37+00:00] DEBUG: Found recipe default in cookbook main 
[2013-08-26T02:51:37+00:00] DEBUG: Loading from cookbook_path: /tmp/chef/cookbooks 
Converging 2 resources 
[2013-08-26T02:51:37+00:00] DEBUG: Converging node cloud.terry.im 
Recipe: main::default 
    * remote_file[/var/chef/cache/hello.jar] action create[2013-08-26T02:51:37+00:00] INFO: Processing remote_file[/var/chef/cache/hello.jar] action create (main::default line 3) 
[2013-08-26T02:51:37+00:00] DEBUG: touching /var/chef/cache/hello.jar to create it 
[2013-08-26T02:51:37+00:00] INFO: remote_file[/var/chef/cache/hello.jar] created file /var/chef/cache/hello.jar 

    - create new file /var/chef/cache/hello.jar[2013-08-26T02:51:37+00:00] DEBUG: remote_file[/var/chef/cache/hello.jar] checking for changes 
[2013-08-26T02:51:37+00:00] DEBUG: Cache control headers: {} 
[2013-08-26T02:51:37+00:00] DEBUG: Sending HTTP Request via GET to github.com:443/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar 
[2013-08-26T02:51:38+00:00] DEBUG: Streaming download from https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar to tempfile /tmp/chef-rest20130826-18991-11kv5ag 
[2013-08-26T02:51:38+00:00] DEBUG: Following redirect 1/10 
[2013-08-26T02:51:38+00:00] DEBUG: Sending HTTP Request via GET to s3.amazonaws.com:443/github-cloud/releases/12257402/d34edcec-0ba1-11e3-9ef0-268967c7e46f.jar 
[2013-08-26T02:51:39+00:00] DEBUG: Streaming download from https://s3.amazonaws.com/github-cloud/releases/12257402/d34edcec-0ba1-11e3-9ef0-268967c7e46f.jar?response-content-disposition=attachment%3B%20filename%3Dhelloworld-1.0.jar&AWSAccessKeyId=AKIAISTNZFOVBIJMK3TQ&Expires=1377485552&Signature=ahoXNIhegXTdEvUE0FoimC34x%2Bg%3D to tempfile /tmp/chef-rest20130826-18991-1ojysmn 
[2013-08-26T02:51:39+00:00] DEBUG: reading modes from /var/chef/cache/hello.jar file 
[2013-08-26T02:51:39+00:00] DEBUG: applying mode = 640, uid = 0, gid = 0 to /tmp/chef-rest20130826-18991-1ojysmn 
[2013-08-26T02:51:39+00:00] DEBUG: moving temporary file /tmp/chef-rest20130826-18991-1ojysmn into place at /var/chef/cache/hello.jar 
[2013-08-26T02:51:39+00:00] INFO: remote_file[/var/chef/cache/hello.jar] updated file contents /var/chef/cache/hello.jar 

    - update content in file /var/chef/cache/hello.jar from none to 9d1706 
     (new content is binary, diff output suppressed)[2013-08-26T02:51:39+00:00] DEBUG: found current_mode == nil, so we are creating a new file, updating mode 
[2013-08-26T02:51:39+00:00] DEBUG: found current_mode == nil, so we are creating a new file, updating mode 
[2013-08-26T02:51:39+00:00] DEBUG: found target_uid == nil, so no owner was specified on resource, not managing owner 
[2013-08-26T02:51:39+00:00] DEBUG: found target_gid == nil, so no group was specified on resource, not managing group 
[2013-08-26T02:51:39+00:00] DEBUG: found target_uid == nil, so no owner was specified on resource, not managing owner 
[2013-08-26T02:51:39+00:00] DEBUG: found target_gid == nil, so no group was specified on resource, not managing group 
[2013-08-26T02:51:39+00:00] DEBUG: found current_mode == nil, so we are creating a new file, updating mode 
[2013-08-26T02:51:39+00:00] INFO: remote_file[/var/chef/cache/hello.jar] mode changed to 644 

    - change mode from '' to '0644'[2013-08-26T02:51:39+00:00] DEBUG: selinux utilities can not be found. Skipping selinux permission fixup. 


    * bash[run_jar] action run[2013-08-26T02:51:39+00:00] INFO: Processing bash[run_jar] action run (main::default line 8) 
[2013-08-26T02:51:39+00:00] DEBUG: Platform ubuntu version 12.04 found 
[2013-08-26T02:51:41+00:00] INFO: bash[run_jar] ran successfully 

    - execute "bash" "/tmp/chef-script20130826-18991-jvx1bi" 

[2013-08-26T02:51:41+00:00] INFO: Chef Run complete in 3.688815399 seconds 
[2013-08-26T02:51:41+00:00] INFO: Running report handlers 
[2013-08-26T02:51:41+00:00] INFO: Report handlers complete 
Chef Client finished, 2 resources updated 
[2013-08-26T02:51:44+00:00] DEBUG: Forked child successfully reaped (pid: 18991) 
[2013-08-26T02:51:44+00:00] DEBUG: Exiting 
[email protected]:/tmp/chef# cat /var/chef/cache/hello.log 
Hello World 
+2

+1用於推薦remote_file並將輸出重定向到日誌文件。另一個shell改進可能是使用「nohup」將java命令與父進程斷開連接。 –

+0

我需要放置dropwizard microservice運行的jar和yaml文件,並給我示例recepie的位置,請 – madhu

1

我認爲這是更好地使用像上司給它的工具。 我正在使用poise主管食譜,你可以閱讀它here

這裏是一個示例代碼:

include_recipe "supervisor" 

service "supervisor" do 
    supports :status => true, :restart => true 
    action [:enable, :start] 
end 

# configure supervisor 
supervisor_service "sample-java-app" do 
    command "java -jar hello.jar" 
    directory <path-to-folder> 
    environment <environment-hash> 
    action [:enable, :start] 
    autostart true 
    autorestart true 
    stdout_logfile /var/log/sample-java-app.log 
    redirect_stderr true 
    user root 
    notifies :restart, 'service[supervisor]', :delayed 
end 

你可以用下面的命令來控制您的服務:

$> supervisorctl start/stop/restart sample-java-app 
+0

根據其中一位維護人員,主管「cookbook實際上已被棄用,等待重寫」。它有十幾個公開問題和PR,並且在一年半之內沒有更新。 –