popularity question answers views user  
0
How to define before_symlink callback using application cookbook?

I am developing a cookbook to deploy simple jekyll based blog. I use ‘application’ cookbook as a base. Everything works smoothly except chef simply ignore my defined callbacks.

Chef log contains entries about my callback, but I see no errors or outcomes of the callback execution.

Here is recipes/default.rb file:

<!— language: ruby —>

include_recipe "git"

application "xxx_com" do
  path "/var/www/xxx.com"
  repository "git@xxx.org:xxx/xxx.git"
  revision "master"

  deploy_key <<EOF
-----BEGIN RSA PRIVATE KEY-----
..skip...
-----END RSA PRIVATE KEY-----
EOF

  # before_symlink "script/bootstrap"

  before_deploy do
    # raise
    # Chef::Log.info("INSANE!")
    execute "bundle exec rake generate" do
      cwd release_path
    end
  end
end

Here is the log where chef-client says it executes before_deploy callback:

[2012-10-06T10:06:20-04:00] INFO: Processing ruby_block[xxx_com before_deploy] action create (/var/cache/chef/cookbooks/application/providers/default.rb line 107)
[2012-10-06T10:06:20-04:00] DEBUG: Got callback before_deploy: #<Proc:0x00007f0509788088@/var/cache/chef/cookbooks/xxx_com/recipes/default.rb:24>
[2012-10-06T10:06:20-04:00] INFO: application[xxx_com] running callback before_deploy
[2012-10-06T10:06:20-04:00] INFO: ruby_block[xxx_com before_deploy] called
[2012-10-06T10:06:20-04:00] INFO: Processing deploy_revision[xxx_com] action deploy (/var/cache/chef/cookbooks/application/providers/default.rb line 122)

I also try to define callback using string, but the result is the same. Chef shows error when there is no file with callback definition and simply ignores the file if it is the right place.

Update 1

After digging deeper inside Chef sources I realized that callbacks are executed in separate chef runner and this happens not instantly. When code in application cookbook calls recipe_eval to evaluate callback body Chef creates separate runner and the code does not executed until something call converge method.

So, I modified application cookbook a bit. I added call converge just after recipe_eval and everything starts works.

I think this is a bug and I am going to create an issue in official Opscode tracker. However, any comments are very welcome!

Here is modifed version of application/libraries/default.rb:

<!— language: ruby —>

class Chef
  class Provider
    module ApplicationBase

    # ...skip...

      def callback(what, callback_code=nil)
        Chef::Log.debug("Got callback #{what}: #{callback_code.inspect}")
        @collection = Chef::ResourceCollection.new
        case callback_code
        when Proc
          Chef::Log.info "#{@new_resource} running callback #{what}"
          # recipe_eval(&callback_code)
          recipe_eval(&callback_code)
          converge # <--- Remove this line and no callbacks will work.
        when String
          callback_file = "#{release_path}/#{callback_code}"
          unless ::File.exist?(callback_file)
            raise RuntimeError, "Can't find your callback file #{callback_file}"
          end
          run_callback_from_file(callback_file)
        when nil
          nil
        else
          raise RuntimeError, "You gave me a callback I don't know what to do with: #{callback_code.inspect}"
        end
      end
    end
  end
end
more →
1 185  
0
application cookbook with node js

I have modified that opscode application cookbook to include support for node servers and applications. So far things have worked out pretty well and I only have one issue.

When I get to the point where the recipe should clone the repository from git I am prompted to enter the passphrase for the deploy key. The odd thing about this is no passphrase was created for this deploy key.

This is what I am seeing during provisioning:

ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: execute[npm] ran successfully 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: Processing directory[/data/shared] action create (application::node line 23) 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: directory[/data/shared] created directory /data/shared 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: directory[/data/shared] owner changed to 0 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: directory[/data/shared] group changed to 0 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: directory[/data/shared] mode changed to 755 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: Processing ruby_block[write_key] action create (application::node line 32) 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: ruby_block[write_key] called 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: Processing file[/data/id_deploy] action create (application::node line 41) 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: file[/data/id_deploy] mode changed to 600 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: Processing template[/data/deploy-ssh-wrapper] action create (application::node line 47) 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: template[/data/deploy-ssh-wrapper] mode changed to 755 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: template[/data/deploy-ssh-wrapper] updated content 
ec2-23-20-232-88.compute-1.amazonaws.com [Thu, 19 Apr 2012 20:09:23 +0000] INFO: Processing deploy_revision[node_app] action deploy (application::node line 57) 
ec2-23-20-232-88.compute-1.amazonaws.com Enter passphrase for key ‘/data/id_deploy’:

This is the current state of my recipe: https://gist.github.com/2423774

This is the current state of my data bag(deploy_key/repository removed): https://gist.github.com/2423781

more →
0 259