smeeb
smeeb

Reputation: 29477

Writing custom Chef cookbook to install and run homegrown app

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:

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?

Upvotes: 0

Views: 431

Answers (1)

Tejay Cardon
Tejay Cardon

Reputation: 4223

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.

Upvotes: 1

Related Questions