soupybionics
soupybionics

Reputation: 4390

How to deploy a git repo to a chef node using chef?

What is the standard/best/flexible way to simply managing deployment of a git repo to a chef node? Basically I have a git repo (that developers check their code into). Here are the sequence of steps I plan to do:

1) After the jenkins server has done the testing of the code, it should start the deployment by invoking the chef-client on the node[s].

2) Tar the the code (scripts for service) from the git repo and save it somewhere (on jenkins server itself?)

3) The chef client when invoked should do the following things:

a) Kill the existing running service on it.

b) download that tar file from remote location

c) install the dependencies (apt-get, etc)

d) May be update few config files (template resource?)

e) Extract the tar to the desired location.

f) launch the service.

I would like to know broadly what sort of chef resources can be used to achieve the above? There's just one service running on every node in my case.

Upvotes: 0

Views: 592

Answers (1)

Tensibai
Tensibai

Reputation: 15784

In a generic way I would do something like this (untested code as a showcase only):

Using ark resource for the tar file download and unpacking.

Attributes file:

default['my_app']['tarball'] = "url of the tarball created by jenkins"
default['my_app']['checksum'] = "sha256sum of your tarfile"
default['my_app']['dependencies']['apache2'] = '2.4.3'
default['my_app']['dependencies']['libXXXX'] = '1.0.0'

Recipe file:

service 'my_service' do
  action :enable,:start
end

ark 'whatever' do
  action :put
  url node['my_app']['tarball']
  checksum node['my_app']['checksum']
  path "/where/to/extract"
  notifies :restart, "service[my_service]"
end 

# Loop over the dependencies to create a package resource for each
node['my_app']['dependencies'].each do |package_name,package_version|
  package package_name do
    version package_version
  end
end

template "/where/to/extract/app/etc/config.file" do
  source "config.file.erb"
  variables({
    "var1" => "value1",
    "var2" => "value2"
  })
  notifies :restart, "service[my_service]"
end

Using the notification will restart the service if one the parts have moved during the run, this should be idempotent and never do anything if you run chef without changes.

Chaining all this after a jenkins run could be tricky as you'll have to update a part of your cookbooks values.

The checksum should be updated after each build, the dependencies may have to be updated, etc.

All this updates can be achieved in different ways, my personal preference goes to updating the cookbook with the same version number as the application (to keep an easy way to rollback), but the attributes can be passed in a json file to the client too (matter of taste).

Going further would be too much guessing.

Upvotes: 3

Related Questions