0

I am new to Chef and am trying to create a simple custom cookbook. I have an executable JAR that I have hosted on a private binary repo, and I'd like my cookbook to install this JAR, configure the server to be able to run the JAR, and then actually run it.

Specifically, I want the cookbook to:

  • Download http://myrepo.example.com/myapp.jar to /opt/myapp/bin on the node; then
  • Set MYAPP_HOME env var to /opt/myapp/bin; then
  • Run java -jar /opt/myapp/bin/myapp.jar --setup

Obviously this cookbook depends on Java (ideally 8+) being installed on the server ahead of time.

Using this excellent guide as my starting point, here is my best attempt thus far:

Create the cookbook:

knife create cookbook cookbook-myapp -o cookbooks/ -r md

/cookbooks/cookbook-myapp/recipes/default.rb:

package http://myrepo.example.com/myapp.jar

service "myapp" do
    action :install
end

env 'MYAPP_HOME' do
    value '/opt/myapp/bin'
end

bash 'run_jar' do
    code <<-EOH
    java -jar ${MYAPP_HOME}/myapp.jar --setup
    EOH
end

But this is obviously wrong. Any ideas or nudges in the right direction anyone can give me?

smeeb
  • 27,777
  • 57
  • 250
  • 447
  • See: http://stackoverflow.com/questions/18414090/how-to-run-a-java-program-at-background-from-chef-recipe/18418187#18418187 – Mark O'Connor Jul 16 '15 at 00:40

1 Answers1

1
remote_file '/path/to/myapp' do
  source 'http://myapp.repo.com/myapp.jar'
end

template '/etc/init.d/myapp.conf' do
  source 'myapp.init.config.erb'
  variables :home => '/opt/myapp/bin'
end

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

and, of course, you'll need to write myapp.init.conf.erb in cookbook/templates/default. That is your init script for running the app as a service, and includes the bash command and setting the environment variable.

Tejay Cardon
  • 4,193
  • 2
  • 16
  • 31